class User
An application account. Accounts come from the IdP via OIDC or are created locally by seeding or invites; roles and suspension are managed in-app.
Constants
- ROLES
- STATUSES
Public Class Methods
Source
# File app/models/user.rb, line 90 def self.from_omniauth(auth) user = find_by(provider: auth.provider, uid: auth.uid) return user if user email = auth.info.email return nil if email.blank? # Only link a pre-existing local account by email when the IdP verified the # email. An unverified identity claiming someone else's address must not # silently take over an existing account. existing = find_by(email: email) if existing && !omniauth_email_verified?(auth) Rails.logger.warn("from_omniauth: refusing to link #{email} to account #{existing.id} " \ "— IdP did not verify the email") return nil end user = existing || User.new(email: email) user.provider = auth.provider user.uid = auth.uid user.password = Devise.friendly_token(32) if user.new_record? user.role ||= :viewer # First SSO login converts an invited account into a live one. user.status = "active" if user.invited? user.save! user end
Finds or creates the account from what the IdP returned. Role NEVER comes from the IdP by default: promoting to admin is something done locally. New accounts start as viewer.
# File app/models/user.rb, line 126 def self.omniauth_email_verified?(auth) info = auth.info || {} raw = auth.dig("extra", "raw_info") || {} [ info["verified"], info["email_verified"], raw["email_verified"] ] .any? { |v| v == true || v == "true" } end
Whether the IdP asserted the email address is verified. Accepts the common conventions: info.verified, info.email_verified, or raw_info.email_verified (booleans or “true”/“false” strings). When the IdP sends no verification flag we assume NOT verified — linking an existing account by email is only safe on an explicit assertion.
@param auth [OmniAuth::AuthHash] the IdP response @return [Boolean]
Public Instance Methods
Source
# File app/models/user.rb, line 33 def active? = status == "active"
Whether the user’s account is active. @return [Boolean]
# File app/models/user.rb, line 49 def active_for_authentication? super && active? end
Suspended accounts cannot sign in, even via the IdP. Without this, an OIDC session would keep working after an admin suspends the user.
# File app/models/user.rb, line 72 def change_role!(new_role, changed_by:, reason: nil) new_role = new_role.to_s return self if role == new_role old_role = role update!(role: new_role) role_change_logs.create!( changed_by: changed_by, from_role: self.class.roles.fetch(old_role), to_role: self.class.roles.fetch(new_role), reason: reason ) self end
Promote / demote a user. Every change is recorded in role_change_logs.
Source
# File app/models/user.rb, line 56 def inactive_message suspended? ? :suspended : super end
Message shown when an inactive user tries to sign in; suspended accounts get a dedicated message. @return [Symbol, String]
Source
# File app/models/user.rb, line 36 def invited? = status == "invited"
Whether the user was invited but has not signed in yet. @return [Boolean]
Source
# File app/models/user.rb, line 67 def reactivate!(by: nil) update!(status: "active", suspended_at: nil, suspended_by_id: nil) end
Reactivates the user’s account and clears suspension metadata. @param by [User, nil] the admin performing the reactivation
Source
# File app/models/user.rb, line 43 def sso? provider.present? end
Whether the user signed in through the SSO provider. @return [Boolean]
# File app/models/user.rb, line 61 def suspend!(by: nil, reason: nil) update!(status: "suspended", suspended_at: Time.current, suspended_by_id: by&.id) end
Suspend / reactivate with an audit trail.
Source
# File app/models/user.rb, line 39 def suspended? = status == "suspended"
Whether the user’s account is suspended. @return [Boolean]