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

+56
View File
@@ -0,0 +1,56 @@
# 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
Generated
+16 -2
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2026_11_15_020000) do
ActiveRecord::Schema[8.1].define(version: 2026_11_16_010000) do
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
@@ -210,11 +210,12 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_15_020000) do
create_table "aircraft_roles", force: :cascade do |t|
t.integer "aircraft_id", null: false
t.integer "user_id", null: false
t.string "role", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "role_id", null: false
t.index ["aircraft_id", "user_id"], name: "index_aircraft_roles_on_aircraft_id_and_user_id", unique: true
t.index ["aircraft_id"], name: "index_aircraft_roles_on_aircraft_id"
t.index ["role_id"], name: "index_aircraft_roles_on_role_id"
t.index ["user_id"], name: "index_aircraft_roles_on_user_id"
end
@@ -785,6 +786,18 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_15_020000) do
t.datetime "updated_at", null: false
end
create_table "roles", force: :cascade do |t|
t.string "name", null: false
t.string "key"
t.string "description"
t.json "permissions", default: [], null: false
t.integer "position", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["key"], name: "index_roles_on_key", unique: true
t.index ["name"], name: "index_roles_on_name", unique: true
end
create_table "sessions", force: :cascade do |t|
t.integer "user_id", null: false
t.string "ip_address"
@@ -958,6 +971,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_15_020000) do
add_foreign_key "aircraft_notes", "aircraft"
add_foreign_key "aircraft_notes", "users", on_delete: :nullify
add_foreign_key "aircraft_roles", "aircraft"
add_foreign_key "aircraft_roles", "roles"
add_foreign_key "aircraft_roles", "users"
add_foreign_key "airport_updates", "users", column: "requested_by_id", on_delete: :nullify
add_foreign_key "airworthiness_directives", "documents", on_delete: :nullify