Ruby-on-rails 仅对生产环境禁用设计注册

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5370164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:43:13  来源:igfitidea点击:

disabling Devise registration for production environment only

ruby-on-railsdeviseregistrationproduction-environment

提问by panzhuli

I am launching a beta site with a select group of users. I want to disable registration in the production environment only, and only for a short period of time (i.e. I don't want to nuke my registration altogether). I know I can simply hide the "sign up" link, but I suspect that hackers smarter than I can still use the RESTful routes to accomplish registrations. What's the best way to disable registration so my test/development environments still work, but production is affected? Thanks for any pointers.

我正在推出一个包含选定用户组的测试版网站。我只想在生产环境中禁用注册,并且只禁用一小段时间(即我不想完全取消我的注册)。我知道我可以简单地隐藏“注册”链接,但我怀疑比我更聪明的黑客仍然可以使用 RESTful 路由来完成注册。禁用注册以便我的测试/开发环境仍然有效,但生产受到影响的最佳方法是什么?感谢您的指点。

I've tried pointing named scopes in such a way that "sign_up" goes to "sign_in", but it didn't work. Here's what I've tried:

我试过以“sign_up”转到“sign_in”的方式指向命名范围,但它没有用。这是我尝试过的:

devise_scope :user do
    get "users/sign_in", :to => "devise/sessions#new", :as => :sign_in
    get "users/sign_up", :to => "devise/sessions#new", :as => :sign_up
end

Ideally, we'd send the user to a "pages#registration_disabled" page or something like that. I just wanted to get something working I can play around with.

理想情况下,我们会将用户发送到“pages#registration_disabled”页面或类似的页面。我只是想让我可以玩的东西工作。

EDIT: I've changed the model as requested, then added the following to /spec/user_spec.rb

编辑:我已按要求更改了模型,然后将以下内容添加到 /spec/user_spec.rb

describe "validations" do
    it "should fail registration if in production mode" do
      ENV['RAILS_ENV'] = "production"
      @user = Factory(:user).should_not be_valid
    end
end

it is passing as "true" rather than false. Is there a way to mock up the production environment? I'm just spit-balling this one.

它传递为“真”而不是假。有没有办法模拟生产环境?我只是在吐槽这个。

Thanks!

谢谢!

回答by Fareesh Vijayarangam

Edit the usermodel and remove :registerable, I think that should give you what you want.

编辑user模型并删除:registerable,我认为这应该给你你想要的。

Edit:

编辑:

I think this would work:

我认为这会奏效:

if Rails.env.production?
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
else
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable 
end

回答by Chris Nicola

Since others are having the problem I'm having (see my comments). Here is exactly how I fixed it. I used murphyslaw's idea. But you also need to make sure devise uses your new controller for the registration routing, or it won't do much for you.

由于其他人遇到了我遇到的问题(请参阅我的评论)。这正是我修复它的方法。我使用了墨菲斯劳的想法。但是您还需要确保设计使用您的新控制器进行注册路由,否则它不会为您做太多。

Here is my controller override:

这是我的控制器覆盖:

class RegistrationsController < Devise::RegistrationsController
  def new
    flash[:info] = 'Registrations are not open yet, but please check back soon'
    redirect_to root_path
  end

  def create
    flash[:info] = 'Registrations are not open yet, but please check back soon'
    redirect_to root_path
  end
end

I've added flash messages to inform anyone who somehow stumbles upon the registration page why it isn't working.

我已经添加了 flash 消息,以告知以某种方式偶然发现注册页面为什么它不起作用的任何人。

Here is what is in my routes.rb

这是我的 routes.rb

  if Rails.env.production?
    devise_for :users, :controllers => { :registrations => "registrations" } 
  else
    devise_for :users
  end

The controllers hash specifies that I want it to use my overridden registrations controller.

控制器哈希指定我希望它使用我覆盖的注册控制器。

Anyways, I hope that saves someone some time.

无论如何,我希望能节省一些时间。

回答by Mindbreaker

Only remove :registerablewill not solve the problem. If you have some routes in your view you will get an error:

只删除:registerable并不能解决问题。如果您的视图中有一些路线,您将收到错误消息:

undefined local variable or method 'edit_user_registration_path'

undefined local variable or method 'edit_user_registration_path'

Take care of this.

照顾好这个。

回答by murphyslaw

you could override the Devise::RegistrationsController and the create action to redirect to the page you want. The Controller should probably look something like this:

您可以覆盖 Devise::RegistrationsController 和 create 操作以重定向到您想要的页面。控制器应该看起来像这样:

class User::RegistrationsController < Devise::RegistrationsController

  def create
    redirect_to your_page_path if Rails.env.production?
  end

end