From 8589b4e137f50293952923bb07e2814257d7784d Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Thu, 8 Jan 2015 00:22:50 -0800
Subject: [PATCH] Init ApplicationSettings resource with defaults from config
 file

---
 .../admin/application_settings_controller.rb  | 31 +++++++++++++++++++
 app/controllers/registrations_controller.rb   |  4 ++-
 app/helpers/application_helper.rb             |  8 +++++
 app/helpers/application_settings_helper.rb    |  2 ++
 app/models/application_setting.rb             |  5 +++
 app/services/gravatar_service.rb              |  2 +-
 .../application_settings/_form.html.haml      | 29 +++++++++++++++++
 .../admin/application_settings/edit.html.haml |  5 +++
 .../admin/application_settings/show.html.haml | 18 +++++++++++
 app/views/devise/sessions/new.html.haml       |  2 +-
 app/views/devise/shared/_signin_box.html.haml |  6 ++--
 config/initializers/8_application_settings.rb | 12 +++++++
 config/routes.rb                              |  2 ++
 ...50108073740_create_application_settings.rb | 13 ++++++++
 db/schema.rb                                  | 12 ++++++-
 15 files changed, 144 insertions(+), 7 deletions(-)
 create mode 100644 app/controllers/admin/application_settings_controller.rb
 create mode 100644 app/helpers/application_settings_helper.rb
 create mode 100644 app/models/application_setting.rb
 create mode 100644 app/views/admin/application_settings/_form.html.haml
 create mode 100644 app/views/admin/application_settings/edit.html.haml
 create mode 100644 app/views/admin/application_settings/show.html.haml
 create mode 100644 config/initializers/8_application_settings.rb
 create mode 100644 db/migrate/20150108073740_create_application_settings.rb

diff --git a/app/controllers/admin/application_settings_controller.rb b/app/controllers/admin/application_settings_controller.rb
new file mode 100644
index 00000000000..d6e950b0007
--- /dev/null
+++ b/app/controllers/admin/application_settings_controller.rb
@@ -0,0 +1,31 @@
+class Admin::ApplicationSettingsController < Admin::ApplicationController
+  before_filter :set_application_setting
+
+  def show
+  end
+
+  def edit
+  end
+
+  def update
+    @application_setting.update_attributes(application_setting_params)
+
+    redirect_to admin_application_settings_path
+  end
+
+  private
+
+  def set_application_setting
+    @application_setting = ApplicationSetting.last
+  end
+
+  def application_setting_params
+    params.require(:application_setting).permit(
+      :default_projects_limit,
+      :signup_enabled,
+      :signin_enabled,
+      :gravatar_enabled,
+      :sign_in_text,
+    )
+  end
+end
diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb
index 6d3214b70a8..7c15eab4345 100644
--- a/app/controllers/registrations_controller.rb
+++ b/app/controllers/registrations_controller.rb
@@ -26,7 +26,9 @@ class RegistrationsController < Devise::RegistrationsController
   private
 
   def signup_enabled?
-    redirect_to new_user_session_path unless Gitlab.config.gitlab.signup_enabled
+    unless ApplicationSetting.current.signup_enabled
+      redirect_to new_user_session_path
+    end
   end
 
   def sign_up_params
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index f21b0bd1f50..c339b3597ec 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -310,4 +310,12 @@ module ApplicationHelper
     request.env['rack.session']['user_return_to'] ==
       '/'
   end
+
+  def signup_enabled?
+    ApplicationSetting.current.signup_enabled
+  end
+
+  def signin_enabled?
+    ApplicationSetting.current.signin_enabled
+  end
 end
diff --git a/app/helpers/application_settings_helper.rb b/app/helpers/application_settings_helper.rb
new file mode 100644
index 00000000000..bb39a3cf4f0
--- /dev/null
+++ b/app/helpers/application_settings_helper.rb
@@ -0,0 +1,2 @@
+module ApplicationSettingsHelper
+end
diff --git a/app/models/application_setting.rb b/app/models/application_setting.rb
new file mode 100644
index 00000000000..4b885461cbb
--- /dev/null
+++ b/app/models/application_setting.rb
@@ -0,0 +1,5 @@
+class ApplicationSetting < ActiveRecord::Base
+  def self.current
+    ApplicationSetting.last
+  end
+end
diff --git a/app/services/gravatar_service.rb b/app/services/gravatar_service.rb
index a69c7c78377..d8c9436aaa5 100644
--- a/app/services/gravatar_service.rb
+++ b/app/services/gravatar_service.rb
@@ -1,6 +1,6 @@
 class GravatarService
   def execute(email, size = nil)
-    if gravatar_config.enabled && email.present?
+    if ApplicationSetting.current.gravatar_enabled && email.present?
       size = 40 if size.nil? || size <= 0
 
       sprintf gravatar_url,
