groups.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2019 The Matrix.org Foundation C.I.C.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from http import HTTPStatus
  16. from typing import TYPE_CHECKING, Tuple
  17. from synapse.api.errors import SynapseError
  18. from synapse.http.servlet import RestServlet
  19. from synapse.http.site import SynapseRequest
  20. from synapse.rest.admin._base import admin_patterns, assert_user_is_admin
  21. from synapse.types import JsonDict
  22. if TYPE_CHECKING:
  23. from synapse.server import HomeServer
  24. logger = logging.getLogger(__name__)
  25. class DeleteGroupAdminRestServlet(RestServlet):
  26. """Allows deleting of local groups"""
  27. PATTERNS = admin_patterns("/delete_group/(?P<group_id>[^/]*)$")
  28. def __init__(self, hs: "HomeServer"):
  29. self.group_server = hs.get_groups_server_handler()
  30. self.is_mine_id = hs.is_mine_id
  31. self.auth = hs.get_auth()
  32. async def on_POST(
  33. self, request: SynapseRequest, group_id: str
  34. ) -> Tuple[int, JsonDict]:
  35. requester = await self.auth.get_user_by_req(request)
  36. await assert_user_is_admin(self.auth, requester.user)
  37. if not self.is_mine_id(group_id):
  38. raise SynapseError(HTTPStatus.BAD_REQUEST, "Can only delete local groups")
  39. await self.group_server.delete_group(group_id, requester.user.to_string())
  40. return HTTPStatus.OK, {}