You are not logged in Log in Join
You are here: Home » Members » hdw » How-to » View user names and roles

Log in
Name

Password

 

View user names and roles

by Heiner de Wendt

This How-To is for beginning users with a basic HTML understanding, who want to experiment with user roles and permissions; the result is a (maybe not even completely worthless ;) DTML method that shows all users of the current hierarchy.


Starting from scratch

First of all, we need to create a DTML method. If you want to be able to use it throughout your whole Zope system, add it to your root folder.
While the name is not really of importance, I assume that the new DTML method has the name "showusers". When creating it, click on "Add&Edit"; now, we're going to do the first programming. Delete all text except of the following:


<!--#var standard_html_header-->

<!--#var standard_html_footer-->

All following code will be inserted between these two lines above.


Finding out who's logged in

Now, let's try to find out who's the one calling our method. But we're not doing it without a reason; we want to give a nice little greeting. Insert the following code:


<p>
<h3>Hello <dtml-var "AUTHENTICATED_USER.getUserName()">!</h3>
</p>

So, what are we doing here?

AUTHENTICATED_USER
is a Zope function which contains the name of the user who's logged in. We could also write
REQUEST['AUTHENTICATED_USER']
to get the user name.
Putting all this into the
<dtml-var>
tag results in this value (i.e. the user name) being displayed. And since, thus, we do nothing but displaying text, we can also put text before and after this tag. So, if the logged in user has the user name "Joe", the following text would be displayed:

Hello Joe!


A nice little title

Following now would be the list of users who've got a login at the current hierarchy. But, simply giving out that list isn't very nice, so let's include a bit of text here:


The following users exist here:<br>


Getting the usernames

Now, we're going to the important part. Type in the following code:


<dtml-in "acl_users.getUserNames()">
<p>
<b><dtml-var sequence-item></b><br>
</p>
</dtml-in>

To understand what's going on here, take a look at the

<dtml-in>
tag. This command creates a loop, similar to "for"-loops known from other computer languages. It loops through a list of objects, no matter if those are numbers, strings, or whatever. Each step in this loop represents an object in this list.
So, what's our list? We access
acl_users.getUserNames()
here. "acl_users" simply is the standard name of Zope's user folders, in which all user-relevant data gets stored. The "." shows that the item we access lies within this user folder; and the item we access is the result of the function "getUserNames()", which creates a list from all the names of the users within the user folder defined before the ".". Sounds complex, but, actually, is quite simple. Put short, we get the list of users this way, as a list, through which we loop with the dtml-in command.

We access the value of the current item (the current "step" we've got in the loop) with the "sequence-item" method; it simply returns the item's "contents". As we access user names, looping through the whole acl_users folder, we thus display the names of all users.
And yes, that's actually it.


A little bonus: User roles

What if we don't just want to know the user's names, but also their roles? How do we find out that? Again, this isn't really a complex thing to do, as long as you know the right tags ;) After the "br" tag behind your <dtml-var sequence-item>, type in the following:


<dtml-var "acl_users.getUser(_['sequence-item']).getRoles()">

The syntax is the same as before. With "_['sequence-item']", we access the value of the current item in the loop, being a user name, and with "getRoles()", we get a user's role. So, within the loop, we access:
The role [getRoles()] of the users [sequence-item] in the user-folder [acl_users]. Now, this way isn't perfect, for some users may have a couple of roles; so the displayed result would be a list. What to do? Well, if it's a list, we can again loop through it. Replace the above code with the following:


<dtml-in "acl_users.getUser(_['sequence-item']).getRoles()">
<dtml-var sequence-item><br>
</dtml-in>

By now, you should understand how this works. From here you can go about anywhere you want. Check out the documentation about how to access and change user data, the importance of roles, etc. etc. etc.

Finally, here's the complete code of the "showusers" method, in one piece. To address it, you need to add it to the URL, e.g. http://mysite.org/showusers or http://mysite.org/Members/myname/showusers . Note, though, that this method only shows users of the current hierarchy. If there's just one users folder in all of your Zope, this doesn't matter. But if, e.g., you have a user folder in myname/ , and adress myname/showusers , that will list only the users existing in that user folder, not those of the root user folder.


<dtml-var standard_html_header>
<p>
<h3>Hello <dtml-var "AUTHENTICATED_USER.getUserName()">!</h3>
</p>
<p>
The following users exist here:<br>
<dtml-in "acl_users.getUserNames()">
<p>
<b><dtml-var sequence-item></b><br>
<dtml-in "acl_users.getUser(_['sequence-item']).getRoles()">
<dtml-var sequence-item><br>
</dtml-in>
</p>
</dtml-in>
</p>
<dtml-var standard_html_footer>

A last note: Any comments, criticism, suggestions, questions, etc. are welcome. To contact me, write to: [email protected].

Have fun!