Add api and api setting feature.

This commit is contained in:
BoHung Chiu 2022-04-10 17:14:13 +08:00
parent 6d33769ee6
commit 1d7149180c
7 changed files with 105 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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| %>
<fieldset>
<!-- Input Area -->
<div class="input-area">
<!-- Module Tabs -->
<div class="nav-name"><strong><%= t(:module) %></strong></div>
<ul class="nav nav-pills module-nav">
<li class="active">
<a href="#basic" data-toggle="tab"><%= t(:basic) %></a>
</li>
</ul>
<!-- Module -->
<div class="tab-content module-area">
<!-- Basic Module -->
<div class="tab-pane fade in active" id="basic">
<div class="control-group">
<%= f.label :enable_api , t("client_management.enable_api"), :class => "control-label muted" %>
<div class="controls">
<%= f.check_box :enable_api %>
</div>
</div>
<div class="control-group">
<%= f.label :api_key , t("client_management.api_key"), :class => "control-label muted" %>
<div class="controls">
<%= f.text_field :api_key %>
</div>
</div>
</div>
</div>
<div class="form-actions">
<%= f.submit t('submit'), class: 'btn btn-primary' %>
</div>
</div>
</fieldset>
<% end %>

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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,