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

Commit d19b163

Browse files
committed
ENH: refs #236. Add community.list and user.folders methods to web API
1 parent 55c028c commit d19b163

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

modules/api/controllers/IndexController.php

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
class Api_IndexController extends Api_AppController
2424
{
2525
public $_moduleModels = array('Userapi');
26-
public $_models = array('Community', 'ItemRevision', 'Item', 'User', "Folderpolicyuser", 'Folderpolicygroup', 'Folder');
26+
public $_models = array('Community', 'ItemRevision', 'Item', 'User', 'Folderpolicyuser', 'Folderpolicygroup', 'Folder');
2727
public $_components = array('Upload', 'Search', 'Uuid', 'Sortdao');
2828

2929
var $kwWebApiCore = null;
@@ -98,16 +98,16 @@ private function _setApiCallbacks($apiMethodPrefix)
9898
$help['example'] = array();
9999
$help['return'] = 'String version';
100100
$help['description'] = 'Return the version of MIDAS';
101-
$this->helpContent[$apiMethodPrefix.'version'] = $help;
102-
$this->apicallbacks[$apiMethodPrefix.'version'] = array(&$this, 'version');
101+
$this->helpContent[$apiMethodPrefix.'version'] = $help;
102+
$this->apicallbacks[$apiMethodPrefix.'version'] = array(&$this, 'version');
103103

104104
$help = array();
105105
$help['params'] = array();
106106
$help['example'] = array();
107107
$help['return'] = 'MIDAS info';
108108
$help['description'] = 'Get information about this MIDAS instance';
109-
$this->helpContent[$apiMethodPrefix.'info'] = $help;
110-
$this->apicallbacks[$apiMethodPrefix.'info'] = array(&$this, 'info');
109+
$this->helpContent[$apiMethodPrefix.'info'] = $help;
110+
$this->apicallbacks[$apiMethodPrefix.'info'] = array(&$this, 'info');
111111

112112
$help = array();
113113
$help['params'] = array();
@@ -120,7 +120,7 @@ private function _setApiCallbacks($apiMethodPrefix)
120120
$help['example']['?method=midas.login&appname=test&email=user@test.com&apikey=YourKey'] = 'Authenticate using key';
121121
$help['return'] = 'Token';
122122
$help['description'] = 'Authenticate an user';
123-
$this->helpContent[$apiMethodPrefix.'login'] = $help;
123+
$this->helpContent[$apiMethodPrefix.'login'] = $help;
124124
$this->apicallbacks[$apiMethodPrefix.'login'] = array(&$this, 'login');
125125

126126
$help = array();
@@ -151,7 +151,7 @@ private function _setApiCallbacks($apiMethodPrefix)
151151
$help['return'] = 'Array of resource)';
152152
$help['description'] = 'Global search';
153153
$this->helpContent[$apiMethodPrefix.'resource.search'] = $help;
154-
$this->apicallbacks[$apiMethodPrefix.'resource.search'] = array(&$this, 'resourcesSearch');
154+
$this->apicallbacks[$apiMethodPrefix.'resource.search'] = array(&$this, 'resourcesSearch');
155155

156156
$help = array();
157157
$help['params'] = array();
@@ -199,10 +199,18 @@ private function _setApiCallbacks($apiMethodPrefix)
199199
$help['return'] = 'Item information';
200200
$help['description'] = 'Upload a file (using put or post method)';
201201
$this->helpContent[$apiMethodPrefix.'upload.file'] = $help;
202-
$this->apicallbacks[$apiMethodPrefix.'upload.file'] = array(&$this, 'uploadFile');
203-
202+
$this->apicallbacks[$apiMethodPrefix.'upload.file'] = array(&$this, 'uploadFile');
204203

205204
/* ----- Community ------*/
205+
$help = array();
206+
$help['params'] = array();
207+
$help['params']['token'] = '(Optional) Authentification token';
208+
$help['example'] = array();
209+
$help['return'] = 'List of communities';
210+
$help['description'] = 'Get the list of all communities visible to the given user';
211+
$this->helpContent[$apiMethodPrefix.'community.list'] = $help;
212+
$this->apicallbacks[$apiMethodPrefix.'community.list'] = array(&$this, 'communityList');
213+
206214
$help = array();
207215
$help['params'] = array();
208216
$help['params']['token'] = 'Authentification token';
@@ -249,7 +257,7 @@ private function _setApiCallbacks($apiMethodPrefix)
249257
$help['return'] = 'Folder information';
250258
$help['description'] = 'Create or edit a folder';
251259
$this->helpContent[$apiMethodPrefix.'folder.create'] = $help;
252-
$this->apicallbacks[$apiMethodPrefix.'folder.create'] = array(&$this, 'folderCreate');
260+
$this->apicallbacks[$apiMethodPrefix.'folder.create'] = array(&$this, 'folderCreate');
253261

254262
$help = array();
255263
$help['params'] = array();
@@ -259,7 +267,7 @@ private function _setApiCallbacks($apiMethodPrefix)
259267
$help['return'] = '';
260268
$help['description'] = 'Delete a folder';
261269
$this->helpContent[$apiMethodPrefix.'folder.delete'] = $help;
262-
$this->apicallbacks[$apiMethodPrefix.'folder.delete'] = array(&$this, 'folderDelete');
270+
$this->apicallbacks[$apiMethodPrefix.'folder.delete'] = array(&$this, 'folderDelete');
263271

264272
$help = array();
265273
$help['params'] = array();
@@ -269,7 +277,7 @@ private function _setApiCallbacks($apiMethodPrefix)
269277
$help['return'] = 'Folder Information';
270278
$help['description'] = 'Get a folder';
271279
$this->helpContent[$apiMethodPrefix.'folder.get'] = $help;
272-
$this->apicallbacks[$apiMethodPrefix.'folder.get'] = array(&$this, 'folderGet');
280+
$this->apicallbacks[$apiMethodPrefix.'folder.get'] = array(&$this, 'folderGet');
273281

274282
$help = array();
275283
$help['params'] = array();
@@ -279,15 +287,15 @@ private function _setApiCallbacks($apiMethodPrefix)
279287
$help['return'] = 'File';
280288
$help['description'] = 'Download a folder';
281289
$this->helpContent[$apiMethodPrefix.'folder.download'] = $help;
282-
$this->apicallbacks[$apiMethodPrefix.'folder.download'] = array(&$this, 'folderDownload');
290+
$this->apicallbacks[$apiMethodPrefix.'folder.download'] = array(&$this, 'folderDownload');
283291

284292
$help = array();
285293
$help['params'] = array();
286294
$help['params']['token'] = '(Optional) Authentification token';
287295
$help['params']['id'] = 'Id of the folder';
288296
$help['example'] = array();
289297
$help['return'] = 'Array of Items and Folders';
290-
$help['description'] = 'Get folder Content';
298+
$help['description'] = 'Get folder content';
291299
$this->helpContent[$apiMethodPrefix.'folder.content'] = $help;
292300
$this->apicallbacks[$apiMethodPrefix.'folder.content'] = array(&$this, 'folderContent');
293301

@@ -297,9 +305,18 @@ private function _setApiCallbacks($apiMethodPrefix)
297305
$help['params']['id'] = 'Id of the folder';
298306
$help['example'] = array();
299307
$help['return'] = 'Array of Folders';
300-
$help['description'] = 'Get folder Tree';
308+
$help['description'] = 'Get folder tree';
301309
$this->helpContent[$apiMethodPrefix.'folder.tree'] = $help;
302-
$this->apicallbacks[$apiMethodPrefix.'folder.tree'] = array(&$this, 'folderTree');
310+
$this->apicallbacks[$apiMethodPrefix.'folder.tree'] = array(&$this, 'folderTree');
311+
312+
$help = array();
313+
$help['params'] = array();
314+
$help['params']['token'] = '(Optional) Authentification token';
315+
$help['example'] = array();
316+
$help['return'] = 'List of Folders';
317+
$help['description'] = 'Get the list of top level folders belonging to a given user';
318+
$this->helpContent[$apiMethodPrefix.'user.folders'] = $help;
319+
$this->apicallbacks[$apiMethodPrefix.'user.folders'] = array(&$this, 'userFolders');
303320

304321
/** ------ ITEM --- */
305322
$help = array();
@@ -342,7 +359,7 @@ private function _setApiCallbacks($apiMethodPrefix)
342359
$help['return'] = '';
343360
$help['description'] = 'Get metadata';
344361
$this->helpContent[$apiMethodPrefix.'item.getmetadata'] = $help;
345-
$this->apicallbacks[$apiMethodPrefix.'item.getmetadata'] = array(&$this, 'itemGetMetadata');
362+
$this->apicallbacks[$apiMethodPrefix.'item.getmetadata'] = array(&$this, 'itemGetMetadata');
346363
}
347364

