【问题标题】:Modify Devise confirmed? to include password.blank?修改设计确认?包括密码。空白?
【发布时间】:2012-11-23 17:46:25
【问题描述】:

我需要修改 Devise 决定用户帐户是否被确认的方式。

我设置了两步注册,用户输入电子邮件,然后在单击电子邮件中的链接后设置密码。

这很好用,但我有一个场景,登录用户可以创建一个用户并向他们发送一个链接以确认他们的帐户。这是通过自定义邮件手动发送的(这包括请求其帐户的用户的电子邮件地址以及确认令牌),这意味着如果他们的帐户是通过另一个用户创建的,则用户将收到 2 封电子邮件,标准设计确认指令和自定义邮件“邀请”。

接下来我设置了skip_confirmation!在保存之前,这意味着只有我发送的电子邮件才能发送给用户,但是由于这设置了 confimed_at 字段,因此用户无法设置密码,因为它会生成一个错误,提示帐户已确认。

如何更改已确认?方法也检查密码不为空?

这是我的两步注册过程的覆盖确认控制器:

class ConfirmationsController < Devise::ConfirmationsController
  def show
    self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token])
    super if resource.confirmed? 
  end

  def confirm
    self.resource = resource_class.find_by_confirmation_token(params[resource_name][:confirmation_token])
    if resource.update_attributes(params[resource_name].except(:confirmation_token)) && resource.password_match?
      self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
      set_flash_message :notice, :confirmed
      sign_in_and_redirect(resource_name, resource)
    else
      render :action => "show"
    end
  end
end

这是自定义邮件:

class MyMailer < Devise::Mailer   
  helper :application # gives access to all helpers defined within `application_helper`.


   def invite(sender, recipient)
        @sender = sender
        @recipient = recipient

        mail( :to => recipient.email,
              :subject => "Account created by #{sender.email}",
              :from => "noreply@noreply.com"
            )
  end

end

登录用户创建新用户的控制器中的代码(显示适用的部分)

.
.
.
            newContact = User.create(:email => params[:contact_email])
            newContact.skip_confirmation!
            if newContact.save
                 MyMailer.invite(current_user, newContact).deliver
                 .
                 .
                 .
             end
.
.
.

【问题讨论】:

    标签: ruby-on-rails devise devise-confirmable


    【解决方案1】:

    所以我破解了这个并设法让它工作,所以我想我会分享我是如何到达那里的。

    所以要求是:

    1) 用户从注册页面注册,发送确认邮件是标准的confirmation_instructions 设计邮件。

    2) 用户是由另一个用户创建的,发送的电子邮件是包含创建者用户名的自定义邮件给发件人。

    我对上述内容进行了以下更改以使其正常工作:

    向用户表添加列:invited:Integer

    在用户模型中添加了以下内容:

    protected
      def send_confirmation_instructions
        if !self.invited.nil?
          self.confirmation_token = nil if reconfirmation_required?
          @reconfirmation_required = false
          generate_confirmation_token! if self.confirmation_token.blank?
        else
          self.confirmation_token = nil if reconfirmation_required?
          @reconfirmation_required = false
          generate_confirmation_token! if self.confirmation_token.blank?
          self.devise_mailer.confirmation_instructions(self).deliver
        end
      end
    
      def send_on_create_confirmation_instructions
        self.devise_mailer.confirmation_instructions(self).deliver if self.invited.nil?
      end
    

    在由登录用户创建用户的控制器中更改为

    .
    .
    .
                newContact = User.create(:email => params[:contact_email], :invited => 1)
                if newContact.save
                     MyMailer.invite(current_user, newContact).deliver
                     .
                     .
                     .
                 end
    .
    .
    .
    

    还在 ConfirmationsController 中添加了以下内容,以处理用户再次单击确认链接的可能性。

      def show
        self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token])
        if self.resource.nil?
          redirect_to new_user_session_path, :notice => 'This link is no longer active, please sigin in or request new password'
        else
          super if resource.confirmed?
        end
      end
    

    我确信有更好的方法,但这可以满足我们的需要。如果有人想提出更好的方法,请务必成为我的客人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 2013-06-17
      相关资源
      最近更新 更多