Monday, February 1, 2016

Re: Manually setting the session cookie

On Mon, Feb 1, 2016 at 10:13 AM, monoBOT <monobot.soft@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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1JjAzjA7rgpNLR4iQvFzqyD7M9GtYSXzYtauK4eacjhnw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment