Skip to content

Commit edca9e8

Browse files
committed
refactor: update view_auth_classes decorator to include permission checks for authenticated users
1 parent 64f7505 commit edca9e8

2 files changed

Lines changed: 11 additions & 13 deletions

File tree

openedx_authz/rest_api/utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,30 @@
44
from django.db.models import Q
55
from edx_rest_framework_extensions.auth.jwt.authentication import JwtAuthentication
66
from edx_rest_framework_extensions.auth.session.authentication import SessionAuthenticationAllowInactiveUser
7+
from rest_framework.permissions import IsAuthenticated
78

89
User = get_user_model()
910

1011

11-
def view_auth_classes(func_or_class):
12+
def view_auth_classes(is_authenticated=True):
1213
"""
13-
Function and class decorator that abstracts the authentication classes for api views.
14+
Function and class decorator that abstracts the authentication and permission checks for api views.
1415
"""
1516

1617
def _decorator(func_or_class):
1718
"""
18-
Requires either OAuth2 or Session-based authentication;
19-
are the same authentication classes used on edx-platform
19+
Requires either OAuth2 or Session-based authentication.
2020
"""
2121
func_or_class.authentication_classes = (
2222
JwtAuthentication,
2323
SessionAuthenticationAllowInactiveUser,
2424
)
25+
func_or_class.permission_classes = ()
26+
if is_authenticated:
27+
func_or_class.permission_classes += (IsAuthenticated,)
2528
return func_or_class
2629

27-
return _decorator(func_or_class)
30+
return _decorator
2831

2932

3033
def get_user_by_username_or_email(username_or_email: str) -> User:

openedx_authz/rest_api/v1/views.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from django.contrib.auth import get_user_model
1212
from django.http import HttpRequest
1313
from rest_framework import status
14-
from rest_framework.permissions import IsAuthenticated
1514
from rest_framework.response import Response
1615
from rest_framework.views import APIView
1716

@@ -34,7 +33,7 @@
3433
User = get_user_model()
3534

3635

37-
@view_auth_classes
36+
@view_auth_classes()
3837
class PermissionValidationView(APIView):
3938
"""
4039
API view for validating user permissions against authorization policies.
@@ -44,8 +43,6 @@ class PermissionValidationView(APIView):
4443
Supports batch permission validation through POST request.
4544
"""
4645

47-
permission_classes = [IsAuthenticated]
48-
4946
@apidocs.schema(
5047
body=PermissionValidationSerializer(help_text="The permissions to validate", many=True),
5148
responses={
@@ -81,13 +78,12 @@ def post(self, request: HttpRequest) -> Response:
8178
return Response(serializer.data, status=status.HTTP_200_OK)
8279

8380

84-
@view_auth_classes
81+
@view_auth_classes()
8582
class RoleUserAPIView(APIView):
8683
"""
8784
API view for managing user-role assignments within specific scope.
8885
"""
8986

90-
permission_classes = [IsAuthenticated]
9187
pagination_class = AuthZAPIViewPagination
9288

9389
@apidocs.schema(
@@ -187,13 +183,12 @@ def delete(self, request: HttpRequest) -> Response:
187183
return Response(response_data, status=status.HTTP_207_MULTI_STATUS)
188184

189185

190-
@view_auth_classes
186+
@view_auth_classes()
191187
class RoleListView(APIView):
192188
"""
193189
API view for retrieving role definitions and their associated permissions.
194190
"""
195191

196-
permission_classes = [IsAuthenticated]
197192
pagination_class = AuthZAPIViewPagination
198193

199194
@apidocs.schema(

0 commit comments

Comments
 (0)