@@ -25,6 +25,21 @@ class Api_ApiComponent extends AppComponent
25
25
public $ apiSetup ;
26
26
public $ userSession ;
27
27
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
+
28
43
/** Return the user dao */
29
44
private function _getUser ($ args )
30
45
{
@@ -62,20 +77,7 @@ public function info($args)
62
77
*/
63
78
function login ($ args )
64
79
{
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 ' ));
79
81
80
82
$ data ['token ' ] = '' ;
81
83
$ email = $ args ['email ' ];
@@ -100,14 +102,7 @@ function login($args)
100
102
*/
101
103
public function uuidGet ($ args )
102
104
{
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 ' ));
111
106
112
107
$ id = $ args ['id ' ];
113
108
$ type = $ args ['type ' ];
@@ -162,10 +157,7 @@ public function uuidGet($args)
162
157
*/
163
158
function resourceGet ($ args )
164
159
{
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 ' ));
169
161
170
162
$ uuid = $ args ['uuid ' ];
171
163
$ componentLoader = new MIDAS_ComponentLoader ();
@@ -197,10 +189,7 @@ function pathFromRoot($args)
197
189
*/
198
190
function pathToRoot ($ args )
199
191
{
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 ' ));
204
193
205
194
$ componentLoader = new MIDAS_ComponentLoader ();
206
195
$ uuidComponent = $ componentLoader ->loadComponent ('Uuid ' );
@@ -236,10 +225,7 @@ function pathToRoot($args)
236
225
*/
237
226
function resourceSearch ($ args )
238
227
{
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 ' ));
243
229
$ userDao = $ this ->_getUser ($ args );
244
230
245
231
$ order = 'view ' ;
@@ -399,25 +385,21 @@ function uploadPerform($args)
399
385
* Create a new community
400
386
* @param token Authentication token
401
387
* @param name The community name
402
- * @param description The community description
388
+ * @param description (Optional) The community description
403
389
* @param uuid (Optional) Uuid of the community. If none is passed, will generate one.
404
390
* @param privacy (Optional) Default 'Public'.
405
391
* @param canjoin (Optional) Default 'Everyone'.
406
392
* @return The community dao that was created
407
393
*/
408
394
function communityCreate ($ args )
409
395
{
396
+ $ this ->_validateParams (&$ args , array ('name ' ));
410
397
$ userDao = $ this ->_getUser ($ args );
411
398
if ($ userDao == false )
412
399
{
413
400
throw new Exception ('Unable to find user ' , MIDAS_INVALID_POLICY );
414
401
}
415
402
416
- if (!array_key_exists ('name ' , $ args ))
417
- {
418
- throw new Exception ('Parameter name is not defined ' , MIDAS_INVALID_PARAMETER );
419
- }
420
-
421
403
$ name = $ args ['name ' ];
422
404
$ uuid = isset ($ args ['uuid ' ]) ? $ args ['uuid ' ] : '' ;
423
405
@@ -546,18 +528,17 @@ function communityList($args)
546
528
*/
547
529
function communityDelete ($ args )
548
530
{
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
+
555
533
$ userDao = $ this ->_getUser ($ args );
556
534
if ($ userDao == false )
557
535
{
558
536
throw new Exception ('Unable to find user ' , MIDAS_INVALID_TOKEN );
559
537
}
560
538
$ id = $ args ['id ' ];
539
+
540
+ $ modelLoader = new MIDAS_ModelLoader ();
541
+ $ communityModel = $ modelLoader ->loadModel ('Community ' );
561
542
$ community = $ communityModel ->load ($ id );
562
543
563
544
if ($ community === false || !$ communityModel ->policyCheck ($ community , $ userDao , MIDAS_POLICY_ADMIN ))
@@ -572,29 +553,21 @@ function communityDelete($args)
572
553
* Create a folder
573
554
* @param token Authentication token
574
555
* @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
576
557
* @param uuid (Optional) Uuid of the folder. If none is passed, will generate one.
577
558
* @param privacy (Optional) Default 'Public'.
578
559
* @param parentid The id of the parent folder
579
560
* @return The folder object that was created
580
561
*/
581
562
function folderCreate ($ args )
582
563
{
564
+ $ this ->_validateParams (&$ args , array ('name ' ));
583
565
$ userDao = $ this ->_getUser ($ args );
584
566
if ($ userDao == false )
585
567
{
586
568
throw new Exception ('Unable to find user ' , MIDAS_INVALID_TOKEN );
587
569
}
588
570
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
-
598
571
$ modelLoader = new MIDAS_ModelLoader ();
599
572
$ folderModel = $ modelLoader ->loadModel ('Folder ' );
600
573
$ name = $ args ['name ' ];
@@ -670,10 +643,7 @@ function folderCreate($args)
670
643
*/
671
644
function folderGet ($ args )
672
645
{
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 ' ));
677
647
$ userDao = $ this ->_getUser ($ args );
678
648
679
649
$ modelLoader = new MIDAS_ModelLoader ();
@@ -698,10 +668,7 @@ function folderGet($args)
698
668
*/
699
669
function folderChildren ($ args )
700
670
{
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 ' ));
705
672
706
673
$ id = $ args ['id ' ];
707
674
$ modelLoader = new MIDAS_ModelLoader ();
@@ -722,10 +689,7 @@ function folderChildren($args)
722
689
*/
723
690
function folderDelete ($ args )
724
691
{
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 ' ));
729
693
730
694
$ userDao = $ this ->_getUser ($ args );
731
695
if ($ userDao == false )
@@ -753,10 +717,7 @@ function folderDelete($args)
753
717
*/
754
718
function folderDownload ($ args )
755
719
{
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 ' ));
760
721
$ userDao = $ this ->_getUser ($ args );
761
722
762
723
$ id = $ args ['id ' ];
@@ -780,10 +741,7 @@ function folderDownload($args)
780
741
*/
781
742
function itemGet ($ args )
782
743
{
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 ' ));
787
745
$ userDao = $ this ->_getUser ($ args );
788
746
789
747
$ itemid = $ args ['id ' ];
@@ -824,10 +782,7 @@ function itemGet($args)
824
782
*/
825
783
function itemDownload ($ args )
826
784
{
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 ' ));
831
786
$ userDao = $ this ->_getUser ($ args );
832
787
833
788
$ id = $ args ['id ' ];
@@ -857,10 +812,7 @@ function itemDownload($args)
857
812
*/
858
813
function itemDelete ($ args )
859
814
{
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 ' ));
864
816
865
817
$ userDao = $ this ->_getUser ($ args );
866
818
if ($ userDao == false )
@@ -888,10 +840,7 @@ function itemDelete($args)
888
840
*/
889
841
function itemGetmetadata ($ args )
890
842
{
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 ' ));
895
844
$ userDao = $ this ->_getUser ($ args );
896
845
897
846
$ itemid = $ args ['id ' ];
@@ -960,18 +909,11 @@ function userFolders($args)
960
909
*/
961
910
function userApikeyDefault ($ args )
962
911
{
912
+ $ this ->_validateParams (&$ args , array ('email ' , 'password ' ));
963
913
if (!$ this ->controller ->getRequest ()->isPost ())
964
914
{
965
915
throw new Exception ('POST method required ' , MIDAS_HTTP_ERROR );
966
916
}
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
- }
975
917
976
918
$ salt = Zend_Registry::get ('configGlobal ' )->password ->prefix ;
977
919
$ defaultApiKey = $ key = md5 ($ args ['email ' ].md5 ($ salt .$ args ['password ' ]).'Default ' );
0 commit comments