Skip to content

Commit bb75b17

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 87f3155 commit bb75b17

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)