[Zope] Determining Local Roles

Flynt rhess@bic.ch
Sat, 07 Jul 2001 00:44:48 +0200


Eric Vautour wrote:
> 
> Hey,
> thanks for the response.  Did you have to import a library before this would
> work?  I tried
> 
> "from AccessControl import import getRolesInContext"
> 
> but it didn't seem to work, what did you do?
> 

Hi Eric,

you don't have to import anything. The expression:

<dtml-var expr="AUTHENTICATED_USER.getRolesInContext(this())">

is right there in a DTML method.
When I was looking for that stuff myself, I searched the mailing list
archives. Got to:

http://zope.nipltd.com/public/lists/zope-archive.nsf/

and search for *getRolesInContext*.

I also use it in an external method. I show the users on the site, who
has *Editor* roles on a specific object. The code for the external
method is:

def showEditors(self, role, userfolder, obj):
        """
        This function gets the users, who have a certain
        role *role* in context of object *obj* and gives
        back a list of those users. In *userfolder* the actual
        userfolder object has to be brought in (this is usually
        *acl_users*).
        """

        roles = {}
        editors = []
        userids = userfolder.getUserNames()
        for userid in userids:
                user = userfolder.getUser(userid)
                roles[userid] = user.getRolesInContext(obj)
        for k in roles.keys():
                for v in roles[k]:
                        if v == role:
                                editors.append(k)
        return editors

Also here, I don't have to import anything. For example I can call the
above (external) method from a dtml method like (id of the external
method is *showEditorsExt*):

<dtml-if expr="_.len( showEditorsExt('Editor', _['acl_users'], this()) )
> 0">
  <dtml-in expr="showEditorsExt('Editor', _['acl_users'], this())">
    <dtml-var sequence-item><br>
  </dtml-in>
<dtml-else>
  -- Nobody --
</dtml-if>

And all this works quite nicely with local roles. The *Editor* role is
set mostly as a local role in my case.

HTH

--- Flynt