Module: Devise::Models::SecureValidatable

Defined in:
lib/devise_security_extension/models/secure_validatable.rb

Overview

SecureValidatable creates better validations with more validation for security

Options

SecureValidatable adds the following options to devise_for:

* +email_regexp+: the regular expression used to validate e-mails;
* +password_length+: a range expressing password length. Defaults from devise
* +password_regex+: need strong password. Defaults to /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.assert_secure_validations_api!(base) ⇒ Object

:nodoc:



35
36
37
# File 'lib/devise_security_extension/models/secure_validatable.rb', line 35

def self.assert_secure_validations_api!(base) #:nodoc:
  raise "Could not use SecureValidatable on #{base}" unless base.respond_to?(:validates)
end

.included(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/devise_security_extension/models/secure_validatable.rb', line 14

def self.included(base)
  base.extend ClassMethods
  assert_secure_validations_api!(base)

  base.class_eval do

    # uniq login
    validates authentication_keys[0], :uniqueness => {:scope => authentication_keys[1..-1], :case_sensitive => (case_insensitive_keys != false)}, :if => :email_changed?

    # validates email
    validates :email, :presence => true, :if => :email_required?
    validates :email, :email => email_validation if email_validation # use rails_email_validator or similar
    
    # validates password
    validates :password, :presence => true, :length => password_length, :format => password_regex, :confirmation => true, :if => :password_required?

    # don't allow use same password
    validate :current_equal_password_validation
  end
end

Instance Method Details

#current_equal_password_validationObject



39
40
41
42
43
44
45
46
# File 'lib/devise_security_extension/models/secure_validatable.rb', line 39

def current_equal_password_validation
  if not self.new_record? and not self.encrypted_password_change.nil?
    dummy = self.class.new
    dummy.encrypted_password = self.encrypted_password_change.first
    dummy.password_salt = self.password_salt_change.first if self.respond_to? :password_salt_change and not self.password_salt_change.nil?
    self.errors.add(:password, :equal_to_current_password) if dummy.valid_password?(self.password)
  end
end