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

Commit 34913f8

Browse files
committed
ENH: refs #256. Add a validation helper method for the core web api methods
1 parent 8ccaf77 commit 34913f8

File tree

1 file changed

+38
-96
lines changed

1 file changed

+38
-96
lines changed

modules/api/controllers/components/ApiComponent.php

Lines changed: 38 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ class Api_ApiComponent extends AppComponent
2525
public $apiSetup;
2626
public $userSession;
2727

28+
/**
29+
* Pass the args and a list of required parameters.
30+
* Will throw an exception if a required one is missing.
31+
*/
32+
private function _validateParams($args, $requiredList)
33+
{
34+
foreach($requiredList as $param)
35+
{
36+
if(!array_key_exists(&$args, $param))
37+
{
38+
throw new Exception('Parameter '.$param.' is not defined', MIDAS_INVALID_PARAMETER);
39+
}
40+
}
41+
}
42+
2843
/** Return the user dao */
2944
private function _getUser($args)
3045
{
@@ -62,20 +77,7 @@ public function info($args)
6277
*/
6378
function login($args)
6479
{
65-
if(!array_key_exists('email', $args))
66-
{
67-
throw new Exception('Parameter email is not defined', MIDAS_INVALID_PARAMETER);
68-
}
69-
70-
if(!array_key_exists('appname', $args))
71-
{
72-
throw new Exception('Parameter appname is not defined', MIDAS_INVALID_PARAMETER);
73-
}
74-
75-
if(!array_key_exists('apikey', $args))
76-
{
77-
throw new Exception('Parameter apikey is not defined', MIDAS_INVALID_PARAMETER);
78-
}
80+
$this->_validateParams(&$args, array('email', 'appname', 'apikey'));
7981

8082
$data['token'] = '';
8183
$email = $args['email'];
@@ -100,14 +102,7 @@ function login($args)
100102
*/
101103
public function uuidGet($args)
102104
{
103-
if(!array_key_exists('id', $args))
104-
{
105-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
106-
}
107-
if(!array_key_exists('type', $args))
108-
{
109-
throw new Exception('Parameter type is not defined', MIDAS_INVALID_PARAMETER);
110-
}
105+
$this->_validateParams(&$args, array('id', 'type'));
111106

112107
$id = $args['id'];
113108
$type = $args['type'];
@@ -162,10 +157,7 @@ public function uuidGet($args)
162157
*/
163158
function resourceGet($args)
164159
{
165-
if(!array_key_exists('uuid', $args))
166-
{
167-
throw new Exception('Parameter uuid is not defined', MIDAS_INVALID_PARAMETER);
168-
}
160+
$this->_validateParams(&$args, array('uuid'));
169161

170162
$uuid = $args['uuid'];
171163
$componentLoader = new MIDAS_ComponentLoader();
@@ -197,10 +189,7 @@ function pathFromRoot($args)
197189
*/
198190
function pathToRoot($args)
199191
{
200-
if(!array_key_exists('uuid', $args))
201-
{
202-
throw new Exception('Parameter uuid is not defined', MIDAS_INVALID_PARAMETER);
203-
}
192+
$this->_validateParams(&$args, array('uuid'));
204193

205194
$componentLoader = new MIDAS_ComponentLoader();
206195
$uuidComponent = $componentLoader->loadComponent('Uuid');
@@ -236,10 +225,7 @@ function pathToRoot($args)
236225
*/
237226
function resourceSearch($args)
238227
{
239-
if(!array_key_exists('search', $args))
240-
{
241-
throw new Exception('Parameter search is not defined', MIDAS_INVALID_PARAMETER);
242-
}
228+
$this->_validateParams(&$args, array('search'));
243229
$userDao = $this->_getUser($args);
244230

245231
$order = 'view';
@@ -399,25 +385,21 @@ function uploadPerform($args)
399385
* Create a new community
400386
* @param token Authentication token
401387
* @param name The community name
402-
* @param description The community description
388+
* @param description (Optional) The community description
403389
* @param uuid (Optional) Uuid of the community. If none is passed, will generate one.
404390
* @param privacy (Optional) Default 'Public'.
405391
* @param canjoin (Optional) Default 'Everyone'.
406392
* @return The community dao that was created
407393
*/
408394
function communityCreate($args)
409395
{
396+
$this->_validateParams(&$args, array('name'));
410397
$userDao = $this->_getUser($args);
411398
if($userDao == false)
412399
{
413400
throw new Exception('Unable to find user', MIDAS_INVALID_POLICY);
414401
}
415402

416-
if(!array_key_exists('name', $args))
417-
{
418-
throw new Exception('Parameter name is not defined', MIDAS_INVALID_PARAMETER);
419-
}
420-
421403
$name = $args['name'];
422404
$uuid = isset($args['uuid']) ? $args['uuid'] : '';
423405

@@ -546,18 +528,17 @@ function communityList($args)
546528
*/
547529
function communityDelete($args)
548530
{
549-
if(!array_key_exists('id', $args))
550-
{
551-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
552-
}
553-
$modelLoader = new MIDAS_ModelLoader();
554-
$communityModel = $modelLoader->loadModel('Community');
531+
$this->_validateParams(&$args, array('id'));
532+
555533
$userDao = $this->_getUser($args);
556534
if($userDao == false)
557535
{
558536
throw new Exception('Unable to find user', MIDAS_INVALID_TOKEN);
559537
}
560538
$id = $args['id'];
539+
540+
$modelLoader = new MIDAS_ModelLoader();
541+
$communityModel = $modelLoader->loadModel('Community');
561542
$community = $communityModel->load($id);
562543

563544
if($community === false || !$communityModel->policyCheck($community, $userDao, MIDAS_POLICY_ADMIN))
@@ -572,29 +553,21 @@ function communityDelete($args)
572553
* Create a folder
573554
* @param token Authentication token
574555
* @param name The name of the folder to create
575-
* @param description The description of the folder
556+
* @param description (Optional) The description of the folder
576557
* @param uuid (Optional) Uuid of the folder. If none is passed, will generate one.
577558
* @param privacy (Optional) Default 'Public'.
578559
* @param parentid The id of the parent folder
579560
* @return The folder object that was created
580561
*/
581562
function folderCreate($args)
582563
{
564+
$this->_validateParams(&$args, array('name'));
583565
$userDao = $this->_getUser($args);
584566
if($userDao == false)
585567
{
586568
throw new Exception('Unable to find user', MIDAS_INVALID_TOKEN);
587569
}
588570

589-
if(!array_key_exists('name', $args))
590-
{
591-
throw new Exception('Parameter name is not defined', MIDAS_INVALID_PARAMETER);
592-
}
593-
if(!array_key_exists('description', $args))
594-
{
595-
throw new Exception('Parameter name is not defined', MIDAS_INVALID_PARAMETER);
596-
}
597-
598571
$modelLoader = new MIDAS_ModelLoader();
599572
$folderModel = $modelLoader->loadModel('Folder');
600573
$name = $args['name'];
@@ -670,10 +643,7 @@ function folderCreate($args)
670643
*/
671644
function folderGet($args)
672645
{
673-
if(!array_key_exists('id', $args))
674-
{
675-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
676-
}
646+
$this->_validateParams(&$args, array('id'));
677647
$userDao = $this->_getUser($args);
678648

679649
$modelLoader = new MIDAS_ModelLoader();
@@ -698,10 +668,7 @@ function folderGet($args)
698668
*/
699669
function folderChildren($args)
700670
{
701-
if(!array_key_exists('id', $args))
702-
{
703-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
704-
}
671+
$this->_validateParams(&$args, array('id'));
705672

706673
$id = $args['id'];
707674
$modelLoader = new MIDAS_ModelLoader();
@@ -722,10 +689,7 @@ function folderChildren($args)
722689
*/
723690
function folderDelete($args)
724691
{
725-
if(!array_key_exists('id', $args))
726-
{
727-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
728-
}
692+
$this->_validateParams(&$args, array('id'));
729693

730694
$userDao = $this->_getUser($args);
731695
if($userDao == false)
@@ -753,10 +717,7 @@ function folderDelete($args)
753717
*/
754718
function folderDownload($args)
755719
{
756-
if(!array_key_exists('id', $args))
757-
{
758-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
759-
}
720+
$this->_validateParams(&$args, array('id'));
760721
$userDao = $this->_getUser($args);
761722

762723
$id = $args['id'];
@@ -780,10 +741,7 @@ function folderDownload($args)
780741
*/
781742
function itemGet($args)
782743
{
783-
if(!array_key_exists('id', $args))
784-
{
785-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
786-
}
744+
$this->_validateParams(&$args, array('id'));
787745
$userDao = $this->_getUser($args);
788746

789747
$itemid = $args['id'];
@@ -824,10 +782,7 @@ function itemGet($args)
824782
*/
825783
function itemDownload($args)
826784
{
827-
if(!array_key_exists('id', $args))
828-
{
829-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
830-
}
785+
$this->_validateParams(&$args, array('id'));
831786
$userDao = $this->_getUser($args);
832787

833788
$id = $args['id'];
@@ -857,10 +812,7 @@ function itemDownload($args)
857812
*/
858813
function itemDelete($args)
859814
{
860-
if(!array_key_exists('id', $args))
861-
{
862-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
863-
}
815+
$this->_validateParams(&$args, array('id'));
864816

865817
$userDao = $this->_getUser($args);
866818
if($userDao == false)
@@ -888,10 +840,7 @@ function itemDelete($args)
888840
*/
889841
function itemGetmetadata($args)
890842
{
891-
if(!array_key_exists('id', $args))
892-
{
893-
throw new Exception('Parameter id is not defined', MIDAS_INVALID_PARAMETER);
894-
}
843+
$this->_validateParams(&$args, array('id'));
895844
$userDao = $this->_getUser($args);
896845

897846
$itemid = $args['id'];
@@ -960,18 +909,11 @@ function userFolders($args)
960909
*/
961910
function userApikeyDefault($args)
962911
{
912+
$this->_validateParams(&$args, array('email', 'password'));
963913
if(!$this->controller->getRequest()->isPost())
964914
{
965915
throw new Exception('POST method required', MIDAS_HTTP_ERROR);
966916
}
967-
if(!array_key_exists('email', $args))
968-
{
969-
throw new Exception('Parameter email is not defined', MIDAS_INVALID_PARAMETER);
970-
}
971-
if(!array_key_exists('password', $args))
972-
{
973-
throw new Exception('Parameter password is not defined', MIDAS_INVALID_PARAMETER);
974-
}
975917

976918
$salt = Zend_Registry::get('configGlobal')->password->prefix;
977919
$defaultApiKey = $key = md5($args['email'].md5($salt.$args['password']).'Default');

0 commit comments

Comments
 (0)