-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions.py
106 lines (71 loc) · 3.03 KB
/
permissions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"""
Application have:
- Administrator: Full access to CRUD Any User in his org and RU Organization
- Viewer: List and Retrieve and User in his org.
- User: CRU his own user
"""
from rest_framework.permissions import SAFE_METHODS, BasePermission
from .constants import ADMIN, VIEWER
def is_admin(user):
return bool(user.groups.filter(name__in=[ADMIN]))
def is_viewer(user):
return bool(user.groups.filter(name__in=[VIEWER]))
def is_admin_or_viewer(user):
return bool(user.groups.filter(name__in=[ADMIN, VIEWER]))
class OrgPermissions(BasePermission):
"""
Permission for Organization Endpoints:
- GET /api/organizations/{id}/ Retrieve organization information
if request user is `Administrator` or `Viewer`.
- PATCH /api/organizations /{id} Update organization if request user is `Administrator`.
"""
def has_object_permission(self, request, view, obj):
user = request.user
is_his_org = user.organization.id == obj.id
if request.method in SAFE_METHODS:
return is_admin_or_viewer(user)
if request.method == 'PATCH':
return is_admin(user) and is_his_org
class UserPermissions(BasePermission):
"""
Permission for User Endpoints:
- GET /api/users/ List all the users for the user organization
if user is `Administrator` or `Viewer`.
- GET /api/users/{id}/ Retrieve user information, and the organization id and name
- POST /api/users/ Request user must be Administrator
- PATCH /api/users/{id} Update user information for the user_id
if request user is `Administrator` of his organization. Or request user is user_id
- DELETE /api/users/{id} Delete user for the user_id
if request user is `Administrator` of his organization
"""
def has_permission(self, request, view):
user = request.user
user_id = view.kwargs.get('pk', None)
if request.method == 'POST':
return is_admin(user)
if request.method == 'GET' and not user_id:
return is_admin_or_viewer(user)
return True
def has_object_permission(self, request, view, obj):
user = request.user
is_owner = user.id == obj.id
if request.method in SAFE_METHODS:
return is_admin_or_viewer(user) or is_owner
if request.method == 'PATCH':
return is_admin(user) or is_owner
if request.method == 'DELETE':
return is_admin(user)
class UserOrgPermissions(BasePermission):
"""
Permission for Organization User Endpoints:
- GET /api/organization/{id}/users List all the users for the user organization
if user is `Administrator` or `Viewer`.
- GET /api/organization/{id}/users/{id}/ Retrieve user id and name
if user is `Administrator` or `Viewer`
"""
def has_permission(self, request, view):
user = request.user
org_id = int(view.kwargs['org_id'])
is_his_org = user.organization.id == org_id
if request.method == 'GET':
return is_admin_or_viewer(user) and is_his_org