仅禁用生产环境的Devise注册

pan*_*uli 69 ruby-on-rails registration production-environment devise

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

我已经尝试以"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
Run Code Online (Sandbox Code Playgroud)

理想情况下,我们会将用户发送到"pages#registration_disabled"页面或类似的内容.我只是想得到一些我可以玩的东西.

编辑:我已根据要求更改了模型,然后将以下内容添加到/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
Run Code Online (Sandbox Code Playgroud)

它传递的是"真实的"而不是虚假的.有没有办法模拟生产环境?我只是在吐这个.

谢谢!

Far*_*gam 101

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

编辑:

我认为这会奏效:

if Rails.env.production?
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
else
  devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable 
end
Run Code Online (Sandbox Code Playgroud)

  • 根据Chris Nicola的答案http://stackoverflow.com/a/8291318/9344,这将禁用编辑注册的能力,这可能超过了预期的效果(绝对是我的情况). (8认同)
  • 不需要这个,但发现我可以在模型中使用"Rails.env".无价:)谢谢. (7认同)

Chr*_*ola 88

因为其他人遇到了我遇到的问题(请参阅我的评论).这正是我修复它的方式.我使用了墨菲斯的想法.但是你还需要确保设计使用你的新控制器进行注册路由,否则它对你没有多大帮助.

这是我的控制器覆盖:

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
Run Code Online (Sandbox Code Playgroud)

我已经添加了flash消息,以通知任何不知何故在注册页面上发现它无法正常工作的人.

这是我的 routes.rb

  if Rails.env.production?
    devise_for :users, :controllers => { :registrations => "registrations" } 
  else
    devise_for :users
  end
Run Code Online (Sandbox Code Playgroud)

控制器哈希指定我希望它使用我的重写注册控制器.

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

  • 很酷的解决方案,但这不会杀死编辑路线? (3认同)
  • 不,因为它继承自包含所有路由处理程序的`Devise:RegistrationsController`.我所做的就是覆盖创建路线. (3认同)

Min*_*ker 11

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

undefined local variable or method 'edit_user_registration_path'

照顾好这一点.


小智 6

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

class User::RegistrationsController < Devise::RegistrationsController

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

end
Run Code Online (Sandbox Code Playgroud)