348365
/** Initialize property allowing to generate XML */
@@ -1279,10 +1296,51 @@ function itemDelete($args)
12791296

12801297
if($item === false || !$this->Item->policyCheck($item, $userDao, MIDAS_POLICY_ADMIN))
12811298
{
1282-
throw new Exception("This item doesn't exist or you don't have the permissions.", MIDAS_INVALID_POLICY);
1299+
throw new Exception("This item doesn't exist or you don't have the permissions.", MIDAS_INVALID_POLICY);
12831300
}
12841301

12851302
$this->Item->delete($item);
12861303
}
1304+
1305+
/**
1306+
* Return a list of all communities visible to a user
1307+
*/
1308+
function communityList($args)
1309+
{
1310+
$userDao = $this->_getUser($args);
1311+
1312+
if($userDao && $userDao->isAdmin())
1313+
{
1314+
$communities = $this->Community->getAll();
1315+
}
1316+
else
1317+
{
1318+
$communities = $this->Community->getPublicCommunities();
1319+
if($userDao)
1320+
{
1321+
$communities = array_merge($communities, $this->User->getUserCommunities($userDao));
1322+
}
1323+
}
1324+
1325+
$this->Component->Sortdao->field = 'name';
1326+
$this->Component->Sortdao->order = 'asc';
1327+
usort($communities, array($this->Component->Sortdao, 'sortByName'));
1328+
return $this->Component->Sortdao->arrayUniqueDao($communities);
1329+
}
1330+
1331+
/**
1332+
* Return a list of top level folders belonging to the user
1333+
*/
1334+
function userFolders($args)
1335+
{
1336+
$userDao = $this->_getUser($args);
1337+
if($userDao == false)
1338+
{
1339+
return array();
1340+
}
1341+
1342+
$userRootFolder = $userDao->getFolder();
1343+
return $this->Folder->getChildrenFoldersFiltered($userRootFolder, $userDao, MIDAS_POLICY_READ);
1344+
}
12871345
} // end class
12881346
?>

0 commit comments

Comments
 (0)