使用设备列入白名单

发布于 2024-10-20 11:31:41 字数 252 浏览 6 评论 0原文

我正在使用 devise 来管理 Rails 应用程序中的用户身份验证。 Devise 确实非常适合这一点。

但是,我对我的应用程序有一个特殊要求:用户必须先列入白名单,然后才能注册为用户。

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

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

提前致谢。

I am using devise to manage user authentication in my rails app. Devise is really great for that.

However I have a special requirement for my application: A user must be whitelisted before he can register as a User.

So there is a admin which creates a list of allowed emails. A user registers with a email and if the email is in the whitelist table he will be registered. If however, the mail is not in the whitelist, the registration should be aborted with a message like "You are not yet invited".

Do you have an idea how that could be solved with devise?

Thanks in advance.

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

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

发布评论

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

评论(3

一个人的旅程 2024-10-27 11:31:41

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

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

I would just use model validation. I'm assuming your User class has the devise method

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
兔姬 2024-10-27 11:31:41

您可以做的是创建自己的注册控制器并扩展设备,例如:

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:-自定义用户注册页面的路由

祝你好运!

What you can do is create your own registrations controller and extend the device one like:

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

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

Good luck!

墨洒年华 2024-10-27 11:31:41

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

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 中,因为未使用默认视图。

现在正在运行,感谢您的帮助

I did create my own controller as suggested:

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

I placed it in app/users/registrations_controller.rb. Then I had to copy the devise registration views into app/views/users/registrations because the default views were not used.

It is working now, thanks for your help

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