"Impersonate" user

133 views
Skip to first unread message

joe.ba...@gmail.com

unread,
Apr 3, 2022, 7:55:34 PM4/3/22
to py4web
One of the cooler and more useful features of web2py is the "impersonate" feature for Auth.  It lets me log in as one of my users and see EXACTLY what the user sees and access their information directly.  Another click and I'm back to being "me" without missing a beat.

Looking into migrating to py4web, I'm wondering how this useful feature can be added (safely) to py4web sites using "tags" as we now have.

-- Joe

Massimo DiPierro

unread,
Apr 3, 2022, 8:18:09 PM4/3/22
to joe.ba...@gmail.com, py4web
Good idea. Will add this asap

--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/py4web/5b47c390-2f7c-47b3-99bb-f19d5c719898n%40googlegroups.com.

icodk

unread,
Oct 4, 2023, 5:03:37 PM10/4/23
to py4web
web2py impersonate user feature is  very useful for me also, from the obvious reason that Joe mentioned.
Is it already implemented ?

Massimo DiPierro

unread,
Oct 5, 2023, 9:54:20 AM10/5/23
to icodk, py4web
There is no API for it but it is possible. Let me create an example and maybe add an API for it

icodk

unread,
Oct 5, 2023, 10:03:53 AM10/5/23
to py4web
That will be great 
Thanks

Massimo

unread,
Oct 8, 2023, 5:56:32 PM10/8/23
to py4web
Please give this a try (requires latest master):

@action("switch_impersonation")
@action.uses(auth.user)
def "switch_impersonation():
     if not auth.is_impersonating():
          # impersonate and redirect
          auth.impersonate(db.auth(email="a...@example.com").id, next_url=URL("index"))
     else:
           auth.stop_impersonation(next_url=URL("index"))

icodk

unread,
Oct 9, 2023, 5:51:18 AM10/9/23
to py4web
Tested it and got the following error:
    auth.impersonate(db.auth(email="a...@example.com").id, next_url=URL("index"))
  File "C:\Python310\lib\site-packages\pydal\base.py", line 769, in __getattr__
    return BasicStorage.__getattribute__(self, key)
AttributeError: 'DAL' object has no attribute 'auth'

Massimo DiPierro

unread,
Oct 9, 2023, 11:15:51 AM10/9/23
to icodk, py4web
my bad. in the example

auth.impersonate(db.auth(email="a...@example.com").id)

should be

auth.impersonate(db.auth_user(email="a...@example.com").id)

and you should use an existing email to impersonate. 


Massimo

unread,
Oct 22, 2023, 9:21:33 PM10/22/23
to py4web
I have a new version and I think it works fine. Please check it. I also added an example in the documentation (not published yet).
The reason the email appears as "None" when impersonating a user is probably that the user being impersonated does not have an email.
That would be a bigger problem than just displaying None.

Can you please confirm this works now and you can display the email of a valid impersonated user?

Massimo

Massimo DiPierro

unread,
Oct 23, 2023, 6:34:46 AM10/23/23
to py4web
looks like this needs one more iteration. ;-)

Reply all
Reply to author
Forward
0 new messages