1
+ """
2
+ Dashboard User Permission Hierarchy:
3
+
4
+ - `is_superuser`:
5
+ Grants unrestricted access to all dashboard functionalities without any limitations.
6
+ Inherits all permissions of `is_store_admin` by default.
7
+
8
+ - `is_staff`:
9
+ Required for all users to access the dashboard.
10
+
11
+ - `is_store_admin`:
12
+ Has extensive permissions and authority within the dashboard.
13
+ Can perform almost all operations except a few critical ones restricted to the superuser.
14
+ Inherits all permissions assigned to other users by default.
15
+
16
+ - `is_store_staff`:
17
+ Has limited access to the dashboard.
18
+ Their permissions can be extended granularly by assigning specific permissions, which are managed by the store admin.
19
+
20
+ **Additional Notes:**
21
+ - All users have read permissions by default, except for certain critical data that require explicit authorization.
22
+ - The superuser automatically inherits all permissions of a store admin.
23
+ - A store admin inherits all permissions granted to other users by default.
24
+ """
25
+
26
+
27
+
1
28
from rest_framework .permissions import BasePermission
2
29
from rest_framework .permissions import SAFE_METHODS
3
30
7
34
import functools
8
35
from graphql import GraphQLError
9
36
10
- class IsStaffUser (BasePermission ):
37
+
38
+ class IsStoreAdmin (BasePermission ):
39
+ def has_permission (self , request , view ):
40
+ if not request .user .is_staff :
41
+ return False
42
+ return request .user .is_store_admin
43
+
44
+ class IsStoreStaff (BasePermission ):
11
45
def has_permission (self , request , view ):
12
- return request .user .is_staff
46
+ if not request .user .is_staff :
47
+ return False
48
+
49
+ if request .user .is_superuser :
50
+ return True
51
+
52
+ if request .user .is_store_admin :
53
+ return True
54
+
55
+ return request .user .is_store_staff
13
56
14
57
class GranularPermission (BasePermission ):
15
58
def get_permission_name (self , model_name , action ):
16
59
17
60
return f"{ model_name } .{ action } "
18
61
19
62
def has_permission (self , request , view ):
20
- if not request .user .is_authenticated :
63
+ if not request .user .is_staff :
21
64
return False
22
65
23
66
if request .user .is_superuser :
24
67
return True
25
68
69
+ if request .user .is_store_admin :
70
+ return True
71
+
26
72
if request .method in SAFE_METHODS and request .user .is_staff : # Every staff can view
27
73
return True
28
74
@@ -47,12 +93,15 @@ def has_permission(self, request, view):
47
93
class CommonPermissions (BasePermission ):
48
94
49
95
def has_permission (self , request , view ):
50
- if not request .user .is_authenticated :
96
+ if not request .user .is_staff :
51
97
return False
52
98
53
99
if request .user .is_superuser :
54
100
return True
55
101
102
+ if request .user .is_store_admin :
103
+ return True
104
+
56
105
if request .method in SAFE_METHODS and request .user .is_staff : # Every staff can view
57
106
return True
58
107
@@ -88,11 +137,14 @@ def has_permission(self, request, view):
88
137
89
138
90
139
def has_required_perm (user , code : str , model_cls = None ):
91
- if not user .is_authenticated :
140
+ if not user .is_staff :
92
141
return False
93
142
94
143
if user .is_superuser :
95
144
return True
145
+
146
+ if user .is_store_admin :
147
+ return True
96
148
97
149
perm_code = model_cls ._meta .app_label + '.' + code
98
150
return user .has_perm (perm_code )
0 commit comments