How to list all groups that a user belongs to (memberships) ?

1,921 views
Skip to first unread message

Francisco Barretto

unread,
Oct 30, 2012, 11:15:23 AM10/30/12
to web...@googlegroups.com
Hi Folks!

How do I list all groups that a user belong? I've found out that Auth has this function:

groups(self)
displays the groups
and their roles for the logged in user

I dont manage to get it working. I've tried something like this:

def myGroups():
   
return dict(auth.groups(auth.user_id))

and get this error:

<type 'exceptions.TypeError'> groups() takes exactly 1 argument (2 given)


any idea on how to do this properly? Thanks!

Richard Vézina

unread,
Oct 30, 2012, 11:51:26 AM10/30/12
to web...@googlegroups.com
I don't know if web2py has a given command or how to use it, but this query should give you what you the information you are searching :

db((db.auth_user.email == 'USER_EMAIL')&(db.auth_membership.user_id == db.auth_user.id)&(db.auth_group.id==db.auth_membership.group_id)).select(db.auth_group.ALL)

Richard

--
 
 
 

Richard Vézina

unread,
Oct 30, 2012, 12:07:22 PM10/30/12
to web...@googlegroups.com
Ok!

I read the book.

You should not pass the user id.

auth.groups() and not auth.groups(auth.user.id)

Also you can go to this URL :

http://127.0.0.1/yourappname/default/user/groups and you will get the list of group your current logged user is in.

Richard

Francisco Barretto

unread,
Oct 30, 2012, 12:35:46 PM10/30/12
to web...@googlegroups.com
Ok Richard, I've got it. You are right. Without parameters it works but I cant manage to get a userfull list out of it. Accesint the webpage doesn't help either, since I need to compare memberships inside the controller actions.

I would need a list containing all user groups but I got instead, some kind of raw table which I don't know how to iterate with.

Can you help me go further?

Thanks, mate!

Francisco Barretto

unread,
Oct 30, 2012, 12:45:34 PM10/30/12
to
In addition to what I've just said, I've looked up here (http://www.web2py.com/examples/static/epydoc/web2py.gluon.tools.Auth-class.html) for more info about this groups() method but I've found nothing about what "type" does it returns.

When I've tried to make a dict out of it, here's what I've got in a view page: {'Groups': <gluon.html.TABLE object at 0x3e9c790>}
Otherwise, if i throw it to the view page without trying to make a dictionary, it shows the groups information but all page formatting, including headings and footers disappears.

Richard Vézina

unread,
Oct 30, 2012, 12:56:55 PM10/30/12
to web...@googlegroups.com
I didn't test, but you may try this :

for row in auth.groups():
    print row 

row should contain the different column of auth_group table and you can usually get those column like this row.name (column name in this example).

You could also try as_list() :


If it doesn't work, use the query I wrote above :

rows = query
for row in rows:
     do stuff

Richard

--
 
 
 

Niphlod

unread,
Oct 30, 2012, 12:59:07 PM10/30/12
to web...@googlegroups.com
groups returns a TABLE(), not a list of records.

Francisco Barretto

unread,
Oct 30, 2012, 1:19:29 PM10/30/12
to web...@googlegroups.com
Ok, It does return a table(). I've tried to parse it so I could access the information I need (group names) without success.

Richard, Since the name of the db column which stores the group name (role) is auth_group.role, could the solution be something like this?

groups = []
for row in auth.groups():
    groups
.append(row.role)
   
return (groups)

I'm getting

<type 'exceptions.AttributeError'> 'TR' object has no attribute 'auth_group'

I've also tried "row.auth_group.role" but I won't work either. Obviously it is returning some kind of HTML table formated data. Will I have to parse it manually?

Francisco Barretto

unread,
Oct 30, 2012, 1:37:23 PM10/30/12
to web...@googlegroups.com
Got it!

def myGroups():
    groups = []
    rows = db((db.auth_user.email == auth.user.email)&(db.auth_membership.user_id == auth.user_id)&(db.auth_group.id==db.auth_membership.group_id)).select(db.auth_group.ALL)
    for row in rows:
        groups.append(row.role)
    return (groups)

Works just fine: returns a list of user's group names.

Thanks Richard!

villas

unread,
Oct 30, 2012, 1:59:42 PM10/30/12
to web...@googlegroups.com
For logged in user just do:

>>> auth.user_groups
{1: 'admin', 2: 'wiki_editor'}

Richard Vézina

unread,
Oct 30, 2012, 2:09:09 PM10/30/12
to web...@googlegroups.com
:)

auth.user_groups[auth.user.id]

Richard

--
 
 
 

villas

unread,
Oct 30, 2012, 2:30:10 PM10/30/12
to web...@googlegroups.com
Hi Richard,
Hmm,  not sure what you mean,  I believe that auth.user_groups is a dict:
{auth_group.id : auth_group.role }
so what you appear to suggest may not make sense...


On Tuesday, October 30, 2012 6:09:33 PM UTC, Richard wrote:
:)

auth.user_groups[auth.user.id]

Richard


Richard Vézina

unread,
Oct 30, 2012, 3:35:57 PM10/30/12
to web...@googlegroups.com
You right... I wrongly interpret the order of the id as user id I don't why...

So, the solution Francisco was searching for is :

list123 = auth.user_groups.values()

Richard

--
 
 
 

Reply all
Reply to author
Forward
0 new messages