mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
Invitation codes: a link that gives an account and roles
Ported from the original (InvitationCode), adapted to configurable roles. - Admin → Invitations: a label, optionally one email address (only it can use the link, and it's emailed) and a name, how many uses (or unlimited), an expiry (or never), and a role on any aircraft (or none, for an account only). - An aircraft's Access page: whoever manages its people invites to that aircraft, with only the roles they can give; they see and revoke its invitations. - /join/<code>: shows what it gives; create an account (when password sign-in is on) or sign in, then accept. Existing accounts and SSO users accept too. A role someone already has on an aircraft stays. - Roles are given as the invitation's creator (AircraftAccess), and only while they can still manage that aircraft's people. Codes are kept as digests and shown once; uses are counted under a lock; account creation and accepting are rate-limited; any invalid link gets the same answer. - Docs: guide (Administration, Access & sharing), README, and why /join isn't on the Authentik bypass list. The README's not-yet-ported list now names Prometheus metrics, and no longer claims oil analysis import as new. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a645778f26
commit
b4c7aaa3ff
34 files changed
+822
-7
No files matched your search
@@ -0,0 +1,4 @@
|
||||
<p><%= t(".invites_you_html", who: (@invitation.created_by&.display_name || t(".an_administrator"))) %></p>
|
||||
<% if @grants.any? %><ul><% @grants.each do |g| %><li><strong><%= g.aircraft.tail_number %></strong>: <%= g.role.name %></li><% end %></ul><% end %>
|
||||
<p><%= link_to t(".accept_the_invitation"), @url %></p>
|
||||
<% if @invitation.expires_at %><p style="color:#64748b;font-size:12px"><%= t(".expires_html", date: fmt_date(@invitation.expires_at.to_date)) %></p><% end %>
|
||||
@@ -0,0 +1,11 @@
|
||||
<%= raw t(".invites_you_text", who: (@invitation.created_by&.display_name || t(".an_administrator"))) %>
|
||||
<% @grants.each do |g| %>
|
||||
- <%= g.aircraft.tail_number %>: <%= g.role.name %>
|
||||
<% end %>
|
||||
|
||||
<%= raw t(".open_the_link_text") %>
|
||||
<%= @url %>
|
||||
<% if @invitation.expires_at %>
|
||||
|
||||
<%= raw t(".expires_text", date: fmt_date(@invitation.expires_at.to_date)) %>
|
||||
<% end %>
|
||||
@@ -0,0 +1,2 @@
|
||||
<% content_for :title, t("shared.invitation_created.title") %>
|
||||
<%= render "shared/invitation_created", invitation: @invitation, back_path: admin_invitations_path %>
|
||||
@@ -0,0 +1,28 @@
|
||||
<% content_for :title, t(".invitations") %>
|
||||
<div class="mb-2 flex items-center justify-between"><h1 class="text-2xl font-bold"><%= t(".invitations") %></h1><%= link_to t(".new_invitation"), new_admin_invitation_path, class: "btn-primary" %></div>
|
||||
<p class="mb-4 text-sm text-slate-600"><%= t(".intro") %></p>
|
||||
<section class="card">
|
||||
<% if @invitations.any? %>
|
||||
<table class="table">
|
||||
<thead><tr><th><%= t(".label") %></th><th><%= t(".gives") %></th><th><%= t(".uses") %></th><th><%= t(".status") %></th><th></th></tr></thead>
|
||||
<tbody>
|
||||
<% @invitations.each do |inv| %>
|
||||
<tr class="invitation-row">
|
||||
<td><span class="font-medium"><%= inv.label %></span><div class="text-xs text-slate-500"><%= [ inv.email, inv.created_by&.display_name, fmt_date(inv.created_at) ].compact.join(" · ") %></div></td>
|
||||
<td class="text-xs" data-label="<%= t(".gives") %>"><%= inv.grants.map { "#{_1.aircraft.tail_number} (#{_1.role.name})" }.join(", ").presence || t(".account_only") %></td>
|
||||
<td class="tabular-nums" data-label="<%= t(".uses") %>"><%= inv.uses_count %> / <%= inv.max_uses || "∞" %></td>
|
||||
<td data-label="<%= t(".status") %>">
|
||||
<% if inv.revoked? %><span class="badge-gray"><%= t(".revoked") %></span>
|
||||
<% elsif inv.expired? %><span class="badge-red"><%= t(".expired") %></span>
|
||||
<% elsif inv.used_up? %><span class="badge-gray"><%= t(".used") %></span>
|
||||
<% else %><span class="badge-green"><%= t(".active") %></span><span class="block text-xs text-slate-500"><%= inv.expires_at ? t(".until", time: l(inv.expires_at, format: :app)) : t(".never_expires") %></span><% end %>
|
||||
</td>
|
||||
<td class="text-right"><% if inv.usable? %><%= delete_button t(".revoke"), admin_invitation_path(inv), confirm: t(".revoke_invitation") %><% end %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
</table>
|
||||
<% else %>
|
||||
<%= empty_state t(".no_invitations") %>
|
||||
<% end %>
|
||||
</section>
|
||||
@@ -0,0 +1,29 @@
|
||||
<% content_for :title, t(".new_invitation") %>
|
||||
<h1 class="mb-4 text-2xl font-bold"><%= t(".new_invitation") %></h1>
|
||||
<%= form_with model: @invitation, url: admin_invitations_path, method: :post, 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 :label, required: true, hint: t(".label_hint") %>
|
||||
<%= f.field :email, as: :email, label: t(".email"), hint: t(".email_hint") %>
|
||||
<%= f.field :name, label: t(".name"), hint: t(".name_hint") %>
|
||||
<%= f.field :max_uses, as: :number, label: t(".max_uses"), hint: t(".max_uses_hint"), min: 1, max: 1000 %>
|
||||
<%= f.field :expires_at, as: :datetime, label: t(".expires"), hint: t(".expires_hint") %>
|
||||
</div>
|
||||
<fieldset>
|
||||
<legend class="mb-2 text-sm font-semibold"><%= t(".roles") %></legend>
|
||||
<% aircraft = Aircraft.ordered.to_a %>
|
||||
<% if aircraft.any? %>
|
||||
<div class="grid gap-2 sm:grid-cols-2">
|
||||
<% roles = Role.ordered.map { [ _1.name, _1.id ] } %>
|
||||
<% aircraft.each do |a| %>
|
||||
<label class="flex items-center justify-between gap-3 text-sm">
|
||||
<span><span class="font-medium"><%= a.tail_number %></span> <span class="text-xs text-slate-500"><%= [ a.make, a.model ].compact_blank.join(" ") %></span></span>
|
||||
<%= select_tag "roles[#{a.id}]", options_for_select([ [ t(".no_access"), "" ] ] + roles, params.dig(:roles, a.id.to_s)), class: "input w-36" %>
|
||||
</label>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
<p class="hint mt-2"><%= t(".roles_hint") %></p>
|
||||
</fieldset>
|
||||
<div class="flex flex-wrap gap-2"><%= f.submit t(".create") %><%= link_to t(".cancel"), admin_invitations_path, class: "btn-secondary" %></div>
|
||||
<% end %>
|
||||
@@ -0,0 +1,3 @@
|
||||
<% content_for :title, t("shared.invitation_created.title") %>
|
||||
<%= render "shared/aircraft_header", aircraft: @aircraft %>
|
||||
<%= render "shared/invitation_created", invitation: @invitation, back_path: aircraft_aircraft_roles_path(@aircraft, anchor: "invitations") %>
|
||||
@@ -38,6 +38,38 @@
|
||||
<p class="hint mt-2"><%= t(".roles_are_set_by_an_administrator") %></p>
|
||||
</details>
|
||||
</section>
|
||||
|
||||
<%# Invitation codes for this aircraft (AircraftInvitationsController). %>
|
||||
<section class="card" id="invitations">
|
||||
<div class="card-header"><h2 class="card-title"><%= t(".invitations") %></h2></div>
|
||||
<% if @invitations.any? %>
|
||||
<ul class="divide-y divide-slate-100">
|
||||
<% @invitations.each do |inv| %>
|
||||
<li class="flex flex-wrap items-center justify-between gap-2 px-4 py-3 text-sm">
|
||||
<div>
|
||||
<span class="font-medium"><%= inv.label %></span>
|
||||
<span class="badge-blue"><%= inv.grants.first&.role&.name %></span>
|
||||
<% if inv.expired? %><span class="badge-red"><%= t(".expired") %></span><% elsif inv.used_up? %><span class="badge-gray"><%= t(".used") %></span><% end %>
|
||||
<div class="text-xs text-slate-500"><%= [ inv.email, t(".uses_html", used: inv.uses_count, max: inv.max_uses || "∞"), (inv.expires_at ? t(".until", time: l(inv.expires_at, format: :app)) : t(".never_expires")), inv.created_by&.display_name ].compact.join(" · ") %></div>
|
||||
</div>
|
||||
<% if inv.usable? %><%= delete_button t(".revoke"), aircraft_invitation_path(@aircraft, inv), confirm: t(".revoke_invitation") %><% end %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% else %>
|
||||
<%= empty_state t(".no_invitations") %>
|
||||
<% end %>
|
||||
<%= form_with url: aircraft_invitations_path(@aircraft), class: "space-y-3 border-t border-slate-100 p-4" do |f| %>
|
||||
<div class="grid gap-3 sm:grid-cols-2">
|
||||
<div><%= f.label :email, t(".invite_email"), class: "label" %><%= f.email_field :email, class: "input", placeholder: t(".optional") %></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" %></div>
|
||||
<div><%= f.label :max_uses, t(".max_uses"), class: "label" %><%= f.number_field :max_uses, value: 1, min: 1, max: 1000, class: "input" %></div>
|
||||
<div><%= f.label :expires_at, t(".expires"), class: "label" %><%= f.datetime_field :expires_at, value: AccessInvitation::VALID_FOR.from_now.strftime("%Y-%m-%dT%H:%M"), class: "input" %></div>
|
||||
</div>
|
||||
<p class="hint"><%= t(".invitation_hint") %></p>
|
||||
<%= f.submit t(".create_invitation"), class: "btn-primary" %>
|
||||
<% end %>
|
||||
</section>
|
||||
<% end %>
|
||||
|
||||
<% if can?(:manage_sharing) %>
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<%# What the invitation gives: a role on each of its aircraft (or just an account). %>
|
||||
<% grants = invitation.grants.includes(:aircraft, :role).sort_by { _1.aircraft.tail_number } %>
|
||||
<% if grants.any? %>
|
||||
<ul class="space-y-1 text-sm">
|
||||
<% grants.each do |g| %>
|
||||
<li class="flex items-baseline gap-2"><span class="font-semibold"><%= g.aircraft.tail_number %></span> <span class="text-slate-500"><%= [ g.aircraft.make, g.aircraft.model ].compact_blank.join(" ") %></span> <span class="badge-blue" title="<%= g.role.description %>"><%= g.role.name %></span></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% else %>
|
||||
<p class="text-sm text-slate-600"><%= t("joins.grants.account_only") %></p>
|
||||
<% end %>
|
||||
@@ -0,0 +1,11 @@
|
||||
<% content_for :title, t(".invitation") %>
|
||||
<div class="mx-auto max-w-md space-y-4">
|
||||
<h1 class="text-2xl font-bold"><%= t(".youre_invited") %></h1>
|
||||
<div class="card card-body space-y-3">
|
||||
<p class="text-sm text-slate-600"><%= t(".invited_by_html", who: (@invitation.created_by&.display_name || t(".an_administrator")), label: @invitation.label) %></p>
|
||||
<%= render "joins/grants", invitation: @invitation %>
|
||||
<p class="hint"><%= t(".existing_roles_stay") %></p>
|
||||
<%= button_to t(".accept"), accept_join_path(params[:token]), class: "btn-primary w-full justify-center", form: { class: "pt-1" } %>
|
||||
</div>
|
||||
<p class="text-center text-sm text-slate-500"><%= t(".signed_in_as", email: current_user.email_address) %></p>
|
||||
</div>
|
||||
@@ -0,0 +1,5 @@
|
||||
<% content_for :title, t(".invitation") %>
|
||||
<div class="mx-auto max-w-md card card-body">
|
||||
<h1 class="card-title"><%= t(".not_valid") %></h1>
|
||||
<p class="mt-1 text-sm text-slate-600"><%= t(".ask_for_a_new_one") %></p>
|
||||
</div>
|
||||
@@ -0,0 +1,27 @@
|
||||
<% content_for :title, t(".invitation") %>
|
||||
<div class="mx-auto max-w-md space-y-4">
|
||||
<h1 class="text-2xl font-bold"><%= t(".youre_invited") %></h1>
|
||||
<div class="card card-body space-y-3">
|
||||
<p class="text-sm text-slate-600"><%= t(".invited_by_html", who: (@invitation.created_by&.display_name || t(".an_administrator")), label: @invitation.label) %></p>
|
||||
<%= render "joins/grants", invitation: @invitation %>
|
||||
</div>
|
||||
<div class="card card-body space-y-3">
|
||||
<%= link_to t(".i_have_an_account"), new_session_path, class: "btn-primary w-full justify-center" %>
|
||||
<% if @invitation.email %><p class="hint"><%= t(".for_email", email: @invitation.email) %></p><% end %>
|
||||
</div>
|
||||
<% if Sso.password_login? && !(@invitation.email && User.exists?(email_address: @invitation.email)) %>
|
||||
<%= form_with url: account_join_path(params[:token]), class: "card card-body space-y-3" do %>
|
||||
<h2 class="card-title"><%= t(".or_create_an_account") %></h2>
|
||||
<% if @account_errors.present? %><div class="rounded-md bg-red-50 p-2 text-sm text-red-700" role="alert"><%= @account_errors.to_sentence %></div><% end %>
|
||||
<% if @invitation.email %>
|
||||
<div><label class="label" for="invited_email"><%= t(".email") %></label><input id="invited_email" class="input" value="<%= @invitation.email %>" disabled></div>
|
||||
<% else %>
|
||||
<div><%= label_tag :email_address, t(".email"), class: "label" %><%= email_field_tag :email_address, params[:email_address], class: "input", required: true, autocomplete: "username" %></div>
|
||||
<% end %>
|
||||
<div><%= label_tag :name, t(".your_name"), class: "label" %><%= text_field_tag :name, params[:name] || @invitation.name, class: "input", required: true, autocomplete: "name" %></div>
|
||||
<div><%= label_tag :password, t(".password"), class: "label" %><%= password_field_tag :password, nil, class: "input", required: true, autocomplete: "new-password", minlength: User::PASSWORD_MIN, maxlength: 72 %><p class="hint"><%= t(".password_hint", min: User::PASSWORD_MIN) %></p></div>
|
||||
<div><%= label_tag :password_confirmation, t(".confirm_password"), class: "label" %><%= password_field_tag :password_confirmation, nil, class: "input", required: true, autocomplete: "new-password" %></div>
|
||||
<%= submit_tag t(".create_account"), class: "btn-secondary", data: { turbo_submits_with: t(".creating") } %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
@@ -0,0 +1,6 @@
|
||||
<% content_for :title, t(".invitation") %>
|
||||
<div class="mx-auto max-w-md card card-body space-y-3">
|
||||
<h1 class="card-title"><%= t(".wrong_account") %></h1>
|
||||
<p class="text-sm text-slate-600"><%= t(".for_another_email_html", email: @invitation.email, email_address: current_user.email_address) %></p>
|
||||
<%= button_to t(".sign_out"), session_path, method: :delete, class: "btn-secondary" %>
|
||||
</div>
|
||||
@@ -50,7 +50,7 @@
|
||||
<%= link_to t(".help"), help_path, class: "block px-4 py-2.5 hover:bg-slate-100" %>
|
||||
<% if current_user.admin? %>
|
||||
<p class="border-t border-slate-100 px-4 pt-2 pb-1 text-xs font-semibold text-slate-500"><%= t(".admin") %></p>
|
||||
<% [ [ t(".users"), admin_users_path ], [ t(".roles"), admin_roles_path ], [ t(".integrations"), admin_integrations_path ], [ t(".component_types"), admin_component_types_path ], [ t(".airports"), admin_airports_path ], [ t(".logbook_templates"), admin_logbook_templates_path ], [ t(".settings"), edit_admin_settings_path ], [ t(".off_site_backup"), admin_offsite_backup_path ], [ t(".recordkeeping_procedures"), admin_procedures_path ], [ t(".records_audits"), admin_audits_path ], [ t(".about"), admin_about_path ] ].each do |label, path| %>
|
||||
<% [ [ t(".users"), admin_users_path ], [ t(".roles"), admin_roles_path ], [ t(".invitations"), admin_invitations_path ], [ t(".integrations"), admin_integrations_path ], [ t(".component_types"), admin_component_types_path ], [ t(".airports"), admin_airports_path ], [ t(".logbook_templates"), admin_logbook_templates_path ], [ t(".settings"), edit_admin_settings_path ], [ t(".off_site_backup"), admin_offsite_backup_path ], [ t(".recordkeeping_procedures"), admin_procedures_path ], [ t(".records_audits"), admin_audits_path ], [ t(".about"), admin_about_path ] ].each do |label, path| %>
|
||||
<%= link_to label, path, class: "block px-4 py-2.5 hover:bg-slate-100" %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
@@ -71,7 +71,7 @@
|
||||
<%= t(".admin") %> <span aria-hidden="true" class="text-xs">▾</span>
|
||||
</summary>
|
||||
<div class="absolute right-0 z-30 mt-2 w-48 rounded-md bg-surface py-1 text-slate-700 shadow-lg ring-1 ring-slate-200">
|
||||
<% [ [ t(".users"), admin_users_path ], [ t(".roles"), admin_roles_path ], [ t(".integrations"), admin_integrations_path ], [ t(".component_types"), admin_component_types_path ], [ t(".airports"), admin_airports_path ], [ t(".logbook_templates"), admin_logbook_templates_path ], [ t(".settings"), edit_admin_settings_path ], [ t(".off_site_backup"), admin_offsite_backup_path ], [ t(".recordkeeping_procedures"), admin_procedures_path ], [ t(".records_audits"), admin_audits_path ], [ t(".about"), admin_about_path ] ].each do |label, path| %>
|
||||
<% [ [ t(".users"), admin_users_path ], [ t(".roles"), admin_roles_path ], [ t(".invitations"), admin_invitations_path ], [ t(".integrations"), admin_integrations_path ], [ t(".component_types"), admin_component_types_path ], [ t(".airports"), admin_airports_path ], [ t(".logbook_templates"), admin_logbook_templates_path ], [ t(".settings"), edit_admin_settings_path ], [ t(".off_site_backup"), admin_offsite_backup_path ], [ t(".recordkeeping_procedures"), admin_procedures_path ], [ t(".records_audits"), admin_audits_path ], [ t(".about"), admin_about_path ] ].each do |label, path| %>
|
||||
<%= link_to label, path, class: [ "block px-4 py-2 text-sm hover:bg-slate-100", ("font-semibold text-sky-700" if current_page?(path)) ] %>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<%# A just-made invitation's link: shown this once (only a digest is kept). %>
|
||||
<section class="card card-body max-w-xl space-y-3">
|
||||
<h1 class="card-title"><%= t("shared.invitation_created.title") %></h1>
|
||||
<p class="text-sm text-slate-600"><%= t("shared.invitation_created.copy_now") %></p>
|
||||
<input type="text" class="input font-mono text-xs" value="<%= join_url(invitation.token) %>" readonly data-select-on-click aria-label="<%= t("shared.invitation_created.link") %>">
|
||||
<%= render "joins/grants", invitation: invitation %>
|
||||
<p class="hint">
|
||||
<%= [ (t("shared.invitation_created.emailed", email: invitation.email) if invitation.email && MailSettings.configured?),
|
||||
(t("shared.invitation_created.only_for", email: invitation.email) if invitation.email),
|
||||
(invitation.max_uses ? t("shared.invitation_created.uses", count: invitation.max_uses) : t("shared.invitation_created.unlimited")),
|
||||
(invitation.expires_at ? t("shared.invitation_created.expires", time: l(invitation.expires_at, format: :app)) : t("shared.invitation_created.never_expires")) ].compact.join(" ") %>
|
||||
</p>
|
||||
<p><%= link_to t("shared.invitation_created.done"), back_path, class: "btn-primary" %></p>
|
||||
</section>
|
||||
Reference in new issue
Block a user