| author | gibus |
| Fri, 05 Oct 2012 10:21:07 +0200 | |
| changeset 470 | 077be006891e |
| parent 449 | 5387c032df35 |
| child 522 | c9c2148f09c9 |
| permissions | -rw-r--r-- |
| 0 | 1 |
from django.conf import settings |
2 |
from django.contrib.auth import REDIRECT_FIELD_NAME |
|
3 |
from django.contrib.auth.models import Permission |
|
4 |
from django.contrib.contenttypes.models import ContentType |
|
5 |
from django.shortcuts import get_object_or_404 |
|
6 |
from django.core.urlresolvers import reverse |
|
7 |
from django.http import HttpResponseRedirect |
|
8 |
from django.utils.http import urlquote |
|
9 |
from django.db.models import Q |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
10 |
from piston.utils import rc |
| 0 | 11 |
import logging |
12 |
||
13 |
from cm.models import * |
|
14 |
from cm import cm_settings |
|
15 |
from cm.exception import UnauthorizedException |
|
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
16 |
from cm.cm_settings import DECORATED_CREATORS |
| 0 | 17 |
|
18 |
def get_request_user(request): |
|
19 |
if request and request.user and not request.user.is_anonymous(): |
|
20 |
user = request.user |
|
21 |
else: |
|
22 |
user = None |
|
23 |
return user |
|
24 |
||
25 |
## Permission functions |
|
26 |
class FakeRequest(object): |
|
27 |
def __init__(self, user): |
|
28 |
self.user = user |
|
29 |
||
30 |
def user_has_perm(user, perm_name, text=None): |
|
31 |
return has_perm(FakeRequest(user),perm_name, text) |
|
32 |
||
33 |
def has_perm(request, perm_name, text=None): |
|
34 |
# bypass sec if NO_SECURITY |
|
35 |
if cm_settings.NO_SECURITY: |
|
36 |
return True |
|
37 |
||
38 |
# make sure perm exist |
|
39 |
assert Permission.objects.get(codename=perm_name) |
|
40 |
||
41 |
user = get_request_user(request) |
|
42 |
||
43 |
if user and user.is_staff: |
|
44 |
return True |
|
45 |
||
46 |
if not text: |
|
47 |
return UserRole.objects.filter(user=user, text=None).filter(Q(role__permissions__codename__exact=perm_name)).count() != 0 |
|
48 |
else: |
|
49 |
# local role only ADDS permissions: |
|
50 |
# either a global or a local role with appropriate permissions |
|
51 |
#return UserRole.objects.filter(user=user).filter(Q(text=text) | Q(text=None)).filter(Q(role__permissions__codename__exact=perm_name)).count() != 0 |
|
52 |
||
53 |
# local role OVERRIDES global role: |
|
54 |
if UserRole.objects.filter(Q(user=user),Q(text=text),~Q(role=None)): # if non void local role |
|
55 |
return UserRole.objects.filter(user=user).filter(text=text).filter(Q(role__permissions__codename__exact=perm_name)).count() != 0 |
|
56 |
else: |
|
|
210
e4715ab65e2d
fix security pb (too restrictive): logged users should inherit anon roles (if no text role is defined)
raph
parents:
102
diff
changeset
|
57 |
# local role for anon users |
|
e4715ab65e2d
fix security pb (too restrictive): logged users should inherit anon roles (if no text role is defined)
raph
parents:
102
diff
changeset
|
58 |
# OR global role for anon users |
|
e4715ab65e2d
fix security pb (too restrictive): logged users should inherit anon roles (if no text role is defined)
raph
parents:
102
diff
changeset
|
59 |
# OR global role for this user |
|
e4715ab65e2d
fix security pb (too restrictive): logged users should inherit anon roles (if no text role is defined)
raph
parents:
102
diff
changeset
|
60 |
return UserRole.objects.filter(Q(user=user) | Q(user=None)).filter(Q(text=None) | Q(text=text)).filter(Q(role__permissions__codename__exact=perm_name)).count() != 0 |
|
e4715ab65e2d
fix security pb (too restrictive): logged users should inherit anon roles (if no text role is defined)
raph
parents:
102
diff
changeset
|
61 |
#return UserRole.objects.filter(user=user).filter(text=None).filter(Q(role__permissions__codename__exact=perm_name)).count() != 0 |
| 0 | 62 |
|
63 |
def has_own_perm(request, perm_name, text, comment): |
|
64 |
||
65 |
user = get_request_user(request) |
|
66 |
||
67 |
if not user: |
|
68 |
return False |
|
69 |
||
70 |
# bypass sec if NO_SECURITY |
|
71 |
if cm_settings.NO_SECURITY: |
|
72 |
return True |
|
73 |
||
74 |
# make sure perm exist |
|
75 |
assert Permission.objects.get(codename=perm_name) |
|
76 |
||
77 |
# 2 special cases for comment own edition: |
|
78 |
||
79 |
# 1 |
|
80 |
# if perm = can_edit_own_comment and |
|
81 |
# text is a priori moderated and |
|
82 |
# comment is approved and |
|
83 |
# don't have moderation rights and |
|
84 |
if comment and comment.state == 'approved' and \ |
|
85 |
perm_name == 'can_edit_comment_own' and \ |
|
86 |
text.last_text_version.mod_posteriori == False and \ |
|
87 |
not has_perm(request, 'can_manage_text', text=text): |
|
88 |
return False |
|
89 |
||
90 |
# 2 |
|
91 |
# if perm = can_edit_own_comment and and |
|
92 |
# text is a posteriori moderated and |
|
93 |
# there is a reply |
|
94 |
# don't have moderation rights and |
|
95 |
if comment and comment.state == 'approved' and \ |
|
96 |
perm_name == 'can_edit_comment_own' and \ |
|
97 |
text.last_text_version.mod_posteriori == True and \ |
|
98 |
comment.comment_set.count() != 0 and \ |
|
99 |
not has_perm(request, 'can_manage_text', text=text): |
|
100 |
return False |
|
101 |
||
|
355
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
102 |
actual_own_user = False |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
103 |
if comment.user == request.user: |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
104 |
if DECORATED_CREATORS: |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
105 |
if request.GET.get('name', None) == comment.get_name(): |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
106 |
actual_own_user = True |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
107 |
else: |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
108 |
actual_own_user = True |
|
c926868cf7e6
if DECORATED_CREATORS take into account the "fake" username if has_own_perm()
gibus
parents:
295
diff
changeset
|
109 |
return (actual_own_user and has_perm(request, perm_name, text=text)) |
| 0 | 110 |
|
111 |
def is_authenticated(request): |
|
112 |
# We customize this to be able to monkey patch it if needed |
|
113 |
return request.user.is_authenticated() |
|
114 |
||
115 |
||
116 |
## Content access functions |
|
117 |
||
118 |
def get_texts_with_perm(request, perm_name): |
|
119 |
assert Permission.objects.get(codename=perm_name) |
|
120 |
||
121 |
user = get_request_user(request) |
|
122 |
||
123 |
if user and user.is_staff: |
|
124 |
return Text.objects.all() |
|
125 |
||
126 |
# local role only ADDS permissions: |
|
127 |
## global perm |
|
128 |
# if UserRole.objects.filter(text=None).filter(role__permissions__codename__exact=perm_name).filter(Q(user=user) | Q(user=None) ).count() != 0: |
|
129 |
# return Text.objects.all().distinct() |
|
130 |
## only texts with role with perm |
|
131 |
#else: |
|
132 |
# return Text.objects.filter(Q(userrole__role__permissions__codename__exact=perm_name), Q(userrole__user=user) | Q(userrole__user=None)).distinct() |
|
133 |
||
134 |
# local role OVERRIDES global role: |
|
| 102 | 135 |
texts_with_local_role = Text.objects.filter(userrole__in=UserRole.objects.filter(Q(user=user) | Q(user=None)).filter(~Q(role=None))) |
| 0 | 136 |
#Text.objects.filter(Q(userrole__user=user) & ~Q(userrole__role=None)) |
137 |
texts_without_local_role = Text.objects.exclude(id__in=texts_with_local_role) |
|
138 |
||
139 |
texts_with_local_role_with_perm = Text.objects.filter(id__in=texts_with_local_role).filter(Q(userrole__role__permissions__codename__exact=perm_name), Q(userrole__user=user) | Q(userrole__user=None)).distinct() |
|
140 |
||
141 |
# global perm? |
|
142 |
if UserRole.objects.filter(text=None).filter(role__permissions__codename__exact=perm_name).filter(Q(user=user) | Q(user=None) ).count() != 0: |
|
143 |
texts_without_local_role_with_perm = Text.objects.filter(id__in=texts_without_local_role) |
|
144 |
else: |
|
145 |
texts_without_local_role_with_perm = [] |
|
146 |
||
147 |
ids = set([t.id for t in texts_with_local_role_with_perm]).union(set([t.id for t in texts_without_local_role_with_perm])) |
|
148 |
return Text.objects.filter(id__in=ids) |
|
149 |
||
150 |
def get_viewable_comments(request, comments, text, order_by=('created',)): |
|
151 |
""" |
|
152 |
Get comments visibles by user |
|
153 |
comments: queryset |
|
154 |
""" |
|
155 |
user = get_request_user(request) |
|
156 |
||
157 |
if user and has_perm(request, 'can_view_unapproved_comment', text=text): |
|
158 |
return list(comments.order_by(*order_by)) |
|
159 |
else: |
|
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
160 |
# Fetch role_model to process specific behaviour for role_teacher model |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
161 |
from cm.models import ApplicationConfiguration |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
162 |
role_model = ApplicationConfiguration.get_key('workspace_role_model') |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
163 |
|
| 0 | 164 |
if has_perm(request, 'can_view_approved_comment', text=text): |
165 |
visible_comments = comments.filter(state = 'approved').order_by(*order_by) |
|
166 |
# filter comments with a non visible (i.e. moderated) comment in the above thread |
|
167 |
comments_thread_viewable = [c for c in visible_comments if c.is_thread_full_visible()] |
|
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
168 |
|
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
169 |
# for role_teacher role model, do not show 'individual student' comments |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
170 |
if (role_model == 'teacher'): |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
171 |
unfiltered_comments = list(comments_thread_viewable) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
172 |
for c in unfiltered_comments: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
173 |
if c.user_id and c.user_id != 1: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
174 |
try: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
175 |
userrole = UserRole.objects.get(user=c.user, text=text) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
176 |
except: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
177 |
userrole = UserRole.objects.get(user=None, text=None) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
178 |
if userrole.role_id == None: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
179 |
role = c.user.get_profile().global_userrole().role |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
180 |
else: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
181 |
role = userrole.role |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
182 |
if role.name == 'Individual student': |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
183 |
comments_thread_viewable.remove(c) |
| 0 | 184 |
return comments_thread_viewable |
185 |
elif user and has_perm(request, 'can_view_comment_own', text=text): |
|
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
186 |
if DECORATED_CREATORS: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
187 |
visible_comments = comments.filter(name=request.GET.get('name', None)).order_by(*order_by) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
188 |
else: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
189 |
visible_comments = comments.filter(user=user).order_by(*order_by) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
190 |
|
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
191 |
# for role_teacher role model, add 'teacher' comments |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
192 |
if (role_model == 'teacher'): |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
193 |
with_teachers = [] |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
194 |
for u in list(User.objects.filter(userrole__role__name = 'Teacher')): |
|
470
077be006891e
Fix filtering of teachers' comments for individual students in role_teacher role model when using DECORATED_CREATORS.
gibus
parents:
449
diff
changeset
|
195 |
with_teachers.append(u.id) |
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
196 |
|
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
197 |
# add admin and current user |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
198 |
admin = User.objects.get(id=1) |
|
470
077be006891e
Fix filtering of teachers' comments for individual students in role_teacher role model when using DECORATED_CREATORS.
gibus
parents:
449
diff
changeset
|
199 |
with_teachers.append(admin.id) |
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
200 |
if DECORATED_CREATORS: |
|
470
077be006891e
Fix filtering of teachers' comments for individual students in role_teacher role model when using DECORATED_CREATORS.
gibus
parents:
449
diff
changeset
|
201 |
myself = request.GET.get('name', None) |
|
077be006891e
Fix filtering of teachers' comments for individual students in role_teacher role model when using DECORATED_CREATORS.
gibus
parents:
449
diff
changeset
|
202 |
visible_comments = comments.filter(Q(user__id__in=with_teachers) | Q(name=myself)).order_by(*order_by) |
|
449
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
203 |
else: |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
204 |
with_teachers.append(user.id) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
205 |
visible_comments = comments.filter(user__id__in=with_teachers).order_by(*order_by) |
|
5387c032df35
In role_teacher model, individual students can see their own comments but also teacher's ones, whereas individual students' comments cannot be seen by students.
gibus
parents:
355
diff
changeset
|
206 |
|
| 0 | 207 |
# filter comments with a non visible (i.e. moderated) comment in the above thread |
| 16 | 208 |
comments_thread_viewable = [c for c in visible_comments if c.is_thread_full_visible(own_user=user)] |
| 0 | 209 |
return comments_thread_viewable |
210 |
else: |
|
211 |
return [] |
|
212 |
||
213 |
def get_viewable_activities(request=None, act_types={}, text=None): |
|
214 |
""" |
|
215 |
Get activities user in request is allowed to see |
|
216 |
""" |
|
217 |
from cm.security import has_perm, get_texts_with_perm, get_viewable_comments |
|
218 |
||
219 |
selected_activities = reduce(list.__add__,[Activity.VIEWABLE_ACTIVITIES[k] for k in act_types.keys() if act_types[k]], []) |
|
220 |
||
221 |
activities = Activity.objects.filter(type__in=selected_activities) |
|
222 |
if text: |
|
223 |
activities = activities.filter(text=text) |
|
224 |
||
225 |
if not has_perm(request, 'can_manage_workspace'): |
|
226 |
texts = get_texts_with_perm(request, 'can_view_text') |
|
227 |
activities = activities.filter(Q(text__in=texts)) |
|
228 |
||
229 |
comments = [] |
|
230 |
[comments.extend(get_viewable_comments(request, t.last_text_version.comment_set.all(), t)) for t in texts] |
|
231 |
||
232 |
activities = activities.filter(Q(comment__in=comments) | Q(comment=None)) |
|
233 |
return activities.order_by('-created') |
|
234 |
||
235 |
||
236 |
# won't need to be overridden, should it be moved to another file ? |
|
237 |
def list_viewable_comments(request, comments_list, text): |
|
238 |
ret = [] |
|
239 |
for comment in comments_list : |
|
240 |
ret += [comment] + list_viewable_comments(request, get_viewable_comments(request, comment.comment_set, text), text) |
|
241 |
return ret |
|
242 |
||
243 |
||
244 |
# decorators (simple wrappers around above functions) |
|
245 |
def has_global_perm(perm_name, must_be_logged_in=False, redirect_field_name=REDIRECT_FIELD_NAME): |
|
246 |
def _dec(view_func): |
|
247 |
def _check_global_perm(request, *args, **kwargs): |
|
248 |
if must_be_logged_in and not is_authenticated(request): |
|
|
284
730dd9fb2c77
simplification of security check: all unauthorized exceptions are dealt with by context processors
raph
parents:
210
diff
changeset
|
249 |
raise UnauthorizedException('Should be logged in') |
| 0 | 250 |
|
251 |
if has_perm(request, perm_name, text=None): |
|
252 |
return view_func(request, *args, **kwargs) |
|
253 |
||
254 |
raise UnauthorizedException('No global perm %s' % perm_name) |
|
255 |
_check_global_perm.__doc__ = view_func.__doc__ |
|
256 |
_check_global_perm.__dict__ = view_func.__dict__ |
|
257 |
||
258 |
return _check_global_perm |
|
259 |
return _dec |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
260 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
261 |
def has_perm_on_text_api(perm_name, must_be_logged_in=False, redirect_field_name=REDIRECT_FIELD_NAME): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
262 |
return _has_perm_on_text(perm_name, must_be_logged_in, redirect_field_name, api=True) |
| 0 | 263 |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
264 |
def has_perm_on_text(perm_name, must_be_logged_in=False, redirect_field_name=REDIRECT_FIELD_NAME, api=False): |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
265 |
return _has_perm_on_text(perm_name, must_be_logged_in, redirect_field_name, api) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
266 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
267 |
def _has_perm_on_text(perm_name, must_be_logged_in=False, redirect_field_name=REDIRECT_FIELD_NAME, api=False): |
| 0 | 268 |
""" |
269 |
decorator protection checking for perm for logged in user |
|
270 |
force logged in (i.e. redirect to connection screen if not if must_be_logged_in |
|
271 |
""" |
|
272 |
def _dec(view_func): |
|
273 |
def _check_local_perm(request, *args, **kwargs): |
|
274 |
if cm_settings.NO_SECURITY: |
|
275 |
return view_func(request, *args, **kwargs) |
|
276 |
||
277 |
if must_be_logged_in and not is_authenticated(request): |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
278 |
if not api: |
| 295 | 279 |
raise UnauthorizedException('Should be logged in') |
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
280 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
281 |
return rc.FORBIDDEN |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
282 |
|
| 0 | 283 |
|
284 |
if 'key' in kwargs: |
|
285 |
text = get_object_or_404(Text, key=kwargs['key']) |
|
286 |
else: |
|
287 |
raise Exception('no security check possible') |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
288 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
289 |
# in api, the view has an object as first parameter, request is args[0] |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
290 |
if not api: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
291 |
req = request |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
292 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
293 |
req = args[0] |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
294 |
if has_perm(req, perm_name, text=text): |
| 0 | 295 |
return view_func(request, *args, **kwargs) |
296 |
#else: |
|
297 |
# TODO: (? useful ?) if some user have the perm and not logged-in : redirect to login |
|
298 |
#if not request.user.is_authenticated() and number_has_perm_on_text(permission, text_id) > 0: |
|
299 |
# return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, urlquote(request.get_full_path()))) |
|
300 |
# else : unauthorized |
|
301 |
||
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
302 |
if not api: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
303 |
raise UnauthorizedException('No perm %s' % perm_name) |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
304 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
305 |
return rc.FORBIDDEN |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
306 |
|
| 0 | 307 |
_check_local_perm.__doc__ = view_func.__doc__ |
308 |
_check_local_perm.__dict__ = view_func.__dict__ |
|
309 |
||
310 |
return _check_local_perm |
|
311 |
return _dec |
|
312 |
||
313 |
def has_perm_on_comment(perm_name): |
|
314 |
""" |
|
315 |
decorator protection checking for perm for logged in user on to comment |
|
316 |
perm_name: 'virtual' permission name |
|
317 |
""" |
|
318 |
def _dec(view_func): |
|
319 |
def _check_local_perm(request, *args, **kwargs): |
|
320 |
if cm_settings.NO_SECURITY: |
|
321 |
return view_func(request, *args, **kwargs) |
|
322 |
||
323 |
if 'key' in kwargs: |
|
324 |
text = get_object_or_404(Text, key=kwargs['key']) |
|
325 |
# first try permission on text |
|
326 |
if has_perm(request, perm_name, text=text) : |
|
327 |
return view_func(request, *args, **kwargs) |
|
328 |
if 'comment_key' in kwargs: |
|
329 |
comment = get_object_or_404(Comment, key=kwargs['comment_key']) |
|
330 |
if has_own_perm(request, perm_name + "_own", text, comment) : |
|
331 |
return view_func(request, *args, **kwargs) |
|
332 |
else: |
|
333 |
raise Exception('no security check possible: no comment key') |
|
334 |
else: |
|
335 |
raise Exception('no security check possible: no text key') |
|
336 |
||
337 |
raise UnauthorizedException('No perm %s on comment' % perm_name) |
|
338 |
_check_local_perm.__doc__ = view_func.__doc__ |
|
339 |
_check_local_perm.__dict__ = view_func.__dict__ |
|
340 |
||
341 |
return _check_local_perm |
|
342 |
return _dec |
|
343 |
||
344 |
||
345 |