Files
MatchLiveTv/backend/app/models/user.rb
Emiliano Frascaro 471291b2c4 Sito marketing, SEO, legal, live programmata e branding Match Live Tv.
Logo e favicon nel header, partite programmate su web e mobile, reset password,
pagine FAQ/sitemap, privacy/termini GDPR e icona Android «Match Live Tv».

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-26 23:17:15 +02:00

46 lines
1.1 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 :stream_sessions, dependent: :nullify
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