0
간단한 응용 프로그램을 작성 중이며 레일 콘솔에서 사용자 암호를 업데이트하는 데 어려움이 있습니다. 나는 RoR (그리고 Ruby)에 아주 익숙하다. 내가 잃어버린 뭔가 간단하다고 확신합니다. 내 모델뿐만 아니라 터미널에서 얻은 출력도 포함 시켰습니다.레일 콘솔에서 업데이트 된 암호 레코드를 저장하는 데 문제가 있습니다.
admin_users.rb 모델
require 'digest/sha1'
class AdminUser < ActiveRecord::Base
attr_accessor :password
EMAIL_REGEX = /^[A-Z0-9._%+-][email protected][A-Z0-9.-]\.[A-Z]{2,4}$/i
def self.authenticate(username="", password="")
user = AdminUser.find_by_username(username)
if user && user.password_match?(password)
return user
else
return false
end
end
def password_match?(password="")
hashed_password == AdminUser.hash_with_salt(password, salt)
end
validates :first_name, :presence => true, :length => { :maximum => 25 }
validates :last_name, :presence => true, :length => { :maximum => 50 }
validates :username, :presence => true, :length => { :within => 8..25 }, :uniqueness => true
validates :email, :presence => true, :length => { :maximum => 100 }, :uniqueness => true, :format=> EMAIL_REGEX, :confirmation => true
# only on create ,so other attributes of this user can be changed
validates_length_of :password, :within => 8..25, :on => :create
before_save :create_hashed_password
after_save :clear_password
# scope :named, lambda {|first, last| where (:first_name => first, :last_name => last)}
attr_protected :hashed_password, :salt
def self.make_salt(username="")
Digest::SHA1.hexdigest("Use #{username} with #{Time.now} to make salt")
end
def self.hash_with_salt(password="", salt="")
Digest::SHA1.hexdigest("Put #{salt} on #{password}")
end
private
def create_hashed_password
# Whenever :password has a value, hashing is needed
unless password.blank?
#always use "self" when assigning values
self.salt = AdminUser.make_salt(username) if salt.blank?
self.hashed_password = AdminUser.hash_with_salt(password, salt)
end
end
def clear_password
# for security reasons and because hasing is not needed
self.password = nil
end
end
터미널 -
user = AdminUser.find(1)
user.password = ('secretpassword')
user.save
다음 터미널이 날이 있습니다 ...
(0.1ms) BEGIN
AdminUser Exists (0.4ms) SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`username` = BINARY 'benlinus' AND `admin_users`.`id` != 1) LIMIT 1
AdminUser Exists (0.2ms) SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`email` = BINARY '[email protected]' AND `admin_users`.`id` != 1) LIMIT 1
(0.1ms) ROLLBACK
=> false
2.0.0-p195 :018 > user.save
(0.1ms) BEGIN
AdminUser Exists (0.3ms) SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`username` = BINARY 'benlinus' AND `admin_users`.`id` != 1) LIMIT 1
AdminUser Exists (0.2ms) SELECT 1 AS one FROM `admin_users` WHERE (`admin_users`.`email` = BINARY '[email protected]' AND `admin_users`.`id` != 1) LIMIT 1
(0.1ms)
내가 어디 잘 모르겠어요 전화 끊기입니다. 유효성 검사에서 오류가 발생하는 것으로 보이지 않으므로 조금 혼란스러워합니다.
도착 방법. "AdminUser Exists"응답을 제공하지 않지만 여전히 롤백하고 false를 반환합니다. 생각? –
이해가 안됩니다. 귀하의 터미널 결과에 쿼리의 시작 부분에 "AdminUser Exists"가 표시됩니다. user.save를 실행 한 후에 user.errors.full_messages를 인쇄 해보고 오류가 무엇인지 확인하십시오. – usha
하. 대단히 감사합니다. 나의 첫 번째 코멘트 "Getting there ..."에 관해 나는 처음에 제안한 것을 시도했다. 그런 다음 제안한대로 모든 오류 메시지를 인쇄하면 전자 메일의 유효성을 확인할 수 없습니다. :-) 아, 학습의 즐거움. 도와 주셔서 대단히 감사합니다! –