mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
New accounts only from an administrator or an administrator's invitation
Anyone can add an aircraft and so manage its people, so invitations from non-administrators could bring in new accounts without an administrator. - An invitation makes a new account only if whoever made it is still an administrator when it's used; an aircraft's Access page invitations are for people who already have an account (its welcome page says so). - Signing invitations no longer make accounts: the mechanic needs one first, and the owner is told when they don't have one. - With SSO, "Continue with <provider>" on an administrator's invitation makes the account and accepts the invitation (only for its address when it has one), so invitations work without OIDC_AUTO_CREATE_USERS. That setting stays, documented as giving accounts without an invitation; the Authentik example now leaves it off. - Docs: who makes accounts (guide, README, Authentik, .env.example, API). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b4c7aaa3ff
commit
19bc3efa19
19 files changed
+159
-85
No files matched your search
@@ -1,11 +1,11 @@
|
||||
# The invited signer's side: sign in (or create an account for the invited
|
||||
# address), add a certificate if needed, review the entry and sign it. The
|
||||
# invitation gives access to that one entry only.
|
||||
# The invited signer's side: sign in with their account for the invited
|
||||
# address, add a certificate if needed, review the entry and sign it. The
|
||||
# invitation gives access to that one entry only. It doesn't make accounts:
|
||||
# only an administrator (or an administrator's invitation) does.
|
||||
class InvitedSigningsController < ApplicationController
|
||||
include EntrySigning
|
||||
allow_unauthenticated_access only: %i[show account]
|
||||
allow_unauthenticated_access only: :show
|
||||
before_action :set_invitation
|
||||
rate_limit to: 10, within: 15.minutes, only: :account, with: -> { redirect_to new_session_path, alert: t("flash.invited_signings.try_again_later") }
|
||||
|
||||
def show
|
||||
@entry = @invitation.logbook_entry
|
||||
@@ -20,24 +20,6 @@ class InvitedSigningsController < ApplicationController
|
||||
render :new
|
||||
end
|
||||
|
||||
# Creating an account for the invited email address.
|
||||
def account
|
||||
return redirect_to(invited_signing_path(params[:token])) if authenticated?
|
||||
if User.exists?(email_address: @invitation.email) || !Sso.password_login?
|
||||
return redirect_to invited_signing_path(params[:token]), alert: t("flash.invited_signings.sign_in_to_continue")
|
||||
end
|
||||
user = User.new(params.permit(:name, :password, :password_confirmation).merge(email_address: @invitation.email))
|
||||
if user.save
|
||||
reset_session
|
||||
start_new_session_for user
|
||||
redirect_to invited_signing_path(params[:token]), notice: t("flash.invited_signings.account_created_add_your")
|
||||
else
|
||||
@entry = @invitation.logbook_entry
|
||||
@account_errors = user.errors.full_messages
|
||||
render :welcome, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
@entry = @invitation.logbook_entry
|
||||
@aircraft = @entry.aircraft
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
# Opening an invitation link (AccessInvitation): see what it gives, then
|
||||
# create an account (when sign-in by password is on) or sign in, and accept.
|
||||
# An invitation for one email address only works for that address. An
|
||||
# invalid link (unknown, expired, used up, revoked) gets one answer for all.
|
||||
# Opening an invitation link (AccessInvitation): see what it gives, then sign
|
||||
# in and accept. Only an administrator's invitation can make a new account
|
||||
# (with a password here, or by signing in with SSO: SsoController); others
|
||||
# need an existing one. An invitation for one email address only works for
|
||||
# that address. Any invalid link (unknown, expired, used up, revoked) gets
|
||||
# one answer.
|
||||
class JoinsController < ApplicationController
|
||||
allow_unauthenticated_access only: %i[show account]
|
||||
before_action :set_invitation
|
||||
@@ -11,6 +13,7 @@ class JoinsController < ApplicationController
|
||||
def show
|
||||
unless current_user
|
||||
session[:return_to_after_authenticating] = request.url
|
||||
session[:join_token] = params[:token] # for an account made by signing in with SSO
|
||||
return render(:welcome)
|
||||
end
|
||||
return redirect_to(root_path, notice: t("flash.joins.already_accepted")) if @invitation.redeemed_by?(current_user)
|
||||
@@ -21,7 +24,9 @@ class JoinsController < ApplicationController
|
||||
# A new account, then the invitation accepted with it.
|
||||
def account
|
||||
return redirect_to(join_path(params[:token])) if current_user
|
||||
return redirect_to(join_path(params[:token]), alert: t("flash.joins.sign_in_to_continue")) unless Sso.password_login?
|
||||
unless Sso.password_login? && @invitation.creates_accounts?
|
||||
return redirect_to(join_path(params[:token]), alert: t("flash.joins.sign_in_to_continue"))
|
||||
end
|
||||
email = @invitation.email || params[:email_address].to_s.strip.downcase
|
||||
if User.exists?(email_address: email)
|
||||
return redirect_to join_path(params[:token]), alert: t("flash.joins.account_exists")
|
||||
@@ -35,7 +40,7 @@ class JoinsController < ApplicationController
|
||||
if results
|
||||
reset_session
|
||||
start_new_session_for user
|
||||
redirect_to root_path, notice: summary(results, t("flash.joins.welcome"))
|
||||
redirect_to root_path, notice: [ t("flash.joins.welcome"), AccessInvitation.describe(results) ].join(" ").strip
|
||||
else
|
||||
@account_errors = user.errors.full_messages
|
||||
render :welcome, status: :unprocessable_entity
|
||||
@@ -47,7 +52,7 @@ class JoinsController < ApplicationController
|
||||
def accept
|
||||
return redirect_to(root_path, notice: t("flash.joins.already_accepted")) if @invitation.redeemed_by?(current_user)
|
||||
return render(:wrong_account) unless @invitation.for?(current_user)
|
||||
redirect_to root_path, notice: summary(@invitation.redeem!(current_user), t("flash.joins.accepted"))
|
||||
redirect_to root_path, notice: [ t("flash.joins.accepted"), AccessInvitation.describe(@invitation.redeem!(current_user)) ].join(" ").strip
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
render :invalid, layout: "public", status: :not_found
|
||||
end
|
||||
@@ -59,13 +64,5 @@ class JoinsController < ApplicationController
|
||||
render :invalid, layout: "public", status: :not_found unless @invitation&.usable?
|
||||
end
|
||||
|
||||
# "Invitation accepted. N12345: Mechanic. N54321: already Owner, unchanged."
|
||||
def summary(results, lead)
|
||||
parts = results.map do |aircraft, role, message|
|
||||
"#{aircraft.tail_number}: #{[ role&.name, message ].compact.join(', ')}"
|
||||
end
|
||||
[ lead, *parts ].join(" ")
|
||||
end
|
||||
|
||||
def current_user = Current.user || (resume_session && Current.user)
|
||||
end
|
||||
@@ -22,7 +22,9 @@ class SigningInvitationsController < ApplicationController
|
||||
tail: @aircraft.tail_number, name: current_user.display_name)
|
||||
end
|
||||
log_event("logbook", t("flash.signing_invitations.signature_requested_from_entry", email: @invitation.email, date: l(@entry.date, format: :app)))
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry), notice: t("flash.signing_invitations.invitation_sent_to_the", email: @invitation.email, days: SigningInvitation::VALID_FOR.in_days.to_i)
|
||||
notice = t("flash.signing_invitations.invitation_sent_to_the", email: @invitation.email, days: SigningInvitation::VALID_FOR.in_days.to_i)
|
||||
notice = "#{notice} #{t('flash.signing_invitations.no_account_yet')}" unless invitee
|
||||
redirect_to aircraft_logbook_entry_path(@aircraft, @entry), notice: notice
|
||||
else
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
# email is verified (or OIDC_TRUST_EMAIL is on), since some providers let
|
||||
# users edit their email. Otherwise, sign in with a password once and link
|
||||
# from the profile page.
|
||||
#
|
||||
# Someone without an account gets one only from an administrator's
|
||||
# invitation they opened first (AccessInvitation, accepted at once), or for
|
||||
# anyone the provider lets in when OIDC_AUTO_CREATE_USERS is on.
|
||||
class SsoController < ApplicationController
|
||||
allow_unauthenticated_access only: %i[callback failure]
|
||||
before_action :require_sso
|
||||
@@ -29,6 +33,7 @@ class SsoController < ApplicationController
|
||||
return_to = session[:return_to_after_authenticating]
|
||||
reset_session # fresh session id after signing in
|
||||
start_new_session_for user
|
||||
return redirect_to(root_url, notice: [ t("flash.joins.welcome"), AccessInvitation.describe(@joined) ].join(" ").strip) if @joined
|
||||
redirect_to return_to || root_url
|
||||
end
|
||||
|
||||
@@ -81,15 +86,27 @@ class SsoController < ApplicationController
|
||||
end
|
||||
|
||||
def create_user(auth)
|
||||
return unless Sso.auto_create_users?
|
||||
email = auth.info&.email.to_s.strip.downcase
|
||||
return if email.blank? || User.exists?(email_address: email)
|
||||
# The random password is never shown; a password reset can set a real one.
|
||||
User.create!(email_address: email, name: auth.info&.name.presence, sso_uid: auth.uid, password: SecureRandom.base58(40))
|
||||
invitation = invitation_making_account(email)
|
||||
return unless invitation || Sso.auto_create_users?
|
||||
User.transaction do
|
||||
# The random password is never shown; a password reset can set a real one.
|
||||
user = User.create!(email_address: email, name: auth.info&.name.presence, sso_uid: auth.uid, password: SecureRandom.base58(40))
|
||||
@joined = invitation&.redeem!(user)
|
||||
user
|
||||
end
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
nil
|
||||
end
|
||||
|
||||
# The invitation opened in this browser before signing in, when it can
|
||||
# make an account (an administrator's) for this email address.
|
||||
def invitation_making_account(email)
|
||||
invitation = AccessInvitation.find_by_token(session[:join_token])
|
||||
invitation if invitation&.usable? && invitation.creates_accounts? && invitation.for_email?(email)
|
||||
end
|
||||
|
||||
def email_verified?(auth)
|
||||
[ true, "true" ].include?(raw_info(auth)["email_verified"])
|
||||
end
|
||||
|
||||
@@ -4,6 +4,10 @@
|
||||
# people (its Access page, that aircraft). Limited by number of uses, an
|
||||
# expiry, and optionally to one email address; it can be revoked. Only a
|
||||
# digest of the code is kept, so the link is shown once, when it's made.
|
||||
#
|
||||
# New accounts are only ever made by an administrator or from an
|
||||
# administrator's invitation: anyone can add an aircraft and so manage its
|
||||
# people, so other invitations are for people who already have an account.
|
||||
class AccessInvitation < ApplicationRecord
|
||||
VALID_FOR = 14.days
|
||||
|
||||
@@ -40,14 +44,23 @@ class AccessInvitation < ApplicationRecord
|
||||
invitation
|
||||
end
|
||||
|
||||
# "N12345: Mechanic. N54321: already Owner, unchanged" (from #redeem!).
|
||||
def self.describe(results)
|
||||
results.map { |aircraft, role, message| "#{aircraft.tail_number}: #{[ role&.name, message ].compact.join(', ')}" }.join(" ")
|
||||
end
|
||||
|
||||
def self.digest(token) = OpenSSL::Digest::SHA256.hexdigest(token.to_s)
|
||||
def self.find_by_token(token) = token.present? ? find_by(token_digest: digest(token)) : nil
|
||||
|
||||
def usable? = !revoked? && !expired? && !used_up?
|
||||
# Whether someone without an account can make one with it (see above),
|
||||
# decided when it's used: its creator must still be an administrator.
|
||||
def creates_accounts? = created_by.present? && created_by.admin? && !created_by.disabled?
|
||||
def for_email?(email) = self.email.nil? || self.email == email.to_s.strip.downcase
|
||||
def revoked? = revoked_at.present?
|
||||
def expired? = expires_at.present? && expires_at <= Time.current
|
||||
def used_up? = max_uses.present? && uses_count >= max_uses
|
||||
def for?(user) = email.nil? || email == user.email_address
|
||||
def for?(user) = for_email?(user.email_address)
|
||||
def redeemed_by?(user) = redemptions.exists?(user: user)
|
||||
|
||||
# Gives the person the invitation's roles, as if its creator added them
|
||||
|
||||
@@ -5,15 +5,7 @@
|
||||
<div class="card card-body space-y-3">
|
||||
<%= link_to t(".i_have_an_account_sign"), new_session_path, class: "btn-primary w-full justify-center" %>
|
||||
</div>
|
||||
<% if Sso.password_login? && !User.exists?(email_address: @invitation.email) %>
|
||||
<%= form_with url: account_invited_signing_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"><%= @account_errors.to_sentence %></div><% end %>
|
||||
<div><label class="label"><%= t(".email") %></label><input class="input" value="<%= @invitation.email %>" disabled></div>
|
||||
<div><%= label_tag :name, t(".your_name"), class: "label" %><%= text_field_tag :name, params[:name], class: "input", required: true %></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(".at_least_characters_not_one_html", 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" %>
|
||||
<% end %>
|
||||
<% unless User.exists?(email_address: @invitation.email) %>
|
||||
<p class="text-sm text-slate-600"><%= t(".no_account_yet", email: @invitation.email) %></p>
|
||||
<% end %>
|
||||
</div>
|
||||
@@ -6,10 +6,15 @@
|
||||
<%= 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 Sso.enabled? && @invitation.creates_accounts? %>
|
||||
<%= button_to t(".continue_with", provider: Sso.label), "/auth/#{Sso::PROVIDER}", class: "btn-primary w-full justify-center", data: { turbo: false } %>
|
||||
<p class="hint"><%= t(".sso_hint", provider: Sso.label) %></p>
|
||||
<% end %>
|
||||
<%= link_to t(".i_have_an_account"), new_session_path, class: [ "w-full justify-center", (Sso.enabled? && @invitation.creates_accounts? ? "btn-secondary" : "btn-primary") ] %>
|
||||
<% if @invitation.email %><p class="hint"><%= t(".for_email", email: @invitation.email) %></p><% end %>
|
||||
<% unless @invitation.creates_accounts? %><p class="hint"><%= t(".existing_accounts_only") %></p><% end %>
|
||||
</div>
|
||||
<% if Sso.password_login? && !(@invitation.email && User.exists?(email_address: @invitation.email)) %>
|
||||
<% if @invitation.creates_accounts? && 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 %>
|
||||
|
||||
Reference in new issue
Block a user