Files
MatchLiveTv/backend/app/models/user.rb
Emiliano Frascaro 4083bc5dee Club come entità principale, branding e fix infrastruttura streaming.
Introduce clubs con abbonamento, branding Active Storage e flusso registrazione società; corregge healthcheck MediaMTX (immagine distroless) e allinea dominio produzione matchlivetv.it.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-27 09:16:24 +02:00

59 lines
1.6 KiB
Ruby

class User < ApplicationRecord
ROLES = %w[admin coach parent volunteer].freeze
has_secure_password
has_many :user_teams, dependent: :destroy
has_many :teams, through: :user_teams
has_many :club_memberships, dependent: :destroy
has_many :clubs, through: :club_memberships
has_many :owned_clubs, -> { where(club_memberships: { role: "owner" }) }, through: :club_memberships, source: :club
has_many :stream_sessions, dependent: :nullify
def manageable_teams
staff_ids = teams.select(:id)
owner_ids = Team.where(club_id: owned_clubs.select(:id)).select(:id)
Team.where(id: staff_ids).or(Team.where(id: owner_ids))
end
def primary_club
owned_clubs.order(:created_at).first
end
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :name, presence: true
validates :role, inclusion: { in: ROLES }
def admin?
role == "admin"
end
def generate_password_reset!
raw = SecureRandom.urlsafe_base64(32)
update!(
password_reset_digest: User.digest_token(raw),
password_reset_sent_at: Time.current
)
raw
end
def password_reset_expired?
password_reset_sent_at.blank? ||
password_reset_sent_at < MatchLiveTv.password_reset_expiry_hours.hours.ago
end
def clear_password_reset!
update!(password_reset_digest: nil, password_reset_sent_at: nil)
end
def self.find_by_password_reset_token(token)
return if token.blank?
find_by(password_reset_digest: User.digest_token(token))
end
def self.digest_token(token)
Digest::SHA256.hexdigest(token.to_s)
end
end