11import logging
2- from typing import TYPE_CHECKING , Any , Union
2+ from typing import TYPE_CHECKING , Any
33
44from django .http import HttpRequest
55
66if TYPE_CHECKING :
7- from django .contrib .auth .models import AbstractUser # pragma: no cover
87 from ninja_extra .controllers .base import ControllerBase # pragma: no cover
98
109
1110logger = logging .getLogger (__name__ )
1211
1312
1413class PermissionService (object ):
14+ """Base permission service for extra customization needs"""
15+
1516 def check_permission (
1617 self , request : HttpRequest , controller : "ControllerBase"
1718 ) -> bool :
1819 """
1920 Return `True` if permission is granted, `False` otherwise.
2021 """
21- user : Union [AbstractUser ] = request .user # type: ignore
22- has_perm : bool = True
23- if request .method == "DELETE" :
24- has_perm = bool (user .is_superuser )
25- if request .method in ("PUT" , "PATCH" , "POST" ):
26- has_perm = bool (user .is_staff or user .is_superuser )
27- return bool (user and user .is_authenticated and user .is_active and has_perm )
22+ return True
2823
2924 def check_object_permission (
3025 self , request : HttpRequest , controller : "ControllerBase" , obj : Any
@@ -33,56 +28,3 @@ def check_object_permission(
3328 Return `True` if permission is granted, `False` otherwise.
3429 """
3530 return True
36-
37- #
38- # def check_permission_v2(
39- # self, request: HttpRequest, controller: "ControllerBase"
40- # ) -> bool:
41- # """
42- # Return `True` if permission is granted, `False` otherwise.
43- # """
44- # user = request.user
45- # has_perm = True
46- # if hasattr(controller, "model"):
47- # model = controller.model
48- # app = model._meta.app_label
49- # has_perm = user.has_perm(f"{app}.view_{model.__name__}")
50- # if request.method in ("PUT",):
51- # has_perm = user.has_perm(f"{app}.add_{model.__name__}")
52- # if request.method in ("PATCH", "POST"):
53- # has_perm = user.has_perm(f"{app}.change_{model.__name__}")
54- # if request.method in ("DELETE",):
55- # has_perm = user.has_perm(f"{app}.delete_{model.__name__}")
56- # if user.is_superuser:
57- # has_perm = True
58- # return bool(user and user.is_authenticated and user.is_active and has_perm)
59- #
60- # async def async_check_permission(
61- # self, request: HttpRequest, controller: "ControllerBase"
62- # ) -> bool:
63- # """
64- # Return `True` if permission is granted, `False` otherwise.
65- # """
66- # user = request.user
67- # has_perm = False
68- # if hasattr(controller, "model"):
69- # model = controller.model
70- # app = model._meta.app_label
71- # has_perm = await sync_to_async(user.has_perm)(
72- # f"{app}.view_{model.__name__}"
73- # )
74- # if request.method in ("PUT",):
75- # has_perm = await sync_to_async(user.has_perm)(
76- # f"{app}.add_{model.__name__}"
77- # )
78- # if request.method in ("PATCH", "POST"):
79- # has_perm = await sync_to_async(user.has_perm)(
80- # f"{app}.change_{model.__name__}"
81- # )
82- # if request.method in ("DELETE",):
83- # has_perm = await sync_to_async(user.has_perm)(
84- # f"{app}.delete_{model.__name__}"
85- # )
86- # if user.is_superuser:
87- # has_perm = True
88- # return bool(user and user.is_authenticated and user.is_active and has_perm)
0 commit comments