|
3 | 3 | from django.conf import settings |
4 | 4 | from django.contrib.auth.models import AnonymousUser |
5 | 5 | from api import manager |
6 | | -from api.models.blocklist import Blocklist, App |
| 6 | +from api.models import app |
| 7 | +from api.models import blocklist |
| 8 | +from api.models.base import object_policy_registry |
7 | 9 |
|
8 | 10 |
|
9 | 11 | def get_app_status(app): |
10 | | - blocklist = Blocklist.get_blocklist(app) |
11 | | - if blocklist: |
12 | | - return False, blocklist.remark |
| 12 | + block = blocklist.Blocklist.get_blocklist(app) |
| 13 | + if block: |
| 14 | + return False, block.remark |
13 | 15 | if settings.WORKFLOW_MANAGER_URL: |
14 | 16 | status = manager.User().get_status(app.owner.pk) |
15 | 17 | if not status["is_active"]: |
16 | 18 | return False, status["message"] |
17 | 19 | return True, None |
18 | 20 |
|
19 | 21 |
|
20 | | -def has_app_permission(user, obj, method): |
21 | | - if isinstance(obj, App) or hasattr(obj, 'app'): |
22 | | - app = obj if isinstance(obj, App) else obj.app |
23 | | - is_ok, message = get_app_status(app) |
24 | | - if is_ok: |
25 | | - if user.is_superuser: |
26 | | - return True, None |
27 | | - elif app.owner == user: |
28 | | - return True, None |
29 | | - elif user.is_staff or user.has_perm('use_app', app): |
30 | | - if method != 'DELETE': |
31 | | - return True, None |
32 | | - else: |
33 | | - return False, "User does not have permission to delete" |
| 22 | +def has_object_permission(user, obj, method): |
| 23 | + obj = getattr(obj, 'app', obj) |
| 24 | + object_policy = object_policy_registry.get(obj)[1] |
| 25 | + has_permission, message = False, f"{obj} object does not exist or does not have permission." |
| 26 | + if user.is_superuser: |
| 27 | + has_permission, message = True, None |
| 28 | + elif getattr(obj, "owner", None) == user: |
| 29 | + has_permission, message = True, None |
| 30 | + elif user.is_staff or (object_policy and user.has_perm(object_policy.codename, obj)): |
| 31 | + if method != 'DELETE': |
| 32 | + has_permission, message = True, None |
34 | 33 | else: |
35 | | - return is_ok, message |
36 | | - return False, "App object does not exist or does not have permission." |
| 34 | + has_permission, message = False, "{user} does not have permission to delete." |
| 35 | + elif has_permission and isinstance(obj, app.App): |
| 36 | + return get_app_status(obj) |
| 37 | + return has_permission, message |
37 | 38 |
|
38 | 39 |
|
39 | 40 | class IsAnonymous(permissions.BasePermission): |
@@ -75,13 +76,13 @@ def has_object_permission(self, request, view, obj): |
75 | 76 | return False |
76 | 77 |
|
77 | 78 |
|
78 | | -class IsAppUser(permissions.BasePermission): |
| 79 | +class IsObjectUser(permissions.BasePermission): |
79 | 80 | """ |
80 | 81 | Object-level permission to allow owners or collaborators to access |
81 | 82 | an app-related model. |
82 | 83 | """ |
83 | 84 | def has_object_permission(self, request, view, obj): |
84 | | - return has_app_permission(request.user, obj, request.method)[0] |
| 85 | + return has_object_permission(request.user, obj, request.method)[0] |
85 | 86 |
|
86 | 87 |
|
87 | 88 | class IsAdmin(permissions.BasePermission): |
|
0 commit comments