| author | gibus |
| Tue, 12 Jun 2012 14:00:32 +0200 | |
| changeset 449 | 5387c032df35 |
| parent 355 | c926868cf7e6 |
| child 470 | 077be006891e |
| 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')): |
|
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
|
195 |
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
|
196 |
with_teachers.append(u.username) |
|
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 |
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
|
198 |
with_teachers.append(u.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
|
199 |
|
|
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 |
# 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
|
201 |
admin = User.objects.get(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
|
202 |
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
|
203 |
with_teachers.append(admin.username) |
|
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(request.GET.get('name', 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
|
205 |
visible_comments = comments.filter(name__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 |
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
|
207 |
with_teachers.append(admin.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
|
208 |
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
|
209 |
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
|
210 |
|
| 0 | 211 |
# filter comments with a non visible (i.e. moderated) comment in the above thread |
| 16 | 212 |
comments_thread_viewable = [c for c in visible_comments if c.is_thread_full_visible(own_user=user)] |
| 0 | 213 |
return comments_thread_viewable |
214 |
else: |
|
215 |
return [] |
|
216 |
||
217 |
def get_viewable_activities(request=None, act_types={}, text=None): |
|
218 |
""" |
|
219 |
Get activities user in request is allowed to see |
|
220 |
""" |
|
221 |
from cm.security import has_perm, get_texts_with_perm, get_viewable_comments |
|
222 |
||
223 |
selected_activities = reduce(list.__add__,[Activity.VIEWABLE_ACTIVITIES[k] for k in act_types.keys() if act_types[k]], []) |
|
224 |
||
225 |
activities = Activity.objects.filter(type__in=selected_activities) |
|
226 |
if text: |
|
227 |
activities = activities.filter(text=text) |
|
228 |
||
229 |
if not has_perm(request, 'can_manage_workspace'): |
|
230 |
texts = get_texts_with_perm(request, 'can_view_text') |
|
231 |
activities = activities.filter(Q(text__in=texts)) |
|
232 |
||
233 |
comments = [] |
|
234 |
[comments.extend(get_viewable_comments(request, t.last_text_version.comment_set.all(), t)) for t in texts] |
|
235 |
||
236 |
activities = activities.filter(Q(comment__in=comments) | Q(comment=None)) |
|
237 |
return activities.order_by('-created') |
|
238 |
||
239 |
||
240 |
# won't need to be overridden, should it be moved to another file ? |
|
241 |
def list_viewable_comments(request, comments_list, text): |
|
242 |
ret = [] |
|
243 |
for comment in comments_list : |
|
244 |
ret += [comment] + list_viewable_comments(request, get_viewable_comments(request, comment.comment_set, text), text) |
|
245 |
return ret |
|
246 |
||
247 |
||
248 |
# decorators (simple wrappers around above functions) |
|
249 |
def has_global_perm(perm_name, must_be_logged_in=False, redirect_field_name=REDIRECT_FIELD_NAME): |
|
250 |
def _dec(view_func): |
|
251 |
def _check_global_perm(request, *args, **kwargs): |
|
252 |
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
|
253 |
raise UnauthorizedException('Should be logged in') |
| 0 | 254 |
|
255 |
if has_perm(request, perm_name, text=None): |
|
256 |
return view_func(request, *args, **kwargs) |
|
257 |
||
258 |
raise UnauthorizedException('No global perm %s' % perm_name) |
|
259 |
_check_global_perm.__doc__ = view_func.__doc__ |
|
260 |
_check_global_perm.__dict__ = view_func.__dict__ |
|
261 |
||
262 |
return _check_global_perm |
|
263 |
return _dec |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
264 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
265 |
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
|
266 |
return _has_perm_on_text(perm_name, must_be_logged_in, redirect_field_name, api=True) |
| 0 | 267 |
|
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
268 |
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
|
269 |
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
|
270 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
271 |
def _has_perm_on_text(perm_name, must_be_logged_in=False, redirect_field_name=REDIRECT_FIELD_NAME, api=False): |
| 0 | 272 |
""" |
273 |
decorator protection checking for perm for logged in user |
|
274 |
force logged in (i.e. redirect to connection screen if not if must_be_logged_in |
|
275 |
""" |
|
276 |
def _dec(view_func): |
|
277 |
def _check_local_perm(request, *args, **kwargs): |
|
278 |
if cm_settings.NO_SECURITY: |
|
279 |
return view_func(request, *args, **kwargs) |
|
280 |
||
281 |
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
|
282 |
if not api: |
| 295 | 283 |
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
|
284 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
285 |
return rc.FORBIDDEN |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
286 |
|
| 0 | 287 |
|
288 |
if 'key' in kwargs: |
|
289 |
text = get_object_or_404(Text, key=kwargs['key']) |
|
290 |
else: |
|
291 |
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
|
292 |
|
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
293 |
# 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
|
294 |
if not api: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
295 |
req = request |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
296 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
297 |
req = args[0] |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
298 |
if has_perm(req, perm_name, text=text): |
| 0 | 299 |
return view_func(request, *args, **kwargs) |
300 |
#else: |
|
301 |
# TODO: (? useful ?) if some user have the perm and not logged-in : redirect to login |
|
302 |
#if not request.user.is_authenticated() and number_has_perm_on_text(permission, text_id) > 0: |
|
303 |
# return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, urlquote(request.get_full_path()))) |
|
304 |
# else : unauthorized |
|
305 |
||
|
287
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
306 |
if not api: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
307 |
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
|
308 |
else: |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
309 |
return rc.FORBIDDEN |
|
fc5ed157ebfe
add api: basic auth / unit tests / online doc (based on django-piston)
raph
parents:
210
diff
changeset
|
310 |
|
| 0 | 311 |
_check_local_perm.__doc__ = view_func.__doc__ |
312 |
_check_local_perm.__dict__ = view_func.__dict__ |
|
313 |
||
314 |
return _check_local_perm |
|
315 |
return _dec |
|
316 |
||
317 |
def has_perm_on_comment(perm_name): |
|
318 |
""" |
|
319 |
decorator protection checking for perm for logged in user on to comment |
|
320 |
perm_name: 'virtual' permission name |
|
321 |
""" |
|
322 |
def _dec(view_func): |
|
323 |
def _check_local_perm(request, *args, **kwargs): |
|
324 |
if cm_settings.NO_SECURITY: |
|
325 |
return view_func(request, *args, **kwargs) |
|
326 |
||
327 |
if 'key' in kwargs: |
|
328 |
text = get_object_or_404(Text, key=kwargs['key']) |
|
329 |
# first try permission on text |
|
330 |
if has_perm(request, perm_name, text=text) : |
|
331 |
return view_func(request, *args, **kwargs) |
|
332 |
if 'comment_key' in kwargs: |
|
333 |
comment = get_object_or_404(Comment, key=kwargs['comment_key']) |
|
334 |
if has_own_perm(request, perm_name + "_own", text, comment) : |
|
335 |
return view_func(request, *args, **kwargs) |
|
336 |
else: |
|
337 |
raise Exception('no security check possible: no comment key') |
|
338 |
else: |
|
339 |
raise Exception('no security check possible: no text key') |
|
340 |
||
341 |
raise UnauthorizedException('No perm %s on comment' % perm_name) |
|
342 |
_check_local_perm.__doc__ = view_func.__doc__ |
|
343 |
_check_local_perm.__dict__ = view_func.__dict__ |
|
344 |
||
345 |
return _check_local_perm |
|
346 |
return _dec |
|
347 |
||
348 |
||
349 |