From d3098cf6bc9ebeec91b217ee33136c349ffa81ab Mon Sep 17 00:00:00 2001
From: Raphael <raphael@joindiaspora.com>
Date: Mon, 15 Nov 2010 12:15:40 -0800
Subject: [PATCH] Add option to close registration in app_config

---
 Gemfile                                       |  1 +
 Gemfile.lock                                  |  2 ++
 app/controllers/registrations_controller.rb   | 14 ++++++++++++++
 app/views/devise/shared/_links.haml           |  2 +-
 chef/cookbooks/common/recipes/daemontools.rb  |  5 +++++
 config/app_config.yml.example                 |  4 ++++
 config/locales/diaspora/en.yml                |  1 +
 .../registrations_controller_spec.rb          | 19 +++++++++++++++++++
 8 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/Gemfile b/Gemfile
index 882f14c92a..5071cec4d6 100644
--- a/Gemfile
+++ b/Gemfile
@@ -62,6 +62,7 @@ group :test do
   gem 'webmock', :require => false
   gem 'jasmine', :path => 'vendor/gems/jasmine', :require => false
   gem 'mongrel', :require => false if RUBY_VERSION.include? "1.8"
+  gem 'rspec-instafail', :require => false
 end
 
 group :deployment do
diff --git a/Gemfile.lock b/Gemfile.lock
index 4c3c1920ed..760d087fc3 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -318,6 +318,7 @@ GEM
     rspec-core (2.1.0)
     rspec-expectations (2.1.0)
       diff-lcs (~> 1.1.2)
+    rspec-instafail (0.1.2)
     rspec-mocks (2.1.0)
     rspec-rails (2.1.0)
       rspec (~> 2.1.0)
@@ -400,6 +401,7 @@ DEPENDENCIES
   rails (>= 3.0.0)
   roxml!
   rspec (>= 2.0.0)
+  rspec-instafail
   rspec-rails (>= 2.0.0)
   ruby-debug
   sprinkle!
diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb
index 5ba0fb3c52..4cb5be4896 100644
--- a/app/controllers/registrations_controller.rb
+++ b/app/controllers/registrations_controller.rb
@@ -3,6 +3,8 @@
 #   the COPYRIGHT file.
 
 class RegistrationsController < Devise::RegistrationsController
+  before_filter :check_registrations_open!
+
   def create
     @user = User.build(params[:user])
     if @user.save
@@ -14,4 +16,16 @@ class RegistrationsController < Devise::RegistrationsController
       render :new
     end
   end
+
+  def new
+    super
+  end
+
+  private
+  def check_registrations_open!
+    if APP_CONFIG[:registrations_closed]
+      flash[:error] = t('registrations.closed')
+      redirect_to root_url
+    end
+  end
 end
diff --git a/app/views/devise/shared/_links.haml b/app/views/devise/shared/_links.haml
index fa0aea8cad..ef8b2c6e1f 100644
--- a/app/views/devise/shared/_links.haml
+++ b/app/views/devise/shared/_links.haml
@@ -1,7 +1,7 @@
 - if controller_name != 'sessions'
   = link_to t('.sign_in'), new_session_path(resource_name)
   %br/
-- if devise_mapping.registerable? && controller_name != 'registrations'
+- if !APP_CONFIG[:registrations_closed] && devise_mapping.registerable? && controller_name != 'registrations'
   = link_to t('.sign_up'), new_registration_path(resource_name)
   %br/
 - if devise_mapping.recoverable? && controller_name != 'passwords'
diff --git a/chef/cookbooks/common/recipes/daemontools.rb b/chef/cookbooks/common/recipes/daemontools.rb
index 84e4a4f105..15a1c4c341 100644
--- a/chef/cookbooks/common/recipes/daemontools.rb
+++ b/chef/cookbooks/common/recipes/daemontools.rb
@@ -32,6 +32,11 @@ config.each do |thin|
   end
 end
 
+#service for mongo tunnel
+#execute "mongo ssh tunnel" do
+  #command "mkdir -p /service/mongo_ssh_tunnel && echo '#!/bin/sh' > /service/mongo_ssh_tunnel/run && echo 'exec ssh -N -f -L 27017:localhost:27017 caesar@184.106.233.43' >> /service/websocket/run"
+#end
+
 execute "websocket run" do
   command "mkdir -p /service/websocket && echo '#!/bin/sh' > /service/websocket/run && echo 'cd /usr/local/app/diaspora && exec /usr/local/bin/ruby /usr/local/app/diaspora/script/websocket_server.rb' >> /service/websocket/run"
 end
diff --git a/config/app_config.yml.example b/config/app_config.yml.example
index 2445453aa5..3e9016a7ff 100644
--- a/config/app_config.yml.example
+++ b/config/app_config.yml.example
@@ -7,6 +7,10 @@ default:
   # Hostname of this host, as seen from the internet.
   pod_url: "http://example.org/"
 
+  # Set this to true in order to close signups.  Users will still be
+  # able to invite other people to join.
+  registrations_closed: false
+
   # Enable extensive logging to log/{development,test,production}.log
   debug: false
 
diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml
index ff904c28d9..0d09be8119 100644
--- a/config/locales/diaspora/en.yml
+++ b/config/locales/diaspora/en.yml
@@ -226,6 +226,7 @@ en:
           back: "Back"
           update: "Update"
           cancel_my_account: "Cancel my account"
+      closed: "Signups are closed on this Diaspora pod."
   invitations:
       create:
           sent: "Your invitation has been sent."
diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb
index 9b4e6f768e..4881eb7963 100644
--- a/spec/controllers/registrations_controller_spec.rb
+++ b/spec/controllers/registrations_controller_spec.rb
@@ -17,6 +17,25 @@ describe RegistrationsController do
                                 "password_confirmation" => "password"}}
   end
 
+  describe '#check_registrations_open!' do
+    before do
+      APP_CONFIG[:registrations_closed] = true
+    end
+    after do
+      APP_CONFIG[:registrations_closed] = false
+    end
+    it 'stops a #new request' do
+      get :new
+      flash[:error].should == I18n.t('registrations.closed')
+      response.should redirect_to root_url
+    end
+    it 'stops a #create request' do
+      post :create, @valid_params
+      flash[:error].should == I18n.t('registrations.closed')
+      response.should redirect_to root_url
+    end
+  end
+
   describe "#create" do
     context "with valid parameters" do
       before do
-- 
GitLab