Here we have a typical Devise user model. What’s new is the password complexity validation.
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me,
validate :password_complexity
def password_complexity
if password.present? and not password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d). /)
errors.add :password, "must include at least one lowercase letter, one uppercase letter, and one digit"
end
end
end
As for the regex, it uses positive look aheads to check for a lowercase letter, uppercase letter, and a digit. Each of those is required for validation.
If you'd like some more advanced security, you can use the Devise Security Extension which provides the above functionality and a whole lot more.
password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d). /) = > password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)./)
ReplyDeleteI like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting ruby on rails online training
ReplyDelete