S
S
Sergey Ganzhela2017-06-24 15:21:51
Django
Sergey Ganzhela, 2017-06-24 15:21:51

How to deal with account sharing?

Good afternoon!
The essence of the question is how in django you can deal with a group account, that is, do not allow more than one account (single) to be in the system, can there be any stray?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-06-24
@Sergiy_Hanzhela

Fourth link on google:

from django.conf import settings
from django.core.cache import cache, get_cache
from django.utils.importlib import import_module

class UserRestrictMiddleware(object):
    def process_request(self, request):
        """
        Checks if different session exists for user and deletes it.
        """
        if request.user.is_authenticated():
            cache = get_cache('default')
            cache_timeout = 86400
            cache_key = "user_pk_%s_restrict" % request.user.pk
            cache_value = cache.get(cache_key)

            if cache_value is not None:
                if request.session.session_key != cache_value:
                    engine = import_module(settings.SESSION_ENGINE)
                    session = engine.SessionStore(session_key=cache_value)
                    session.delete()
                    cache.set(cache_key, request.session.session_key, 
                              cache_timeout)
            else:
                cache.set(cache_key, request.session.session_key, cache_timeout)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question