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

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

问题描述

我正在推出一个使用精选群组的测试版网站。我只想在生产环境中禁用注册,并且只能在短时间内(即我不想完全注册我的注册)。我知道我可以简单地隐藏注册链接,但我怀疑黑客比我仍然可以使用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

理想情况下,我们会将用户发送到##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

它传递为true而不是false。有没有办法模拟生产环境?我只是吐了一口气。

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.

谢谢!

推荐答案

由于其他人遇到问题,有(见我的评论)。这是我如何修复它。我使用了murphyslaw的想法。但是您还需要确保设计人员使用您的新控制器进行注册路由,否则对您而言不会太多。

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.

这是我的控制器覆盖: p>

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

我已经添加了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.

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

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