From 41d7be3ce1ae9a4bff93b62322f35989b6ad4cf6 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 16 Jan 2015 16:01:15 -0800 Subject: Allow to specify home page for non logged-in users --- app/controllers/admin/application_settings_controller.rb | 1 + app/controllers/application_controller.rb | 11 +++++++++++ app/helpers/application_helper.rb | 5 ----- app/models/application_setting.rb | 3 +++ app/views/admin/application_settings/_form.html.haml | 4 ++++ app/views/layouts/devise.html.haml | 3 +-- ...150116234544_add_home_page_url_for_application_settings.rb | 5 +++++ db/schema.rb | 5 +++-- 8 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20150116234544_add_home_page_url_for_application_settings.rb diff --git a/app/controllers/admin/application_settings_controller.rb b/app/controllers/admin/application_settings_controller.rb index 5116f1f177a..a937f484877 100644 --- a/app/controllers/admin/application_settings_controller.rb +++ b/app/controllers/admin/application_settings_controller.rb @@ -26,6 +26,7 @@ class Admin::ApplicationSettingsController < Admin::ApplicationController :signin_enabled, :gravatar_enabled, :sign_in_text, + :home_page_url ) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b83de68c5d2..4780a7a2a9a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -48,6 +48,17 @@ class ApplicationController < ActionController::Base end end + def authenticate_user! + # If user is not signe-in and tries to access root_path - redirect him to landing page + if current_application_settings.home_page_url.present? + if current_user.nil? && controller_name == 'dashboard' && action_name == 'show' + redirect_to current_application_settings.home_page_url and return + end + end + + super + end + def log_exception(exception) application_trace = ActionDispatch::ExceptionWrapper.new(env, exception).application_trace application_trace.map!{ |t| " #{t}\n" } diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 67c02f5dfa4..f65e04af205 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -292,9 +292,4 @@ module ApplicationHelper profile_key_path(key) end end - - def redirect_from_root? - request.env['rack.session']['user_return_to'] == - '/' - end end diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb index 47fa6f1071c..d9c73559098 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -1,4 +1,7 @@ class ApplicationSetting < ActiveRecord::Base + validates :home_page_url, allow_blank: true, + format: { with: URI::regexp(%w(http https)), message: "should be a valid url" } + def self.current ApplicationSetting.last end diff --git a/app/views/admin/application_settings/_form.html.haml b/app/views/admin/application_settings/_form.html.haml index 5ca9585e9a9..481e7882300 100644 --- a/app/views/admin/application_settings/_form.html.haml +++ b/app/views/admin/application_settings/_form.html.haml @@ -25,6 +25,10 @@ = f.label :default_projects_limit, class: 'control-label' .col-sm-10 = f.number_field :default_projects_limit, class: 'form-control' + .form-group + = f.label :home_page_url, class: 'control-label' + .col-sm-10 + = f.text_field :home_page_url, class: 'form-control', placeholder: 'http://company.example.com' .form-group = f.label :sign_in_text, class: 'control-label' .col-sm-10 diff --git a/app/views/layouts/devise.html.haml b/app/views/layouts/devise.html.haml index 857ebd9b8d9..6f805f1c9d1 100644 --- a/app/views/layouts/devise.html.haml +++ b/app/views/layouts/devise.html.haml @@ -6,8 +6,7 @@ = render "layouts/public_head_panel", title: '' .container.navless-container .content - - unless redirect_from_root? - = render "layouts/flash" + = render "layouts/flash" .row.prepend-top-20 .col-sm-5.pull-right = yield diff --git a/db/migrate/20150116234544_add_home_page_url_for_application_settings.rb b/db/migrate/20150116234544_add_home_page_url_for_application_settings.rb new file mode 100644 index 00000000000..aa179ce3a4d --- /dev/null +++ b/db/migrate/20150116234544_add_home_page_url_for_application_settings.rb @@ -0,0 +1,5 @@ +class AddHomePageUrlForApplicationSettings < ActiveRecord::Migration + def change + add_column :application_settings, :home_page_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index dedfce4797b..96f66ac3634 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150108073740) do +ActiveRecord::Schema.define(version: 20150116234544) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -24,6 +24,7 @@ ActiveRecord::Schema.define(version: 20150108073740) do t.text "sign_in_text" t.datetime "created_at" t.datetime "updated_at" + t.string "home_page_url" end create_table "broadcast_messages", force: true do |t| @@ -423,6 +424,7 @@ ActiveRecord::Schema.define(version: 20150108073740) do t.integer "notification_level", default: 1, null: false t.datetime "password_expires_at" t.integer "created_by_id" + t.datetime "last_credential_check_at" t.string "avatar" t.string "confirmation_token" t.datetime "confirmed_at" @@ -430,7 +432,6 @@ ActiveRecord::Schema.define(version: 20150108073740) do t.string "unconfirmed_email" t.boolean "hide_no_ssh_key", default: false t.string "website_url", default: "", null: false - t.datetime "last_credential_check_at" t.string "github_access_token" end -- cgit v1.2.1 From e7f772550c5fad5761777b76f97726be84693746 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 16 Jan 2015 16:09:20 -0800 Subject: Add tests to home page url redirect --- app/views/admin/application_settings/_form.html.haml | 1 + features/admin/settings.feature | 4 ++-- features/steps/admin/settings.rb | 6 ++++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/views/admin/application_settings/_form.html.haml b/app/views/admin/application_settings/_form.html.haml index 481e7882300..9423a207068 100644 --- a/app/views/admin/application_settings/_form.html.haml +++ b/app/views/admin/application_settings/_form.html.haml @@ -29,6 +29,7 @@ = f.label :home_page_url, class: 'control-label' .col-sm-10 = f.text_field :home_page_url, class: 'form-control', placeholder: 'http://company.example.com' + %span.help-block We will redirect non-logged in users to this page .form-group = f.label :sign_in_text, class: 'control-label' .col-sm-10 diff --git a/features/admin/settings.feature b/features/admin/settings.feature index 8799c053ea2..8fdf0575c2c 100644 --- a/features/admin/settings.feature +++ b/features/admin/settings.feature @@ -5,5 +5,5 @@ Feature: Admin Settings And I visit admin settings page Scenario: Change application settings - When I disable gravatars and save form - Then I should be see gravatar disabled + When I modify settings and save form + Then I should see application settings saved diff --git a/features/steps/admin/settings.rb b/features/steps/admin/settings.rb index e8168e85def..c2d0d2a3fa3 100644 --- a/features/steps/admin/settings.rb +++ b/features/steps/admin/settings.rb @@ -4,13 +4,15 @@ class Spinach::Features::AdminSettings < Spinach::FeatureSteps include SharedAdmin include Gitlab::CurrentSettings - step 'I disable gravatars and save form' do + step 'I modify settings and save form' do uncheck 'Gravatar enabled' + fill_in 'Home page url', with: 'https://about.gitlab.com/' click_button 'Save' end - step 'I should be see gravatar disabled' do + step 'I should see application settings saved' do current_application_settings.gravatar_enabled.should be_false + current_application_settings.home_page_url.should == 'https://about.gitlab.com/' page.should have_content 'Application settings saved successfully' end end -- cgit v1.2.1 From 38600e328bf4bacf2d3e4288943fb7652ee6c674 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 16 Jan 2015 17:22:17 -0800 Subject: Validate application settings only if column exists --- app/models/application_setting.rb | 7 ++++++- db/schema.rb | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb index d9c73559098..aed4068f309 100644 --- a/app/models/application_setting.rb +++ b/app/models/application_setting.rb @@ -1,6 +1,7 @@ class ApplicationSetting < ActiveRecord::Base validates :home_page_url, allow_blank: true, - format: { with: URI::regexp(%w(http https)), message: "should be a valid url" } + format: { with: URI::regexp(%w(http https)), message: "should be a valid url" }, + if: :home_page_url_column_exist def self.current ApplicationSetting.last @@ -15,4 +16,8 @@ class ApplicationSetting < ActiveRecord::Base sign_in_text: Settings.extra['sign_in_text'], ) end + + def home_page_url_column_exist + ActiveRecord::Base.connection.column_exists?(:application_settings, :home_page_url) + end end diff --git a/db/schema.rb b/db/schema.rb index 96f66ac3634..b453164d712 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -424,7 +424,6 @@ ActiveRecord::Schema.define(version: 20150116234544) do t.integer "notification_level", default: 1, null: false t.datetime "password_expires_at" t.integer "created_by_id" - t.datetime "last_credential_check_at" t.string "avatar" t.string "confirmation_token" t.datetime "confirmed_at" @@ -432,6 +431,7 @@ ActiveRecord::Schema.define(version: 20150116234544) do t.string "unconfirmed_email" t.boolean "hide_no_ssh_key", default: false t.string "website_url", default: "", null: false + t.datetime "last_credential_check_at" t.string "github_access_token" end -- cgit v1.2.1 From f2eb234c068ccb57f100080a499d307b9b2f5502 Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Fri, 16 Jan 2015 18:12:15 -0800 Subject: Fix passign args to original authenticate_user! --- app/controllers/application_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4780a7a2a9a..6da4f91c3f4 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -48,7 +48,7 @@ class ApplicationController < ActionController::Base end end - def authenticate_user! + def authenticate_user!(*args) # If user is not signe-in and tries to access root_path - redirect him to landing page if current_application_settings.home_page_url.present? if current_user.nil? && controller_name == 'dashboard' && action_name == 'show' @@ -56,7 +56,7 @@ class ApplicationController < ActionController::Base end end - super + super(*args) end def log_exception(exception) -- cgit v1.2.1