Changements au buildout + settings pour me permettre de builder le projet
[auf_rh_dae.git] / project / permissions.py
CommitLineData
7000b7b3
DB
1# -*- encoding: utf-8 -*
2
9daa9f48
OL
3from django.core.cache import cache
4
7000b7b3 5from project import groups
9daa9f48 6from project.groups import get_zones_from_user
7000b7b3
DB
7from project.decorators import in_drh_or_admin
8
9from rh import models as rh
10
9daa9f48
OL
11
12def get_user_groupnames(user):
13 key = "get_user_groupnames_%s" % user.id
14 group_names = cache.get(key, None)
15 if group_names is not None:
16 return group_names.split(',')
17 group_names = [g.name for g in user.groups.all()]
18 cache.set(key, ",".join(group_names))
19 return group_names
20
21
7000b7b3 22def user_gere_obj_de_sa_region(user):
9daa9f48
OL
23 key = "user_gere_obj_de_sa_region_%s" % user.id
24 granted = cache.get(key, None)
25 if granted is not None:
26 return granted
27
3383b2d1
OL
28 user_groups = [g.name for g in user.groups.all()]
29 if groups.CORRESPONDANT_RH in user_groups or \
30 groups.ADMINISTRATEURS in user_groups or \
31 groups.DIRECTEUR_DE_BUREAU in user_groups:
9daa9f48
OL
32 granted = True
33 else:
34 granted = False
35 cache.set(key, granted)
36 return granted
37
7000b7b3
DB
38
39def user_can_add_obj(user):
40 if user_gere_obj_de_sa_region(user) or \
41 in_drh_or_admin(user):
42 return True
43 return False
44
9daa9f48 45
25f2c148
OL
46def user_can_list_obj(user):
47 if user_gere_obj_de_sa_region(user) or \
48 in_drh_or_admin(user):
49 return True
50 return False
51
9daa9f48 52
7000b7b3
DB
53def user_can_change_obj(user, obj):
54 if in_drh_or_admin(user) or (
9daa9f48 55 user_gere_obj_de_sa_region(user) and
7000b7b3
DB
56 obj_in_region_user(user, obj)):
57 return True
58 return False
9daa9f48
OL
59
60
7000b7b3
DB
61def user_can_delete_obj(user, obj):
62 return in_drh_or_admin(user)
9daa9f48 63
e8b6a20c 64
7000b7b3
DB
65# helpers
66def obj_in_region_user(user, obj):
e8b6a20c 67 zones = get_zones_from_user(user)
7000b7b3
DB
68 if isinstance(obj, rh.Employe):
69 return True
70 if isinstance(obj, rh.Dossier):
71 return True
72 if isinstance(obj, rh.Poste):
e8b6a20c 73 return obj.implantation.zone_administrative in zones
7000b7b3 74 return False