models: extract password complexity to a concern
This commit is contained in:
parent
02bdfef893
commit
e97b7164dd
3 changed files with 29 additions and 3 deletions
23
app/models/concerns/password_complexity_concern.rb
Normal file
23
app/models/concerns/password_complexity_concern.rb
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
module PasswordComplexityConcern
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
# Allows adding a condition to the password complexity validation.
|
||||||
|
# Default is yes. Can be overridden in included classes.
|
||||||
|
def validate_password_complexity?
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
included do
|
||||||
|
# Add a validator for password complexity.
|
||||||
|
#
|
||||||
|
# The validator triggers as soon as the password is long enough (to avoid presenting
|
||||||
|
# two errors when the password is too short, one about length and one about complexity).
|
||||||
|
validates :password, password_complexity: true, if: -> { password_has_minimum_length? && validate_password_complexity? }
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def password_has_minimum_length?
|
||||||
|
self.class.password_length.include?(password.try(:size))
|
||||||
|
end
|
||||||
|
end
|
|
@ -25,11 +25,11 @@
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
#
|
#
|
||||||
class SuperAdmin < ApplicationRecord
|
class SuperAdmin < ApplicationRecord
|
||||||
|
include PasswordComplexityConcern
|
||||||
|
|
||||||
devise :rememberable, :trackable, :validatable, :lockable, :async, :recoverable,
|
devise :rememberable, :trackable, :validatable, :lockable, :async, :recoverable,
|
||||||
:two_factor_authenticatable, :otp_secret_encryption_key => Rails.application.secrets.otp_secret_key
|
:two_factor_authenticatable, :otp_secret_encryption_key => Rails.application.secrets.otp_secret_key
|
||||||
|
|
||||||
validates :password, password_complexity: true, if: -> (u) { Devise.password_length.include?(u.password.try(:size)) }
|
|
||||||
|
|
||||||
def enable_otp!
|
def enable_otp!
|
||||||
self.otp_secret = SuperAdmin.generate_otp_secret
|
self.otp_secret = SuperAdmin.generate_otp_secret
|
||||||
self.otp_required_for_login = true
|
self.otp_required_for_login = true
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#
|
#
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
include EmailSanitizableConcern
|
include EmailSanitizableConcern
|
||||||
|
include PasswordComplexityConcern
|
||||||
|
|
||||||
enum loged_in_with_france_connect: {
|
enum loged_in_with_france_connect: {
|
||||||
particulier: 'particulier',
|
particulier: 'particulier',
|
||||||
|
@ -57,7 +58,9 @@ class User < ApplicationRecord
|
||||||
|
|
||||||
before_validation -> { sanitize_email(:email) }
|
before_validation -> { sanitize_email(:email) }
|
||||||
|
|
||||||
validates :password, password_complexity: true, if: -> (u) { u.administrateur.present? && Devise.password_length.include?(u.password.try(:size)) }
|
def validate_password_complexity?
|
||||||
|
administrateur?
|
||||||
|
end
|
||||||
|
|
||||||
# Override of Devise::Models::Confirmable#send_confirmation_instructions
|
# Override of Devise::Models::Confirmable#send_confirmation_instructions
|
||||||
def send_confirmation_instructions
|
def send_confirmation_instructions
|
||||||
|
|
Loading…
Reference in a new issue