permissionsplonezopezope3

What is the way to know a Zope permission title by knowing its id (and vice-versa)?


On Zope and Plone you can register permissions like this:

<permission
   id="choosen.id.for.your.permission"
   title="Old Zope 2 permission, shown in ZMI"
   />

What is the way to get the permission's title from Python when you know the permission's id?

I mean something like:

>>> something_magic.get('choosen.id.for.your.permission')
'Old Zope 2 permission, shown in ZMI'

Solution

  • Permissions are registered as zope.security.interfaces.IPermission utilities by their id; you can thus look them up by their ids by using zope.component.getUtility():

    from zope.component import getUtility
    from zope.security.interfaces import IPermission
    
    permission = getUtility(IPermission, name=u'choosen.id.for.your.permission')
    print permission.title
    

    To go the other way, from title to id, you need to search all utilities:

    from zope.component import getUtilitiesFor
    from zope.security.interfaces import IPermission
    
    searched_title = u'Old Zope 2 permission, shown in ZMI'
    name = next((name for name, p in getUtilitiesFor(IPermission)
                 if p.title == searched_title), None)