From bf104980150a374d14238eb4686d6f691b31bfcf Mon Sep 17 00:00:00 2001 From: Jamie Snape Date: Mon, 17 Nov 2014 10:56:12 -0500 Subject: [PATCH] Migrate api module settings to database --- modules/api/configs/module.ini | 19 ++-- modules/api/constant/module.php | 22 ++++ modules/api/controllers/AdminController.php | 68 ++++++++++++ modules/api/controllers/ConfigController.php | 67 ----------- modules/api/controllers/IndexController.php | 4 +- .../InstallScript.php} | 28 ++--- modules/api/database/upgrade/1.1.0.php | 49 +++++++++ modules/api/forms/Admin.php | 46 ++++++++ modules/api/public/js/config/config.index.js | 36 ------ .../api/public/js/config/config.usertab.js | 63 ----------- modules/api/views/admin/index.phtml | 29 +++++ modules/api/views/config/index.phtml | 51 --------- modules/api/views/config/usertab.phtml | 86 --------------- modules/api/views/index/user.phtml | 104 ------------------ 14 files changed, 235 insertions(+), 437 deletions(-) create mode 100644 modules/api/constant/module.php create mode 100644 modules/api/controllers/AdminController.php delete mode 100644 modules/api/controllers/ConfigController.php rename modules/api/{controllers/forms/ConfigForm.php => database/InstallScript.php} (63%) create mode 100644 modules/api/database/upgrade/1.1.0.php create mode 100644 modules/api/forms/Admin.php delete mode 100644 modules/api/public/js/config/config.index.js delete mode 100644 modules/api/public/js/config/config.usertab.js create mode 100644 modules/api/views/admin/index.phtml delete mode 100644 modules/api/views/config/index.phtml delete mode 100644 modules/api/views/config/usertab.phtml delete mode 100644 modules/api/views/index/user.phtml diff --git a/modules/api/configs/module.ini b/modules/api/configs/module.ini index 5d4b98083..fde8ccf2c 100644 --- a/modules/api/configs/module.ini +++ b/modules/api/configs/module.ini @@ -1,13 +1,8 @@ -[global] -; version of the module -version = 1.0.0 -; full name -fullname = Web API -; description -description = "Allows external applications to communicate with MIDAS" -; category -category = Core +; MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0. -; specify the prefix of the method exposed to the client API -; for example: by setting 'midas', the method signature will be 'midas.something.get' -methodprefix = midas +[global] +fullname = "API" +description = "Allow external applications to communicate the server" +category = "Core" +uuid = "4dedda5f-d96a-449d-a504-24e1c070ed18" +version = "1.1.0" diff --git a/modules/api/constant/module.php b/modules/api/constant/module.php new file mode 100644 index 000000000..581435228 --- /dev/null +++ b/modules/api/constant/module.php @@ -0,0 +1,22 @@ +requireAdminPrivileges(); + + $this->view->pageTitle = 'API Module Configuration'; + $form = new Api_Form_Admin(); + + if ($this->getRequest()->isPost()) { + $data = $this->getRequest()->getPost(); + + if ($form->isValid($data)) { + $values = $form->getValues(); + + foreach ($values as $key => $value) { + if ($value !== null) { + $this->Setting->setConfig($key, $value, $this->moduleName); + } + } + } + + $form->populate($data); + } else { + $elements = $form->getElements(); + + foreach ($elements as $element) { + $name = $element->getName(); + + if ($name !== 'csrf' && $name !== 'submit') { + $value = $this->Setting->getValueByName($name, $this->moduleName); + + if (!is_null($value)) { + $form->setDefault($name, $value); + } + } + } + } + + $this->view->form = $form; + session_start(); + } +} diff --git a/modules/api/controllers/ConfigController.php b/modules/api/controllers/ConfigController.php deleted file mode 100644 index dfe6e3fef..000000000 --- a/modules/api/controllers/ConfigController.php +++ /dev/null @@ -1,67 +0,0 @@ -requireAdminPrivileges(); - - $options = array('allowModifications' => true); - if (file_exists(LOCAL_CONFIGS_PATH.'/'.$this->moduleName.'.local.ini')) { - $config = new Zend_Config_Ini( - LOCAL_CONFIGS_PATH.'/'.$this->moduleName.'.local.ini', - 'global', - $options - ); - } else { - $config = new Zend_Config_Ini( - BASE_PATH.'/modules/'.$this->moduleName.'/configs/module.ini', - 'global', - $options - ); - } - - $configForm = $this->ModuleForm->Config->createConfigForm(); - $formArray = $this->getFormAsArray($configForm); - $formArray['methodprefix']->setValue($config->methodprefix); - $this->view->configForm = $formArray; - - if ($this->_request->isPost()) { - $this->_helper->layout->disableLayout(); - $this->_helper->viewRenderer->setNoRender(); - $submitConfig = $this->getParam('submitConfig'); - if (isset($submitConfig)) { - $config->methodprefix = $this->getParam('methodprefix'); - - $writer = new Zend_Config_Writer_Ini(); - $writer->setConfig($config); - $writer->setFilename(LOCAL_CONFIGS_PATH.'/'.$this->moduleName.'.local.ini'); - $writer->write(); - echo JsonComponent::encode(array(true, 'Changed saved')); - } - } - } -} diff --git a/modules/api/controllers/IndexController.php b/modules/api/controllers/IndexController.php index 4c6faf27e..6f687428f 100644 --- a/modules/api/controllers/IndexController.php +++ b/modules/api/controllers/IndexController.php @@ -23,6 +23,7 @@ /** Main controller for the web api module */ class Api_IndexController extends Api_AppController { + public $_models = array('Setting'); public $_moduleComponents = array('Api'); public $kwWebApiCore = null; @@ -40,10 +41,9 @@ public function preDispatch() $this->apiEnable = true; // define api parameters - $modulesConfig = Zend_Registry::get('configsModules'); $this->apiSetup['testing'] = Zend_Registry::get('configGlobal')->environment == 'testing'; $this->apiSetup['tmpDirectory'] = $this->getTempDirectory(); - $this->apiSetup['apiMethodPrefix'] = $modulesConfig['api']->methodprefix; + $this->apiSetup['apiMethodPrefix'] = $this->Setting->getValueByName(API_METHOD_PREFIX_KEY, $this->moduleName); $this->action = $actionName = Zend_Controller_Front::getInstance()->getRequest()->getActionName(); switch ($this->action) { diff --git a/modules/api/controllers/forms/ConfigForm.php b/modules/api/database/InstallScript.php similarity index 63% rename from modules/api/controllers/forms/ConfigForm.php rename to modules/api/database/InstallScript.php index a4b0cbd55..f706656c9 100644 --- a/modules/api/controllers/forms/ConfigForm.php +++ b/modules/api/database/InstallScript.php @@ -18,23 +18,19 @@ limitations under the License. =========================================================================*/ -/** api config form */ -class Api_ConfigForm extends AppForm -{ - /** create form */ - public function createConfigForm() - { - $form = new Zend_Form(); - - $form->setAction($this->webroot.'/api/config/index')->setMethod('post'); - - $methodprefix = new Zend_Form_Element_Text('methodprefix'); +require_once BASE_PATH.'/modules/api/constant/module.php'; - $submit = new Zend_Form_Element_Submit('submitConfig'); - $submit->setLabel('Save configuration'); - - $form->addElements(array($methodprefix, $submit)); +/** Install the api module. */ +class Api_InstallScript extends MIDASModuleInstallScript +{ + /** @var string */ + public $moduleName = 'api'; - return $form; + /** Post database install. */ + public function postInstall() + { + /** @var SettingModel $settingModel */ + $settingModel = MidasLoader::loadModel('Setting'); + $settingModel->setConfig(API_METHOD_PREFIX_KEY, API_METHOD_PREFIX_DEFAULT_VALUE, $this->moduleName); } } diff --git a/modules/api/database/upgrade/1.1.0.php b/modules/api/database/upgrade/1.1.0.php new file mode 100644 index 000000000..1fa183b8c --- /dev/null +++ b/modules/api/database/upgrade/1.1.0.php @@ -0,0 +1,49 @@ +moduleName.'.local.ini'; + + if (file_exists($configPath)) { + $config = new Zend_Config_Ini($configPath, 'global'); + $settingModel->setConfig(API_METHOD_PREFIX_KEY, $config->get('methodprefix', API_METHOD_PREFIX_DEFAULT_VALUE), $this->moduleName); + + $config = new Zend_Config_Ini($configPath, null, true); + unset($config->global->methodprefix); + + $writer = new Zend_Config_Writer_Ini(); + $writer->setConfig($config); + $writer->setFilename($configPath); + $writer->write(); + } else { + $settingModel->setConfig(API_METHOD_PREFIX_KEY, API_METHOD_PREFIX_DEFAULT_VALUE, $this->moduleName); + } + } +} diff --git a/modules/api/forms/Admin.php b/modules/api/forms/Admin.php new file mode 100644 index 000000000..af32058ab --- /dev/null +++ b/modules/api/forms/Admin.php @@ -0,0 +1,46 @@ +setName('api_admin'); + $this->setMethod('POST'); + + $csrf = new Midas_Form_Element_Hash('csrf'); + $csrf->setSalt('7ASBLg59r2KdBHbANpgJ7Vev'); + $csrf->setDecorators(array('ViewHelper')); + + $methodPrefix = new Zend_Form_Element_Text(API_METHOD_PREFIX_KEY); + $methodPrefix->setLabel('Method Prefix'); + $methodPrefix->setRequired(true); + $methodPrefix->addValidator('NotEmpty', true); + + $this->addDisplayGroup(array($methodPrefix), 'global'); + + $submit = new Zend_Form_Element_Submit('submit'); + $submit->setLabel('Save'); + + $this->addElements(array($csrf, $methodPrefix, $submit)); + } +} diff --git a/modules/api/public/js/config/config.index.js b/modules/api/public/js/config/config.index.js deleted file mode 100644 index 7562617df..000000000 --- a/modules/api/public/js/config/config.index.js +++ /dev/null @@ -1,36 +0,0 @@ -// MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0. - -var midas = midas || {}; -midas.api = midas.api || {}; - -midas.api.validateConfig = function (formData, jqForm, options) {}; - -midas.api.successConfig = function (responseText, statusText, xhr, form) { - 'use strict'; - var jsonResponse; - try { - jsonResponse = $.parseJSON(responseText); - } - catch (e) { - midas.createNotice("An error occured. Please check the logs.", 4000, 'error'); - return false; - } - if (jsonResponse === null) { - midas.createNotice('Error', 4000, 'error'); - return; - } - if (jsonResponse[0]) { - midas.createNotice(jsonResponse[1], 4000); - } - else { - midas.createNotice(jsonResponse[1], 4000, 'error'); - } -}; - -$(document).ready(function () { - 'use strict'; - $('#configForm').ajaxForm({ - beforeSubmit: midas.api.validateConfig, - success: midas.api.successConfig - }); -}); diff --git a/modules/api/public/js/config/config.usertab.js b/modules/api/public/js/config/config.usertab.js deleted file mode 100644 index 4f1f5a0c7..000000000 --- a/modules/api/public/js/config/config.usertab.js +++ /dev/null @@ -1,63 +0,0 @@ -// MIDAS Server. Copyright Kitware SAS. Licensed under the Apache License 2.0. - -/* global json */ - -var midas = midas || {}; -midas.api = midas.api || {}; - -midas.api.validateApiConfig = function (formData, jqForm, options) {}; - -midas.api.successApiConfig = function (responseText, statusText, xhr, form) { - 'use strict'; - var jsonResponse; - try { - jsonResponse = $.parseJSON(responseText); - } - catch (e) { - midas.createNotice("An error occured. Please check the logs.", 4000, 'error'); - return false; - } - if (jsonResponse === null) { - midas.createNotice('Error', 4000, 'error'); - return; - } - if (jsonResponse[0]) { - midas.createNotice(jsonResponse[1], 4000); - $('#tabsSettings').tabs('load', $('#tabsSettings').tabs('option', 'selected')); // reload tab - } - else { - midas.createNotice(jsonResponse[1], 4000, 'error'); - } -}; - -$(document).ready(function () { - 'use strict'; - $('#generateKeyForm').ajaxForm({ - beforeSubmit: midas.api.validateApiConfig, - success: midas.api.successApiConfig - }); - - $('a.deleteApiKeyLink').click(function () { - var obj = $(this); - $.post(json.global.webroot + '/api/config/usertab', { - deleteAPIKey: true, - element: $(this).attr('element'), - userId: $('#apiUserId').val() - }, - function (data) { - var jsonResponse = $.parseJSON(data); - if (jsonResponse === null) { - midas.createNotice('Error', 4000, 'error'); - return; - } - if (jsonResponse[0]) { - midas.createNotice(jsonResponse[1], 2000); - obj.parents('tr').remove(); - } - else { - midas.createNotice(jsonResponse[1], 4000, 'error'); - } - } - ); - }); -}); diff --git a/modules/api/views/admin/index.phtml b/modules/api/views/admin/index.phtml new file mode 100644 index 000000000..bfd77d837 --- /dev/null +++ b/modules/api/views/admin/index.phtml @@ -0,0 +1,29 @@ +declareVars('form', 'pageTitle'); +$this->headTitle($this->escape($this->pageTitle)); +?> + +
+

escape($this->pageTitle); ?>

+ form; ?> +

« Back to Modules Administration

+
diff --git a/modules/api/views/config/index.phtml b/modules/api/views/config/index.phtml deleted file mode 100644 index 560f8a888..000000000 --- a/modules/api/views/config/index.phtml +++ /dev/null @@ -1,51 +0,0 @@ -headScript()->appendFile($this->coreWebroot.'/public/js/jquery/jquery.form.js'); -$this->headScript()->appendFile($this->moduleWebroot.'/public/js/config/config.index.js'); -?> - - -
-
- '; - echo ""; - echo $this->t('Back'); - echo ""; - echo '
'; - - ?> -
- configForm['method']}' action='{$this->configForm['action']}'> -

