8000 feat: add users activate, deactivate functionality · python-gitlab/python-gitlab@32ad669 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 32ad669

Browse files
committed
feat: add users activate, deactivate functionality
These were introduced in GitLab 12.4
1 parent ac2266b commit 32ad669

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

docs/gl_objects/users.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ Block/Unblock a user::
6262
user.block()
6363
user.unblock()
6464

65+
Activate/Deactivate a user::
66+
67+
user.activate()
68+
user.deactivate()
69+
6570
Set the avatar image for a user::
6671

6772
# the avatar image can be passed as data (content of the file) or as a file

gitlab/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ class GitlabUnblockError(GitlabOperationError):
157157
pass
158158

159159

160+
class GitlabDeactivateError(GitlabOperationError):
161+
pass
162+
163+
164+
class GitlabActivateError(GitlabOperationError):
165+
pass
166+
167+
160168
class GitlabSubscribeError(GitlabOperationError):
161169
pass
162170

gitlab/tests/test_gitlab.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,31 @@ def resp_deployment_update(url, request):
695695
deployment.save()
696696
self.assertEqual(deployment.status, "failed")
697697

698+
def test_user_activate_deactivate(self):
699+
@urlmatch(
700+
scheme="http",
701+
netloc="localhost",
702+
path="/api/v4/users/1/activate",
703+
method="post",
704+
)
705+
def resp_activate(url, request):
706+
headers = {"content-type": "application/json"}
707+
return response(201, {}, headers, None, 5, request)
708+
709+
@urlmatch(
710+
scheme="http",
711+
netloc="localhost",
712+
path="/api/v4/users/1/deactivate",
713+
method="post",
714+
)
715+
def resp_deactivate(url, request):
716+
headers = {"content-type": "application/json"}
717+
return response(201, {}, headers, None, 5, request)
718+
719+
with HTTMock(resp_activate), HTTMock(resp_deactivate):
720+
self.gl.users.get(1, lazy=True).activate()
721+
self.gl.users.get(1, lazy=True).deactivate()
722+
698723
def test_update_submodule(self):
699724
@urlmatch(
700725
scheme="http", netloc="localhost", path="/api/v4/projects/1$", method="get"

gitlab/v4/objects.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,48 @@ def unblock(self, **kwargs):
340340
self._attrs["state"] = "active"
341341
return server_data
342342

343+
@cli.register_custom_action("User")
344+
@exc.on_http_error(exc.GitlabDeactivateError)
345+
def deactivate(self, **kwargs):
346+
"""Deactivate the user.
347+
348+
Args:
349+
**kwargs: Extra options to send to the server (e.g. sudo)
350+
351+
Raises:
352+
GitlabAuthenticationError: If authentication is not correct
353+
GitlabDeactivateError: If the user could not be deactivated
354+
355+
Returns:
356+
bool: Whether the user status has been changed
357+
"""
358+
path = "/users/%s/deactivate" % self.id
359+
server_data = self.manager.gitlab.http_post(path, **kwargs)
360+
if server_data:
361+
self._attrs["state"] = "deactivated"
362+
return server_data
363+
364+
@cli.register_custom_action("User")
365+
@exc.on_http_error(exc.GitlabActivateError)
366+
def activate(self, **kwargs):
367+
"""Activate the user.
368+
369+
Args:
370+
**kwargs: Extra options to send to the server (e.g. sudo)
371+
372+ 57A7
Raises:
373+
GitlabAuthenticationError: If authentication is not correct
374+
GitlabActivateError: If the user could not be activated
375+
376+
Returns:
377+
bool: Whether the user status has been changed
378+
"""
379+
path = "/users/%s/activate" % self.id
380+
server_data = self.manager.gitlab.http_post(path, **kwargs)
381+
if server_data:
382+
self._attrs["state"] = "active"
383+
return server_data
384+
343385

344386
class UserManager(CRUDMixin, RESTManager):
345387
_path = "/users"

0 commit comments

Comments
 (0)
0