Grant roles issue with userid

Good afternoon, community. I am currently trying to add a role to a current user in plone programmatically via python schema in dexterity. There were practically no errors shown in the console, but when i went to check to see if any changes occured, nothing changed.

This is the python code I was using:

def setManagers(self):
    user = api.user.get_current()
    userid = user.getProperty('username')
    api.user.grant_roles(
        username=userid,
        roles=['Manager', 'Reader'],
    )

The current role is set to 'Owner' however i would like to have it set to manager and reader as well. What do I need to change? I thank you for your help in advance!

...supposed to work that what. What is get_roles() returning before and after the call?

This should work to grans global roles.

Standard questions:

  • CSRF token provided (if used in a view)?
  • transaction committed (if used in some script)?
  • is something around masking exceptions?

Could you try the following code?

def setManagers(self):
    user = api.user.get_current()
    userid = user.getId()
    api.user.grant_roles(
        userid=userid,
        roles=['Manager', 'Reader'],
    )

By the way, grant_roles accepts the user object like api.user.grant_roles(user=api.user.get_current(). roles=.... as well.

2 Likes

Thank you for your help!