Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a portal role by python code

Tags:

roles

plone

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.py#L62

The question is: is this the way to go?

like image 449
simahawk Avatar asked Sep 04 '26 02:09

simahawk


1 Answers

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.

like image 196
yboussard Avatar answered Sep 06 '26 04:09

yboussard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!