Configurable roles: Owner, Mechanic, Pilot, Viewer and custom roles

What people can do on an aircraft now comes from their role there, and
administrators set what each role allows (Admin → Roles).

- Roles are installation-wide sets of permissions (31, grouped as flying,
  squawks and notes, maintenance records, imports and exports, the
  aircraft, notifications). Seeing an aircraft's records needs none.
- Built in: Owner (always every permission, so an aircraft can always be
  managed), Mechanic (shop maintenance: logbook, signing, components,
  inspections, ADs, squawks, reminders), Pilot (what pilots could do
  before) and Viewer (view only). Administrators can change all but
  Owner's, and add their own; a role can be deleted once nobody has it.
- Existing owners and pilots keep exactly what they could do.
- Admin → Users → a user lists their role on every aircraft, to change,
  remove or add. On the aircraft's Access page, anyone who manages people
  hands out roles, but only an Owner can give or take away Owner, nobody
  changes their own (except administrators), and an aircraft always
  keeps an Owner (AircraftAccess).
- Controllers declare the permission each action needs; a request that
  changes something with none declared is refused (and raises in
  development and tests), and a test walks every route to check.
- Weekly reminders and overdue alerts go to roles that get reminders;
  the DRS reminder also needs Manage ADs. The API follows the person's
  role; its owners_only error is now not_permitted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rickyandClaude Opus 5.5 committed 2026-11-15 17:43:38 -05:00
1 parent d2cd60c6a7
commit d8cd462194
101 files changed
+1032 -232

No files matched your search

+53 -15
View File
@@ -1,16 +1,38 @@
# Scopes a controller to one aircraft the current user can access and exposes
# role helpers. Unknown and inaccessible aircraft both 404 (enumeration-safe).
# Scopes a controller to one aircraft the current user can access, and checks
# what their role there allows (Role, Permission). Unknown and inaccessible
# aircraft both 404 (enumeration-safe). Administrators can do everything.
#
# Access matrix (mirrors the original):
# admin – everything
# owner – everything on their aircraft
# pilot – view all; update hours; create squawks, notes, oil/fuel, flight logs
# Each controller says which permission its actions need:
#
# requires_permission :manage_components, except: %i[index show]
# requires_permission :report_squawks, only: %i[new create]
# checks_permission_itself only: %i[edit update] # decided in the controller
#
# Looking needs no permission. A request that changes something (anything but
# GET/HEAD) with no permission declared for its action is refused, so nothing
# is left open by mistake; in development and tests it raises instead.
module AircraftScoped
extend ActiveSupport::Concern
SELF_CHECKED = "(checked by the controller)".freeze
class MissingPermission < StandardError; end
included do
class_attribute :permission_rules, instance_accessor: false, default: []
before_action :set_aircraft
helper_method :current_role, :owner?, :can_write?
before_action :authorize_permission!
helper_method :current_role, :can?, :owner?
end
class_methods do
def requires_permission(permission, only: nil, except: nil)
self.permission_rules += [ [ permission.to_s, only && Array(only).map(&:to_s), except && Array(except).map(&:to_s) ] ]
end
def checks_permission_itself(only:)
requires_permission(SELF_CHECKED, only: only)
end
end
private
@@ -21,18 +43,34 @@ module AircraftScoped
@aircraft = current_user.accessible_aircraft.find(aircraft_param)
end
# Their Role here (nil for an administrator without one).
def current_role
@current_role ||= current_user.role_for(@aircraft)
return @current_role if defined?(@current_role)
@current_role = current_user.role_for(@aircraft)
end
# Owner-level write access (admins included).
def owner? = current_role.in?(%w[admin owner])
alias_method :can_write?, :owner?
def can?(permission) = current_user.admin? || current_role&.can?(permission) || false
def require_owner!
return if owner?
redirect_back fallback_location: aircraft_path(@aircraft),
alert: t("flash.common.only_owners_can_do")
# The Owner role (or an administrator): everything.
def owner? = current_user.admin? || current_role&.owner? || false
def authorize_permission!
rules = self.class.permission_rules.select do |_, only, except|
(only.nil? || only.include?(action_name)) && (except.nil? || except.exclude?(action_name))
end
if rules.empty?
return if request.get? || request.head?
raise MissingPermission, "#{self.class.name}##{action_name} changes data without declaring a permission" if Rails.env.local?
return permission_denied
end
permission_denied unless rules.all? { |permission, *| permission == SELF_CHECKED || can?(permission) }
end
def permission_denied
respond_to do |format|
format.html { redirect_back fallback_location: aircraft_path(@aircraft), alert: t("flash.common.not_allowed") }
format.any { head :forbidden }
end
end
# Returns id only if it belongs to the given (aircraft-scoped) relation; else nil.