|
| 1 | +from django.contrib.auth.models import AbstractUser, AnonymousUser |
1 | 2 | from django.db import models |
| 3 | +from guardian.shortcuts import get_perms |
2 | 4 |
|
3 | 5 | import ami.tasks |
4 | 6 |
|
@@ -29,5 +31,94 @@ def update_calculated_fields(self, *args, **kwargs): |
29 | 31 | """Update calculated fields specific to each model.""" |
30 | 32 | pass |
31 | 33 |
|
| 34 | + def _get_object_perms(self, user): |
| 35 | + """ |
| 36 | + Get the object-level permissions for the user on this instance. |
| 37 | + This method retrieves permissions like `update_modelname`, `create_modelname`, etc. |
| 38 | + """ |
| 39 | + project = self.get_project() |
| 40 | + if not project: |
| 41 | + return [] |
| 42 | + |
| 43 | + model_name = self._meta.model_name |
| 44 | + all_perms = get_perms(user, project) |
| 45 | + object_perms = [perm for perm in all_perms if perm.endswith(f"_{model_name}")] |
| 46 | + return object_perms |
| 47 | + |
| 48 | + def check_permission(self, user: AbstractUser | AnonymousUser, action: str) -> bool: |
| 49 | + """ |
| 50 | + Check if the user has permission to perform the action |
| 51 | + on this instance. |
| 52 | + This method is used to determine if the user can perform |
| 53 | + CRUD operations or custom actions on the model instance. |
| 54 | + """ |
| 55 | + project = self.get_project() if hasattr(self, "get_project") else None |
| 56 | + if not project: |
| 57 | + return False |
| 58 | + if action == "retrieve": |
| 59 | + # Allow view |
| 60 | + return True |
| 61 | + |
| 62 | + model = self._meta.model_name |
| 63 | + crud_map = { |
| 64 | + "create": f"create_{model}", |
| 65 | + "update": f"update_{model}", |
| 66 | + "partial_update": f"update_{model}", |
| 67 | + "destroy": f"delete_{model}", |
| 68 | + } |
| 69 | + |
| 70 | + if action in crud_map: |
| 71 | + return user.has_perm(crud_map[action], project) |
| 72 | + |
| 73 | + # Delegate to model-specific logic |
| 74 | + return self.check_custom_permission(user, action) |
| 75 | + |
| 76 | + def check_custom_permission(self, user: AbstractUser | AnonymousUser, action: str) -> bool: |
| 77 | + """Check custom permissions for the user on this instance. |
| 78 | + This is used for actions that are not standard CRUD operations. |
| 79 | + """ |
| 80 | + assert self._meta.model_name is not None, "Model must have a model_name defined in Meta class." |
| 81 | + model_name = self._meta.model_name.lower() |
| 82 | + permission_codename = f"{action}_{model_name}" |
| 83 | + project = self.get_project() if hasattr(self, "get_project") else None |
| 84 | + |
| 85 | + return user.has_perm(permission_codename, project) |
| 86 | + |
| 87 | + def get_user_object_permissions(self, user) -> list[str]: |
| 88 | + """ |
| 89 | + Returns a list of object-level permissions the user has on this instance. |
| 90 | + This is used by frontend to determine what actions the user can perform. |
| 91 | + """ |
| 92 | + # Return all permissions for superusers |
| 93 | + if user.is_superuser: |
| 94 | + allowed_custom_actions = self.get_custom_user_permissions(user) |
| 95 | + return ["update", "delete"] + allowed_custom_actions |
| 96 | + |
| 97 | + object_perms = self._get_object_perms(user) |
| 98 | + # Check for update and delete permissions |
| 99 | + allowed_actions = set() |
| 100 | + for perm in object_perms: |
| 101 | + action = perm.split("_", 1)[0] |
| 102 | + if action in {"update", "delete"}: |
| 103 | + allowed_actions.add(action) |
| 104 | + |
| 105 | + allowed_custom_actions = self.get_custom_user_permissions(user) |
| 106 | + allowed_actions.update(set(allowed_custom_actions)) |
| 107 | + return list(allowed_actions) |
| 108 | + |
| 109 | + def get_custom_user_permissions(self, user: AbstractUser | AnonymousUser) -> list[str]: |
| 110 | + """ |
| 111 | + Returns a list of custom permissions (not standard CRUD actions) that the user has on this instance. |
| 112 | + """ |
| 113 | + object_perms = self._get_object_perms(user) |
| 114 | + custom_perms = set() |
| 115 | + # Extract custom permissions that are not standard CRUD actions |
| 116 | + for perm in object_perms: |
| 117 | + action = perm.split("_", 1)[0] |
| 118 | + # Make sure to exclude standard CRUD actions |
| 119 | + if action not in ["view", "create", "update", "delete"]: |
| 120 | + custom_perms.add(action) |
| 121 | + return list(custom_perms) |
| 122 | + |
32 | 123 | class Meta: |
33 | 124 | abstract = True |
0 commit comments