| author | ymh <ymh.work@gmail.com> |
| Sun, 23 Aug 2015 22:37:27 +0200 | |
| changeset 1414 | 9c76c7eea3fd |
| parent 1360 | f69b5d8ba4b9 |
| permissions | -rw-r--r-- |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
1 |
from cache import get_cached_userlist, cached_assign |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
2 |
from django.conf import settings |
|
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
3 |
from django.contrib.contenttypes.models import ContentType |
| 549 | 4 |
from django.core.cache import cache |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
5 |
from guardian.shortcuts import (remove_perm, get_users_with_perms, |
|
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
6 |
get_groups_with_perms, get_objects_for_user) |
| 1013 | 7 |
from ldt.security import change_security |
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
8 |
import types |
| 1093 | 9 |
|
|
350
c6953232099f
Anonymous users can see pages even if they are not logged in + factor code to decrease number of SQL requests
verrierj
parents:
340
diff
changeset
|
10 |
|
|
340
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
11 |
def unprotect_instance(instance): |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
12 |
if hasattr(instance, 'old_save'): |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
13 |
instance.save = instance.old_save |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
14 |
instance.delete = instance.old_delete |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
15 |
|
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
16 |
def protect_instance(instance): |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
17 |
class_name = instance.__class__.__name__.lower() |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
18 |
cls = ContentType.objects.get(model=class_name) |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
19 |
cls = cls.model_class() |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
20 |
|
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
21 |
save = types.MethodType(change_security('project')(cls.save), instance, cls) |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
22 |
instance.save = save |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
23 |
|
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
24 |
delete = types.MethodType(change_security('project')(cls.delete), instance, cls) |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
25 |
instance.delete = delete |
|
5f919a978f50
Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents:
292
diff
changeset
|
26 |
|
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
27 |
|
| 245 | 28 |
def set_forbidden_stream(xml, user): |
29 |
cls = ContentType.objects.get(model='content') |
|
30 |
cls = cls.model_class() |
|
31 |
||
| 1093 | 32 |
m_cls = ContentType.objects.get(model='media') |
33 |
m_cls = m_cls.model_class() |
|
34 |
||
35 |
content_ids = xml.xpath('/iri/medias/media/@id') |
|
36 |
contents = dict( [(c.iri_id, c) for c in cls.safe_objects.filter(iri_id__in=content_ids).select_related("media_obj")]) |
|
37 |
||
38 |
medias = dict([(m.id,m) for m in m_cls.safe_objects.filter(id__in=[c.media_obj.id for c in contents.values()])]) |
|
39 |
||
| 245 | 40 |
for elem in xml.xpath('/iri/medias/media'): |
| 1093 | 41 |
content = contents.get(elem.get('id'), None) |
42 |
if content and (content.media_obj and content.media_obj.id not in medias) : |
|
| 245 | 43 |
elem.set('video', settings.FORBIDDEN_STREAM_URL) |
44 |
return xml |
|
45 |
||
| 482 | 46 |
def use_forbidden_url(content): |
|
795
923429f142ea
Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents:
549
diff
changeset
|
47 |
m_cls = ContentType.objects.get(model='media') |
|
923429f142ea
Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents:
549
diff
changeset
|
48 |
m_cls = m_cls.model_class() |
|
923429f142ea
Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents:
549
diff
changeset
|
49 |
|
| 1093 | 50 |
if content.media_obj: |
51 |
media = m_cls.safe_objects.filter(id=content.media_obj.id) |
|
52 |
if not media: |
|
53 |
return True |
|
|
795
923429f142ea
Possibility to list a content on the front or not, with a private media. A media can be private, and the content public (listed or not)
rougeronj
parents:
549
diff
changeset
|
54 |
return False |
| 482 | 55 |
|
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
56 |
def add_change_attr(user, obj_list): |
| 274 | 57 |
""" |
58 |
Add a change attribute set to True to objects of obj_list |
|
59 |
if permissions change_object is set with respect to user. |
|
60 |
""" |
|
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
61 |
if len(obj_list) == 0: |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
62 |
return [] |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
63 |
model_name = obj_list[0].__class__.__name__.lower() |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
64 |
ctype = ContentType.objects.get(model=model_name) |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
65 |
cls = ctype.model_class() |
| 1146 | 66 |
# We don't use obj.values_list('pk', flat=True) because the full queryset will be calculated and used after anyway. |
67 |
pk_list = [item.pk for item in obj_list] |
|
| 290 | 68 |
if model_name in [cls_name.lower() for cls_name in settings.USE_GROUP_PERMISSIONS] or model_name == 'group': |
|
289
f78273a17bb3
Speed up response time when retrieving group projects
verrierj
parents:
285
diff
changeset
|
69 |
to_check = True |
| 1146 | 70 |
# Filter SUPER usefull : avoid to load ALL objects. We only check permissions on obj_list's objects |
71 |
change_list = get_objects_for_user(user, '%s.change_%s' % (cls._meta.app_label, model_name)).filter(pk__in=pk_list) |
|
| 274 | 72 |
else: |
| 1146 | 73 |
to_check = False |
|
1132
026d510a3dc3
Finally super useful optimization on guardian request.
cavaliet
parents:
1093
diff
changeset
|
74 |
|
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
75 |
for obj in obj_list: |
|
289
f78273a17bb3
Speed up response time when retrieving group projects
verrierj
parents:
285
diff
changeset
|
76 |
if not to_check or obj in change_list: |
|
251
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
77 |
obj.change = True |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
78 |
else: |
|
81417fd477b0
Display specific tooltip when project edition is not allowed
verrierj
parents:
245
diff
changeset
|
79 |
obj.change = False |
|
1132
026d510a3dc3
Finally super useful optimization on guardian request.
cavaliet
parents:
1093
diff
changeset
|
80 |
|
| 274 | 81 |
return obj_list |
| 269 | 82 |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
83 |
def assign_perm_to_obj(obj, read_list, write_list, owner): |
|
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
84 |
name = obj.__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
|
85 |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
86 |
old_users = get_users_with_perms(obj).exclude(id=owner.id) |
|
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
87 |
old_groups = get_groups_with_perms(obj) |
|
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
|
88 |
|
|
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
|
89 |
for elem in read_list: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
90 |
cached_assign('view_%s' % name, elem, obj) |
|
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
|
91 |
if elem in write_list: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
92 |
cached_assign('change_%s' % name, elem, obj) |
|
239
352be36c9fd7
Moved code about group security into a separate module
verrierj
parents:
diff
changeset
|
93 |
else: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
94 |
remove_perm('change_%s' % name, elem, obj) |
|
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
|
95 |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
96 |
def remove_perms(new_list, old_list, o, name): |
|
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
|
97 |
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
|
98 |
if e not in new_list: |
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
99 |
remove_perm('view_%s' % name, e, o) |
|
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
100 |
remove_perm('change_%s' % name, e, o) |
|
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
|
101 |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
102 |
remove_perms(read_list, old_users, obj, name) |
|
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
103 |
remove_perms(read_list, old_groups, obj, name) |
|
1357
dd3b4c9d5035
add command to create media and content
ymh <ymh.work@gmail.com>
parents:
1193
diff
changeset
|
104 |
cache.delete('userlist') |
| 549 | 105 |
|
| 269 | 106 |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
107 |
def get_userlist(user, filter=None): # @ReservedAssignment |
| 282 | 108 |
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
|
109 |
if filter: |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
110 |
user_list = user_list.filter(username__icontains=filter) |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
111 |
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
|
112 |
return elem_list |
|
832c5049b358
Factor code to display userlist in project/content edition
verrierj
parents:
279
diff
changeset
|
113 |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1180
diff
changeset
|
114 |
def get_userlist_model(object, owner): # @ReservedAssignment |
|
275
a14509d74e13
Change modal window size + improve get_urserlist_model function for public contents
verrierj
parents:
274
diff
changeset
|
115 |
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
|
116 |
return [None, None] |
|
a14509d74e13
Change modal window size + improve get_urserlist_model function for public contents
verrierj
parents:
274
diff
changeset
|
117 |
|
| 285 | 118 |
users = get_users_with_perms(object, attach_perms=True, with_group_users=False) |
| 273 | 119 |
groups = get_groups_with_perms(object, attach_perms=True) |
120 |
object_name = object.__class__.__name__.lower() |
|
| 263 | 121 |
|
| 273 | 122 |
def create_dict (users_or_groups, name, groups=True): |
123 |
l = [] |
|
124 |
admin_list = [] |
|
125 |
||
126 |
for elem in users_or_groups.keys(): |
|
127 |
if elem == owner: |
|
128 |
continue |
|
129 |
||
130 |
if groups: |
|
131 |
elem_dict = {'name': elem.name, 'type': 'group', 'id': elem.id} |
|
132 |
else: |
|
133 |
elem_dict = {'name': elem.username, 'type': 'user', 'id': elem.id} |
|
134 |
||
135 |
for perm in users_or_groups[elem]: |
|
136 |
if perm == 'change_%s' % name: |
|
137 |
elem_dict['change'] = True |
|
138 |
admin_list.append(elem_dict) |
|
139 |
continue |
|
140 |
||
141 |
l.append(elem_dict) |
|
142 |
return l, admin_list |
|
143 |
||
144 |
users_list, admin_users = create_dict(users, object_name, False) |
|
145 |
groups_list, admin_groups = create_dict(groups, object_name, True) |
|
146 |
||
147 |
return [users_list + groups_list, admin_users + admin_groups] |
|
| 285 | 148 |
|
| 273 | 149 |
def get_userlist_group(group, user): |
|
1360
f69b5d8ba4b9
started upgrading to Django 1.6: fixed transactions, settings, switched django_social_auth to python_social_auth
ndurand
parents:
1357
diff
changeset
|
150 |
members = group.user_set.all() |
| 273 | 151 |
admin = get_users_with_perms(group) |
152 |
||
| 269 | 153 |
member_list = [] |
| 273 | 154 |
for u in members: |
155 |
if u == user: |
|
156 |
continue |
|
| 278 | 157 |
u_dict = {'name': u.username, 'id': u.id, 'type': 'user', 'change': False} |
| 273 | 158 |
if u in admin: |
159 |
u_dict['change'] = True |
|
160 |
member_list.append(u_dict) |
|
| 269 | 161 |
|
| 278 | 162 |
admin_list = [{'name': e.username, 'id': e.id, 'type': 'user', 'change': False} for e in admin] |
| 269 | 163 |
|
| 273 | 164 |
return [member_list, admin_list] |