Skip to content

Commit

Permalink
Disable account signup depending on Settings variable
Browse files Browse the repository at this point in the history
When account creation is disabled, redirect to the homepage with an alert. When enabled, let Devise do its thing. This implementation relies on a small routing change that lets Hyku override two methods in `Devise::RegistrationsController`.

Fixes #1203
  • Loading branch information
mjgiarlo committed Jun 9, 2017
1 parent 34b389e commit d62328b
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Rails/HasAndBelongsToMany:
RSpec/AnyInstance:
Enabled: false

RSpec/InstanceVariable:
Exclude:
- 'spec/controllers/hyku/registrations_controller_spec.rb'

RSpec/NamedSubject:
Enabled: false

Expand Down
15 changes: 15 additions & 0 deletions app/controllers/hyku/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Hyku
class RegistrationsController < Devise::RegistrationsController
def new
return super if Settings.devise.account_signup
flash[:alert] = t(:'hyku.account_signup')
redirect_to root_path
end

def create
return super if Settings.devise.account_signup
flash[:alert] = t(:'hyku.account_signup')
redirect_to root_path
end
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ en:
proprietor:
accounts:
nav: 'Accounts'
account_signup: 'Account registration is disabled'
admin:
title: 'Administration'
flash:
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ es:
user_search:
submit: 'Ir'
hyku:
account_signup: 'El registro de la cuenta está deshabilitado'
admin:
title: 'Administración'
flash:
Expand Down
1 change: 1 addition & 0 deletions config/locales/zh.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ zh:
proprietor:
accounts:
nav: '帐号'
account_signup: '帐户注册被禁用'
admin:
title: '行政'
flash:
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

root 'hyrax/homepage#index'

devise_for :users
devise_for :users, controllers: { registrations: 'hyku/registrations' }
mount Qa::Engine => '/authorities'

mount Blacklight::Engine => '/'
Expand Down
3 changes: 3 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ google_analytics_id:
geonames_username: 'jcoyne'

contact_email: ""

devise:
account_signup: true
47 changes: 47 additions & 0 deletions spec/controllers/hyku/registrations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
RSpec.describe Hyku::RegistrationsController do
before do
allow(Settings.devise).to receive(:account_signup).and_return(account_signup_enabled)
# Recommended by Devise: https://github.com/plataformatec/devise/wiki/How-To:-Test-controllers-with-Rails-3-and-4-%28and-RSpec%29
@request.env['devise.mapping'] = Devise.mappings[:user]
end

context 'with account signup enabled' do
let(:account_signup_enabled) { true }
describe '#new' do
it 'renders the form' do
get :new
expect(response).to render_template('devise/registrations/new')
end
end
describe '#create' do
it 'processes the form' do
post :create, params: {
user: {
email: "user@example.org",
password: "password",
password_confirmation: "password"
}
}
expect(response).to redirect_to dashboard_path(locale: 'en')
expect(flash[:notice]).to eq 'Welcome! You have signed up successfully.'
end
end
end
context 'with account signup disabled' do
let(:account_signup_enabled) { false }
describe '#new' do
it 'redirects with a flash message' do
get :new
expect(response).to redirect_to root_path
expect(flash[:alert]).to eq 'Account registration is disabled'
end
end
describe '#create' do
it 'redirects with a flash message' do
post :create
expect(response).to redirect_to root_path
expect(flash[:alert]).to eq 'Account registration is disabled'
end
end
end
end
10 changes: 10 additions & 0 deletions spec/routing/registrations_routing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
RSpec.describe Hyku::RegistrationsController, type: :routing do
describe "routing" do
it "routes to #new" do
expect(get: "/users/sign_up").to route_to("hyku/registrations#new")
end
it "routes to #edit" do
expect(get: "/users/edit").to route_to("hyku/registrations#edit")
end
end
end

0 comments on commit d62328b

Please sign in to comment.