author | Madhusudan.C.S <madhusudancs@gmail.com> |
Fri, 07 Aug 2009 03:56:08 +0530 | |
changeset 16 | bed14c9685a5 |
parent 12 | aac4944aca52 |
permissions | -rw-r--r-- |
5
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
1 |
"""This module contains the access checks |
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
2 |
""" |
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
3 |
|
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
4 |
|
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
5 |
__authors__ = [ |
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
6 |
'"Madhusudan.C.S" <madhusudancs@gmail.com>', |
88ae12bc6280
Access check helper file added.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
7 |
] |
7
3e6916eb3d2c
Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
8 |
|
3e6916eb3d2c
Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
9 |
|
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
10 |
from functools import wraps |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
11 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
12 |
from django.shortcuts import render_to_response |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
13 |
from django.template import RequestContext |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
14 |
|
12 | 15 |
# Dictionary containing access checks for views. |
16 |
# Keys: View function name. |
|
17 |
# Values: proposer or staff |
|
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
18 |
rights = {} |
12 | 19 |
|
20 |
def register(access_type): |
|
21 |
"""Function to register access type for a View function |
|
22 |
""" |
|
23 |
||
24 |
def wrapper(func): |
|
25 |
"""The decorator for registering access checks. |
|
26 |
""" |
|
27 |
||
28 |
rights['%s.%s' % (func.__module__, func.__name__)] = access_type |
|
29 |
return func |
|
30 |
||
31 |
return wrapper |
|
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
32 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
33 |
def checkAccess(func): |
7
3e6916eb3d2c
Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
34 |
""" To check the access of the user and then return the appropriate function |
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
35 |
object. |
7
3e6916eb3d2c
Added checkAccess routine
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
5
diff
changeset
|
36 |
""" |
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
37 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
38 |
@wraps(func) |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
39 |
def wrapper(request, *args, **kwargs): |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
40 |
"""The decorator for access check. |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
41 |
""" |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
42 |
|
12 | 43 |
user_kind = rights['%s.%s' % (func.__module__, func.__name__)] |
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
44 |
user = request.user |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
45 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
46 |
template = 'projrev/error.html' |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
47 |
context = {} |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
48 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
49 |
if user.is_authenticated(): |
12 | 50 |
if user_kind == 'reviewer': |
8
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
51 |
if user.is_staff: |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
52 |
return func(request, *args, **kwargs) |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
53 |
else: |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
54 |
context['not_staff'] = True |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
55 |
return render_to_response(template, RequestContext(request, context)) |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
56 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
57 |
if user_kind == 'proposer': |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
58 |
if not user.is_staff: |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
59 |
return func(request, *args, **kwargs) |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
60 |
else: |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
61 |
context['not_proposer'] = True |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
62 |
return render_to_response(template, RequestContext(request, context)) |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
63 |
else: |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
64 |
context['not_authenticated'] = True |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
65 |
return render_to_response(template, RequestContext(request, context)) |
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
66 |
|
294ff7ac9cb6
Added new set of files.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
7
diff
changeset
|
67 |
return wrapper |