Skip to content

Refactor permission checks and enhance access control across views#229

Merged
rsmoke merged 22 commits intomainfrom
staging
Mar 18, 2026
Merged

Refactor permission checks and enhance access control across views#229
rsmoke merged 22 commits intomainfrom
staging

Conversation

@rsmoke
Copy link
Copy Markdown
Member

@rsmoke rsmoke commented Mar 18, 2026

This pull request enhances authorization logic across several controllers and views, making permission checks more consistent and efficient. It introduces controller instance variables for permission states, refactors policy checks in views to use these variables, and strengthens policy logic for appointments, calendar events, and questionnaires. Additionally, student listing and enrollment date handling are improved for clarity and maintainability.

Authorization and Permission Handling:

  • Controllers now set instance variables (e.g., @can_create_*, @can_update_*, @can_destroy_*, @can_view_*) for permission checks, replacing direct policy calls in views. This improves efficiency and makes templates cleaner.
  • Views updated to use these instance variables for conditional rendering of actions like "New", "Edit", "Delete", and "View", ensuring UI reflects user permissions accurately.

Policy Logic Improvements:

  • Policies for Appointment, CalendarEvent, and Questionnaire are refactored to ensure department admins can only access resources in their own department, with clearer super admin handling and robust record checks.

Student Listing and Enrollment Dates:

  • Student controller refactored to use a new set_students_and_enrollment_dates method, simplifying student listing and ensuring enrollment dates are available for each student. This method is used in both index and create actions for error handling.

Other Minor Improvements:

  • Questionnaire view now uses .size instead of .count for questions, improving performance and correctness.

rsmoke added 22 commits March 18, 2026 10:16
Updated the calendar events index view to use a new instance variable, @can_update_calendar_events, for determining if the user can update calendar events. This change simplifies the permission check by removing direct policy calls from the view.
Updated various controllers and views to utilize instance variables for permission checks, improving readability and maintainability. This includes adding @can_update and @can_view variables for affiliated resources, appointments, questionnaires, students, and vips, and updating the corresponding views to use these variables instead of direct policy calls.
Created a student user and associated it with a program in the student request specs. Updated response checks to use a custom matcher for action elements, improving clarity in the tests.
Updated the questionnaires index view to use the instance variable @can_create_questionnaires for determining if the user can create new questionnaires. This change enhances readability by removing direct policy calls from the view.
Updated the students controller to introduce a new method, set_students_and_enrollment_dates, for managing student data and their enrollment dates. This change enhances code readability and maintainability by centralizing the logic for fetching student enrollment dates. The index view has been updated accordingly to utilize this new method.
Updated the VipsController to create a VIP instance before authorization checks, enhancing code clarity and maintainability. This change ensures that the authorization logic is more consistent and easier to follow.
Updated the appointment policy to restrict department admins from accessing appointments of programs in other departments. Added tests to verify this behavior in both the appointment request specs and the appointment policy specs, ensuring proper authorization checks are in place.
Updated the calendar event policy to restrict department admins from accessing calendar events of programs in other departments. Added tests to verify this behavior in the calendar events request specs and policy specs, ensuring proper authorization checks are in place.
Updated the index action in CalendarEventsController to include eager loading of associated calendar_event_faculties and vips. This change improves performance by reducing the number of database queries when fetching calendar events, while maintaining the existing order by start_time.
Updated the appointments index view to use the instance variable @can_create_appointments for determining if the user can create new appointments. This change enhances readability by removing direct policy calls from the view. Additionally, the appointments controller was updated to set this variable based on the user's permissions.
Updated the calendar events index view to utilize the instance variable @can_create_calendar_events for determining if the user can create new events. This change improves readability by eliminating direct policy calls from the view. The controller was also modified to set this variable based on the user's permissions.
Updated the affiliated resources index view to utilize the instance variable @can_create_affiliated_resources for determining if the user can create new resources. This change enhances readability by removing direct policy calls from the view. The affiliated resources controller was also modified to set this variable based on the user's permissions.
Updated the questionnaire policy to restrict department admins from accessing questionnaires of programs in other departments. Added tests to verify this behavior in the questionnaires request specs and policy specs, ensuring proper authorization checks are in place. Additionally, improved the request specs to include scenarios for department admins and their access rights.
Updated the Vips index view to utilize the instance variable @can_create_vips for determining if the user can create new VIPs. This change improves readability by removing direct policy calls from the view. The VipsController was also modified to set this variable based on the user's permissions.
Updated the index action in AffiliatedResourcesController to create an instance of AffiliatedResource before performing authorization checks. This change improves code readability and maintainability by centralizing the resource instantiation, ensuring consistent policy evaluations for create, update, and destroy permissions.
Updated the questionnaires index view to use the `size` method instead of `count` for displaying the number of questions. This change improves performance by reducing database queries when accessing the questions associated with each questionnaire.
Modified the appointment request specs to replace the "View" label with "Actions" and included the appointment path in the response body checks. This change ensures that the tests accurately reflect the updated UI and functionality for appointment actions.
Modified the student request specs to check for the presence of the bulk upload link in the response body and action elements. This change ensures that the tests accurately reflect the updated UI and functionality related to bulk uploads for department program students.
Modified the appointment request specs to ensure that the "View" link correctly points to the appointment show path. This change enhances the accuracy of the tests in reflecting the updated UI and functionality for appointment actions.
… anchor

Modified the student request specs to enhance the validation of the bulk upload link by asserting against the specific anchor text "Bulk Upload" in the response body. This change ensures that the tests accurately reflect the updated UI and functionality for bulk uploads in department program students.
Updated the appointment policy methods to improve readability and maintainability by restructuring the authorization checks. Each method now clearly handles user presence and program association, ensuring that department admins can only access appointments for their respective departments. This change enhances the overall clarity of the policy logic.
@rsmoke rsmoke merged commit 4434e8c into main Mar 18, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant