Skip to content

Commit f9b7c7b

Browse files
authored
Merge pull request #2061 from bgamari/patch-1
feat(users): add approve and reject methods to User
2 parents f555bfb + f57139d commit f9b7c7b

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

docs/gl_objects/users.rst

+7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ List a user's starred projects
111111

112112
user.starred_projects.list()
113113

114+
If the GitLab instance has new user account approval enabled some users may
115+
have ``user.state == 'blocked_pending_approval'``. Administrators can approve
116+
and reject such users::
117+
118+
user.approve()
119+
user.reject()
120+
114121
User custom attributes
115122
======================
116123

gitlab/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,14 @@ class GitlabUnfollowError(GitlabOperationError):
298298
pass
299299

300300

301+
class GitlabUserApproveError(GitlabOperationError):
302+
pass
303+
304+
305+
class GitlabUserRejectError(GitlabOperationError):
306+
pass
307+
308+
301309
# For an explanation of how these type-hints work see:
302310
# https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
303311
#

gitlab/v4/objects/users.py

+36
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,42 @@ def activate(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
283283
self._attrs["state"] = "active"
284284
return server_data
285285

286+
@cli.register_custom_action("User")
287+
@exc.on_http_error(exc.GitlabUserApproveError)
288+
def approve(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
289+
"""Approve a user creation request.
290+
291+
Args:
292+
**kwargs: Extra options to send to the server (e.g. sudo)
293+
294+
Raises:
295+
GitlabAuthenticationError: If authentication is not correct
296+
GitlabUserApproveError: If the user could not be activated
297+
298+
Returns:
299+
The new object data (*not* a RESTObject)
300+
"""
301+
path = f"/users/{self.encoded_id}/approve"
302+
return self.manager.gitlab.http_post(path, **kwargs)
303+
304+
@cli.register_custom_action("User")
305+
@exc.on_http_error(exc.GitlabUserRejectError)
306+
def reject(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
307+
"""Reject a user creation request.
308+
309+
Args:
310+
**kwargs: Extra options to send to the server (e.g. sudo)
311+
312+
Raises:
313+
GitlabAuthenticationError: If authentication is not correct
314+
GitlabUserRejectError: If the user could not be rejected
315+
316+
Returns:
317+
The new object data (*not* a RESTObject)
318+
"""
319+
path = f"/users/{self.encoded_id}/reject"
320+
return self.manager.gitlab.http_post(path, **kwargs)
321+
286322
@cli.register_custom_action("User")
287323
@exc.on_http_error(exc.GitlabBanError)
288324
def ban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:

tests/unit/objects/test_users.py

+34
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ def resp_activate():
8080
yield rsps
8181

8282

83+
@pytest.fixture
84+
def resp_approve():
85+
with responses.RequestsMock() as rsps:
86+
rsps.add(
87+
method=responses.POST,
88+
url="http://localhost/api/v4/users/1/approve",
89+
json={"message": "Success"},
90+
content_type="application/json",
91+
status=201,
92+
)
93+
yield rsps
94+
95+
96+
@pytest.fixture
97+
def resp_reject():
98+
with responses.RequestsMock() as rsps:
99+
rsps.add(
100+
method=responses.POST,
101+
url="http://localhost/api/v4/users/1/reject",
102+
json={"message": "Success"},
103+
content_type="application/json",
104+
status=201,
105+
)
106+
yield rsps
107+
108+
83109
@pytest.fixture
84110
def resp_ban():
85111
with responses.RequestsMock() as rsps:
@@ -242,6 +268,14 @@ def test_user_activate_deactivate(user, resp_activate):
242268
user.deactivate()
243269

244270

271+
def test_user_approve_(user, resp_approve):
272+
user.approve()
273+
274+
275+
def test_user_approve_reject(user, resp_reject):
276+
user.reject()
277+
278+
245279
def test_user_ban(user, resp_ban):
246280
user.ban()
247281

0 commit comments

Comments
 (0)