diff --git a/app/views/admin/application_settings/_form.html.haml b/app/views/admin/application_settings/_form.html.haml
new file mode 100644
index 00000000000..846d74d433d
--- /dev/null
+++ b/app/views/admin/application_settings/_form.html.haml
@@ -0,0 +1,29 @@
+= form_for @application_setting, url: admin_application_settings_path, html: { class: 'form-horizontal fieldset-form' } do |f|
+  - if @application_setting.errors.any?
+    #error_explanation
+      .alert.alert-danger
+        - @application_setting.errors.full_messages.each do |msg|
+          %p= msg
+
+  .form-group
+    = f.label :default_projects_limit, class: 'control-label'
+    .col-sm-10
+      = f.number_field :default_projects_limit, class: 'form-control'
+  .form-group
+    = f.label :signup_enabled, class: 'control-label'
+    .col-sm-10
+      = f.check_box :signup_enabled, class: 'checkbox'
+  .form-group
+    = f.label :signin_enabled, class: 'control-label'
+    .col-sm-10
+      = f.check_box :signin_enabled, class: 'checkbox'
+  .form-group
+    = f.label :gravatar_enabled, class: 'control-label'
+    .col-sm-10
+      = f.check_box :gravatar_enabled, class: 'checkbox'
+  .form-group
+    = f.label :sign_in_text, class: 'control-label'
+    .col-sm-10
+      = f.text_area :sign_in_text, class: 'form-control'
+  .form-actions
+    = f.submit 'Save', class: 'btn btn-primary'
diff --git a/app/views/admin/application_settings/edit.html.haml b/app/views/admin/application_settings/edit.html.haml
new file mode 100644
index 00000000000..62c0617ca4f
--- /dev/null
+++ b/app/views/admin/application_settings/edit.html.haml
@@ -0,0 +1,5 @@
+%h1 Editing application_setting
+
+= render 'form'
+
+= link_to 'Back', admin_application_settings_path
diff --git a/app/views/admin/application_settings/show.html.haml b/app/views/admin/application_settings/show.html.haml
new file mode 100644
index 00000000000..1c77886546d
--- /dev/null
+++ b/app/views/admin/application_settings/show.html.haml
@@ -0,0 +1,18 @@
+%table.table
+  %tr
+    %td Default projects limit:
+    %td= @application_setting.default_projects_limit
+  %tr
+    %td Signup enabled:
+    %td= @application_setting.signup_enabled
+  %tr
+    %td Signin enabled:
+    %td= @application_setting.signin_enabled
+  %tr
+    %td Gravatar enabled:
+    %td= @application_setting.gravatar_enabled
+  %tr
+    %td Sign in text:
+    %td= @application_setting.sign_in_text
+
+= link_to 'Edit', edit_admin_application_settings_path
diff --git a/app/views/devise/sessions/new.html.haml b/app/views/devise/sessions/new.html.haml
index 5e31d8e818a..6d8415613d1 100644
--- a/app/views/devise/sessions/new.html.haml
+++ b/app/views/devise/sessions/new.html.haml
@@ -5,7 +5,7 @@
     .prepend-top-20
       = render 'devise/shared/oauth_box'
 
-  - if gitlab_config.signup_enabled
+  - if signup_enabled?
     .prepend-top-20
       = render 'devise/shared/signup_box'
 
diff --git a/app/views/devise/shared/_signin_box.html.haml b/app/views/devise/shared/_signin_box.html.haml
index 3f2161ff6a4..70587329033 100644
--- a/app/views/devise/shared/_signin_box.html.haml
+++ b/app/views/devise/shared/_signin_box.html.haml
@@ -7,18 +7,18 @@
         - @ldap_servers.each_with_index do |server, i|
           %li{class: (:active if i.zero?)}
             = link_to server['label'], "#tab-#{server['provider_name']}", 'data-toggle' => 'tab'
-        - if gitlab_config.signin_enabled
+        - if signin_enabled?
           %li
             = link_to 'Standard', '#tab-signin', 'data-toggle' => 'tab'
       .tab-content
         - @ldap_servers.each_with_index do |server, i|
           %div.tab-pane{id: "tab-#{server['provider_name']}", class: (:active if i.zero?)}
             = render 'devise/sessions/new_ldap', provider: server['provider_name']
-        - if gitlab_config.signin_enabled
+        - if signin_enabled?
           %div#tab-signin.tab-pane
             = render 'devise/sessions/new_base'
 
-    - elsif gitlab_config.signin_enabled
+    - elsif signin_enabled?
       = render 'devise/sessions/new_base'
     - else
       %div
diff --git a/config/initializers/8_application_settings.rb b/config/initializers/8_application_settings.rb
new file mode 100644
index 00000000000..c4706756b64
--- /dev/null
+++ b/config/initializers/8_application_settings.rb
@@ -0,0 +1,12 @@
+begin
+  unless ApplicationSetting.any?
+    ApplicationSetting.create(
+      default_projects_limit: Settings.gitlab['default_projects_limit'],
+      signup_enabled: Settings.gitlab['signup_enabled'],
+      signin_enabled: Settings.gitlab['signin_enabled'],
+      gravatar_enabled: Settings.gravatar['enabled'],
+      sign_in_text: Settings.extra['sign_in_text'],
+    )
+  end
+rescue
+end
diff --git a/config/routes.rb b/config/routes.rb
index d36540024aa..7760f32dc36 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -109,6 +109,8 @@ Gitlab::Application.routes.draw do
       end
     end
 
+    resource :application_settings
+
     root to: "dashboard#index"
   end
 
diff --git a/db/migrate/20150108073740_create_application_settings.rb b/db/migrate/20150108073740_create_application_settings.rb
new file mode 100644
index 00000000000..651e35fdf7a
--- /dev/null
+++ b/db/migrate/20150108073740_create_application_settings.rb
@@ -0,0 +1,13 @@
+class CreateApplicationSettings < ActiveRecord::Migration
+  def change
+    create_table :application_settings do |t|
+      t.integer :default_projects_limit
+      t.boolean :signup_enabled
+      t.boolean :signin_enabled
+      t.boolean :gravatar_enabled
+      t.text :sign_in_text
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index cb945e71665..6cdff168742 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,11 +11,21 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20141226080412) do
+ActiveRecord::Schema.define(version: 20150108073740) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
 
+  create_table "application_settings", force: true do |t|
+    t.integer  "default_projects_limit"
+    t.boolean  "signup_enabled"
+    t.boolean  "signin_enabled"
+    t.boolean  "gravatar_enabled"
+    t.text     "sign_in_text"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
+
   create_table "broadcast_messages", force: true do |t|
     t.text     "message",    null: false
     t.datetime "starts_at"
-- 
GitLab