-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
37 lines (23 loc) · 867 Bytes
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from django.http import HttpResponseRedirect
class RBACMiddleware(object):
def process_request(self, request):
if '_rbac_subject' in request.session:
request.subject = request.session['_rbac_subject']
else:
request.subject = None
def login_required(*args, **kwargs):
"""
This function decorate a request in order to check if there is a user logged in.
"""
def new_f(request, *args, **kwargs):
if '_rbac_subject' in request.session:
return f(request, *args, **kwargs)
return HttpResponseRedirect(redirect)
def dec(f):
return new_f
redirect = kwargs.get('redirect', '/accounts/login')
if (len(args) > 0) and callable(args[0]):
# Is a decorator without arguments.
f = args[0]
return new_f
return dec