API Configuration:

-
- - {$this->configForm['methodprefix']} -
-
- {$this->configForm['submitConfig']} -
- "; - ?> - diff --git a/modules/api/views/config/usertab.phtml b/modules/api/views/config/usertab.phtml deleted file mode 100644 index a6905fbef..000000000 --- a/modules/api/views/config/usertab.phtml +++ /dev/null @@ -1,86 +0,0 @@ -moduleWebroot.'/public/js/config/config.usertab.js">'; -?> - -

t('Generated API keys') ?>

-userapiDaos) == 0) { - echo "You do not have any API keys.
"; -} else { - ?> - - - - - - - - - userapiDaos as $userapiDao) { - ?> - - - - - - - - -
-
t('Application Name') ?> -
-
-
API Key
-
-
t('Default Expiration') ?> -
-
-
Creation
-
-
Action
-
getApplicationName(); ?>getApikey(); ?>getTokenExpirationTime(); ?> minutes - Date->ago($userapiDao->getCreationDate()); ?> - t('delete') ?> -
- - -Web API Information - -
-

t('Generate New API Key') ?>

- -
- - -
- - form['appplication_name'] ?> -
-
- - form['expiration'] ?> -
-
- form['createAPIKey'] ?> -
-
diff --git a/modules/api/views/index/user.phtml b/modules/api/views/index/user.phtml deleted file mode 100644 index 95659fdab..000000000 --- a/modules/api/views/index/user.phtml +++ /dev/null @@ -1,104 +0,0 @@ - -

Web API Configuration

-> My Midas -*".$error; -} ?> -

-Web API URL: webroot.'api/rest' ?> -

-
-

Generated API keys

- - - - - - - - - - - - - - - - - - -
-
Application Name
-
-
API Key
-
-
Default expiration
-
-
Date
-
-
Action
-
minutes - [remove] -
- - -
-

Generate New API key

-
- - - - - - - - - - - - - -
-
Application Name:
-
input('API.applicationName', array('size' => '40', 'label' => '')) ?> -
-
- Default Token Expiration Time (minutes): -
-
input( - 'API.experiationtime', - array('size' => '5', 'label' => '', 'value' => '60') - ) ?> -
  - -
-