mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
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>
85 lines
3.0 KiB
Ruby
85 lines
3.0 KiB
Ruby
# 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.
|
|
#
|
|
# 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
|
|
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
|
|
|
|
def aircraft_param = params[:aircraft_id]
|
|
|
|
def set_aircraft
|
|
@aircraft = current_user.accessible_aircraft.find(aircraft_param)
|
|
end
|
|
|
|
# Their Role here (nil for an administrator without one).
|
|
def current_role
|
|
return @current_role if defined?(@current_role)
|
|
@current_role = current_user.role_for(@aircraft)
|
|
end
|
|
|
|
def can?(permission) = current_user.admin? || current_role&.can?(permission) || false
|
|
|
|
# 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.
|
|
def scoped_id(scope, id)
|
|
id.present? && scope.where(id: id).exists? ? id : nil
|
|
end
|
|
|
|
def log_event(category, name, notes: nil)
|
|
@aircraft.log_event!(category, name, notes: notes)
|
|
end
|
|
end
|