1

In certain circumstances I'd like to let the staff to kick out some users. My django 1.8 site stores sessions in redis.

I tried this solution in my view:

#will be removed in 1.9 
from django.utils.importlib import import_module 

@staff_member_required 
def kickout_user(request, username):    

    u = User.objects.get(username = username)

    SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
    active_users = Request.objects.active_users(seconds=60)
    active_users_ids = [user.id for user in active_users]
    for session in stored_sessions:
            SessionStore = import_module(settings.SESSION_ENGINE).SessionStore
            s = SessionStore(session_key=session.session_key)
            session_uid = session.get_decoded().get('_auth_user_id')
            print 'session', session_uid
            if session_uid == u.id:
                print 'session going to be deleted for uid:',  session_uid
                session.delete()
                print  ' session deleted'+ u.username

But it gives this error:

global name 'Request' is not defined

There also some suggestions here, but they are either flawd or based writing additional middlewares that I find overkill and try to aovid.

Community
  • 1
  • 1
supermario
  • 2,625
  • 4
  • 38
  • 58
  • Is it in the views.py functions that you want to logout the user? If so you can `logout(request)`. – Shang Wang Dec 23 '15 at 17:08
  • Yes it is in the views, but in that case `reques.user` is a staff, not the user who is going to be kicked out. I updated my question to clarify this. – supermario Dec 23 '15 at 17:13
  • 1
    There's [another question](http://stackoverflow.com/questions/953879/how-to-force-user-logout-in-django) on how to log out all users. You might be able to adapt that to log out a particular user. – Travis Dec 23 '15 at 18:34
  • @Travis, please see my updated answer. – supermario Dec 23 '15 at 18:40
  • Looking at the question you refer to, it appears that you need [`django-request`](https://github.com/kylef/django-request) for `Request.objects.active_users(seconds=60)` to work. – Travis Dec 23 '15 at 20:12
  • @Travis if so, it rules out this answer. It would be overkill too. – supermario Dec 23 '15 at 22:48

1 Answers1

0

You can try django-force-logout

and also visit this_link for more assistance

Community
  • 1
  • 1
Amrit
  • 2,115
  • 1
  • 21
  • 41