开发者

django one session per user

开发者 https://www.devze.com 2023-02-20 06:18 出处:网络
based on docs ( http://docs.djangoproject.com/en/1.1/topics/http/sessions/ ) (yes - 1.1) Django creates unique sessions to all users. Logged user contains _auth_开发者_Go百科user_id. How can i achieve

based on docs ( http://docs.djangoproject.com/en/1.1/topics/http/sessions/ ) (yes - 1.1) Django creates unique sessions to all users. Logged user contains _auth_开发者_Go百科user_id. How can i achieve such check in login:

If new_login._auth_user_id in database:
   delete(sessions_containing_same_id_except_new_one)

The main idea is to allow only one session per user and delete old sessions.

UPDATE: The idea right now is to save sessionid while logging and if sessionid changes delete old entry before replacing. ATM missing part is to get that session id.

UPDATE: I got the sessionid with request.session.session_key. The problem was that sessionid is created after login. If you request key before it was created - it creates new one instead of giving any warning.


I created extra field for user (userattributes extends user):

class UserAttributes(User):
    last_session_key = models.CharField(blank=True, null=True, max_length=40)

and method:

def set_session_key(self, key):
    if self.last_session_key and not self.last_session_key == key:
        Session.objects.get(session_key=self.last_session_key).delete()
    self.last_session_key = key
    self.save()  

and i called it just after login:

auth.login(request, user)
user.userattributes.set_session_key(request.session.session_key)
0

精彩评论

暂无评论...
验证码 换一张
取 消