Rails Devise 的 reset_password 如何绕过 password,password_confirmation 之外的其它属性验证?

nanzhang · August 22, 2012 · Last by NanZhang replied at August 22, 2012 · 5762 hits

目前项目中使用的是 Devise 中默认的忘记密码的功能,但由于项目前期需求的变更,导致数据库中的数据不规则。 Devise 重置密码时会对 Model 中的 validate 进行验证,现在没办法通过,有什么方法解决没?

validates_presence_of :username, :email, :if => :email_required?, :on => :create
validates_presence_of :email, :on => :update
validates_presence_of :password, :if => :email_required?, :on => :create
validates_presence_of :is_read, :if => :email_required?, :on => :create
validates_presence_of :phone, :message => "联系电话不能为空!", :if => :not_from_minisite?
validates_presence_of :role_id
validates_presence_of :area_id

以上是部分验证

试一下 user.update_with_password 方法

@stc

# PUT /resource/password
  def update
    self.resource = resource_class.reset_password_by_token(params[resource_name])

    if resource.errors.empty?
      flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
      set_flash_message(:notice, flash_message) if is_navigational_format?
      sign_in(resource_name, resource)
      respond_with resource, :location => after_sign_in_path_for(resource)
    else
      respond_with resource
    end
  end

这个是默认的方法,那 user.update_with_password 我只有去重写这个方法?

非常恶心的办法是 自己在 controller 里 构造这些必须的字段。

@chucai ORZ...实在没办法 就重写上面的update方法

#2 楼 @NanZhang 可以去重写的,devise 的 wiki 也说了它只是给出一个结构,我们不应该被束缚。呵呵

@chucai @stc采用Devise的默认方法很好,不去修改了,自己改的不伦不类也就失去了安全的初衷,在 model 中添加一些过滤条件去过滤些特殊的用户。THX! :D

不知道下面这样行不行? user.update_attrubutes(:password=>'xxx') user.save(:validate => false) #非常危险,自己校验其他问题。

@jimrokliu
user.update_attrubutes 这个是 model 中的属性要验证 user.update_attrubute 这个不需要验证 user.save(:validate => false) 这个不好,password 和 password_confirmation 也都绕过了。 如果自己再去校验太麻烦

You need to Sign in before reply, if you don't have an account, please Sign up first.