Re[2]: [Zope-CMF] Setting local role programmatically

Rainer Thaden Rainer Thaden <thadi@gmx.de>
Mon, 12 Aug 2002 12:04:47 +0200


Dieter,

DM>  > If it does, how do i set the local role 'manager' programmatically for
DM>  > an object? Which API?
DM> I am too lazy to look this up for you. But you can proceed as
DM> follows:

I didn't expect you to look that up for me. I thought maybe someone
has it in mind because he just programmed something related to this.

DM>     look at the ZMI (Zope Management Interface) sources that allow
DM>     you to view and change the local roles for an object

Anyway, of course your solution worked (almost). I found what i
searched for and implemented the following:

In a utils file in my product i added

from AccessControl.Role import RoleManager
from AccessControl.Owned import Owned
from AccessControl.User import BasicUser

def setOwnerToLocalRoleManager(sci):
    obj=sci.object;
    userid = obj.getOwner().getId()
    obj.manage_setLocalRoles(obj, userid, 'Manager')


In a script inside the workflow (CMF Default workflow, rev 2) i do

from Products.ITA.ita_utils import setOwnerToLocalRoleManager

setOwnerToLocalRoleManager(sci)

The script is executed after the transistion.


Error Type: AttributeError 
Error Value: __hash__

...
Traceback (innermost last):
...
---8<--- snipped first part of traceback --->8---

  File C:\Programme\Zope\lib\python\Shared\DC\Scripts\Bindings.py, line 283, in _bindAndExec
    (Object: owner_to_manager)
  File C:\Programme\Zope\lib\python\Products\PythonScripts\PythonScript.py, line 302, in _exec
    (Object: owner_to_manager)
    (Info: ({'script': <PythonScript instance at 02455D48>, 'context': <Scripts instance at 021FAA58>, 'container': <Scripts instance at 021FAA58>, 'traverse_subpath': []}, (<Products.DCWorkflow.Expression.StateChangeInfo instance at 025FF4BC>,), {}, None))
  File Script (Python), line 6, in owner_to_manager
  File C:\Programme\Zope\lib\python\Products\ITA\ita_utils.py, line 10, in setOwnerToLocalRoleManager
  File C:\Programme\Zope\lib\python\AccessControl\Role.py, line 352, in manage_setLocalRoles
    (Object: ohrnah)
AttributeError: (see above)

The error happens here in dict[userid]=roles

    def manage_setLocalRoles(self, userid, roles, REQUEST=None):
        """Set local roles for a user."""
        if not roles:
            raise ValueError, 'One or more roles must be given!'
        dict=self.__ac_local_roles__ or {}
        dict[userid]=roles
        self.__ac_local_roles__=dict
        if REQUEST is not None:
            stat='Your changes have been saved.'
            return self.manage_listLocalRoles(self, REQUEST, stat=stat)

It seems, that the userid is not in the dictionary.

I looked into the BasicUser class and found, that the getId() method
is not implemented.
            
    def getUserName(self):
        """Return the username of a user"""
        raise NotImplemented

    def getId(self):
        """Get the ID of the user. The ID can be used, at least from
        Python, to get the user from the user's
        UserDatabase"""
        return self.getUserName()        

What's wrong? Did i miss something?

I use CMF 1.3, DC Workflow 0.4.2+

TIA.
-- 
Regards,
 Rainer                            mailto:thadi@gmx.de