Multiples zones par user implémenté
[auf_rh_dae.git] / project / permissions.py
1 # -*- encoding: utf-8 -*
2
3 from project import groups
4 from project.groups import get_employe_from_user, get_zones_from_user
5 from project.decorators import in_drh_or_admin
6
7 from auf.django.references import models as ref
8 from rh import models as rh
9
10 def user_gere_obj_de_sa_region(user):
11 user_groups = [g.name for g in user.groups.all()]
12 if groups.CORRESPONDANT_RH in user_groups or \
13 groups.ADMINISTRATEURS in user_groups or \
14 groups.DIRECTEUR_DE_BUREAU in user_groups:
15 return True
16 return False
17
18 def user_can_add_obj(user):
19 if user_gere_obj_de_sa_region(user) or \
20 in_drh_or_admin(user):
21 return True
22 return False
23
24 def user_can_list_obj(user):
25 if user_gere_obj_de_sa_region(user) or \
26 in_drh_or_admin(user):
27 return True
28 return False
29
30 def user_can_change_obj(user, obj):
31 if in_drh_or_admin(user) or (
32 user_gere_obj_de_sa_region(user) and \
33 obj_in_region_user(user, obj)):
34 return True
35 return False
36
37 def user_can_delete_obj(user, obj):
38 return in_drh_or_admin(user)
39
40
41 # helpers
42 def obj_in_region_user(user, obj):
43 zones = get_zones_from_user(user)
44 if isinstance(obj, rh.Employe):
45 return True
46 if isinstance(obj, rh.Dossier):
47 return True
48 if isinstance(obj, rh.Poste):
49 return obj.implantation.zone_administrative in zones
50 return False
51