From c2ee6862c8538c967ba04fa72b702198f31f876c Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Fri, 3 Jul 2015 16:50:21 +0200
Subject: [PATCH] API to set application settings for admin

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
---
 lib/api/api.rb      |  1 +
 lib/api/entities.rb | 22 ++++++++++++++++++++++
 lib/api/settings.rb | 35 +++++++++++++++++++++++++++++++++++
 3 files changed, 58 insertions(+)
 create mode 100644 lib/api/settings.rb

diff --git a/lib/api/api.rb b/lib/api/api.rb
index d2a35c78fc1..eebd44ea5b6 100644
--- a/lib/api/api.rb
+++ b/lib/api/api.rb
@@ -49,5 +49,6 @@ module API
     mount Namespaces
     mount Branches
     mount Labels
+    mount Settings
   end
 end
diff --git a/lib/api/entities.rb b/lib/api/entities.rb
index 14a8f929d76..31202fa8c1f 100644
--- a/lib/api/entities.rb
+++ b/lib/api/entities.rb
@@ -277,5 +277,27 @@ module API
     class BroadcastMessage < Grape::Entity
       expose :message, :starts_at, :ends_at, :color, :font
     end
+
+    class ApplicationSetting < Grape::Entity
+      expose :id
+      expose :default_projects_limit
+      expose :signup_enabled
+      expose :signin_enabled
+      expose :gravatar_enabled
+      expose :sign_in_text
+      expose :created_at
+      expose :updated_at
+      expose :home_page_url
+      expose :default_branch_protection
+      expose :twitter_sharing_enabled
+      expose :restricted_visibility_levels
+      expose :max_attachment_size
+      expose :session_expire_delay
+      expose :default_project_visibility
+      expose :default_snippet_visibility
+      expose :restricted_signup_domains
+      expose :user_oauth_applications
+      expose :after_sign_out_path
+    end
   end
 end
diff --git a/lib/api/settings.rb b/lib/api/settings.rb
new file mode 100644
index 00000000000..28c7a05c629
--- /dev/null
+++ b/lib/api/settings.rb
@@ -0,0 +1,35 @@
+module API
+  class Settings < Grape::API
+    before { authenticate! }
+    before { authorize_admin_project }
+
+    helpers do
+      def current_settings
+        @current_setting ||= ApplicationSetting.current
+      end
+    end
+
+    # Get current applicaiton settings
+    #
+    # Example Request:
+    #   GET /application/settings
+    get "application/settings" do
+      present current_settings, with: Entities::ApplicationSetting
+    end
+
+    # Modify applicaiton settings
+    #
+    # Example Request:
+    #   PUT /application/settings
+    put "application/settings" do
+      attributes = current_settings.attributes.keys - ["id"]
+      attrs = attributes_for_keys(attributes)
+
+      if current_settings.update_attributes(attrs)
+        present current_settings, with: Entities::ApplicationSetting
+      else
+        render_validation_error!(current_settings)
+      end
+    end
+  end
+end
-- 
2.30.9