Skip to content

Commit 6dd24d6

Browse files
Taimoor  AhmedTaimoor  Ahmed
authored andcommitted
docs: add ADR for standardizing permissions usage
Currently, authorization logic is implemented inconsistently across views, serializers, and custom access checks. This ADR will define a consistent approach using DRF permission classes, object-level permissions, and queryset scoping where appropriate.
1 parent ffbedba commit 6dd24d6

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Open edX ADR 002: Standardize Permission Classes Across APIs
2+
============================================================
3+
4+
:Status: Proposed
5+
:Date: 2026-03-18
6+
:Deciders: API Working Group
7+
:Technical Story: Open edX REST API Standards - Permission standardization for security consistency
8+
9+
Context
10+
-------
11+
12+
Permissions are inconsistently applied across Open edX apps using custom decorators, inline role-based checks, and embedded authorization logic within views. This creates security gaps, makes it difficult for external systems to reliably determine access, and leads to duplicate authorization logic across multiple views.
13+
14+
The recommendation in the standards document is to standardize permission handling by adopting DRF permission_classes as the primary mechanism for authorization across all APIs.
15+
16+
Decision
17+
--------
18+
19+
We will standardize all Open edX REST APIs to use **DRF permission_classes** as the primary authorization mechanism.
20+
21+
Implementation requirements:
22+
23+
* Use DRF permission_classes for all authorization logic instead of custom decorators.
24+
* Create reusable permission classes for common authorization patterns (course staff, global staff, etc.).
25+
* Replace inline role-based checks with explicit permission classes.
26+
* Ensure permission classes are properly documented and tested.
27+
* Maintain consistent permission patterns across similar endpoint types.
28+
29+
Relevance in edx-platform
30+
-------------------------
31+
32+
Current patterns that should be migrated:
33+
34+
* **Enrollment API** (``/api/enrollment/v1/enrollment/{username},{course_id}``) uses custom inline role checks.
35+
* **User Tours API** (``/api/user_tours/v1/{username}``) mixes inline checks and permission_classes.
36+
* **Course orphan endpoints** (``^orphan/{settings.COURSE_KEY_PATTERN}$``) use functional views with inline permission logic.
37+
38+
Code example (target permission usage)
39+
--------------------------------------
40+
41+
**Example permission classes and APIView using DRF best practices:**
42+
43+
.. code-block:: python
44+
45+
# permissions.py
46+
from rest_framework.permissions import BasePermission
47+
48+
class IsCourseStaff(BasePermission):
49+
"""
50+
Allows access only to course staff members.
51+
"""
52+
def has_permission(self, request, view):
53+
return request.user.is_authenticated and request.user.is_staff
54+
55+
class IsEnrollmentOwnerOrStaff(BasePermission):
56+
"""
57+
Allows access to enrollment data for the user themselves or course staff.
58+
"""
59+
def has_object_permission(self, request, view, obj):
60+
return (
61+
obj.user == request.user or
62+
request.user.is_staff or
63+
request.user.has_perm('course_staff', obj.course)
64+
)
65+
66+
# views.py
67+
from rest_framework.views import APIView
68+
from rest_framework.response import Response
69+
from rest_framework.permissions import IsAuthenticated
70+
from .permissions import IsCourseStaff
71+
72+
class EnrollmentAPIView(APIView):
73+
permission_classes = [IsAuthenticated, IsCourseStaff]
74+
75+
def get(self, request):
76+
return Response({"detail": "Access granted"})
77+
78+
Consequences
79+
------------
80+
81+
Positive
82+
~~~~~~~~
83+
84+
* Improves security consistency across all APIs.
85+
* Enhances predictability for external integrations.
86+
* Ensures reusable, testable authorization logic.
87+
* Simplifies security audits and permission reviews.
88+
* Enables centralized permission management.
89+
90+
Negative / Trade-offs
91+
~~~~~~~~~~~~~~~~~~~~~
92+
93+
* Requires refactoring existing views with inline permission logic.
94+
* May need to create custom permission classes for complex authorization scenarios.
95+
* Initial development effort to identify and standardize permission patterns.
96+
97+
Alternatives Considered
98+
-----------------------
99+
100+
* **Keep mixed permission approaches**: rejected due to security inconsistencies and maintenance burden.
101+
* **Use only decorators**: rejected because DRF permission_classes provide better integration with the framework.
102+
103+
Rollout Plan
104+
------------
105+
106+
1. Audit existing endpoints to identify inconsistent permission patterns.
107+
2. Create a library of standard permission classes for common use cases.
108+
3. Migrate high-security endpoints first (enrollment, user data, course management).
109+
4. Add comprehensive tests for permission classes and their usage.
110+
5. Update API documentation to clearly specify permission requirements.
111+
112+
References
113+
----------
114+
115+
* Open edX REST API Standards: "Permissions" recommendations for security consistency.

0 commit comments

Comments
 (0)