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'

1 Answer 1

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)
2
  • 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, 2015 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, 2015 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.