src/ldt/ldt/security/__init__.py
changeset 1362 df60d20f965c
parent 1191 b6e0b1811723
child 1407 fc9654218d53
equal deleted inserted replaced
1361:5087560b51b6 1362:df60d20f965c
     2 from django.contrib.auth import get_user_model
     2 from django.contrib.auth import get_user_model
     3 from django.contrib.contenttypes.models import ContentType
     3 from django.contrib.contenttypes.models import ContentType
     4 from django.core.signals import request_started
     4 from django.core.signals import request_started
     5 from ldt.security.permissionchecker import check_object_perm_for_user 
     5 from ldt.security.permissionchecker import check_object_perm_for_user 
     6 
     6 
     7 User = get_user_model()
       
     8 
     7 
     9 try:
     8 try:
    10     from threading import local
     9     from threading import local
    11 except ImportError:
    10 except ImportError:
    12     from django.utils._threading_local import local
    11     from django.utils._threading_local import local
    13         
    12 
    14 _thread_locals = local()
    13 _thread_locals = local()
    15 
    14 
    16 # The function that protect models is called on the first
    15 # The function that protect models is called on the first
    17 # HTTP request sent to the server (see function protect_models_request
    16 # HTTP request sent to the server (see function protect_models_request
    18 # in this file), and can not be called in this file directly 
    17 # in this file), and can not be called in this file directly 
    34     
    33     
    35 def get_anonymous_user():
    34 def get_anonymous_user():
    36     if hasattr(get_anonymous_user, 'anonymous_user'):
    35     if hasattr(get_anonymous_user, 'anonymous_user'):
    37         return get_anonymous_user.anonymous_user
    36         return get_anonymous_user.anonymous_user
    38     
    37     
    39     get_anonymous_user.anonymous_user = User.objects.get(id=settings.ANONYMOUS_USER_ID)
    38     get_anonymous_user.anonymous_user = get_user_model().objects.get(id=settings.ANONYMOUS_USER_ID)
    40     return get_anonymous_user.anonymous_user 
    39     return get_anonymous_user.anonymous_user 
    41 
    40 
    42 def get_current_user_or_admin():
    41 def get_current_user_or_admin():
    43     current = get_current_user()
    42     current = get_current_user()
    44     if current:
    43     if current:
    45         return current
    44         return current
    46     admin = User.objects.filter(is_superuser=True)[0]
    45     admin = get_user_model().objects.filter(is_superuser=True)[0]
    47     return admin
    46     return admin
    48 
    47 
    49 def protect_models():
    48 def protect_models():
    50     cls_list = get_models_to_protect()
    49     cls_list = get_models_to_protect()
    51     if cls_list:
    50     if cls_list: