On Mon, Feb 1, 2016 at 10:13 AM, monoBOT <
monobo...@gmail.com> wrote:
> Hello django!
>
> Im creating a saas with django, due to some project requirements need to
> manually change the user "on the fly" but have problems with the cookie,
> since I dont know how to manually (read programatically) set it.
>
> Any insights or a good place to start? Thanks!
>
I would simply add a secondary custom auth backend that always
authenticates the user if the argument switch_login is true:
class SwitchAuthBackend(object):
def authenticate(self, switch_login=False, new_user=None):
if switch_login:
return new_user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
pass
Now you can switch user simply using authenticate() and login():
new_user = ...
authenticated_user = authenticate(switch_login=True, new_user=new_user)
login(request, authenticated_user)
This would be an alternative to poking around and manually
manipulating the session attributes.
Cheers
Tom