Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Migrate api module settings to database
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Snape committed Dec 5, 2014
1 parent 2008a86 commit bf10498
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 437 deletions.
19 changes: 7 additions & 12 deletions modules/api/configs/module.ini
Original file line number Diff line number Diff line change
@@ -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"
22 changes: 22 additions & 0 deletions modules/api/constant/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

define('API_METHOD_PREFIX_KEY', 'method_prefix');
define('API_METHOD_PREFIX_DEFAULT_VALUE', 'midas');
68 changes: 68 additions & 0 deletions modules/api/controllers/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Admin controller for the api module. */
class Api_AdminController extends Api_AppController
{
/** @var array */
public $_models = array('Setting');

/** Index action */
public function indexAction()
{
$this->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();
}
}
67 changes: 0 additions & 67 deletions modules/api/controllers/ConfigController.php

This file was deleted.

4 changes: 2 additions & 2 deletions modules/api/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
49 changes: 49 additions & 0 deletions modules/api/database/upgrade/1.1.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Upgrade the api module to version 1.1.0. */
class Api_Upgrade_1_1_0 extends MIDASUpgrade
{
/** @var string */
public $moduleName = 'api';

/** Post database upgrade. */
public function postUpgrade()
{
/** @var SettingModel $settingModel */
$settingModel = MidasLoader::loadModel('Setting');
$configPath = LOCAL_CONFIGS_PATH.DIRECTORY_SEPARATOR.$this->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);
}
}
}
46 changes: 46 additions & 0 deletions modules/api/forms/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/*=========================================================================
MIDAS Server
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
All rights reserved.
More information http://www.kitware.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0.txt
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=========================================================================*/

/** Admin form for the api module. */
class Api_Form_Admin extends Zend_Form
{
/** Initialize this form. */
public function init()
{
$this->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));
}
}
36 changes: 0 additions & 36 deletions modules/api/public/js/config/config.index.js

This file was deleted.

Loading

0 comments on commit bf10498

Please sign in to comment.