mirror of
https://github.com/discourse/discourse.git
synced 2025-03-21 15:45:32 +08:00
Move password validation into PasswordValidator
This commit is contained in:
parent
4f7d440fa4
commit
33c6997ded
@ -246,11 +246,23 @@ class User < ActiveRecord::Base
|
||||
@raw_password = password unless password.blank?
|
||||
end
|
||||
|
||||
def password
|
||||
'' # so that validator doesn't complain that a password attribute doesn't exist
|
||||
end
|
||||
|
||||
# Indicate that this is NOT a passwordless account for the purposes of validation
|
||||
def password_required!
|
||||
@password_required = true
|
||||
end
|
||||
|
||||
def password_required?
|
||||
!!@password_required
|
||||
end
|
||||
|
||||
def password_validator
|
||||
PasswordValidator.new(attributes: :password).validate_each(self, :password, @raw_password)
|
||||
end
|
||||
|
||||
def confirm_password?(password)
|
||||
return false unless password_hash && salt
|
||||
self.password_hash == hash_password(password, salt)
|
||||
@ -561,12 +573,6 @@ class User < ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
|
||||
def password_validator
|
||||
if (@raw_password && @raw_password.length < 6) || (@password_required && !@raw_password)
|
||||
errors.add(:password, "must be 6 letters or longer")
|
||||
end
|
||||
end
|
||||
|
||||
def send_approval_email
|
||||
Jobs.enqueue(:user_email,
|
||||
type: :signup_after_approval,
|
||||
|
12
lib/validators/password_validator.rb
Normal file
12
lib/validators/password_validator.rb
Normal file
@ -0,0 +1,12 @@
|
||||
class PasswordValidator < ActiveModel::EachValidator
|
||||
|
||||
def validate_each(record, attribute, value)
|
||||
return unless record.password_required?
|
||||
if value.nil?
|
||||
record.errors.add(attribute, :blank)
|
||||
elsif value.length < 6
|
||||
record.errors.add(attribute, :too_short, count: 6)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
46
spec/components/validators/password_validator_spec.rb
Normal file
46
spec/components/validators/password_validator_spec.rb
Normal file
@ -0,0 +1,46 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe PasswordValidator do
|
||||
|
||||
let(:validator) { described_class.new({attributes: :password}) }
|
||||
subject(:validate) { validator.validate_each(record,:password,@password) }
|
||||
|
||||
context "password required" do
|
||||
let(:record) { u = Fabricate.build(:user, password: @password); u.password_required!; u }
|
||||
|
||||
it "doesn't add an error when password is good" do
|
||||
@password = "weron235alsfn234"
|
||||
validate
|
||||
record.errors[:password].should_not be_present
|
||||
end
|
||||
|
||||
it "adds an error when password is too short" do
|
||||
@password = "p"
|
||||
validate
|
||||
record.errors[:password].should be_present
|
||||
end
|
||||
|
||||
it "adds an error when password is blank" do
|
||||
@password = ''
|
||||
validate
|
||||
record.errors[:password].should be_present
|
||||
end
|
||||
|
||||
it "adds an error when password is nil" do
|
||||
@password = nil
|
||||
validate
|
||||
record.errors[:password].should be_present
|
||||
end
|
||||
end
|
||||
|
||||
context "password not required" do
|
||||
let(:record) { Fabricate.build(:user, password: @password) }
|
||||
|
||||
it "doesn't add an error if password is not required" do
|
||||
@password = nil
|
||||
validate
|
||||
record.errors[:password].should_not be_present
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user