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 22, 2023
1 parent 314eae2 commit 950706a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/plone/restapi/services/users/delete.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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
from zope.publisher.interfaces import IPublishTraverse

Expand All @@ -15,6 +16,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 +33,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 +51,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 950706a

Please sign in to comment.