用设计白名单

时间:2011-03-02 12:40:01

标签: ruby-on-rails authentication devise whitelist

我正在使用设计来管理我的rails应用程序中的用户身份验证。设计真的很棒。

但是我对我的申请有特殊要求:用户必须先列入白名单才能注册为用户。

因此,有一个管理员可以创建允许的电子邮件列表。用户使用电子邮件注册,如果电子邮件在白名单表中,则他将被注册。但是,如果邮件不在白名单中,则应中止注册,并显示“您尚未被邀请”等消息。

你知道如何通过设计来解决这个问题吗?

提前致谢。

3 个答案:

答案 0 :(得分:16)

我只会使用模型验证。我假设你的User类有设计方法

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable #etc

  before_validation :whitelisted

  def whitelisted
    unless celebrityemail.include? email
      errors.add :email, "#{email} is not on our invitation list"  
    end
  end 

end

答案 1 :(得分:6)

您可以创建自己的注册控制器并将设备扩展为:

class MyRegistrationController < Devise::RegistrationsController
  def create
    # do your checks
    super
  end
end

见:https://github.com/plataformatec/devise/blob/master/app/controllers/devise/registrations_controller.rb 并且:https://github.com/plataformatec/devise/wiki/How-to:-Customize-routes-to-user-registration-pages

祝你好运!

答案 2 :(得分:2)

我按照建议创建了自己的控制器:

class Users::RegistrationsController < Devise::RegistrationsController
    def create
        email = params[:user][:email]
        if Admin::Whitelist.find_by_email(email) != nil
            super
        else
            build_resource

            set_flash_message :error, "You are not permitted to sign up yet. If you have already payed your registration fee, try again later."
            render_with_scope :new
        end
    end
end

我把它放在app/users/registrations_controller.rb。然后我不得不将设计注册视图复制到app/views/users/registrations,因为没有使用默认视图。

现在正在工作,谢谢你的帮助