【问题标题】:devise_invitable: Confirm after Invitationdevise_invitable:邀请后确认
【发布时间】:2011-05-18 23:57:13
【问题描述】:

我覆盖了设计的确认!向我的用户发送欢迎消息的方法:

class User < ActiveRecord::Base

  devise :invitable, :database_authenticatable, :registerable, :recoverable, 
         :rememberable, :confirmable, :validatable, :encryptable

  # ...

  # Devise confirm! method overriden
  def confirm!
    UserMailer.welcome_alert(self).deliver
    super
  end

end

使用devise_invitable,当用户接受邀请并设置密码时,confirm!方法永远不会触发,是否可以强制? devise_invitable 如何确认用户?

或者也许我可以以同样的方式覆盖 accept_invite(或任何它所调用的)方法?

我希望受邀用户保持未确认状态,然后在接受邀请后确认。

谢谢,非常感谢任何帮助!

Original Source

更新

查看devise_invitable model我发现了可能导致这种不当行为的两种方法:

  # Accept an invitation by clearing invitation token and confirming it if model
  # is confirmable
  def accept_invitation!
    if self.invited? && self.valid?
      self.invitation_token = nil
      self.save
    end
  end

  # Reset invitation token and send invitation again
  def invite!
    if new_record? || invited?
      @skip_password = true
      self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
      generate_invitation_token if self.invitation_token.nil?
      self.invitation_sent_at = Time.now.utc
      if save(:validate => self.class.validate_on_invite)
        self.invited_by.decrement_invitation_limit! if self.invited_by
        !!deliver_invitation unless @skip_invitation
      end
    end
  end

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-3 devise actionmailer invite


【解决方案1】:
class User < ActiveRecord::Base
  devise :invitable, :database_authenticatable, :registerable, :recoverable, 
         :rememberable, :confirmable, :validatable, :encryptable

  # ...

  # devise confirm! method overriden
  def confirm!
    welcome_message
    super
  end

  # devise_invitable accept_invitation! method overriden
  def accept_invitation!
    self.confirm!
    super
  end

  # devise_invitable invite! method overriden
  def invite!
    super
    self.confirmed_at = nil
    self.save
  end

private

  def welcome_message
    UserMailer.welcome_message(self).deliver
  end

end

【讨论】:

  • 太好了,谢谢!关于验证,这只是一个小小的评论。 accept_invitation! 可能不会产生有效的模型,例如如果受邀用户接受邀请时未满足密码限制。 self.confirm! if model.valid? 抓住这一点,不要让欢迎电子邮件在验证错误的情况下溜走。
【解决方案2】:

我尝试了 benoror 的回答,起初它似乎有效 - 但是当您的用户接受邀请并填写表单无效时,它实际上会覆盖使邀请无效的令牌。

相反,可以使用回调来执行此操作:

class User < ActiveRecord::Base

  devise :invitable, :database_authenticatable, :registerable, :recoverable, 
     :rememberable, :confirmable, :validatable, :encryptable

  after_invitation_accepted :send_welcome_email


  def send_welcome_email
  end

end

【讨论】:

  • 时代变了。这就是答案。谢谢@Dandan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多