仅对生产环境禁用设计注册 [英] disabling Devise registration for production environment only

查看:27
本文介绍了仅对生产环境禁用设计注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试过以sign_up"指向sign_in"的方式指向命名范围,但它没有奏效.这是我尝试过的:

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:

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

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

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.

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

I've changed the model as requested, then added the following to /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.

谢谢!

推荐答案

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

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.

这是我的控制器覆盖:

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

我添加了 flash 消息,以告知任何偶然发现注册页面为什么它不起作用的人.

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

这是我的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.

这篇关于仅对生产环境禁用设计注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