Refactor permissions logic with Pundit gem and write policies for user roles#367
Open
cycomachead wants to merge 1 commit intomainfrom
Open
Refactor permissions logic with Pundit gem and write policies for user roles#367cycomachead wants to merge 1 commit intomainfrom
cycomachead wants to merge 1 commit intomainfrom
Conversation
Refactor permissions logic using the Pundit gem to replace manual role checks. This introduces a granular policy-based system to manage permissions for site admins, teachers, lead TAs, regular TAs, and students. - Add Pundit and pundit-matchers gems - Create policies for Courses, Requests, Assignments, and Settings - Update controllers to use `authorize` calls - Fix Course model to correctly identify lead TAs as instructors - Add comprehensive policy specs for all user roles 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.
General Info
Changes
This PR replaces the existing ad-hoc role-checking logic scattered across controllers with a centralized, policy-based authorization system using the Pundit gem.
Why
The previous approach used manual
@role == 'instructor'checks and custombefore_actionguard methods in individual controllers. This was inconsistent, hard to test in isolation, and didn't correctly handle theleadtarole (lead TAs were silently denied access to actions they should have been permitted to perform due to a bug inCourse#user_role).What Changed
Bug Fix
Course#user_rolenow correctly mapsleadtato'instructor', fixing a regression where lead TAs were denied all access.Authorization Architecture
punditgem andpundit-matchersfor testingPundit::AuthorizationinApplicationControllerwith a centralizedrescue_from Pundit::NotAuthorizedErrorhandlerCoursePolicy,RequestPolicy,CourseSettingsPolicy,FormSettingPolicy,AssignmentPolicy,UserToCoursePolicy) with anApplicationPolicybaseController Refactoring
before_actionguards (ensure_instructor_role,check_instructor_permission,ensure_course_admin) fromCourseSettingsController,FormSettingsController,RequestsController, andUserToCoursesControllerauthorizecalls inCoursesController,AssignmentsController, andRequestsControllerCoursesController#indexto includeleadtain the staff courses queryPermissions Matrix
Testing
Added comprehensive policy specs in
spec/policies/covering all user roles (site admin, teacher, lead TA, regular TA, student, unenrolled) for every policy action usingpundit-matchers. Also added:as_leadtaand:as_tafactory traits tospec/factories/user_to_course.rbto support the new specs.Existing controller specs should continue to pass as the authorization behavior is preserved — only the implementation mechanism has changed.
Documentation
No external documentation required. The policy classes are self-documenting and the permissions matrix above captures the intended behavior.
Checklist
Superconductor Ticket Implementation | App Preview | Guided Review