| author | verrierj |
| Tue, 13 Dec 2011 15:46:34 +0100 | |
| changeset 282 | 7512c33b64be |
| parent 281 | 832c5049b358 |
| child 285 | 1cc364d7b298 |
| permissions | -rw-r--r-- |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
1 |
from django.conf import settings |
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
2 |
from django.contrib.contenttypes.models import ContentType |
| 274 | 3 |
from guardian.shortcuts import assign, remove_perm, get_users_with_perms, get_groups_with_perms |
| 282 | 4 |
from cache import get_cached_checker, get_cached_userlist |
| 278 | 5 |
|
| 241 | 6 |
try: |
7 |
from threading import local |
|
8 |
except ImportError: |
|
9 |
from django.utils._threading_local import local |
|
10 |
||
11 |
_thread_locals = local() |
|
12 |
||
13 |
def get_current_user(): |
|
14 |
return getattr(_thread_locals, 'user', None) |
|
15 |
||
| 278 | 16 |
def set_current_user(user): |
17 |
_thread_locals.user = user |
|
18 |
||
19 |
def del_current_user(): |
|
20 |
del _thread_locals.user |
|
21 |
||
| 241 | 22 |
def protect_models(): |
| 264 | 23 |
cls_list = ToProtect.get_models() |
24 |
if cls_list: |
|
25 |
user = get_current_user() |
|
26 |
for cls in ToProtect.get_models(): |
|
| 282 | 27 |
protect_model(cls, user) |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
28 |
|
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
29 |
def unprotect_models(): |
| 241 | 30 |
for cls in ToProtect.get_models(): |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
31 |
unprotect_model(cls) |
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
32 |
|
| 241 | 33 |
class ToProtect(object): |
34 |
||
35 |
@staticmethod |
|
36 |
def get_models(): |
|
37 |
if hasattr(ToProtect, 'cls_list'): |
|
38 |
return ToProtect.cls_list |
|
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
39 |
|
| 241 | 40 |
cls_list = [] |
41 |
for cls_name in settings.USE_GROUP_PERMISSIONS: |
|
| 242 | 42 |
cls_type = ContentType.objects.get(model=cls_name.lower()) |
| 241 | 43 |
cls_list.append(cls_type.model_class()) |
44 |
ToProtect.cls_list = cls_list |
|
45 |
||
46 |
return cls_list |
|
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
47 |
|
|
240
a46cb257d8ee
Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents:
239
diff
changeset
|
48 |
def protect_model(cls, user): |
| 242 | 49 |
cls.safe_objects.user = user |
|
265
491d057cbfd2
Fix bug when selecting media in content creation window
verrierj
parents:
264
diff
changeset
|
50 |
cls.safe_objects.check_perm = True |
| 241 | 51 |
|
52 |
cls.old_save = cls.save |
|
53 |
cls.old_delete = cls.delete |
|
|
240
a46cb257d8ee
Models in ldt_utils derived from SafeModel instead of django.db.model
verrierj
parents:
239
diff
changeset
|
54 |
class_name = cls.__name__.lower() |
| 264 | 55 |
cls.save = change_security(class_name)(cls.save) |
56 |
cls.delete = change_security(class_name)(cls.delete) |
|
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
57 |
|
| 245 | 58 |
def unprotect_model(cls): |
| 241 | 59 |
if hasattr(cls, 'old_save'): |
60 |
cls.save = cls.old_save |
|
61 |
cls.delete = cls.old_delete |
|
62 |
del cls.old_save |
|
| 245 | 63 |
del cls.old_delete |
64 |
cls.safe_objects.user = None |
|
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
65 |
|
| 264 | 66 |
def change_security(cls_name): |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
67 |
def wrapper(func): |
| 242 | 68 |
def wrapped(self, *args, **kwargs): |
| 241 | 69 |
|
| 264 | 70 |
if self.pk and not get_current_user().has_perm('change_%s' % cls_name, self): |
71 |
raise AttributeError('User %s is not allowed to change object %s' % (get_current_user(), self)) |
|
| 241 | 72 |
|
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
73 |
return func(self, *args, **kwargs) |
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
74 |
return wrapped |
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
75 |
return wrapper |
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
76 |
|
| 245 | 77 |
def set_forbidden_stream(xml, user): |
78 |
cls = ContentType.objects.get(model='content') |
|
79 |
cls = cls.model_class() |
|
80 |
||
81 |
old_user = cls.safe_objects.user |
|
82 |
obj_list = cls.safe_objects.all() |
|
83 |
||
84 |
for elem in xml.xpath('/iri/medias/media'): |
|
85 |
if not obj_list.filter(iri_id=elem.get('id')): |
|
86 |
elem.set('video', settings.FORBIDDEN_STREAM_URL) |
|
87 |
||
88 |
cls.safe_objects.user = old_user |
|
89 |
||
90 |
return xml |
|
91 |
||
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
92 |
def add_change_attr(user, obj_list): |
| 274 | 93 |
""" |
94 |
Add a change attribute set to True to objects of obj_list |
|
95 |
if permissions change_object is set with respect to user. |
|
96 |
""" |
|
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
97 |
if len(obj_list) == 0: |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
98 |
return [] |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
99 |
|
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
100 |
model_name = obj_list[0].__class__.__name__.lower() |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
101 |
ctype = ContentType.objects.get(model=model_name) |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
102 |
cls = ctype.model_class() |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
103 |
|
| 274 | 104 |
if model_name in [cls_name.lower() for cls_name in settings.USE_GROUP_PERMISSIONS]: |
| 282 | 105 |
checker = get_cached_checker(user) |
| 274 | 106 |
else: |
107 |
checker = None |
|
108 |
||
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
109 |
perm_name = "%s.change_%s" % (cls._meta.app_label, model_name) |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
110 |
|
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
111 |
for obj in obj_list: |
|
281
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
112 |
if not checker or checker.has_perm(perm_name, obj): |
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
113 |
obj.change = True |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
114 |
else: |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
115 |
obj.change = False |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
116 |
|
| 274 | 117 |
return obj_list |
| 269 | 118 |
|
119 |
def assign_perm_to_obj(object, read_list, write_list, owner): |
|
| 260 | 120 |
name = object.__class__.__name__.lower() |
|
268
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
121 |
|
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
122 |
old_users = get_users_with_perms(object).exclude(id=owner.id) |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
123 |
old_groups = get_groups_with_perms(object) |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
124 |
|
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
125 |
for elem in read_list: |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
126 |
assign('view_%s' % name, elem, object) |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
127 |
if elem in write_list: |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
128 |
assign('change_%s' % name, elem, object) |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
129 |
else: |
|
268
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
130 |
remove_perm('change_%s' % name, elem, object) |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
131 |
|
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
132 |
def remove_perms(new_list, old_list, obj, name): |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
133 |
for e in old_list: |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
134 |
if e not in new_list: |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
135 |
remove_perm('view_%s' % name, e, obj) |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
136 |
remove_perm('change_%s' % name, e, obj) |
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
137 |
|
|
c0c161736794
Projects can be shared with users and groups. Does not work with contents yet, still some bugs in templates
verrierj
parents:
265
diff
changeset
|
138 |
remove_perms(read_list, old_users, object, name) |
| 274 | 139 |
remove_perms(read_list, old_groups, object, name) |
| 269 | 140 |
|
|
281
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
141 |
def get_userlist(user, filter=None): |
| 282 | 142 |
user_list = get_cached_userlist().exclude(id=user.id) |
|
281
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
143 |
if filter: |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
144 |
user_list = user_list.filter(username__icontains=filter) |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
145 |
elem_list = [{'name': u.username, 'id': u.id, 'type': 'user'} for u in user_list[0:settings.MAX_USERS_SEARCH]] |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
146 |
return elem_list |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
147 |
|
| 273 | 148 |
def get_userlist_model(object, owner): |
|
275
a14509d74e13
Change modal window size + improve get_urserlist_model function for public contents
verrierj
parents:
274
diff
changeset
|
149 |
if hasattr(object, 'is_public') and object.is_public: |
|
a14509d74e13
Change modal window size + improve get_urserlist_model function for public contents
verrierj
parents:
274
diff
changeset
|
150 |
return [None, None] |
|
a14509d74e13
Change modal window size + improve get_urserlist_model function for public contents
verrierj
parents:
274
diff
changeset
|
151 |
|
| 273 | 152 |
users = get_users_with_perms(object, attach_perms=True) |
153 |
groups = get_groups_with_perms(object, attach_perms=True) |
|
154 |
object_name = object.__class__.__name__.lower() |
|
| 263 | 155 |
|
| 273 | 156 |
def create_dict (users_or_groups, name, groups=True): |
157 |
l = [] |
|
158 |
admin_list = [] |
|
159 |
||
160 |
for elem in users_or_groups.keys(): |
|
161 |
if elem == owner: |
|
162 |
continue |
|
163 |
||
164 |
if groups: |
|
165 |
elem_dict = {'name': elem.name, 'type': 'group', 'id': elem.id} |
|
166 |
else: |
|
167 |
elem_dict = {'name': elem.username, 'type': 'user', 'id': elem.id} |
|
168 |
||
169 |
for perm in users_or_groups[elem]: |
|
170 |
if perm == 'change_%s' % name: |
|
171 |
elem_dict['change'] = True |
|
172 |
admin_list.append(elem_dict) |
|
173 |
continue |
|
174 |
||
175 |
l.append(elem_dict) |
|
176 |
return l, admin_list |
|
177 |
||
178 |
users_list, admin_users = create_dict(users, object_name, False) |
|
179 |
groups_list, admin_groups = create_dict(groups, object_name, True) |
|
180 |
||
181 |
return [users_list + groups_list, admin_users + admin_groups] |
|
182 |
||
183 |
def get_userlist_group(group, user): |
|
184 |
members = group.user_set.all() |
|
185 |
admin = get_users_with_perms(group) |
|
186 |
||
| 269 | 187 |
member_list = [] |
| 273 | 188 |
for u in members: |
189 |
if u == user: |
|
190 |
continue |
|
| 278 | 191 |
u_dict = {'name': u.username, 'id': u.id, 'type': 'user', 'change': False} |
| 273 | 192 |
if u in admin: |
193 |
u_dict['change'] = True |
|
194 |
member_list.append(u_dict) |
|
| 269 | 195 |
|
| 278 | 196 |
admin_list = [{'name': e.username, 'id': e.id, 'type': 'user', 'change': False} for e in admin] |
| 269 | 197 |
|
| 273 | 198 |
return [member_list, admin_list] |