设计:在特定情况下是否可以不发送确认邮件?(即使确认为活动)

时间:2022-08-20 23:58:00

Here is my situation, I use devise to allow users to create account on my site and manage their authentication. During the registration process I allow customers to change some options, leading to an actually different account being created but still based on the same core user resource. I would like to choose not to send a confirmation email for some of those account types. I don't care if the account do not get confirmed and user cannot log in, that's ok, no pb with that. How would I go about doing that ? Thanks, Alex

这是我的情况,我使用设计允许用户在我的网站上创建帐户和管理他们的认证。在注册过程中,我允许客户更改一些选项,从而创建了一个实际上不同的帐户,但仍然基于相同的核心用户资源。我想选择不发送确认邮件的一些帐户类型。我不关心帐户是否被确认,用户不能登录,没关系,没有pb。我该怎么做呢?谢谢你,亚历克斯

5 个解决方案

#1


20  

Actually it's quite easy once I dig a little deeper. Just override one method in your User model (or whatever you are using):

其实只要我再深入一点就很容易了。只需覆盖用户模型中的一个方法(或您正在使用的任何方法):

    # Callback to overwrite if confirmation is required or not.
    def confirmation_required?
      !confirmed?
    end

Put your conditions and job's done !

把你的条件和工作做好!

Alex

亚历克斯

#2


12  

If you just want to skip sending the email but not doing confirmation, use:

如果你只是想跳过发送邮件而不做确认,可以使用:

# Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike
# #skip_confirmation!, record still requires confirmation.
@user.skip_confirmation_notification!

If you don't want to call this in your model with a callback overwrite this method:

如果您不想在模型中调用此方法,并使用回调覆盖此方法:

def send_confirmation_notification?
  false
end

#3


10  

You can also simply add the following line of code in your controller before creating the new user:

您还可以在创建新用户之前在控制器中添加以下代码行:

@user.skip_confirmation!

#4


8  

I don't know if Devise added this after the other answers were submitted, but the code for this is right there in confirmable.rb:

我不知道在提交了其他答案之后安德伯德是否添加了这个,但它的代码就在confirmable.rb:

  # If you don't want confirmation to be sent on create, neither a code
  # to be generated, call skip_confirmation!
  def skip_confirmation!
    self.confirmed_at = Time.now
  end

#5


5  

I was able to do something similar with the functions:

我能做一些类似的功能:

registrations_controller.rb

def build_resource(*args)
    super
    if session[:omniauth] # TODO -- what about the case where they have a session, but are not logged in?
      @user.apply_omniauth(session[:omniauth])
      @user.mark_as_confirmed # we don't need to confirm the account if they are using external authentication
      # @user.valid?
    end
  end

And then in my user model:

然后在我的用户模型中

user.rb

  def mark_as_confirmed
    self.confirmation_token = nil
    self.confirmed_at = Time.now
  end

#1


20  

Actually it's quite easy once I dig a little deeper. Just override one method in your User model (or whatever you are using):

其实只要我再深入一点就很容易了。只需覆盖用户模型中的一个方法(或您正在使用的任何方法):

    # Callback to overwrite if confirmation is required or not.
    def confirmation_required?
      !confirmed?
    end

Put your conditions and job's done !

把你的条件和工作做好!

Alex

亚历克斯

#2


12  

If you just want to skip sending the email but not doing confirmation, use:

如果你只是想跳过发送邮件而不做确认,可以使用:

# Skips sending the confirmation/reconfirmation notification email after_create/after_update. Unlike
# #skip_confirmation!, record still requires confirmation.
@user.skip_confirmation_notification!

If you don't want to call this in your model with a callback overwrite this method:

如果您不想在模型中调用此方法,并使用回调覆盖此方法:

def send_confirmation_notification?
  false
end

#3


10  

You can also simply add the following line of code in your controller before creating the new user:

您还可以在创建新用户之前在控制器中添加以下代码行:

@user.skip_confirmation!

#4


8  

I don't know if Devise added this after the other answers were submitted, but the code for this is right there in confirmable.rb:

我不知道在提交了其他答案之后安德伯德是否添加了这个,但它的代码就在confirmable.rb:

  # If you don't want confirmation to be sent on create, neither a code
  # to be generated, call skip_confirmation!
  def skip_confirmation!
    self.confirmed_at = Time.now
  end

#5


5  

I was able to do something similar with the functions:

我能做一些类似的功能:

registrations_controller.rb

def build_resource(*args)
    super
    if session[:omniauth] # TODO -- what about the case where they have a session, but are not logged in?
      @user.apply_omniauth(session[:omniauth])
      @user.mark_as_confirmed # we don't need to confirm the account if they are using external authentication
      # @user.valid?
    end
  end

And then in my user model:

然后在我的用户模型中

user.rb

  def mark_as_confirmed
    self.confirmation_token = nil
    self.confirmed_at = Time.now
  end