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

+1 -1
View File
@@ -32,7 +32,7 @@
</ul>
<% elsif key == "users" %>
<ul class="ml-6 list-disc text-xs text-slate-600">
<% @audit.users.each do |u| %><li><%= t(".last_sign_in_html", display_name: u.display_name, email_address: u.email_address, admin: (t(".admin") if u.admin?), any: (" · #{u.aircraft_roles.map { "#{AircraftRole::ROLES[_1.role]} #{_1.aircraft.tail_number}" }.join(', ')}" if u.aircraft_roles.any?), never: (u.last_signed_in_at ? t(".ago", time: time_ago_in_words(u.last_signed_in_at)) : t(".never")), disabled: (t(".disabled") if u.disabled?)) %></li><% end %>
<% @audit.users.each do |u| %><li><%= t(".last_sign_in_html", display_name: u.display_name, email_address: u.email_address, admin: (t(".admin") if u.admin?), any: (" · #{u.aircraft_roles.map { "#{_1.role.name} #{_1.aircraft.tail_number}" }.join(', ')}" if u.aircraft_roles.any?), never: (u.last_signed_in_at ? t(".ago", time: time_ago_in_words(u.last_signed_in_at)) : t(".never")), disabled: (t(".disabled") if u.disabled?)) %></li><% end %>
</ul>
<% end %>
<%= text_field_tag "answers[#{key}][note]", nil, class: "input", placeholder: t(".note_what_was_checked_or") %>
@@ -2,7 +2,7 @@
Locals: policy, number, adopted_at, adopted_by, revisions, summary. %>
<% info = SystemInfo.new %>
<% aircraft = Aircraft.ordered.includes(:roles) %>
<% users = User.order(:email_address).includes(:certificates, aircraft_roles: :aircraft) %>
<% users = User.order(:email_address).includes(:certificates, aircraft_roles: %i[aircraft role]) %>
<% h2 = "mt-6 text-base font-semibold" %>
<% p_ = "mt-1 text-sm leading-relaxed" %>
<article class="text-slate-900">
@@ -50,7 +50,7 @@
<% users.each do |u| %>
<% current = u.certificates.reject(&:expired?) %>
<tr class="border-b border-slate-100 align-top"><td class="py-1"><%= u.display_name %><div class="text-xs text-slate-500"><%= u.email_address %></div></td>
<td class="text-xs"><%= [ (t(".administrator") if u.admin?), *u.aircraft_roles.map { "#{_1.role.capitalize}, #{_1.aircraft.tail_number}" } ].compact.join("; ").presence || "—" %><%= t(".disabled") if u.disabled? %></td>
<td class="text-xs"><%= [ (t(".administrator") if u.admin?), *u.aircraft_roles.map { "#{_1.role.name}, #{_1.aircraft.tail_number}" } ].compact.join("; ").presence || "—" %><%= t(".disabled") if u.disabled? %></td>
<td class="text-xs"><%= u.certificates.map { "#{_1.short_label} #{_1.number}#{" #{t('.expires_short', date: _1.expires_on)}" if _1.expires_on}" }.join("; ").presence || "—" %></td>
<td class="text-xs"><%= if u.disabled? || current.empty? then t(".no")
elsif current.any?(&:mechanic?) then t(".maintenance_and_inspection_entries")
+27
View File
@@ -0,0 +1,27 @@
<%# A role's name, description and permissions, grouped as in Permission.
Owner's permissions are shown but can't be changed: it always has all. %>
<%= form_with model: [:admin, role], class: "card card-body max-w-3xl space-y-5" do |f| %>
<%= f.errors_summary %>
<div class="grid gap-4 sm:grid-cols-2">
<%= f.field :name, required: true %>
<%= f.field :description, hint: t(".description_hint") %>
</div>
<% if role.owner? %><p class="rounded-md bg-sky-50 px-3 py-2 text-sm text-sky-900 ring-1 ring-sky-200"><%= t(".owner_has_everything") %></p><% end %>
<%= hidden_field_tag "role[permissions][]", "", id: nil %>
<div class="grid gap-5 sm:grid-cols-2">
<% Permission::GROUPS.each do |group, keys| %>
<fieldset class="space-y-2" <%= "disabled" if role.owner? %>>
<legend class="mb-1 text-sm font-semibold"><%= Permission.group_label(group) %></legend>
<% keys.each do |key| %>
<label class="flex items-start gap-2 text-sm">
<%= check_box_tag "role[permissions][]", key, role.can?(key), id: "permission_#{key}", class: "mt-0.5" %>
<span><%= Permission.label(key) %><span class="block text-xs text-slate-500"><%= Permission.description(key) %></span></span>
</label>
<% end %>
</fieldset>
<% end %>
</div>
<p class="hint"><%= t(".viewing_needs_nothing") %></p>
<div class="flex flex-wrap gap-2"><%= f.submit t(".save") %><%= link_to t(".cancel"), admin_roles_path, class: "btn-secondary" %></div>
<% end %>
<% if role.persisted? && !role.built_in? %><div class="mt-4"><%= delete_button t(".delete_role"), admin_role_path(role), confirm: t(".delete_role_confirm", name: role.name) %></div><% end %>
+3
View File
@@ -0,0 +1,3 @@
<% content_for :title, t(".edit_role", name: @role.name_was) %>
<h1 class="mb-4 text-2xl font-bold"><%= t(".edit_role", name: @role.name_was) %></h1>
<%= render "form", role: @role %>
+15
View File
@@ -0,0 +1,15 @@
<% content_for :title, t(".roles") %>
<div class="mb-2 flex items-center justify-between"><h1 class="text-2xl font-bold"><%= t(".roles") %></h1><%= link_to t(".new_role"), new_admin_role_path, class: "btn-primary" %></div>
<p class="mb-4 text-sm text-slate-600"><%= t(".intro") %></p>
<section class="card"><table class="table">
<thead><tr><th><%= t(".name") %></th><th><%= t(".can") %></th><th class="text-right"><%= t(".people") %></th><th></th></tr></thead>
<tbody><% @roles.each do |role| %><tr class="role-row" id="<%= dom_id(role) %>">
<td class="align-top"><span class="font-medium"><%= role.name %></span> <% if role.built_in? %><span class="badge-gray"><%= t(".built_in") %></span><% end %>
<% if role.description.present? %><div class="text-xs text-slate-500"><%= role.description %></div><% end %></td>
<td class="align-top text-xs text-slate-600" data-label="<%= t(".can") %>">
<%= role.owner? ? t(".everything") : (role.permissions.map { Permission.label(_1) }.to_sentence.presence || t(".view_only")) %>
</td>
<td class="align-top text-right tabular-nums" data-label="<%= t(".people") %>"><%= @counts.fetch(role.id, 0) %></td>
<td class="align-top text-right"><%= link_to t(".edit"), edit_admin_role_path(role), class: "btn-secondary btn-sm" %></td>
</tr><% end %></tbody>
</table></section>
+3
View File
@@ -0,0 +1,3 @@
<% content_for :title, t(".new_role") %>
<h1 class="mb-4 text-2xl font-bold"><%= t(".new_role") %></h1>
<%= render "form", role: @role %>
+33
View File
@@ -1,2 +1,35 @@
<h1 class="mb-4 text-2xl font-bold"><%= t(".edit_user") %></h1>
<%= render "form", user: @user %>
<%# Their role on each aircraft (same rules as each aircraft's Access page, AircraftAccess). %>
<section class="card mt-6 max-w-3xl" id="access">
<div class="card-header"><h2 class="card-title"><%= t(".aircraft_access") %></h2></div>
<% roles = Role.ordered.to_a %>
<% memberships = @user.aircraft_roles.includes(:aircraft, :role).sort_by { _1.aircraft.tail_number } %>
<% if memberships.any? %>
<table class="table">
<tbody>
<% memberships.each do |m| %>
<tr>
<td><%= link_to m.aircraft.tail_number, aircraft_path(m.aircraft), class: "link font-medium" %> <span class="text-xs text-slate-500"><%= [ m.aircraft.make, m.aircraft.model ].compact_blank.join(" ") %></span></td>
<td><%= form_with url: admin_user_aircraft_role_path(@user, m), method: :patch do |f| %>
<%= f.select :role_id, roles.map { [ _1.name, _1.id ] }, { selected: m.role_id }, class: "input w-36", "aria-label": t(".role_on", tail_number: m.aircraft.tail_number), data: { submit_on_change: true } %>
<% end %></td>
<td class="text-right"><%= delete_button t(".remove"), admin_user_aircraft_role_path(@user, m), confirm: t(".remove_access", tail_number: m.aircraft.tail_number) %></td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<%= empty_state t(".no_aircraft") %>
<% end %>
<% others = Aircraft.where.not(id: memberships.map(&:aircraft_id)).ordered %>
<% if others.any? %>
<%= form_with url: admin_user_aircraft_roles_path(@user), class: "flex flex-wrap items-end gap-2 border-t border-slate-100 p-4" do |f| %>
<div><%= f.label :aircraft_id, t(".aircraft"), class: "label" %><%= f.select :aircraft_id, others.map { [ _1.tail_number, _1.id ] }, {}, class: "input" %></div>
<div><%= f.label :role_id, t(".role"), class: "label" %><%= f.select :role_id, roles.map { [ _1.name, _1.id ] }, { selected: Role.for_key("pilot").id }, class: "input w-36" %></div>
<%= f.submit t(".add"), class: "btn-primary" %>
<% end %>
<% end %>
<p class="hint px-4 pb-4"><%= t(".roles_hint_html", link: link_to(t(".roles"), admin_roles_path, class: "link")) %></p>
</section>
+1 -1
View File
@@ -5,7 +5,7 @@
<tbody><% @users.each do |u| %><tr>
<td><%= u.name %> <% if u.admin? %><span class="badge-blue"><%= t(".admin") %></span><% end %><% if u.sso_uid? %> <span class="badge-gray" title="Signs in with <%= Sso.label %>"><%= Sso.label %></span><% end %><% if u.otp_enabled? %> <span class="badge-green" title="<%= t(".authenticator_app_on") %>"><%= t(".n2fa") %></span><% end %><% if u.locked? %> <span class="badge-red" title="<%= t(".too_many_failed_sign_ins") %>"><%= t(".locked") %></span><% end %></td>
<td><%= u.email_address %><% if u.certificates.any? %><div class="text-xs text-slate-500"><%= u.certificates.map { "#{_1.short_label} #{_1.number}" }.join(", ") %></div><% end %></td>
<td class="text-xs"><%= u.aircraft_roles.map { "#{_1.aircraft.tail_number} (#{_1.role})" }.join(", ").presence || "—" %></td>
<td class="text-xs"><%= u.aircraft_roles.map { "#{_1.aircraft.tail_number} (#{_1.role.name})" }.join(", ").presence || "—" %></td>
<td class="text-xs text-slate-500"><%= u.last_signed_in_at ? "#{time_ago_in_words(u.last_signed_in_at)} ago" : "never" %></td>
<td class="whitespace-nowrap text-right">
<% if u.disabled? %><span class="badge-red mr-1"><%= t(".disabled") %></span><%= button_to t(".enable"), enable_admin_user_path(u), method: :patch, class: "btn-secondary btn-sm", form: { class: "inline" } %>