Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report non existing permissions #526

Merged
merged 3 commits into from
Apr 29, 2024
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
3 changes: 3 additions & 0 deletions news/515.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Report if a permission does not exist
when calling `api.user.has_permission`.
[gforcada]
7 changes: 4 additions & 3 deletions src/plone/api/tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,15 @@ def test_adopt_user_different_username(self):

def test_roles_restored_after_exception(self):
"""Tests that roles are restored after an exception."""
self.assertFalse(api.user.has_permission("Manage portal content"))
permission = "Manage properties"
self.assertFalse(api.user.has_permission(permission))
try:
with api.env.adopt_roles(["Manager"]):
self.assertTrue(api.user.has_permission("Manage portal content"))
self.assertTrue(api.user.has_permission(permission))
raise TestException("Test exception")
except TestException:
pass
self.assertFalse(api.user.has_permission("Manage portal content"))
self.assertFalse(api.user.has_permission(permission))

def test_user_restored_after_exception(self):
"""Tests that roles are restored after an exception."""
Expand Down
13 changes: 12 additions & 1 deletion src/plone/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,18 @@ def has_permission(permission, username=None, user=None, obj=None):
context = env.adopt_user(username, user)

with context:
return bool(getSecurityManager().checkPermission(permission, obj))
return_value = bool(getSecurityManager().checkPermission(permission, obj))
if not return_value:
names = [x[0] for x in getPermissions()]
if permission not in names:
raise InvalidParameterError(
"Cannot find a permission with name '{permission}'\n"
"Available permissions are:\n"
"{names}".format(
permission=permission, names="\n".join(sorted(names))
)
)
return return_value


@required_parameters("roles")
Expand Down