Skip to content

Commit 159b926

Browse files
committed
feat(users): add ban and unban methods
1 parent 384031c commit 159b926

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

docs/gl_objects/users.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ Activate/Deactivate a user::
7272
user.activate()
7373
user.deactivate()
7474

75+
Ban/Unban a user::
76+
77+
user.ban()
78+
user.unban()
79+
7580
Follow/Unfollow a user::
7681

7782
user.follow()

gitlab/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,14 @@ class GitlabActivateError(GitlabOperationError):
186186
pass
187187

188188

189+
class GitlabBanError(GitlabOperationError):
190+
pass
191+
192+
193+
class GitlabUnbanError(GitlabOperationError):
194+
pass
195+
196+
189197
class GitlabSubscribeError(GitlabOperationError):
190198
pass
191199

gitlab/v4/objects/users.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,48 @@ 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.GitlabBanError)
288+
def ban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
289+
"""Ban the user.
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+
GitlabBanError: If the user could not be banned
297+
298+
Returns:
299+
Whether the user has been banned
300+
"""
301+
path = f"/users/{self.encoded_id}/ban"
302+
server_data = self.manager.gitlab.http_post(path, **kwargs)
303+
if server_data:
304+
self._attrs["state"] = "banned"
305+
return server_data
306+
307+
@cli.register_custom_action("User")
308+
@exc.on_http_error(exc.GitlabUnbanError)
309+
def unban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
310+
"""Unban the user.
311+
312+
Args:
313+
**kwargs: Extra options to send to the server (e.g. sudo)
314+
315+
Raises:
316+
GitlabAuthenticationError: If authentication is not correct
317+
GitlabUnbanError: If the user could not be unbanned
318+
319+
Returns:
320+
Whether the user has been unbanned
321+
"""
322+
path = f"/users/{self.encoded_id}/unban"
323+
server_data = self.manager.gitlab.http_post(path, **kwargs)
324+
if server_data:
325+
self._attrs["state"] = "active"
326+
return server_data
327+
286328

287329
class UserManager(CRUDMixin, RESTManager):
288330
_path = "/users"
@@ -291,6 +333,7 @@ class UserManager(CRUDMixin, RESTManager):
291333
_list_filters = (
292334
"active",
293335
"blocked",
336+
"banned",
294337
"username",
295338
"extern_uid",
296339
"provider",

tests/functional/api/test_users.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def test_block_user(gl, user):
3737
assert user in users
3838

3939

40+
def test_ban_user(gl, user):
41+
user.ban()
42+
retrieved_user = gl.users.get(id=user.id)
43+
assert retrieved_user.state == "banned"
44+
45+
user.unban()
46+
retrieved_user = gl.users.get(id=user.id)
47+
assert retrieved_user.state == "active"
48+
49+
4050
def test_delete_user(gl, wait_for_sidekiq):
4151
new_user = gl.users.create(
4252
{

tests/unit/objects/test_users.py

Lines changed: 34 additions & 0 deletions
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_ban():
85+
with responses.RequestsMock() as rsps:
86+
rsps.add(
87+
method=responses.POST,
88+
url="http://localhost/api/v4/users/1/ban",
89+
json={},
90+
content_type="application/json",
91+
status=201,
92+
)
93+
yield rsps
94+
95+
96+
@pytest.fixture
97+
def resp_unban():
98+
with responses.RequestsMock() as rsps:
99+
rsps.add(
100+
method=responses.POST,
101+
url="http://localhost/api/v4/users/1/unban",
102+
json={},
103+
content_type="application/json",
104+
status=201,
105+
)
106+
yield rsps
107+
108+
83109
@pytest.fixture
84110
def resp_get_user_status():
85111
content = {
@@ -216,6 +242,14 @@ def test_user_activate_deactivate(user, resp_activate):
216242
user.deactivate()
217243

218244

245+
def test_user_ban(user, resp_ban):
246+
user.ban()
247+
248+
249+
def test_user_unban(user, resp_unban):
250+
user.unban()
251+
252+
219253
def test_delete_user_identity(user, resp_delete_user_identity):
220254
user.identityproviders.delete("test_provider")
221255

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy