开发者

how to add a portal role by python code

I'm on plone 4.1 and I need to create a portal role by code and I cannot find any official reference.

After a bit of grepping I found this:

acl_users.portal_role_manager.addRole

Which seems to create the role but the role afaict is not available anywhere, nor in the security tab nor in the roles listed on /@@usergroup-userprefs.

Then I found also an "_addRole" on the portal object (which I think comes from eggs/Zope2-2.13.8-py2.6.egg/OFS/role.py).

I found a use of this in http://repositorio.interlegis.gov.br/ILSAAP/trunk/InstallUtils/installers/installRoles.py

and I'm now using

portal._addRole(new_role)
try:
    acl_users.portal_role_manager.addRole(new_role)
except:
    pass

that works! :)

The use real use-case is a specific blueprint for transmogrifier:

https://github.com/simahawk/collective.blueprint.usersandgroups/blob/master/collective/blueprint/usersandgroups/blueprint.开发者_如何学Gopy#L62

The question is: is this the way to go?


Roles are store localy in each zodb object so you can modify like that

## Roles are store on __ac_roles__ attribute of object.
roles = list(portal.__ac_roles__)
roles += 'yournewrole'
portal.__ac_roles__ = tuple(roles)

I think it's enough for your use case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