Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add GET method to admin API /users/@user:dom/admin #5914

Merged
merged 2 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/5914.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add admin API endpoint for getting whether or not a user is a server administrator.
19 changes: 19 additions & 0 deletions docs/admin_api/user_admin_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ with a body of:
including an ``access_token`` of a server admin.


Get whether a user is a server administrator or not
===================================================


The api is::

GET /_synapse/admin/v1/users/<user_id>/admin

including an ``access_token`` of a server admin.

A response body like the following is returned:

.. code:: json

{
"admin": true
}


Change whether a user is a server administrator or not
======================================================

Expand Down
9 changes: 9 additions & 0 deletions synapse/handlers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def search_users(self, term):

return ret

def get_user_server_admin(self, user):
"""
Get the admin bit on a user.

Args:
user_id (UserID): the (necessarily local) user to manipulate
"""
return self.store.is_server_admin(user)

def set_user_server_admin(self, user, admin):
"""
Set the admin bit on a user.
Expand Down
2 changes: 1 addition & 1 deletion synapse/rest/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@


class UsersRestServlet(RestServlet):
PATTERNS = historical_admin_path_patterns("/users/(?P<user_id>[^/]*)")
PATTERNS = historical_admin_path_patterns("/users/(?P<user_id>[^/]*)$")

def __init__(self, hs):
self.hs = hs
Expand Down
40 changes: 32 additions & 8 deletions synapse/rest/admin/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,34 @@
assert_params_in_dict,
parse_json_object_from_request,
)
from synapse.rest.admin import assert_requester_is_admin
from synapse.rest.admin import assert_requester_is_admin, assert_user_is_admin
from synapse.types import UserID


class UserAdminServlet(RestServlet):
"""
Set whether or not a user is a server administrator.
Get or set whether or not a user is a server administrator.

Note that only local users can be server administrators, and that an
administrator may not demote themselves.

Only server administrators can use this API.

Example:
PUT /_synapse/admin/v1/users/@reivilibre:librepush.net/admin
{
"admin": true
}
Examples:
* Get
GET /_synapse/admin/v1/users/@nonadmin:example.com/admin
response on success:
{
"admin": false
}
* Set
PUT /_synapse/admin/v1/users/@reivilibre:librepush.net/admin
request body:
{
"admin": true
}
response on success:
{}
"""

PATTERNS = (re.compile("^/_synapse/admin/v1/users/(?P<user_id>@[^/]*)/admin$"),)
Expand All @@ -50,9 +60,23 @@ def __init__(self, hs):
self.handlers = hs.get_handlers()

@defer.inlineCallbacks
def on_PUT(self, request, user_id):
def on_GET(self, request, user_id):
yield assert_requester_is_admin(self.auth, request)

target_user = UserID.from_string(user_id)

if not self.hs.is_mine(target_user):
raise SynapseError(400, "Only local users can be admins of this homeserver")

is_admin = yield self.handlers.admin_handler.get_user_server_admin(target_user)
is_admin = bool(is_admin)

return (200, {"admin": is_admin})

@defer.inlineCallbacks
def on_PUT(self, request, user_id):
requester = yield self.auth.get_user_by_req(request)
yield assert_user_is_admin(self.auth, requester.user)
auth_user = requester.user

target_user = UserID.from_string(user_id)
Expand Down