[web2py] Basic authentication user id
Hello, When I do basic authentication to a web2py app from a *desktop* app (tried two test apps I wrote in Python and REALbasic) I can get the user's first name and last name from the web app, but I can't get his/her id. @service.xmlrpc @service.jsonrpc def getUserName(): return str(auth.user_id) + + auth.user.first_name + + auth.user.last_name The above function returns None for the user id and the correct first name and last name. The same function called from a *web based* user interface (written with qooxdoo and authenticated via the standard login form) works fine returning both the user id and his first and last names. What am I doing wrong? I'm on web2py 1.89.5 Thanks. -- Alexei Vinidiktov
Re: [web2py] Basic authentication user id
On Mar 7, 2011, at 7:51 AM, Alexei Vinidiktov wrote: When I do basic authentication to a web2py app from a *desktop* app (tried two test apps I wrote in Python and REALbasic) I can get the user's first name and last name from the web app, but I can't get his/her id. @service.xmlrpc @service.jsonrpc def getUserName(): return str(auth.user_id) + + auth.user.first_name + + auth.user.last_name The above function returns None for the user id and the correct first name and last name. The same function called from a *web based* user interface (written with qooxdoo and authenticated via the standard login form) works fine returning both the user id and his first and last names. What am I doing wrong? I'm on web2py 1.89.5 Just a shot in the dark: try auth.user.id instead.
Re: [web2py] Basic authentication user id
On Mon, Mar 7, 2011 at 10:16 PM, Jonathan Lundell jlund...@pobox.comwrote: On Mar 7, 2011, at 7:51 AM, Alexei Vinidiktov wrote: When I do basic authentication to a web2py app from a *desktop* app (tried two test apps I wrote in Python and REALbasic) I can get the user's first name and last name from the web app, but I can't get his/her id. @service.xmlrpc @service.jsonrpc def getUserName(): return str(auth.user_id) + + auth.user.first_name + + auth.user.last_name The above function returns None for the user id and the correct first name and last name. The same function called from a *web based* user interface (written with qooxdoo and authenticated via the standard login form) works fine returning both the user id and his first and last names. What am I doing wrong? I'm on web2py 1.89.5 Just a shot in the dark: try auth.user.id instead. Thanks, Jonathan! Your suggestion worked. What's odd is that for RIA apps with web based GUI both user.id and user_id work! I triple checked. -- Alexei Vinidiktov
Re: [web2py] Basic authentication user id
In the file gluon/tools.py line 808 in trunk the Auth class __init__ method you will find these lines if auth and auth.last_visit and auth.last_visit\ + datetime.timedelta(days=0, seconds=auth.expiration)\ request.now: self.user = auth.user self.user_id = self.user.id auth.last_visit = request.now I am guessing this branch does not run when used in a standalone environment and the else branch not shown above runs instead which sets the 3 values to None.
Re: [web2py] Basic authentication user id
On Mar 7, 2011, at 8:54 AM, ron_m wrote: In the file gluon/tools.py line 808 in trunk the Auth class __init__ method you will find these lines if auth and auth.last_visit and auth.last_visit\ + datetime.timedelta(days=0, seconds=auth.expiration)\ request.now: self.user = auth.user self.user_id = self.user.id auth.last_visit = request.now I am guessing this branch does not run when used in a standalone environment and the else branch not shown above runs instead which sets the 3 values to None. I was wondering about that, but it doesn't appear that auth.user is set to None; otherwise auth.user.id wouldn't work either.
Re: [web2py] Basic authentication user id
I am guessing some more because I didn't trace the code but I think auth.user eventually gets set in login_bare without setting auth.user_id but then do standalone apps actually login? I could only find 3 places where self.user_id is used in the class, the 2 places in the if else and as a test to see which of the nav bars (login or logout) should be placed at the top of the page. I could not see anywhere else that auth.user_id is set outside the class. I guess bottom line is Alexei has a solution. :-)
Re: [web2py] Basic authentication user id
On Mar 7, 2011, at 9:23 AM, ron_m wrote: I am guessing some more because I didn't trace the code but I think auth.user eventually gets set in login_bare without setting auth.user_id but then do standalone apps actually login? I could only find 3 places where self.user_id is used in the class, the 2 places in the if else and as a test to see which of the nav bars (login or logout) should be placed at the top of the page. I could not see anywhere else that auth.user_id is set outside the class. Good point. I was wondering if auth.user_id ought to be set wherever auth.user is set, for consistency; I see about six places where that doesn't happen. But how about this: make Auth.user_id a read-only property instead of a plain attribute? (Read-only just as a precaution; there's no good reason for someone to write it.) Something like: def _get_user_id(self): accessor for user_id return self.user and self.user.id or None user_id = property(_get_user_id, doc=user.id or None) This would be backward-compatible with the current logic, and make auth.user_id always identical to auth.user.id (whenever auth.user is defined) without having to catch every assignment of auth.user. Work for you, Massimo?