From 71ca2de7aabf3191c4f486ca15a78a5b7f6abd94 Mon Sep 17 00:00:00 2001 From: Felipe Artur Date: Thu, 21 Apr 2016 15:55:54 -0300 Subject: Toggle email signup confirmation in admin settings --- CHANGELOG | 1 + .../admin/application_settings_controller.rb | 1 + app/models/user.rb | 5 ++++ .../admin/application_settings/_form.html.haml | 6 ++++ ...p_confirmation_email_to_application_settings.rb | 8 ++++++ spec/controllers/registrations_controller_spec.rb | 33 ++++++++++++++++++++++ 6 files changed, 54 insertions(+) create mode 100644 db/migrate/20160421141709_add_skip_confirmation_email_to_application_settings.rb create mode 100644 spec/controllers/registrations_controller_spec.rb diff --git a/CHANGELOG b/CHANGELOG index ca59f488e0f..7a86263c0a9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -189,6 +189,7 @@ v 8.7.0 - Add Slack notifications when Wiki is edited (Sebastian Klier) - Diffs load at the correct point when linking from from number - Selected diff rows highlight + - Toggle sign-up confirmation emails in application settings - Fix emoji categories in the emoji picker - API: Properly display annotated tags for GET /projects/:id/repository/tags (Robert Schilling) - Add encrypted credentials for imported projects and migrate old ones diff --git a/app/controllers/admin/application_settings_controller.rb b/app/controllers/admin/application_settings_controller.rb index 8c973f0e4a8..956d145f029 100644 --- a/app/controllers/admin/application_settings_controller.rb +++ b/app/controllers/admin/application_settings_controller.rb @@ -106,6 +106,7 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController :email_author_in_body, :repository_checks_enabled, :metrics_packet_size, + :skip_user_confirmation_email, restricted_visibility_levels: [], import_sources: [], disabled_oauth_sign_in_sources: [] diff --git a/app/models/user.rb b/app/models/user.rb index 489bff3fa4a..470734f5c2f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -112,6 +112,7 @@ class User < ActiveRecord::Base before_save :ensure_external_user_rights after_save :ensure_namespace_correct after_initialize :set_projects_limit + before_create :check_confirmation_email after_create :post_create_hook after_destroy :post_destroy_hook @@ -307,6 +308,10 @@ class User < ActiveRecord::Base @reset_token end + def check_confirmation_email + skip_confirmation! if current_application_settings.skip_user_confirmation_email + end + def recently_sent_password_reset? reset_password_sent_at.present? && reset_password_sent_at >= 1.minute.ago end diff --git a/app/views/admin/application_settings/_form.html.haml b/app/views/admin/application_settings/_form.html.haml index f7c799c968f..6d6d87cdd5a 100644 --- a/app/views/admin/application_settings/_form.html.haml +++ b/app/views/admin/application_settings/_form.html.haml @@ -103,6 +103,12 @@ = f.label :signup_enabled do = f.check_box :signup_enabled Sign-up enabled + .form-group + .col-sm-offset-2.col-sm-10 + .checkbox + = f.label :skip_confirmation_email do + = f.check_box :skip_user_confirmation_email + Skip sign-up email confirmation .form-group .col-sm-offset-2.col-sm-10 .checkbox diff --git a/db/migrate/20160421141709_add_skip_confirmation_email_to_application_settings.rb b/db/migrate/20160421141709_add_skip_confirmation_email_to_application_settings.rb new file mode 100644 index 00000000000..953f1cea896 --- /dev/null +++ b/db/migrate/20160421141709_add_skip_confirmation_email_to_application_settings.rb @@ -0,0 +1,8 @@ +class AddSkipConfirmationEmailToApplicationSettings < ActiveRecord::Migration + def change + #Skip confirmation emails just for new installations + default_value = User.count > 0 ? false : true + + add_column :application_settings, :skip_user_confirmation_email, :boolean, default: default_value + end +end diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb new file mode 100644 index 00000000000..b4ab767f73e --- /dev/null +++ b/spec/controllers/registrations_controller_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe RegistrationsController do + describe '#create' do + around(:each) do |example| + perform_enqueued_jobs do + example.run + end + end + + let(:user_params) { { "user"=> {"name"=>"new_user", "username"=>"new_username", "email"=>"new@user.com", "password"=>"Any_password"} } } + + context 'when skipping email confirmation' do + before { allow(current_application_settings).to receive(:skip_user_confirmation_email).and_return(true) } + + it 'logs user in directly' do + post(:create, user_params) + expect(ActionMailer::Base.deliveries.last).to be_nil + expect(subject.current_user).to be + end + end + + context 'when not skipping email confirmation' do + before { allow(current_application_settings).to receive(:skip_user_confirmation_email).and_return(false) } + + it 'does not authenticate user and sends confirmation email' do + post(:create, user_params) + expect(ActionMailer::Base.deliveries.last.to.first).to eq(user_params["user"]["email"]) + expect(subject.current_user).to be_nil + end + end + end +end -- cgit v1.2.1