Changeset 6144


Ignore:
Timestamp:
19 May 2011, 13:48:09 (13 years ago)
Author:
uli
Message:
  • Add default adapter for ILocalRolesAssignable
File:
1 edited

Legend:

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

    r6142 r6144  
    11import grok
    2 from zc.sourcefactory.basic import BasicSourceFactory
     2from zope.interface import Interface
     3from waeup.sirp.interfaces import ILocalRolesAssignable
    34
    45class Public(grok.Permission):
     
    7172    return sorted(result.keys())
    7273
    73 class RoleSource(BasicSourceFactory):
    74     def getValues(self):
    75         return getRoles()
    76     def getTitle(self, value):
    77         if isinstance(value, basestring):
    78             return value.split('.', 2)[1]
     74class LocalRolesAssignable(grok.Adapter):
     75    """Default implementation for `ILocalRolesAssignable`.
     76
     77    This adapter returns a list for dictionaries for objects for which
     78    we want to know the roles assignable to them locally.
     79
     80    The returned dicts contain a ``name`` and a ``title`` entry which
     81    give a role (``name``) and a description, for which kind of users
     82    the permission is meant to be used (``title``).
     83
     84    Having this adapter registered we make sure, that for each normal
     85    object we get a valid `ILocalRolesAssignable` adapter.
     86
     87    Objects that want to offer certain local roles, can do so by
     88    setting a (preferably class-) attribute to a list of dictionaries.
     89
     90    You can also define different adapters for different contexts to
     91    have different role lookup mechanisms become available. But in
     92    normal cases it should be sufficient to use this basic adapter.
     93    """
     94    grok.context(Interface)
     95    grok.provides(ILocalRolesAssignable)
     96
     97    _roles = []
     98
     99    def __init__(self, context):
     100        self.context = context
     101        self._roles = getattr(context, 'local_roles', self._roles)
     102        return
     103
     104    def __call__(self):
     105        """Get a list of dictionaries containing ``names`` (the roles to
     106        assign) and ``titles`` (some description of the type of user
     107        to assign each role to).
     108        """
     109        return self._roles
     110
     111    def roles(self):
     112        """Return a list of roles assignable to the context object.
     113        """
     114        return [x['name'] for x in self._roles]
Note: See TracChangeset for help on using the changeset viewer.