Plone: assign permission to role
Wed, Feb 22, 2017,
200 Words
I always forget, and a quick search through the eggs folder didn’t yield anything easy to find: how to I assign a permission to a role, in a context?
This is a bit of code:
from Products.DCWorkflow.utils import modifyRolesForPermission
from AccessControl.PermissionMapping import getPermissionMapping
perm = 'Delete objects'
pm = set(getPermissionMapping(perm, context, st=tuple))
pm.add('Contributor')
pm.add('Owner')
modifyRolesForPermission(wc, perm, tuple(pm))
This is based on code found in DCWorkflow. I know, the proper code would be:
from AccessControl.Permission import Permission
p = Permission(name, data, obj)
p.setRole(role_name, True)
But I don’t like that API. What is data? I don’t know. I can understand name, of course, and obj as the context. But data???
Previous: Python packaging vs npm