在Activerecord Model上自定义创建的SecurePassword虚拟属性验证失败

时间:2014-02-04 09:46:34

标签: ruby-on-rails activerecord omniauth activemodel

我正在尝试通过omniauth-facebook gem混合基于SecurePassword的自定义用户身份验证机制和Facebook集成。

我的应用使用Ruby 2.0.0和Rails 4.0.0。

我尝试按照本指南omniauth和其他一些文章为用户和身份验证模型提出类似的内容

用户模型:

class User < ActiveRecord::Base

has_one :user_playlist
has_one :user_info

has_many :band_likes
has_many :song_likes

has_many :band_comments
has_many :song_comments

has_many :authorizations

#many to many relation between User and Band
#todo: make a bands_users migration
has_and_belongs_to_many :bands


has_secure_password

validates :username, presence: true, uniqueness: {case_sensitive: false}, length: {in: 8..64}, format: {with: /\A[a-zA-Z ]+\Z/, message: 'Debe poseer solo letras y espacios.'}
validates :email, presence: true, uniqueness: {case_sensitive: false}, format: {with: /@/, message: 'Dirección de correo inváilda.'}
validates :password, length: {in: 8..24}
validates :password_confirmation, length: {in: 8..24}

def self.create_from_hash!(hash)
        create(:email => hash['info']['email'], :username => hash['info']['name'], :password => hash['uid'], :password_confirmation => hash['uid'] )
end
end

授权模型:

class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider

def self.find_from_hash(hash)
  find_by_provider_and_uid(hash['provider'], hash['uid'])
end

def self.create_from_hash(hash, user = nil)
  user ||= User.create_from_hash!(hash)
  Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider'])
end
end

SessionsController

class SessionsController < ApplicationController

def create
  auth = request.env['omniauth.auth']
  unless @auth = Authorization.find_from_hash(auth)
    # Create a new user or add an auth to existing user, depending on
    # whether there is already a user signed in.
    @auth = Authorization.create_from_hash(auth, current_user)
  end
  # Log the authorizing user in.
  self.current_user = @auth.user

  render :text => "Welcome, #{current_user.username}. <br />User saved = #{current_user.save} .<br/>User valid = #{current_user.valid?}.<br />errors= #{current_user.errors.full_messages}"
end

end

编写最后一个渲染是为了检查我的密码是否未经过验证这一事实,如果我使用hash ['uid'],hash ['info'] ['name']或其他什么都没关系

我使用这个值的原因只是因为,我稍后会弄清楚如何为oauth-ed用户构建随机密码,但我不想要空白密码,也不要禁用验证。

但是,无论我使用什么价值,总是只得到我的名字和电子邮件:

*Welcome, "My Real Name Here.
User saved = false.
User valid = false.
errors= ["Password is too short (minimum is 8 characters)", "Password confirmation is too short (minimum is 8 characters)"]*

在Rails控制台中创建用户时没有问题,就在OAuth尝试使用create_from_hash创建用户时。

另外,如果我尝试将哈希中的非现有值分配给密码字段,则会添加可以为空的消息。所以,它不是空白。

并在控制器中渲染hash ['uid']表明它长于8。

我必须警告我是铁杆的新手,所以如果可以,请用苹果xD解释我

提前致谢!

1 个答案:

答案 0 :(得分:0)

最后我在用户模型上想出了这个:

def self.create_from_hash!(hash)
   self.where(:email => hash.info.email.to_s).first_or_create do |user|
      user.email = hash.info.email
      user.username = hash.info.name
      user.password = hash.uid
      user.password_confirmation = hash.uid
   end
end

我不知道为什么后者不起作用,但至少这个有效!

问候!