Refactor permissions logic with cancancan gem and define Abilities for user roles#368
Draft
cycomachead wants to merge 1 commit intomainfrom
Draft
Refactor permissions logic with cancancan gem and define Abilities for user roles#368cycomachead wants to merge 1 commit intomainfrom
cycomachead wants to merge 1 commit intomainfrom
Conversation
Refactor permissions logic to use the CanCanCan gem, replacing manual role checks with a centralized Ability model. This defines granular permissions for site admins, course admins (teachers/lead TAs), regular TAs, and students. - Site admins: full system access. - Course admins: full management of their specific courses. - TAs: can view data and manage requests but cannot change settings. - Students: can only manage their own requests. - Staff: can create/import new courses. Includes a global access-denied handler and comprehensive test coverage for all roles and edge cases. 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 refactors the permissions system to use the CanCanCan gem, replacing ad-hoc role checks scattered across controllers with a centralized
Abilitymodel.Why
Previously, authorization was handled inconsistently — some controllers checked
@role == 'instructor', others usedparams[:role](an insecure pattern that allowed clients to self-report their role), and some had no checks at all. This made it difficult to reason about what each role could do and easy to miss edge cases.What Changed
Permissions Model (
app/models/ability.rb)A new
Abilityclass defines all permissions in one place:CourseSettings,FormSetting, or toggle extended circumstances statusRemoved enrollments (
removed: true) are excluded from all permission grants.Bug Fix
Course#user_rolewas not mappingleadtato theinstructorrole — it only checked forteacherandta. This has been fixed to useUserToCourse.staff_roles.Controller Updates
ApplicationController: Added a globalrescue_from CanCan::AccessDeniedhandler (supports HTML and JSON responses). Simplifiedensure_instructor_roleto delegate to CanCanCan.CoursesController: Addedauthorize!calls forshow,new,edit,create,sync_assignments,sync_enrollments,enrollments, anddelete. Fixedindexto includeleadtain the staff courses listing.RequestsController: Addedauthorize!for all sensitive actions; removed the oldcheck_instructor_permissionbefore-action.CourseSettingsController/FormSettingsController: Replacedensure_instructor_rolewith model-specific authorization helpers.AssignmentsController: Replaced manual role check (which usedparams[:role]) withauthorize! :toggle_enabled, @assignment.UserToCoursesController: Replacedensure_course_adminwithauthorize! :toggle_allow_extended_requests, @enrollment.Testing
Abilityspec tests covering all roles (admin, teacher, lead TA, TA, student, unauthenticated, multi-role, and removed enrollment scenarios).params[:role], making tests more accurate and secure.:as_taand:as_leadtafactory traits forUserToCourse.Documentation
No additional documentation required. The
Abilitymodel itself serves as the canonical reference for what each role can do.Checklist
Superconductor Ticket Implementation | App Preview | Guided Review