I just tested and reread your original post.
auth.user_groups is a dict containing the id and role for each auth_group
for the logged in user
auth.user_group is a method of auth
from the book:
"auth.user_groups contains a dictionary where each key is the id of a group
of with the current logged in user is member of, the value is the
corresponding group role."
"auth.user_group(user_id) returns the id of the group uniquely associated
to the user identified by user_id."
This should work for both logged-in and logged-out users:
"if any (role in ['customer_service', 'admin'] for role in auth.user_groups.
itervalues()):"
On Tuesday, June 3, 2014 9:02:19 AM UTC-4, Michael Beller wrote:
>
> confirm that you have a logged in user first
>
> On Tuesday, June 3, 2014 3:00:18 AM UTC-4, Yebach wrote:
>>
>> If I use your if statement in my controller, this is the error I get
>>
>> <type 'exceptions.AttributeError'> 'Auth' object has no attribute
>> 'user_groups'
>>
>> why??
>>
>>
>> On Monday, June 2, 2014 3:13:26 PM UTC+2, Michael Beller wrote:
>>>
>>> If the user is logged in, then auth.user_groups contains a dictionary
>>> of the user's roles. You can also use auth.has_membership('role name') to
>>> check membership.
>>>
>>> You can search auth.user_groups for a role, e.g.,
>>> if any (role in ['customer_service', 'admin'] for role in auth.
>>> user_groups.itervalues()):
>>>
>>> On Monday, June 2, 2014 8:53:55 AM UTC-4, Yebach wrote:
>>>>
>>>> Hello
>>>>
>>>> I am trying to get user's role looking into membership table etc.
>>>>
>>>> Following all the codes on majority of forums my code does not work.
>>>>
>>>> this is my controller
>>>>
>>>> from gluon.tools import Auth
>>>>
>>>> database = request.cookies['mycookie'].value
>>>> db = DAL('postgres://postgres:[email protected]/' + database,
>>>> migrate=False)
>>>> auth = Auth(db)
>>>>
>>>> def index():
>>>> if not auth.is_logged_in():
>>>>
>>>> login_adress= auth.settings.login_url = URL('user','user',
>>>> args='login')
>>>> redirect (login_adress)
>>>>
>>>> print auth.user_id ###Works
>>>>
>>>> print auth.user_group ### result is <bound method Auth.user_group
>>>> of <gluon.tools.Auth object at 0x000000000A307F28>>
>>>> print auth.has_membership(auth.id_group('Member'),auth.user.id)
>>>>
>>>> <type 'exceptions.AttributeError'> 'NoneType' object has no attribute
>>>> 'role'
>>>>
>>>> Also, If I try with some DAL select sentances none of them work. error
>>>> is usually
>>>>
>>>> if code is
>>>> rows = db(db.auth_user).select()
>>>>
>>>> or
>>>>
>>>> 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)
>>>>
>>>> error is
>>>>
>>>> <type 'exceptions.KeyError'> 'auth_user'
>>>>
>>>> what am I doing wrong here. It is a f#"$"# simple ask. Get user role so
>>>> I can get a user a specific view based on that role (btw any suggestions
>>>> on
>>>> that would be nice).
>>>>
>>>> thank you
>>>>
>>>
--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.