Skip to content

Commit

Permalink
Merge pull request #55 from zopefoundation/fix-zope-user-roles
Browse files Browse the repository at this point in the history
Fix bug in `getRolesForPrincipal` for non PAS user.
  • Loading branch information
dataflake committed Oct 22, 2019
2 parents 73e5936 + 7dd7c31 commit 22d6f3f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change Log
2.2 (unreleased)
----------------

- Fix bug in ``getRolesForPrincipal`` for non PAS user.


2.1 (2019-08-29)
----------------
Expand Down
2 changes: 1 addition & 1 deletion Products/PluggableAuthService/plugins/ZODBRoleManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def getRolesForPrincipal(self, principal, request=None):
"""
result = list(self._principal_roles.get(principal.getId(), ()))

getGroups = getattr(principal, 'getGroups', lambda x: ())
getGroups = getattr(principal, 'getGroups', lambda: ())
for group_id in getGroups():
result.extend(self._principal_roles.get(group_id, ()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
##############################################################################
import unittest

from AccessControl.users import SpecialUser
from zExceptions import Forbidden

from Products.PluggableAuthService.plugins.tests.helpers import DummyUser
Expand Down Expand Up @@ -54,6 +55,21 @@ def test_empty(self):
roles = zrm.getRolesForPrincipal(user)
self.assertEqual(len(roles), 0)

def test_zope_user(self):
"""It does not break `getRolesForPrincipal` in case of a zope user
which has no `getGroups`.
"""
zrm = self._makeOne()

self.assertEqual(len(zrm.listRoleIds()), 0)
self.assertEqual(len(zrm.enumerateRoles()), 0)

# This user has no `getGroups()` as the typical PAS user.
user = SpecialUser('admin', 'admin', ['Manager'], None)
roles = zrm.getRolesForPrincipal(user)
self.assertEqual(len(roles), 0)

def test_addRole(self):

from Products.PluggableAuthService.tests.test_PluggableAuthService \
Expand Down

0 comments on commit 22d6f3f

Please sign in to comment.