summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/gl_objects/users.rst7
-rw-r--r--gitlab/exceptions.py8
-rw-r--r--gitlab/v4/objects/users.py36
-rw-r--r--tests/unit/objects/test_users.py34
4 files changed, 85 insertions, 0 deletions
diff --git a/docs/gl_objects/users.rst b/docs/gl_objects/users.rst
index 53b00e2..996459d 100644
--- a/docs/gl_objects/users.rst
+++ b/docs/gl_objects/users.rst
@@ -111,6 +111,13 @@ List a user's starred projects
user.starred_projects.list()
+If the GitLab instance has new user account approval enabled some users may
+have ``user.state == 'blocked_pending_approval'``. Administrators can approve
+and reject such users::
+
+ user.approve()
+ user.reject()
+
User custom attributes
======================
diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py
index 602a452..1f7999e 100644
--- a/gitlab/exceptions.py
+++ b/gitlab/exceptions.py
@@ -294,6 +294,14 @@ class GitlabUnfollowError(GitlabOperationError):
pass
+class GitlabUserApproveError(GitlabOperationError):
+ pass
+
+
+class GitlabUserRejectError(GitlabOperationError):
+ pass
+
+
# For an explanation of how these type-hints work see:
# https://mypy.readthedocs.io/en/stable/generics.html#declaring-decorators
#
diff --git a/gitlab/v4/objects/users.py b/gitlab/v4/objects/users.py
index 9e76bd5..bef9fbe 100644
--- a/gitlab/v4/objects/users.py
+++ b/gitlab/v4/objects/users.py
@@ -284,6 +284,42 @@ class User(SaveMixin, ObjectDeleteMixin, RESTObject):
return server_data
@cli.register_custom_action("User")
+ @exc.on_http_error(exc.GitlabUserApproveError)
+ def approve(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
+ """Approve a user creation request.
+
+ Args:
+ **kwargs: Extra options to send to the server (e.g. sudo)
+
+ Raises:
+ GitlabAuthenticationError: If authentication is not correct
+ GitlabUserApproveError: If the user could not be activated
+
+ Returns:
+ The new object data (*not* a RESTObject)
+ """
+ path = f"/users/{self.encoded_id}/approve"
+ return self.manager.gitlab.http_post(path, **kwargs)
+
+ @cli.register_custom_action("User")
+ @exc.on_http_error(exc.GitlabUserRejectError)
+ def reject(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
+ """Reject a user creation request.
+
+ Args:
+ **kwargs: Extra options to send to the server (e.g. sudo)
+
+ Raises:
+ GitlabAuthenticationError: If authentication is not correct
+ GitlabUserRejectError: If the user could not be rejected
+
+ Returns:
+ The new object data (*not* a RESTObject)
+ """
+ path = f"/users/{self.encoded_id}/reject"
+ return self.manager.gitlab.http_post(path, **kwargs)
+
+ @cli.register_custom_action("User")
@exc.on_http_error(exc.GitlabBanError)
def ban(self, **kwargs: Any) -> Union[Dict[str, Any], requests.Response]:
"""Ban the user.
diff --git a/tests/unit/objects/test_users.py b/tests/unit/objects/test_users.py
index 1c89593..392cc3e 100644
--- a/tests/unit/objects/test_users.py
+++ b/tests/unit/objects/test_users.py
@@ -81,6 +81,32 @@ def resp_activate():
@pytest.fixture
+def resp_approve():
+ with responses.RequestsMock() as rsps:
+ rsps.add(
+ method=responses.POST,
+ url="http://localhost/api/v4/users/1/approve",
+ json={"message": "Success"},
+ content_type="application/json",
+ status=201,
+ )
+ yield rsps
+
+
+@pytest.fixture
+def resp_reject():
+ with responses.RequestsMock() as rsps:
+ rsps.add(
+ method=responses.POST,
+ url="http://localhost/api/v4/users/1/reject",
+ json={"message": "Success"},
+ content_type="application/json",
+ status=201,
+ )
+ yield rsps
+
+
+@pytest.fixture
def resp_ban():
with responses.RequestsMock() as rsps:
rsps.add(
@@ -242,6 +268,14 @@ def test_user_activate_deactivate(user, resp_activate):
user.deactivate()
+def test_user_approve_(user, resp_approve):
+ user.approve()
+
+
+def test_user_approve_reject(user, resp_reject):
+ user.reject()
+
+
def test_user_ban(user, resp_ban):
user.ban()