Reputation: 536
I am working in a web2py application, where I need to access current user in modules, so is there any possibility to access current user in web2py modules, and yes then How can I access this?
Thanks
Upvotes: 0
Views: 499
Reputation: 25536
One option is to use the thread local current
object. Assuming you are using the standard name auth
for the Auth
object, in a module, you can do:
from gluon import current
def myfunction():
user = current.globalenv['auth'].user
You can also explicitly add the user object as an attribute of current
within a model file:
from gluon import current
current.auth_user = auth.user
And then in the module, you can access current.auth_user
.
As noted here, you should not assign properties of the current
object to top level variables or class attributes within the module (this is because the current
object is local to each thread, but such assignments will only take place once, when the module is first imported).
Another option is simply to pass the auth
object to your functions or classes from the module. For example, in a module:
def myfunction(auth):
user = auth.user
Upvotes: 1