Skip to content

Commit e387de5

Browse files
author
Gauvain Pocentek
committed
Add support for user block/unblock
1 parent 942468d commit e387de5

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

gitlab/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ class GitlabBuildRetryError(GitlabOperationError):
8383
pass
8484

8585

86+
class GitlabBlockError(GitlabOperationError):
87+
pass
88+
89+
90+
class GitlabUnblockError(GitlabOperationError):
91+
pass
92+
93+
8694
def raise_error_from_response(response, error, expected_code=200):
8795
"""Tries to parse gitlab error message from response and raises error.
8896

gitlab/objects.py

+14
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,20 @@ def Key(self, id=None, **kwargs):
527527
user_id=self.id,
528528
**kwargs)
529529

530+
def block(self, **kwargs):
531+
"""Blocks the user."""
532+
url = '/users/%s/block' % self.id
533+
r = self.gitlab._raw_put(url, **kwargs)
534+
raise_error_from_response(r, GitlabBlockError)
535+
self.state = 'blocked'
536+
537+
def unblock(self, **kwargs):
538+
"""Unblocks the user."""
539+
url = '/users/%s/unblock' % self.id
540+
r = self.gitlab._raw_put(url, **kwargs)
541+
raise_error_from_response(r, GitlabUnblockError)
542+
self.state = 'active'
543+
530544

531545
class UserManager(BaseManager):
532546
obj_cls = User

tools/python_test.py

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
assert(new_user.username == user.username)
4141
assert(new_user.email == user.email)
4242

43+
new_user.block()
44+
new_user.unblock()
45+
4346
# SSH keys
4447
key = new_user.keys.create({'title': 'testkey', 'key': SSH_KEY})
4548
assert(len(new_user.keys.list()) == 1)

0 commit comments

Comments
 (0)