Skip to content

Commit

Permalink
Do not allow the Site Administrator to set the Manager role for a group
Browse files Browse the repository at this point in the history
  • Loading branch information
wesleybl committed Sep 22, 2023
1 parent ab339f2 commit 7abe4f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/plone/restapi/services/groups/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ def __init__(self, context, request):
def is_zope_manager(self):
return getSecurityManager().checkPermission(ManagePortal, self.context)

def can_update(self, group, users):
def can_update(self, group, users, roles):
if self.is_zope_manager:
return True
if "Manager" in group.getRoles() and users:
current_group_roles = group.getRoles()
if "Manager" in current_group_roles and users:
return False
if "Manager" in roles:
return "Manager" in current_group_roles
return "Manager" not in current_group_roles

def publishTraverse(self, request, name):
# Consume any path segments after /@groups as parameters
Expand All @@ -70,11 +74,11 @@ def reply(self):
raise BadRequest("Trying to update a non-existing group.")

users = data.get("users", {})
roles = data.get("roles", None)

if not self.can_update(group, users):
if not self.can_update(group, users, roles):
return self.reply_no_content(status=403)

roles = data.get("roles", None)
groups = data.get("groups", None)

# Disable CSRF protection
Expand Down
11 changes: 11 additions & 0 deletions src/plone/restapi/tests/test_services_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,14 @@ def test_siteadm_not_add_user_to_group_with_manager_role(self):

administrators = self.gtool.getGroupById("Administrators")
self.assertNotIn(TEST_USER_ID, administrators.getGroupMemberIds())

def test_siteadm_not_set_manager_to_group(self):
self.set_siteadm()
payload = {
"roles": ["Manager"],
}
self.api_session.patch("/@groups/ploneteam", json=payload)
transaction.commit()

ploneteam = self.gtool.getGroupById("ploneteam")
self.assertNotIn("Manager", ploneteam.getRoles())

0 comments on commit 7abe4f4

Please sign in to comment.