From 1d7149180ce493f855a5282f5d8e6765bb2f9b9a Mon Sep 17 00:00:00 2001 From: Bohung Date: Sun, 10 Apr 2022 17:14:13 +0800 Subject: [PATCH] Add api and api setting feature. --- .../admin/site_panel_controller.rb | 45 ++++++++++++++++++- app/models/client_manager_setting.rb | 6 +++ app/views/admin/site_panel/setting.erb | 42 +++++++++++++++++ config/locales/en.yml | 3 ++ config/locales/zh_tw.yml | 3 ++ config/routes.rb | 2 + lib/client_management/engine.rb | 5 +++ 7 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 app/models/client_manager_setting.rb create mode 100644 app/views/admin/site_panel/setting.erb diff --git a/app/controllers/admin/site_panel_controller.rb b/app/controllers/admin/site_panel_controller.rb index 219aac3..bd642cc 100644 --- a/app/controllers/admin/site_panel_controller.rb +++ b/app/controllers/admin/site_panel_controller.rb @@ -4,6 +4,49 @@ class Admin::SitePanelController < OrbitAdminController super @app_title = "client_management" end + def can_use + ClientManagerSetting.create if ClientManagerSetting.count == 0 + setting = ClientManagerSetting.first + if setting.enable_api && setting.api_key == params[:api_key] + @allow_api = true + if params[:id].blank? && params[:domain_name].present? + params[:id] = SiteConstruct.where(:domain_name=>/#{params[:domain_name].gsub(".","\\.")}/).first.id rescue nil + end + return true + else + return super + end + end + def current_user + if @allow_api + return User.where(:user_name=>'admin').first + else + return super + end + end + def authenticate_user + ClientManagerSetting.create if ClientManagerSetting.count == 0 + setting = ClientManagerSetting.first + if setting.enable_api && setting.api_key == params[:api_key] + @allow_api = true + if params[:id].blank? && params[:domain_name].present? + params[:id] = SiteConstruct.where(:domain_name=>/#{params[:domain_name].gsub(".","\\.")}/).first.id rescue nil + end + return true + else + return super + end + end + def setting + ClientManagerSetting.create if ClientManagerSetting.count == 0 + @setting = ClientManagerSetting.first + end + def update_setting + setting_params = params.require(:client_manager_setting).permit! + setting = ClientManagerSetting.first + setting.update_attributes(setting_params) + redirect_to :back + end def upload_cert @site_cert = SiteCert.new end @@ -225,7 +268,7 @@ class Admin::SitePanelController < OrbitAdminController Thread.new do system("bundle exec rake exec_commands:exec_commands[#{params[:id]},,close_site]") end - elsif params[:type] == 'open' + elsif params[:type] == 'open' || params[:type] == 'restart' Thread.new do system("bundle exec rake exec_commands:exec_commands[#{params[:id]},,open_site,,#{params[:env]}]") end diff --git a/app/models/client_manager_setting.rb b/app/models/client_manager_setting.rb new file mode 100644 index 0000000..8ccb69d --- /dev/null +++ b/app/models/client_manager_setting.rb @@ -0,0 +1,6 @@ +class ClientManagerSetting + include Mongoid::Document + include Mongoid::Timestamps + field :enable_api, type: Boolean, default: false + field :api_key, type: String, default: "client-manager-api-key" +end \ No newline at end of file diff --git a/app/views/admin/site_panel/setting.erb b/app/views/admin/site_panel/setting.erb new file mode 100644 index 0000000..3553226 --- /dev/null +++ b/app/views/admin/site_panel/setting.erb @@ -0,0 +1,42 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "lib/main-forms" %> +<% end %> +<% content_for :page_specific_javascript do %> +<% end %> +<%= form_for @setting, :url => {:action=>"update_setting"}, :html => {:class => 'form-horizontal main-forms'} do |f| %> +
+ +
+ + + + + + +
+ +
+
+ <%= f.label :enable_api , t("client_management.enable_api"), :class => "control-label muted" %> +
+ <%= f.check_box :enable_api %> +
+
+
+ <%= f.label :api_key , t("client_management.api_key"), :class => "control-label muted" %> +
+ <%= f.text_field :api_key %> +
+
+
+
+
+ <%= f.submit t('submit'), class: 'btn btn-primary' %> +
+
+
+<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index 1fef05a..bd09737 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -7,6 +7,9 @@ en: upload_cert: Upload Cert cert_management: Cert Management client_management: + enable_api: Enable API + api_key: API Key + setting: Setting start_site_in_env: "Start site in %{env}" upgrade_site: "Upgrade site" bundle_update: "Bundle update" diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index deece3c..30eb136 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -7,6 +7,9 @@ zh_tw: upload_cert: 上傳憑證 cert_management: 憑證管理 client_management: + enable_api: 開啟API + api_key: API Key + setting: 設定 start_site_in_env: "開啟網站為%{env}" upgrade_site: 更新網站 bundle_update: "Bundle update" diff --git a/config/routes.rb b/config/routes.rb index 42c60f9..75512c1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -36,6 +36,8 @@ Rails.application.routes.draw do get "contracts" end end + get "site_panel/setting" => "site_panel#setting" + patch "site_panel/update_setting" => "site_panel#update_setting" get "site_panel/edit_site" => "site_panel#edit_site" post "site_panel/edit_site" => "site_panel#edit_site" get "site_panel/site_infos" => "site_panel#site_infos" diff --git a/lib/client_management/engine.rb b/lib/client_management/engine.rb index 12f453b..9124d35 100644 --- a/lib/client_management/engine.rb +++ b/lib/client_management/engine.rb @@ -38,6 +38,11 @@ module ClientManagement :active_for_action=>{'admin/client_managements'=>"contracts"}, :available_for => 'admin' + context_link 'client_management.setting', + :link_path=>"admin_site_panel_setting_path" , + :priority=>1, + :active_for_action=>{'admin/site_panel'=>"setting"}, + :available_for => 'admin' context_link 'client_management.cert_management', :link_path=>"cert_management_admin_site_panel_index_path" , :priority=>1,