Skip to content

Commit 1326aeb

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 1326aeb

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)