File tree Expand file tree Collapse file tree 2 files changed +16
-2
lines changed
python_utils/django/keycloak/api Expand file tree Collapse file tree 2 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44
55[project ]
66name = " cardo-python-utils"
7- version = " 0.5.dev14 "
7+ version = " 0.5.dev15 "
88description = " Python library enhanced with a wide range of functions for different scenarios."
99readme = " README.rst"
1010requires-python = " >=3.8"
Original file line number Diff line number Diff line change @@ -42,18 +42,32 @@ class MyApiView(APIView):
4242 allowed_scopes = ["jobs"]
4343 ...
4444
45+ It is possible to define different scopes per HTTP method
46+ by setting `allowed_scopes` as a dict:
47+
48+ class MyApiView(APIView):
49+ permission_classes = [IsAuthenticated, HasScope]
50+ allowed_scopes = {
51+ "get": ["jobs"],
52+ "post": ["jobs_admin"],
53+ }
54+ ...
55+
4556 If no particular scope is required, you can set `allowed_scopes = "*"`
4657 to allow access without scope checks.
4758 """
4859
4960 def has_permission (self , request , view ):
50- allowed_scopes = getattr (view , "allowed_scopes" , [] )
61+ allowed_scopes = getattr (view , "allowed_scopes" , None )
5162
5263 if not allowed_scopes :
5364 raise Exception (
5465 f"No allowed_scopes defined on the view '{ view .__class__ .__name__ } '. "
5566 "Define allowed_scopes or set it to '*' to allow any scope."
5667 )
68+
69+ if isinstance (allowed_scopes , dict ):
70+ allowed_scopes = allowed_scopes .get (request .method .lower (), [])
5771
5872 if allowed_scopes == "*" :
5973 return True
You can’t perform that action at this time.
0 commit comments