| 1 |
from collections.abc import Iterable |
| 2 |
from typing import Any |
| 3 |
|
| 4 |
from django.contrib.auth import get_user_model |
| 5 |
from django.db import models |
| 6 |
from django.db.models import Model |
| 7 |
from django.http import HttpRequest |
| 8 |
|
| 9 |
from guardian.conf import settings |
| 10 |
from guardian.core import ObjectPermissionChecker |
| 11 |
from guardian.ctypes import get_content_type |
| 12 |
from guardian.exceptions import WrongAppError |
| 13 |
|
| 14 |
|
| 15 |
def check_object_support(obj: Model) -> bool: |
| 16 |
"""Checks if given `obj` is supported |
| 17 |
|
| 18 |
Returns: |
| 19 |
`True` if given `obj` is supported |
| 20 |
""" |
| 21 |
# Backend checks only object permissions (isinstance implies that obj |
| 22 |
# is not None) |
| 23 |
# Backend checks only permissions for Django models |
| 24 |
return isinstance(obj, models.Model) |
| 25 |
|
| 26 |
|
| 27 |
def check_user_support(user_obj: Any) -> tuple[bool, Any]: |
| 28 |
"""Checks if given user is supported. |
| 29 |
|
| 30 |
Checks if the given user is supported. Anonymous users need explicit |
| 31 |
activation via ANONYMOUS_USER_NAME |
| 32 |
|
| 33 |
Returns: |
| 34 |
A tuple of checkresult and `user_obj` which should be used for permission checks |
| 35 |
""" |
| 36 |
# This is how we support anonymous users - simply try to retrieve User |
| 37 |
# instance and perform checks for that predefined user |
| 38 |
if not user_obj.is_authenticated: |
| 39 |
# If anonymous user permission is disabled, then they are always unauthorized |
| 40 |
if settings.ANONYMOUS_USER_NAME is None: |
| 41 |
return False, user_obj |
| 42 |
user_model = get_user_model() |
| 43 |
lookup = {user_model.USERNAME_FIELD: settings.ANONYMOUS_USER_NAME} |
| 44 |
user_obj = user_model.objects.get(**lookup) |
| 45 |
|
| 46 |
return True, user_obj |
| 47 |
|
| 48 |
|
| 49 |
def check_support(user_obj: Any, obj: Model) -> Any: |
| 50 |
"""Checks if given user and object are supported. |
| 51 |
|
| 52 |
Combination of `check_object_support` and `check_user_support` |
| 53 |
""" |
| 54 |
obj_support = check_object_support(obj) |
| 55 |
user_support, user_obj = check_user_support(user_obj) |
| 56 |
return obj_support and user_support, user_obj |
| 57 |
|
| 58 |
|
| 59 |
class ObjectPermissionBackend: |
| 60 |
"""Django backend for checking object-level permissions.""" |
| 61 |
|
| 62 |
supports_object_permissions = True |
| 63 |
supports_anonymous_user = True |
| 64 |
supports_inactive_user = True |
| 65 |
|
| 66 |
def authenticate(self, request: HttpRequest, username: str | None = None, password: str | None = None) -> Any: |
| 67 |
return None |
| 68 |
|
| 69 |
def has_perm(self, user_obj: Any, perm: str, obj: Model | None = None) -> bool: |
| 70 |
"""Check if a user has the permission for a given object. |
| 71 |
|
| 72 |
Returns `True` if given `user_obj` has `perm` for `obj`. |
| 73 |
If no `obj` is given, `False` is returned. |
| 74 |
The main difference between Django's `ModelBackend` is that we can pass |
| 75 |
`obj` instance here and `perm` doesn't have to contain |
| 76 |
`app_label` as it can be retrieved from given `obj`. |
| 77 |
|
| 78 |
**Inactive user support** |
| 79 |
|
| 80 |
If `user` is authenticated but inactive at the same time, all checks |
| 81 |
always return `False`. |
| 82 |
|
| 83 |
Note: |
| 84 |
Remember, that if user is not *active*, all checks would return `False`. |
| 85 |
|
| 86 |
Parameters: |
| 87 |
user_obj (User): User instance. |
| 88 |
perm (str): Permission string. |
| 89 |
obj (Model): Model instance. |
| 90 |
|
| 91 |
Returns: |
| 92 |
`True` if `user_obj` has permission, `False` otherwise. |
| 93 |
""" |
| 94 |
|
| 95 |
# check if user_obj and object are supported |
| 96 |
support, user_obj = check_support(user_obj, obj) |
| 97 |
if not support: |
| 98 |
return False |
| 99 |
|
| 100 |
if "." in perm: |
| 101 |
app_label, _ = perm.split(".", 1) |
| 102 |
# TODO (David Graham): Check if obj is None or change the method signature |
| 103 |
if app_label != obj._meta.app_label: # type: ignore[union-attr] |
| 104 |
# Check the content_type app_label when permission |
| 105 |
# and obj app labels don't match. |
| 106 |
ctype = get_content_type(obj) |
| 107 |
if app_label != ctype.app_label: |
| 108 |
raise WrongAppError( |
| 109 |
f"Passed perm has app label of '{app_label}' while " |
| 110 |
f"given obj has app label '{obj._meta.app_label}' and given obj" # type: ignore[union-attr] |
| 111 |
f"content_type has app label '{ctype.app_label}'" |
| 112 |
) |
| 113 |
|
| 114 |
check = ObjectPermissionChecker(user_obj) |
| 115 |
return check.has_perm(perm, obj) |
| 116 |
|
| 117 |
def get_group_permissions(self, user_obj: Any, obj: Model | None = None) -> Iterable[str]: |
| 118 |
"""Returns group permissions for a given object. |
| 119 |
|
| 120 |
Parameters: |
| 121 |
user_obj (User): User instance. |
| 122 |
obj (Model): Django Model instance. If None, returns empty set |
| 123 |
since this backend only handles object-level permissions. |
| 124 |
|
| 125 |
Returns: |
| 126 |
a set of permission strings that the given `user_obj` has for `obj` |
| 127 |
through their group memberships. |
| 128 |
""" |
| 129 |
# This backend only handles object-level permissions |
| 130 |
if obj is None: |
| 131 |
return set() |
| 132 |
|
| 133 |
# check if user_obj and object are supported |
| 134 |
support, user_obj = check_support(user_obj, obj) |
| 135 |
if not support: |
| 136 |
return set() |
| 137 |
|
| 138 |
check = ObjectPermissionChecker(user_obj) |
| 139 |
return set(check.get_group_perms(obj)) |
| 140 |
|
| 141 |
def get_all_permissions(self, user_obj: Any, obj: Model | None = None) -> Iterable[str]: |
| 142 |
"""Returns all permissions for a given object. |
| 143 |
|
| 144 |
Parameters: |
| 145 |
user_obj (User): User instance. |
| 146 |
obj (Model): Django Model instance. |
| 147 |
|
| 148 |
Returns: |
| 149 |
a set of permission strings that the given `user_obj` has for `obj`. |
| 150 |
""" |
| 151 |
# check if user_obj and object are supported |
| 152 |
support, user_obj = check_support(user_obj, obj) |
| 153 |
if not support: |
| 154 |
return set() |
| 155 |
|
| 156 |
check = ObjectPermissionChecker(user_obj) |
| 157 |
return set(check.get_perms(obj)) |