devise_invitable: 邀请后确认

发布于 2024-11-08 15:21:36 字数 1742 浏览 3 评论 0原文

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

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 (或任何其名称)方法?

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

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

原始来源

更新

浏览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

I override devise's confirm! method to send a welcome message to my users:

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

With devise_invitable when the user accept the invitation and set his password the confirm! method is never triggered, is it possible to force it? How does devise_invitable confirms the User?

Or maybe I can override the accept_invite (or whatever its called) method the same way?

I want that invited users remain unconfirmed, and then confirmed upon accepting the invitation.

Thanks, any help very appreciated!

Original Source

UPDATE

Looking through devise_invitable model I found the two methods who may be causing this misbehavior:

  # 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-11-15 15:21:36
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
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
苏璃陌 2024-11-15 15:21:36

我尝试了 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

I tried benoror's answer and at first it appeared to work - but when you a user accepts the invitation and fills in the form as invalid it will actually override the token invalidating the invitation.

Instead, a callback is available to do this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文