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,74 @@
|
||||
# An invitation code: a link someone opens to create an account (or accept
|
||||
# with the one they have) and get roles on aircraft. Made by administrators
|
||||
# (Admin → Invitations, any aircraft) or by whoever manages an aircraft's
|
||||
# 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.
|
||||
class AccessInvitation < ApplicationRecord
|
||||
VALID_FOR = 14.days
|
||||
|
||||
belongs_to :created_by, class_name: "User", optional: true
|
||||
has_many :grants, class_name: "AccessInvitationRole", dependent: :delete_all
|
||||
has_many :redemptions, class_name: "AccessInvitationRedemption", dependent: :delete_all
|
||||
has_many :aircraft, through: :grants
|
||||
|
||||
attr_reader :token
|
||||
|
||||
normalizes :email, with: ->(email) { email.strip.downcase.presence }
|
||||
normalizes :label, :name, with: ->(text) { text.squish.presence }
|
||||
validates :label, presence: true, length: { maximum: 100 }
|
||||
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }, allow_nil: true
|
||||
validates :max_uses, numericality: { only_integer: true, greater_than: 0, less_than_or_equal_to: 1000 }, allow_nil: true
|
||||
before_validation(on: :create) do
|
||||
@token = SecureRandom.urlsafe_base64(24)
|
||||
self.token_digest = self.class.digest(@token)
|
||||
end
|
||||
|
||||
scope :recent_first, -> { order(created_at: :desc) }
|
||||
scope :usable, -> {
|
||||
where(revoked_at: nil).where("expires_at IS NULL OR expires_at > ?", Time.current).where("max_uses IS NULL OR uses_count < max_uses")
|
||||
}
|
||||
|
||||
# A new invitation by creator giving roles ([[aircraft, role], …]); its
|
||||
# link is emailed when it's for one address and email is set up.
|
||||
def self.create_for(creator, attributes, grants)
|
||||
invitation = new(attributes.merge(created_by: creator))
|
||||
grants.each { |aircraft, role| invitation.grants.build(aircraft: aircraft, role: role) }
|
||||
if invitation.save && invitation.email && MailSettings.configured?
|
||||
AccessInvitationMailer.invite(invitation, invitation.token).deliver_later
|
||||
end
|
||||
invitation
|
||||
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?
|
||||
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 redeemed_by?(user) = redemptions.exists?(user: user)
|
||||
|
||||
# Gives the person the invitation's roles, as if its creator added them
|
||||
# (AircraftAccess: so it can't give more than they could, then or now).
|
||||
# A role they already have on an aircraft stays as it is. Returns
|
||||
# [[aircraft, role or nil, message or nil], …].
|
||||
def redeem!(user)
|
||||
with_lock do
|
||||
raise ActiveRecord::RecordInvalid, self unless usable? && for?(user)
|
||||
redemptions.create!(user: user)
|
||||
increment!(:uses_count)
|
||||
grants.includes(:aircraft, :role).map do |grant|
|
||||
if (existing = grant.aircraft.roles.find_by(user: user))
|
||||
[ grant.aircraft, nil, I18n.t("models.access_invitation.already_has", role: existing.role.name) ]
|
||||
elsif created_by.nil? || created_by.disabled? || !created_by.can?(:manage_people, grant.aircraft)
|
||||
[ grant.aircraft, nil, I18n.t("models.access_invitation.creator_gone") ]
|
||||
else
|
||||
error = AircraftAccess.new(grant.aircraft, actor: created_by).add(user, grant.role)
|
||||
[ grant.aircraft, (grant.role unless error), error ]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user