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
|
||||
|
||||
Reference in new issue
Block a user