通过知道它的 id(和 vice-versa)来知道 Zope 权限标题的方法是什么?

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

在 Zope 和 Plone 上,您可以像这样注册权限:

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

当你知道权限的id时,如何从Python获取权限的标题?

我的意思是:

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

权限注册为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

反之,从标题到 ID,您需要搜索所有实用程序:

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)