Skip to content

Commit

Permalink
Does not allow a Site Administrator delete Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleybl committed Sep 21, 2023
1 parent e05e1ec commit 7e95de2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/plone/restapi/services/users/delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from AccessControl import getSecurityManager
from plone.restapi.services import Service
from Products.CMFCore.permissions import ManagePortal
from Products.CMFCore.utils import getToolByName
from zope.component.hooks import getSite
from zope.interface import implementer
Expand All @@ -15,6 +17,11 @@ class UsersDelete(Service):
def __init__(self, context, request):
super().__init__(context, request)
self.params = []
self.portal_membership = getToolByName(context, "portal_membership")

@property
def is_zope_manager(self):
return getSecurityManager().checkPermission(ManagePortal, self.context)

def publishTraverse(self, request, name):
# Consume any path segments after /@users as parameters
Expand All @@ -27,9 +34,15 @@ def _get_user_id(self):
raise Exception("Must supply exactly one parameter (user id)")
return self.params[0]

def _get_user(self, user_id):
return self.portal_membership.getMemberById(user_id)

def reply(self):
portal = getSite()
portal_membership = getToolByName(portal, "portal_membership")
if not self.is_zope_manager:
user = self._get_user(self._get_user_id)
current_roles = user.getRoles()
if "Manager" in current_roles:
return self.reply_no_content(status=403)

delete_memberareas = (
self.request.get("delete_memberareas", True) not in FALSE_VALUES
Expand All @@ -39,7 +52,7 @@ def reply(self):
self.request.get("delete_localroles", True) not in FALSE_VALUES
)

delete_successful = portal_membership.deleteMembers(
delete_successful = self.portal_membership.deleteMembers(
(self._get_user_id,),
delete_memberareas=delete_memberareas,
delete_localroles=delete_localroles,
Expand Down
9 changes: 9 additions & 0 deletions src/plone/restapi/tests/test_services_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,3 +1354,12 @@ def test_manager_update_manager(self):

noam = api.user.get(userid="noam")
self.assertIn("Manager", noam.getRoles())

def test_siteadm_not_delete_manager(self):
self.set_siteadm()
api.user.grant_roles(username="noam", roles=["Manager"])
transaction.commit()
self.api_session.delete("/@users/noam")
transaction.commit()

self.assertIsNotNone(api.user.get(userid="noam"))

0 comments on commit 7e95de2

Please sign in to comment.