Files
vault171/app/models/user.rb
David Böhm b7f0c35378
Some checks failed
CI / scan_ruby (push) Has been cancelled
CI / scan_js (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / system-test (push) Has been cancelled
Added some models
User
Item
Department
Categorie
AssignmentLog
Room
2026-05-21 15:36:23 +02:00

65 lines
1.7 KiB
Ruby

class User < ApplicationRecord
@@min_length_password = 12
has_secure_password
generates_token_for :email_verification, expires_in: 2.days do
email
end
generates_token_for :password_reset, expires_in: 20.minutes do
password_salt.last(10)
end
has_many :sessions, dependent: :destroy
has_many :events, dependent: :destroy
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, allow_nil: true, length: { minimum: @@min_length_password }
belongs_to :department, optional: true
has_many :items, dependent: :nullify
has_many :assignment_logs, dependent: :destroy
validates :first_name, :last_name, presence: true, on: :update
normalizes :email, with: -> { _1.strip.downcase }
before_validation if: :email_changed?, on: :update do
self.verified = false
end
after_update if: :password_digest_previously_changed? do
sessions.where.not(id: Current.session).delete_all
end
after_update if: :email_previously_changed? do
events.create! action: "email_verification_requested"
end
after_update if: :password_digest_previously_changed? do
events.create! action: "password_changed"
end
after_update if: [ :verified_previously_changed?, :verified? ] do
events.create! action: "email_verified"
end
# Gibt den vollen Namen zurück
def name
if first_name.present? && last_name.present?
"#{first_name} #{last_name}"
else
email
end
end
# Für das Besitzer-Auswahlfeld im Formular
def name_with_department
department.present? ? "#{name} (#{department.name})" : name
end
def self.min_length_password
@@min_length_password
end
end