Changeset 6157


Ignore:
Timestamp:
20 May 2011, 08:52:48 (13 years ago)
Author:
uli
Message:

Zope roles come with a title attribute. Start making use of it and simplify role lookup in w.s.permissions

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/interfaces.py

    r6147 r6157  
    99from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
    1010
    11 
    1211class FatalCSVError(Exception):
    1312    """Some row could not be processed.
     
    2423class RoleSource(BasicSourceFactory):
    2524    def getValues(self):
     25        # late import: in interfaces we should not import local modules
     26        from waeup.sirp.permissions import getWAeUPRoleNames
     27        return getWAeUPRoleNames()
     28
     29    def getTitle(self, value):
     30        # late import: in interfaces we should not import local modules
    2631        from waeup.sirp.permissions import getRoles
    27         return getRoles()
    28     def getTitle(self, value):
    29         if isinstance(value, basestring):
    30             return value.split('.', 2)[1]
     32        roles = dict(getRoles())
     33        if value in roles.keys():
     34            title = roles[value].title
     35        if '.' in title:
     36            title = title.split('.', 2)[1]
     37        return title
    3138
    3239class IWAeUPObject(Interface):
  • main/waeup.sirp/trunk/src/waeup/sirp/permissions.py

    r6155 r6157  
    11import grok
     2from zope.component import getUtilitiesFor
    23from zope.interface import Interface
     4from zope.securitypolicy.interfaces import IRole
    35from waeup.sirp.interfaces import ILocalRolesAssignable
    46
     
    5557
    5658def getRoles():
    57     app = grok.getSite()
    58     app = None
    59     manager = None
    60     if app is not None:
    61         from zope.securitypolicy.interfaces import IRolePermissionManager
    62         manager = IRolePermissionManager(app, None)
    63     else:
    64         from zope.securitypolicy.rolepermission import (
    65             rolePermissionManager as manager)
    66     role_permission_map =  manager.getRolesAndPermissions()
    67     result = dict()
    68     for item in role_permission_map:
    69         if not item[1].startswith('waeup.'):
     59    """Return a list of tuples ``<ROLE-NAME>, <ROLE>``.
     60    """
     61    return getUtilitiesFor(IRole)
     62
     63def getWAeUPRoles(also_local=False):
     64    """Get all WAeUP roles.
     65
     66    WAeUP roles are ordinary roles whose id by convention starts with
     67    a ``waeup.`` prefix.
     68
     69    If `also_local` is ``True`` (``False`` by default), also local
     70    roles are returned. Local WAeUP roles are such whose id starts
     71    with ``waeup.local.`` prefix (this is also a convention).
     72
     73    Returns a generator of the found roles.
     74    """
     75    for name, item in getRoles():
     76        if not name.startswith('waeup.'):
    7077            # Ignore non-WAeUP roles...
    7178            continue
    72         if item[1].startswith('waeup.local.'):
     79        if not also_local and name.startswith('waeup.local.'):
     80            # Ignore local roles...
    7381            continue
    74         result[item[1]] = True
    75     return sorted(result.keys())
     82        yield item
     83
     84def getWAeUPRoleNames():
     85    """Get the ids of all WAeUP roles.
     86
     87    See :func:`getWAeUPRoles` for what a 'WAeUPRole' is.
     88
     89    This function returns a sorted list of WAeUP role names.
     90    """
     91    return sorted([x.id for x in getWAeUPRoles()])
     92
    7693
    7794class LocalRolesAssignable(grok.Adapter):
  • main/waeup.sirp/trunk/src/waeup/sirp/permissions.txt

    r6059 r6157  
    77.. :layer: waeup.sirp.testing.WAeUPSIRPUnitTestLayer
    88
    9 We can get all roles defined in a WAeUP portal (except 'local' roles
    10 that are meant not to be assigned globally):
     9Convenience functions
     10=====================
     11
     12:mod:`waeup.sirp` offers some convenience functions to handle security
     13roles.
     14
     15:func:`getRoles`
     16----------------
     17
     18Gives us all roles defined in a WAeUP SIRP portal. We get tuples of
     19kind
     20
     21  ``(<ROLE-NAME>, <ROLE>)``
     22
     23where ``<ROLE-NAME>`` is the name under which a role was registered
     24with the ZCA (a string) and ``<ROLE>`` is the real role object.
    1125
    1226    >>> from waeup.sirp.permissions import getRoles
    1327    >>> getRoles()
    14     ['waeup.PortalManager', 'waeup.PortalUser']
     28    <generator object at 0x...>
    1529
     30    >>> sorted(list(getRoles()))
     31    [(u'waeup.PortalManager', <waeup...PortalManager object at 0x...>), ...]
     32
     33:func:`getWAeUPRoles`
     34---------------------
     35
     36Gives us all roles, except the WAeUP specific roles. We can get a list
     37with or without local roles:
     38
     39    >>> from waeup.sirp.permissions import getWAeUPRoles
     40    >>> len(list(getWAeUPRoles()))
     41    2
     42
     43    >>> len(list(getWAeUPRoles(also_local=True)))
     44    4
     45
     46
     47:func:`getRoleNames`
     48--------------------
     49
     50We can get all role names defined in a WAeUP portal (except 'local'
     51roles that are meant not to be assigned globally):
     52
     53    >>> from waeup.sirp.permissions import getWAeUPRoleNames
     54    >>> list(getWAeUPRoleNames())
     55    [u'waeup.PortalManager', u'waeup.PortalUser']
Note: See TracChangeset for help on using the changeset viewer.