mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23: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>
57 lines
2.8 KiB
Ruby
57 lines
2.8 KiB
Ruby
# Configurable roles (Role, Permission): the owner/pilot text on each
|
|
# aircraft role becomes a link to a role. Owners and pilots keep exactly what
|
|
# they could do (Owner has everything; Pilot gets what pilots could do), and
|
|
# Mechanic and Viewer are added. The permissions are spelled out here so this
|
|
# migration doesn't change if the defaults in code do.
|
|
class CreateRoles < ActiveRecord::Migration[8.1]
|
|
BUILT_IN = [
|
|
[ "owner", "Owner", "Everything on the aircraft, including its people and sharing.", [] ],
|
|
[ "mechanic", "Mechanic", "Maintenance records: logbook entries, components, inspections, ADs and squawks.",
|
|
%w[update_hours report_squawks defer_squawks manage_squawks write_notes manage_notes record_consumables
|
|
manage_consumables write_logbook sign_logbook manage_components manage_inspections manage_ads
|
|
manage_major_records manage_weight_balance manage_documents record_oil_analyses manage_oil_analyses
|
|
receive_reminders] ],
|
|
[ "pilot", "Pilot", "Flying: logs flights and hours, reports squawks, adds notes and oil and fuel.",
|
|
%w[log_flights update_hours edit_own_flights report_squawks defer_squawks write_notes record_consumables
|
|
record_oil_analyses sign_logbook] ],
|
|
[ "viewer", "Viewer", "Sees everything on the aircraft and changes nothing.", [] ]
|
|
].freeze
|
|
|
|
def up
|
|
create_table :roles do |t|
|
|
t.string :name, null: false
|
|
t.string :key
|
|
t.string :description
|
|
t.json :permissions, null: false, default: []
|
|
t.integer :position, null: false, default: 0
|
|
t.timestamps
|
|
end
|
|
add_index :roles, :key, unique: true
|
|
add_index :roles, :name, unique: true
|
|
|
|
now = Time.current
|
|
BUILT_IN.each_with_index do |(key, name, description, permissions), i|
|
|
execute sanitize_sql([ "INSERT INTO roles (name, key, description, permissions, position, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
|
name, key, description, permissions.to_json, i, now, now ])
|
|
end
|
|
|
|
add_reference :aircraft_roles, :role, foreign_key: true
|
|
execute "UPDATE aircraft_roles SET role_id = (SELECT id FROM roles WHERE roles.key = aircraft_roles.role)"
|
|
execute "UPDATE aircraft_roles SET role_id = (SELECT id FROM roles WHERE key = 'pilot') WHERE role_id IS NULL"
|
|
change_column_null :aircraft_roles, :role_id, false
|
|
remove_column :aircraft_roles, :role
|
|
end
|
|
|
|
def down
|
|
add_column :aircraft_roles, :role, :string
|
|
execute "UPDATE aircraft_roles SET role = CASE (SELECT key FROM roles WHERE roles.id = aircraft_roles.role_id) WHEN 'owner' THEN 'owner' ELSE 'pilot' END"
|
|
change_column_null :aircraft_roles, :role, false
|
|
remove_reference :aircraft_roles, :role, foreign_key: true
|
|
drop_table :roles
|
|
end
|
|
|
|
private
|
|
|
|
def sanitize_sql(args) = ActiveRecord::Base.sanitize_sql_array(args)
|
|
end
|