1 from django.conf import settings |
1 from django.conf import settings |
2 from django.core.exceptions import MiddlewareNotUsed |
2 from django.core.exceptions import MiddlewareNotUsed |
3 from ldt.security.utils import protect_models, unprotect_models |
3 from ldt.security.utils import protect_models, unprotect_models, _thread_locals |
4 |
4 |
5 try: |
|
6 from threading import local |
|
7 except ImportError: |
|
8 from django.utils._threading_local import local |
|
9 |
|
10 _thread_locals = local() |
|
11 |
|
12 class SecurityMiddleware(object): |
5 class SecurityMiddleware(object): |
13 |
6 |
14 def __init__(self): |
7 def __init__(self): |
15 if not hasattr(settings, 'USE_GROUP_PERMISSIONS') or not settings.USE_GROUP_PERMISSIONS: |
8 if not hasattr(settings, 'USE_GROUP_PERMISSIONS') or not settings.USE_GROUP_PERMISSIONS: |
16 raise MiddlewareNotUsed() |
9 raise MiddlewareNotUsed() |
17 |
10 |
18 def process_request(self, request): |
11 def process_request(self, request): |
19 protect_models(request.user) |
12 _thread_locals.user = request.user |
|
13 protect_models() |
20 |
14 |
21 def process_response(self, request, response): |
15 def process_response(self, request, response): |
22 unprotect_models() |
16 unprotect_models() |
23 |
17 del _thread_locals.user |
|
18 |
24 return response |
19 return response |