diff --git a/README.md b/README.md index c5f9563..9af4028 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ To back up or move a single aircraft with all its files, use **More → Download | Reminders | A Monday email to those whose role gets reminders (Owners and Mechanics by default) when something is overdue or coming due, plus push alerts when something becomes overdue, and to anyone whose certificate (e.g. an IA) is about to expire; admins also get the records audit due date (needs SMTP; users can turn it off). | | Oil and fuel | Records, trend charts, and averages (last 20 intervals, IQR outlier filtering, manual exclusion). | | Activity log | Every write is recorded as an `AircraftEvent`; the Activity tab loads them as you scroll. | -| Access | Configurable roles per aircraft (see [Roles](#roles)), invitation links (from Admin → Invitations or an aircraft's Access page; limited uses, expiry, optionally one email address) that give an account and roles, plus share links (Current Status or Maintenance Detail, optional expiry, at most 10 per aircraft). Admins can disable accounts; a disabled account can't sign in or sign entries, and its past signatures stay. | +| Access | Configurable roles per aircraft (see [Roles](#roles)), admins' invitation links (limited uses, expiry, optionally one email address) that give an account and roles, plus share links (Current Status or Maintenance Detail, optional expiry, at most 10 per aircraft). Admins can disable accounts; a disabled account can't sign in or sign entries, and its past signatures stay. | | Sign-in security | Passwords of at least 12 characters, checked against known breaches (Have I Been Pwned, k-anonymity). 10 failed sign-ins in a row lock an account for 15 minutes from any address, and the user is emailed; admins can unlock. Optional authenticator-app (TOTP) codes with recovery codes for password and emailed-link sign-in and for signing entries; admins can reset them. Rate limits per address on every sign-in form. | | Phones and offline | Installable (PWA). Flights and squawks logged without signal, or with one too weak to answer within 10 seconds, are kept on the device and sent later, never twice. The Fleet, Overview, W&B, Log Flight and Report Squawk pages are saved ahead of time; any other page falls back to an offline page. The top bar turns red when the server can't be reached, and a ring shows saving and sending progress. | | REST API | Personal and integration tokens with scopes, read endpoints, squawks, draft logbook entries for signing in the app, and signed webhooks ([docs](docs/guide/api.md)). Follows the person's role. | @@ -202,7 +202,7 @@ Users who aren't on an aircraft get a 404 for it, identical to a missing record. Differences from the original: - Integer primary keys instead of UUIDs. Share tokens are 32-character random strings. - Logbook entries and inspection records don't keep many-to-many links to components. An inspection record or AD compliance can reference one component. -- Accounts are created only by an admin, by the first-run setup, by someone opening an admin's invitation link (with a password, or SSO), by an outside mechanic from an Owner's signing invitation (for its address), or at first SSO sign-in when `OIDC_AUTO_CREATE_USERS` is on. Invitations from an aircraft's Access page are for existing accounts. Who can add aircraft, and so become an Owner, is an admin setting (administrators only by default). +- Accounts are created only by an admin, by the first-run setup, by someone opening an admin's invitation link (with a password, or SSO), by an outside mechanic from an Owner's signing invitation (for its address), or at first SSO sign-in when `OIDC_AUTO_CREATE_USERS` is on. Who can add aircraft, and so become an Owner, is an admin setting (administrators only by default). ### Not yet ported diff --git a/app/controllers/aircraft_invitations_controller.rb b/app/controllers/aircraft_invitations_controller.rb deleted file mode 100644 index 028e18f..0000000 --- a/app/controllers/aircraft_invitations_controller.rb +++ /dev/null @@ -1,34 +0,0 @@ -# Invitation codes for one aircraft (AccessInvitation), from its Access page, -# by whoever manages its people: only the roles they can give (AircraftAccess). -class AircraftInvitationsController < ApplicationController - include AircraftScoped - requires_permission :manage_people - - def create - role = AircraftAccess.new(@aircraft, actor: current_user).assignable_roles.find { _1.id == params[:role_id].to_i } - return back(alert: t("services.aircraft_access.owner_only")) unless role - attributes = params.permit(:label, :email, :name, :max_uses, :expires_at).to_h.symbolize_keys - attributes[:label] = t("flash.aircraft_invitations.default_label", tail_number: @aircraft.tail_number) if attributes[:label].blank? - @invitation = AccessInvitation.create_for(current_user, attributes, [ [ @aircraft, role ] ]) - return back(alert: @invitation.errors.full_messages.to_sentence) unless @invitation.persisted? - log_event("role", t("flash.aircraft_invitations.created_event", role: role.name), notes: @invitation.label) - render :created - end - - # Only invitations for this aircraft alone; others are managed in Admin → Invitations. - def destroy - invitation = AccessInvitation.where(id: self.class.only_for(@aircraft)).find(params[:id]) - invitation.update!(revoked_at: Time.current) - log_event("role", t("flash.aircraft_invitations.revoked_event"), notes: invitation.label) - back(notice: t("flash.aircraft_invitations.revoked")) - end - - # Ids of the invitations that give a role on this aircraft and no other. - def self.only_for(aircraft) - AccessInvitationRole.group(:access_invitation_id).having("COUNT(*) = 1 AND MAX(aircraft_id) = ?", aircraft.id).select(:access_invitation_id) - end - - private - - def back(flash) = redirect_to(aircraft_aircraft_roles_path(@aircraft, anchor: "invitations"), flash) -end diff --git a/app/controllers/aircraft_roles_controller.rb b/app/controllers/aircraft_roles_controller.rb index 675d112..1eabc3b 100644 --- a/app/controllers/aircraft_roles_controller.rb +++ b/app/controllers/aircraft_roles_controller.rb @@ -11,8 +11,6 @@ class AircraftRolesController < ApplicationController @access = AircraftAccess.new(@aircraft, actor: current_user) @roles = @aircraft.roles.includes(:user, :role).joins(:role).order("roles.position", :created_at) @share_tokens = @aircraft.share_tokens.order(created_at: :desc).includes(:created_by) - @invitations = AccessInvitation.where(id: AircraftInvitationsController.only_for(@aircraft), revoked_at: nil) - .recent_first.includes(:created_by, grants: :role).limit(20) end def create diff --git a/app/views/aircraft_invitations/created.html.erb b/app/views/aircraft_invitations/created.html.erb deleted file mode 100644 index 540be74..0000000 --- a/app/views/aircraft_invitations/created.html.erb +++ /dev/null @@ -1,3 +0,0 @@ -<% 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") %> diff --git a/app/views/aircraft_roles/index.html.erb b/app/views/aircraft_roles/index.html.erb index fd51e03..8e663a8 100644 --- a/app/views/aircraft_roles/index.html.erb +++ b/app/views/aircraft_roles/index.html.erb @@ -39,37 +39,6 @@ - <%# Invitation codes for this aircraft (AircraftInvitationsController). %> -
-

<%= t(".invitations") %>

- <% if @invitations.any? %> - - <% 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| %> -
-
<%= f.label :email, t(".invite_email"), class: "label" %><%= f.email_field :email, class: "input", placeholder: t(".optional") %>
-
<%= 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" %>
-
<%= f.label :max_uses, t(".max_uses"), class: "label" %><%= f.number_field :max_uses, value: 1, min: 1, max: 1000, class: "input" %>
-
<%= 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" %>
-
-

<%= t(".invitation_hint") %>

- <%= f.submit t(".create_invitation"), class: "btn-primary" %> - <% end %> -
<% end %> <% if can?(:manage_sharing) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index fa37d6a..b8cf9bf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -887,11 +887,6 @@ en: account_exists: There's already an account for that email address. Sign in, then open the link again. welcome: Welcome! Your account is ready. accepted: Invitation accepted. - aircraft_invitations: - default_label: Invitation to %{tail_number} - created_event: Invitation made (%{role}) - revoked_event: Invitation revoked - revoked: Invitation revoked. admin_invitations: revoked: Invitation revoked. aircraft_roles: @@ -1248,7 +1243,7 @@ en: index: invitations: Invitations new_invitation: New invitation - intro: An invitation is a link someone opens to create an account, or accept with the one they have, and get roles on aircraft. New accounts come only from administrators and their invitations; an aircraft's Owner can invite people who already have an account from its Access page. + intro: An invitation is a link someone opens to create an account, or accept with the one they have, and get roles on aircraft. An aircraft's Owner adds people who already have an account from its Access page (by email), and invites outside mechanics to sign an entry. label: Invitation gives: Gives uses: Used @@ -1962,21 +1957,6 @@ en: role_for: Role for %{display_name} role: Role what_each_role_can_do: What each role can do - invitations: Invitations - expired: Expired - used: Used - uses_html: "%{used} of %{max} used" - until: until %{time} - never_expires: never expires - revoke: Revoke - revoke_invitation: Revoke this invitation? Its link stops working; people who already used it keep their access. - no_invitations: No open invitations. - invite_email: Email - optional: Optional - max_uses: Uses - expires: Expires - invitation_hint: A link that gives the role on this aircraft to whoever opens it and signs in. It's for people who already have an account (only the administrator makes new ones). With an email, it only works for that address (and is emailed to it when email is set up). Leave the expiry empty for never, or the uses empty for unlimited. - create_invitation: Create invitation roles_are_set_by_an_administrator: An administrator sets what each role can do, and can add roles (Admin → Roles). Only an Owner can give or take away the Owner role. share_links: Share Links expired: Expired diff --git a/config/routes.rb b/config/routes.rb index 936f933..db4f30c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -112,7 +112,6 @@ Rails.application.routes.draw do resource :loading_setup, path: "weight_balance/loading", only: %i[edit update] # before the records' :id routes resources :weight_balance_records, path: "weight_balance", except: :show resources :aircraft_roles, path: "access", only: %i[index create update destroy] - resources :invitations, controller: "aircraft_invitations", only: %i[create destroy] resources :share_tokens, only: %i[create destroy] resource :export, controller: "aircraft_exports", only: :show resource :report, controller: "maintenance_reports", only: :show diff --git a/docs/guide/administration.md b/docs/guide/administration.md index d33d176..f6ccbcd 100644 --- a/docs/guide/administration.md +++ b/docs/guide/administration.md @@ -28,7 +28,7 @@ To give someone a role on any aircraft, open them in **Admin → Users**: **Airc ## Invitations -**Admin → Invitations → New invitation** makes a link someone opens to create their own account (or accept with the one they have) and get roles on aircraft. New accounts are made by an administrator (here, or from **Users**), by an administrator's invitation, or by an outside mechanic from an Owner's signing invitation (**Ask someone to sign**). Invitations from an aircraft's Access page are for people who already have an account. Since Owners can bring in mechanics, **Settings → Who can add aircraft** decides who can become one. An invitation has a label for you, optionally an email address (then only that address can use it, and the link is emailed to it when email is set up) and a name, how many times it works (empty for unlimited, e.g. for a flying club), when it expires (empty for never), and a role on any of the aircraft (none at all gives just an account). The link is shown once, when it's made; copy it then. +**Admin → Invitations → New invitation** makes a link someone opens to create their own account (or accept with the one they have) and get roles on aircraft. New accounts are made by an administrator (here, or from **Users**), by an administrator's invitation, or by an outside mechanic from an Owner's signing invitation (**Ask someone to sign**). Since Owners can bring in mechanics, **Settings → Who can add aircraft** decides who can become one. An invitation has a label for you, optionally an email address (then only that address can use it, and the link is emailed to it when email is set up) and a name, how many times it works (empty for unlimited, e.g. for a flying club), when it expires (empty for never), and a role on any of the aircraft (none at all gives just an account). The link is shown once, when it's made; copy it then. Where someone already has a role on an aircraft, an invitation leaves it as it is. An invitation never gives more than whoever made it can give when it's used. **Revoke** stops a link working; people who already used it keep their access. With single sign-on, **Continue with** on the invitation page signs them in and makes their account. `OIDC_AUTO_CREATE_USERS=true` in the server's settings instead gives an account to anyone the provider lets in, without an invitation; leave it off to keep accounts to administrators and their invitations. diff --git a/docs/guide/sharing.md b/docs/guide/sharing.md index b206fa7..076a053 100644 --- a/docs/guide/sharing.md +++ b/docs/guide/sharing.md @@ -8,9 +8,9 @@ Add people by email with a role: **Owner**, **Mechanic**, **Pilot**, **Viewer**, Only an Owner can give or take away the Owner role, nobody can change their own, and an aircraft always keeps at least one Owner. An administrator can also give people roles on any aircraft from **Admin → Users**. -## Invitations +## Someone without an account -To bring in someone who has an account, without knowing their email, use **Invitations** on the same page (new accounts are made only by the administrator or an administrator's invitation, so ask the administrator for someone who has no account yet): choose the role, optionally their email (then only they can use it, and it's emailed to them when email is set up), how many times it works and when it expires, then **Create invitation**. Copy the link shown and send it; it isn't shown again. Whoever opens it creates an account, or signs in, and gets the role on this aircraft. **Revoke** stops a link that hasn't been used. Administrators can also make invitations for several aircraft at once (Admin → Invitations). +For someone who doesn't have an account yet, ask the administrator: they add the account, or send an invitation link that makes it and gives the role (Admin → Invitations). An outside mechanic who only needs to sign an entry can create their own account from your signing invitation (**Ask someone to sign** on the entry; see [Electronic records and signing](records.md)). ## Share links diff --git a/test/integration/invitations_test.rb b/test/integration/invitations_test.rb index ffd8a14..73df4ef 100644 --- a/test/integration/invitations_test.rb +++ b/test/integration/invitations_test.rb @@ -112,37 +112,25 @@ class InvitationsTest < ActionDispatch::IntegrationTest assert_equal 0, inv.reload.uses_count end - test "an aircraft's manager invites to it from its Access page, emailed, with only roles they can give" do - sign_in @owner + test "an administrator's invitation for one address is emailed to it" do + sign_in @admin assert_emails 1 do perform_enqueued_jobs do - post aircraft_invitations_path(@a), params: { email: "Mech@Example.com", role_id: Role.for_key("mechanic").id, max_uses: 1, expires_at: "" } + post admin_invitations_path, params: { access_invitation: { label: "Mechanic", email: "Mech@Example.com", max_uses: 1 }, roles: { @a.id => Role.for_key("mechanic").id } } end end - assert_response :success link = join_link_in(response.body) - assert_includes ActionMailer::Base.deliveries.last.text_part.body.to_s, link - assert_equal [ "mech@example.com" ], ActionMailer::Base.deliveries.last.to - inv = AccessInvitation.last - assert_equal "Invitation to N12345", inv.label + mail = ActionMailer::Base.deliveries.last + assert_equal [ "mech@example.com" ], mail.to + assert_includes mail.text_part.body.to_s, link + end + + test "an aircraft's Access page adds people by email, without invitation links" do + sign_in @owner get aircraft_aircraft_roles_path(@a) - assert_select "#invitations li", 1 - assert_select "#invitations select[name=role_id] option", text: "Owner" - - partner_role = Role.create!(name: "Partner", permissions: %w[manage_people]) - partner = create_user("partner@example.com") - @a.roles.create!(user: partner, role: partner_role) - sign_in partner - post aircraft_invitations_path(@a), params: { role_id: Role.owner.id, max_uses: 1 } - assert_equal 1, AccessInvitation.count - delete aircraft_invitation_path(@a, inv) - assert inv.reload.revoked? - - viewer = create_user("viewer@example.com") - @a.roles.create!(user: viewer, role: "viewer") - sign_in viewer - post aircraft_invitations_path(@a), params: { role_id: Role.for_key("viewer").id, max_uses: 1 } - assert_equal 1, AccessInvitation.count + assert_select "form[action=?]", aircraft_aircraft_roles_path(@a) + assert_select "#invitations", 0 + assert_not_includes response.body, "Create invitation" end test "an invitation can't give more than its creator can when it's used" do @@ -159,15 +147,7 @@ class InvitationsTest < ActionDispatch::IntegrationTest assert_match "N12345:", flash[:notice] end - test "an invitation of someone else's aircraft can't be revoked from another aircraft's page" do - inv = invitation(grants: [ [ @b, "pilot" ] ]) - sign_in @owner - delete aircraft_invitation_path(@a, inv) - assert_response :not_found - assert_not inv.reload.revoked? - end - - test "only an administrator's invitation makes new accounts; an owner's is for existing accounts" do + test "only an administrator's invitation makes new accounts; others' are for existing accounts" do owners = invitation(creator: @owner, grants: [ [ @a, "pilot" ] ]) get join_path(owners.token) assert_select "form[action=?]", account_join_path(owners.token), 0