Added authentication_zero

This commit is contained in:
2024-08-26 19:20:06 +02:00
parent 70606f6890
commit e23b41b950
48 changed files with 1047 additions and 73 deletions

6
app/models/current.rb Normal file
View File

@@ -0,0 +1,6 @@
class Current < ActiveSupport::CurrentAttributes
attribute :session
attribute :user_agent, :ip_address
delegate :user, to: :session, allow_nil: true
end

View File

@@ -17,7 +17,9 @@ class Job < ApplicationRecord
before_save :set_cost_qm
before_save :calc_cost, if: :printed_pages_changes?
# TODO: works only when job is created. Should move analyzer to activestorage :https://discuss.rubyonrails.org/t/active-storage-in-production-lessons-learned-and-in-depth-look-at-how-it-works/83289
# TODO: works only when job is created. Should move analyzer to activestorage :
# https://discuss.rubyonrails.org/t/active-storage-in-production-lessons-learned-and-in-depth-look-at-how-it-works/83289
# https://redgreen.no/2021/01/24/custom-analyzer-for-activestorage.html
after_create_commit :analyze_pdf
# NOTE: Multiple status if paing before brinting?

8
app/models/session.rb Normal file
View File

@@ -0,0 +1,8 @@
class Session < ApplicationRecord
belongs_to :user
before_create do
self.user_agent = Current.user_agent
self.ip_address = Current.ip_address
end
end

View File

@@ -1,4 +1,35 @@
class User < ApplicationRecord
has_secure_password
has_many :jobs_as_costumer, foreign_key: :costumer_id, class_name: "Job"
has_many :jobs_as_operator, foreign_key: :operator_id, class_name: "Job"
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
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, allow_nil: true, length: { minimum: 12 }
validates :password, not_pwned: { message: "might easily be guessed" }
normalizes :email, with: -> { _1.strip.downcase }
enum :role, {
user: 0,
operator: 1,
admin: 2
}
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
end