如何配置我的应用程序以仅允许特定电子邮件向devise注册?

我正在设置CRUD功能,我只希望几个人(整个电子邮件地址)能够通过设计进行注册。

我经历了无数的帖子,但是它们大多使用电子邮件域或单人登录。我也尝试在User.rb中创建自己的验证方法,但似乎无法获取。

validate :check_email

  private

  def check_email
    @users = User.all

    if @users == '123.example@gmail.com')
        events_path
    else
        errors.add(:email,'is not authorized')
    end
  end

我没有得到明确的错误,但该应用似乎跳过了我的“ if”条件,并输出了“ else”条件。

ezeke123 回答:如何配置我的应用程序以仅允许特定电子邮件向devise注册?

最简单的方法是像最初那样创建自定义验证。在用户模型中,您可以创建如下所示的验证:

validate :is_email_valid?


def is_email_valid
    if ["example@gmail.com","example@yahoo.com","123.example@gmail.com"].include?(self.email)
        errors.add :base,"Your email is not authorized for use!"
    end
end

此代码在保存或创建新记录时将起作用。

代码的问题是您试图在ActiveRecord_Relation对象上进行验证,该对象无法直接访问类实例的验证。就像试图从类级别调用实例方法一样;您必须一次验证一个用户。您将需要参考self在对象实例上执行验证。因此,遍历您的用户然后进行验证将起作用。这是一个示例:

User.all.each do |i|

    if i.valid?
        puts "VALID"
    else
        puts "INVALID"
    end
end
,

要仅允许具有特定电子邮件地址的用户,您可以简单地添加一个inclusion验证。

class User < ApplicationRecord
  ALLOWED_EMAILS = %w[
    123.example@gmail.com
    456.example@gmail.com
    789.example@gmail.com
  ].freeze

  validates :email,inclusion: { in: ALLOWED_EMAILS,message: :invalid }

  # ...
end

您还可以选择从设置或文件中加载ALLOWED_EMAILS


load from the Rails config,您必须在配置文件中定义电子邮件地址。

config.allowed_user_emails = %w[
  123.example@gmail.com
  456.example@gmail.com
  789.example@gmail.com
]

然后使用以下命令将它们加载到控制器中

ALLOWED_EMAILS = Rails.configuration.allowed_user_emails.freeze

对于load from for example a yaml file,您可以执行以下操作:

ALLOWED_EMAILS = YAML.load_file(Rails.root.join('config','allowed_user_emails.yml')).freeze

文件中包含以下内容:

- 123.example@gmail.com
- 456.example@gmail.com
- 789.example@gmail.com
,

萨拉,

完成此任务的第一步是在devise的registrations_controller中工作。这是将向注册用户提供服务的控制器,在这里您将要覆盖一些Devise代码。 确保通过运行Devise的控制器生成器首先创建控制器:

rails generate devise:controllers users

然后,您将要查找用户的registrations_controller.rb,并在create动作下覆盖其中的代码。 这是我为重写Devise的管理控制器而编写的一些代码的示例:

def create
    build_resource(sign_up_params)
    # Below - If admin is coming from an email invite or shared invite link,grabs both token and workplace id from params.
    @token = params[:invite_token] if params[:invite_token]
    @workplace_id = params[:workplace_id] if params[:workplace_id]
    @workplace = Workplace.find(params[:workplace_id]) if params[:workplace_id] # Finds the workplace from the workplace_id,works for both invite email and shared link.
    @institute = @workplace.institute if params[:workplace_id]
    if @institute && @institute.has_super_admins?
      resource.super_admin = false
    end 

    if resource.save
      yield resource if block_given?
      if resource.persisted?
        if resource.active_for_authentication?
          # Below - If admin came from a shared workpalce invite link or email workplace invite
          if @token != nil # Admin signed up via a workplace invite email or the shared link
            # Below - Checks Payment plan for admins joining an institute to make sure the institute didn't exceed user count subscription permission.
            unless @institute.plan.unlimited_users? 
              if @institute.plan.user_count <= @institute.admins.count # Admin doesn't join workplace since institute has excited user allowance
                set_flash_message! :alert,:signed_up_no_workplace,:workplace => @workplace.name,:institute => @institute.name,:workplace_owner => @institute.subscription.admin.name
                sign_up(resource_name,resource)
                respond_with resource,location: institute_admin_path(resource.institute,resource)
              else # Admin successfully signs up and joins the workplace. Method is below in protected
                join_workplace_and_redirect
              end 
            else # Admin successfully signs up and joins the workplace. Method is below in protected
              join_workplace_and_redirect
            end
          else # Fresh admin signup
            sign_up(resource_name,resource)
            if resource.super_admin? # Checks if the admin is a super_admin and set as true,if so redirects to another page
              set_flash_message! :notice,:super_admin_signed_up,:name => resource.first_name
              respond_with resource,location: new_institute_path()
            else # Admin is not a super_admin and not first one who signed up inside a city. 
              set_flash_message! :notice,:admin_signed_up,:link => edit_institute_admin_path(resource.institute,resource),:city => resource.institute.name
              respond_with resource,location: after_sign_up_path_for(resource)
            end 
          end
        else
          set_flash_message! :notice,:"signed_up_but_#{resource.inactive_message}"
          expire_data_after_sign_in!
          respond_with resource,location: after_inactive_sign_up_path_for(resource)
        end
      else
        flash[:alert] = "Your profile could not be created. See why below!"
        set_flash_message! :alert,:"signed_up_but_#{resource.inactive_message}"
        redirect_to request.referrer
      end
    else  # Failed to save
      clean_up_passwords resource
      respond_with resource,location: new_admin_registration_path(workplace: @workplace)
    end
  end

要将其应用于您的案例,您可能想使用案例声明来查看电子邮件是否与您想要的匹配。例如,在下面,您需要检查resource(在此情况下为单个用户,即注册用户)的电子邮件属性,以确定它是成功还是失败:

# Checks emails that are allowed
case resource.email 
when "123.example@gmail.com" 
  if resource.save # Success
    set_flash_message! :notice,:signup_succeed
    respond_with resource,location: home_page(resource)
  else # For some reason the allowed email didn't go through due to other validations
    set_flash_message! :alert,:signup_failure
    respond_with resource,location: new_user_path(resource)
  end 
else # Entered an email that is not allowed
  set_flash_message! :alert,:email_invalid
  respond_with resource,location: new_user_path(resource)
end

set_flash_message!是Devises自定义消息,可以在config/locales/devise.en.yaml中进行编辑。第二个关键字是yaml键的名称,您可以在其中自定义错误或成功消息。 respond_with是重定向和位置。您可以根据需要使用任意多个when语句。这只是做到这一点的一种方法。 希望这会有所帮助。

本文链接:https://www.f2er.com/3121251.html

大家都在问