Refactor RBAC permissions logic to centralize and test without external gems#369
Open
cycomachead wants to merge 1 commit intomainfrom
Open
Refactor RBAC permissions logic to centralize and test without external gems#369cycomachead wants to merge 1 commit intomainfrom
cycomachead wants to merge 1 commit intomainfrom
Conversation
…oncern Replaces decentralized `@role` string comparisons with a dedicated CoursePolicy class and controller Authorization concern. This refactor removes reliance on external gems while implementing modern RBAC practices. Key changes: - Created CoursePolicy to define four role tiers: site_admin, course_admin (teacher/leadta), staff (TA), and student. - Restricted course/form settings and assignment toggling to course admins only (previously open to all TAs). - Prevented students from canceling or deleting requests. - Added 58 comprehensive tests for all role/permission combinations. - Removed legacy `@role == 'instructor'` checks across controllers and views. Co-authored-by: Claude Code <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Centralize RBAC Permissions with CoursePolicy and Authorization Concern
This PR replaces decentralized
@rolestring comparisons scattered across controllers and views with a dedicatedCoursePolicyclass andAuthorizationcontroller concern, removing reliance on external gems while implementing modern RBAC practices.General Info
Changes
Previously, authorization was handled ad-hoc throughout controllers and views using
@role == 'instructor'string comparisons. This approach was fragile, untested, and didn't correctly handle all role types (e.g.,leadtawas not recognized as a staff role in several places).New files:
app/policies/course_policy.rb— Centralized RBAC policy class defining four role tiers:site_admin:User.admin?flag, can do everythingcourse_admin: teacher or leadta enrollment, can manage everything in a coursestaff(TA): regular TA, can view everything, approve/deny requests, sync assignments/enrollmentsstudent: can only manage their own extensionsapp/controllers/concerns/authorization.rb— Controller concern providingcurrent_policy,authorize!, anddeny_access!helpers with consistent redirect/JSON error behaviorKey behavioral changes:
leadtarolecourses_pathcourse_path(@course)when course is knownThe
ensure_instructor_rolemethod inApplicationControllernow delegates toauthorize! :staff?rather than checking@role == 'instructor'. Theuser_rolemethod onCoursenow usesUserToCourse.staff_rolesto correctly includeleadtain the staff check.Testing
Added 58 comprehensive unit tests in
spec/policies/course_policy_spec.rbcovering every permission method for all role combinations (site_admin, teacher, leadta, ta, student, unenrolled).Updated existing controller specs to use real database enrollments instead of
allow_any_instance_ofstubs, making tests more accurate and resilient. Added new controller-level tests for:All 448 existing tests continue to pass.
Documentation
No external documentation required. The
CoursePolicyclass is thoroughly documented inline with role definitions and usage examples.Checklist
Superconductor Ticket Implementation | App Preview | Guided Review