Help with Authentication

7 views
Skip to first unread message

El Tea

unread,
Jul 21, 2009, 1:42:58 PM7/21/09
to TurboGears
I have a pretty good grip on predicates. What I am not seeing in the
TG documentation is how to find user information for use in the
controllers and templates. What I want to do:

a) Get the object for the current user.
b) Determine, in a template, if the current user's group is 'manager'
so I can render the page differently.


My apologies if this is an easy one; I've been in the docs for an hour
and all I can find info on is authorization to protect areas of the
site. If you have a handy link with examples, I'd be happy to take
that.

El Tea

unread,
Jul 21, 2009, 3:29:10 PM7/21/09
to TurboGears
More searching as per the post on the bottom of this page (
http://www.turbogears.org/2.0/docs/main/Auth/Authentication.html ) has
led me to believe the solutions are in tg.identity, but I don't know
where the documentation is.

El Tea

percious

unread,
Jul 21, 2009, 3:50:27 PM7/21/09
to TurboGears
you can get the current logged in identity by grabbing it from the
environ:

identity = request.environ.get('repoze.who.identity')
user = identity.user
group_names = identity.groups

Lots of folks do something like this in their __call__ method of their
BaseController:

tmpl_context.identity = request.environ.get('repoze.who.identity')

See docs here:
http://www.turbogears.org/2.0/docs/main/Auth/Customization.html#enabling-the-quickstart-in-an-existing-project

On Jul 21, 1:29 pm, El Tea <the.el....@gmail.com> wrote:
> More searching as per the post on the bottom of this page (http://www.turbogears.org/2.0/docs/main/Auth/Authentication.html) has

Michael Pedersen

unread,
Jul 22, 2009, 1:08:54 AM7/22/09
to turbo...@googlegroups.com
I've been using my medium sized application as a reference on the IRC channel, and it's helped people a lot. So, here's the link to the app itself: http://hgweb.icelus.tzo.com/cardlist/file/3a29f01078a4

In particular, it seems that you would benefit from looking at the following template: http://hgweb.icelus.tzo.com/cardlist/file/3a29f01078a4/cardlist/templates/master.html

I have examples in there of permission based accesses (look at lines 22, 23, 25), and group based accesses (look at line 39).

That answers (b) from your email.  To answer (a), look at the same link, line 35. That gets you your identity object in a template. In controller code, or model code, look at this link: http://hgweb.icelus.tzo.com/cardlist/file/3a29f01078a4/cardlist/controllers/signups.py line 71. copy_from_vb is a method I need for my app, but not generally applicable elsewhere. However, request.identity will give you what you're looking for.

Hope that helps!
--
Michael J. Pedersen
My IM IDs: Jabber/pede...@icelus.tzo.com, ICQ/103345809, AIM/pedermj022171
         Yahoo/pedermj2002, MSN/pederm...@hotmail.com

El Tea

unread,
Jul 31, 2009, 1:12:07 PM7/31/09
to TurboGears
I could not actually get user=identity.user to work :(

Two options for getting the user; is one better than the other?

1)
uid=request.identity['repoze.who.userid']
user=User.by_user_name(uid)

2)
user2=request.identity['user']

These both get the same user object in my tests. Are they both
correct to use, or is option number 2 an undocumented feature that may
go away in a future TG version?


El Tea

On Jul 21, 3:50 pm, percious <ch...@percious.com> wrote:
> you can get the current logged in identity by grabbing it from the
> environ:
>
> identity = request.environ.get('repoze.who.identity')
> user = identity.user
> group_names = identity.groups
>
> Lots of folks do something like this in their __call__ method of their
> BaseController:
>
> tmpl_context.identity = request.environ.get('repoze.who.identity')
>
> See docs here:http://www.turbogears.org/2.0/docs/main/Auth/Customization.html#enabl...
Reply all
Reply to author
Forward
0 new messages