2

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'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
keul
  • 7,673
  • 20
  • 45

1 Answers1

5

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)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • For some reason it was not working for me, but probably I was using a wrong id. Thanks! So what is quicker way to get the id from the title? – keul Feb 28 '15 at 17:15
  • 1
    I don't think there is one; you'd have to list all utilities and search: `next((name for name, p in getUtilitiesFor(IPermission) if p.title == searched_title), None)` will return the name or `None` if there isn't any such permission. – Martijn Pieters Feb 28 '15 at 17:24