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

Commit 5830e3b

Browse files
committed
ENH: refs #0377. Expose quota management in the web api
1 parent 58a30af commit 5830e3b

File tree

4 files changed

+162
-4
lines changed

4 files changed

+162
-4
lines changed

modules/sizequota/Notification.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
See the License for the specific language governing permissions and
1818
limitations under the License.
1919
=========================================================================*/
20+
require_once BASE_PATH . '/modules/api/library/APIEnabledNotification.php';
2021

2122
/** notification manager for sizequota module */
22-
class Sizequota_Notification extends MIDAS_Notification
23+
class Sizequota_Notification extends ApiEnabled_Notification
2324
{
2425
public $moduleName = 'sizequota';
25-
public $_moduleComponents = array();
26+
public $_moduleComponents = array('Api');
2627
public $_models = array('Folder');
2728

2829
/** init notification process */
@@ -31,6 +32,8 @@ public function init()
3132
$this->addCallBack('CALLBACK_CORE_GET_COMMUNITY_MANAGE_TABS', 'getCommunityTab');
3233
$this->addCallBack('CALLBACK_CORE_GET_USER_TABS', 'getUserTab');
3334
//$this->addCallBack('CALLBACK_CORE_VALIDATE_UPLOAD', 'validateUpload');
35+
36+
$this->enableWebAPI($this->moduleName);
3437
}
3538

3639
/** Add a tab to the manage community page for size quota */

modules/sizequota/configs/module.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ fullname = Size Quotas
77
description = "Allows maximum size quotas to be set on communities and users"
88
;Category
99
category = Core
10+
; Dependencies (comma separated list)
11+
dependencies = api
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 26 rue Louis Guérin. 69100 Villeurbanne, FRANCE
5+
All rights reserved.
6+
More information http://www.kitware.com
7+
8+
Licensed under the Apache License, Version 2.0 (the "License");
9+
you may not use this file except in compliance with the License.
10+
You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0.txt
13+
14+
Unless required by applicable law or agreed to in writing, software
15+
distributed under the License is distributed on an "AS IS" BASIS,
16+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
See the License for the specific language governing permissions and
18+
limitations under the License.
19+
=========================================================================*/
20+
21+
// Web API error codes
22+
define('MIDAS_SIZEQUOTA_INVALID_POLICY', -151);
23+
define('MIDAS_SIZEQUOTA_INVALID_PARAMETER', -150);
24+
25+
/** Component for api methods */
26+
class Sizequota_ApiComponent extends AppComponent
27+
{
28+
public $userSession;
29+
30+
/**
31+
* Helper function for verifying keys in an input array
32+
*/
33+
private function _checkKeys($keys, $values)
34+
{
35+
foreach($keys as $key)
36+
{
37+
if(!array_key_exists($key, $values))
38+
{
39+
throw new Exception('Parameter '.$key.' must be set.', MIDAS_SIZEQUOTA_INVALID_PARAMETER);
40+
}
41+
}
42+
}
43+
44+
/** Authenticate via token or session */
45+
private function _getUser($args)
46+
{
47+
$componentLoader = new MIDAS_ComponentLoader();
48+
$authComponent = $componentLoader->loadComponent('Authentication', 'api');
49+
$default = $this->userSession ? $this->userSession->Dao : null;
50+
return $authComponent->getUser($args, $default);
51+
}
52+
53+
/**
54+
* Get the size quota for a user.
55+
* @param token Authentication token
56+
* @param user Id of the user to check
57+
* @return array('quota' => The size quota in bytes for the user, or empty string if unlimited,
58+
'used' => Size in bytes currently used)
59+
*/
60+
public function userGet($args)
61+
{
62+
$this->_checkKeys(array('token', 'user'), $args);
63+
$requestUser = $this->_getUser($args);
64+
65+
$modelLoader = new MIDAS_ModelLoader();
66+
$folderModel = $modelLoader->loadModel('Folder');
67+
$userModel = $modelLoader->loadModel('User');
68+
69+
$user = $userModel->load($args['user']);
70+
if(!$user)
71+
{
72+
throw new Exception('Invalid user id', MIDAS_SIZEQUOTA_INVALID_PARAMETER);
73+
}
74+
75+
if(!$folderModel->policyCheck($user->getFolder(), $requestUser, MIDAS_POLICY_READ))
76+
{
77+
throw new Exception('Read permission required', MIDAS_SIZEQUOTA_INVALID_POLICY);
78+
}
79+
$quotaModel = $modelLoader->loadModel('FolderQuota', 'sizequota');
80+
$quota = $quotaModel->getUserQuota($user);
81+
$used = $folderModel->getSizeFiltered($user->getFolder(), $requestUser, MIDAS_POLICY_READ);
82+
return array('quota' => $quota, 'used' => $used[0]->size);
83+
}
84+
85+
/**
86+
* Get the size quota for a community.
87+
* @param token Authentication token
88+
* @param community Id of the community to check
89+
* @return array('quota' => The size quota in bytes for the community, or empty string if unlimited,
90+
'used' => Size in bytes currently used)
91+
*/
92+
public function communityGet($args)
93+
{
94+
$this->_checkKeys(array('token', 'community'), $args);
95+
$requestUser = $this->_getUser($args);
96+
97+
$modelLoader = new MIDAS_ModelLoader();
98+
$folderModel = $modelLoader->loadModel('Folder');
99+
$commModel = $modelLoader->loadModel('Community');
100+
101+
$comm = $commModel->load($args['community']);
102+
if(!$comm)
103+
{
104+
throw new Exception('Invalid community id', MIDAS_SIZEQUOTA_INVALID_PARAMETER);
105+
}
106+
107+
if(!$folderModel->policyCheck($comm->getFolder(), $requestUser, MIDAS_POLICY_READ))
108+
{
109+
throw new Exception('Read permission required', MIDAS_SIZEQUOTA_INVALID_POLICY);
110+
}
111+
$quotaModel = $modelLoader->loadModel('FolderQuota', 'sizequota');
112+
$quota = $quotaModel->getCommunityQuota($comm);
113+
$used = $folderModel->getSizeFiltered($comm->getFolder(), $requestUser, MIDAS_POLICY_READ);
114+
return array('quota' => $quota, 'used' => $used[0]->size);
115+
}
116+
117+
/**
118+
* Set a quota for a folder. For MIDAS admin use only.
119+
* @param token Authentication token
120+
* @param folder The folder id
121+
* @param quota (optional) The quota. Pass a number of bytes or the empty string for unlimited.
122+
If this parameter isn't specified, deletes the current quota entry if one exists.
123+
*/
124+
public function set($args)
125+
{
126+
$this->_checkKeys(array('token', 'folder'), $args);
127+
$user = $this->_getUser($args);
128+
129+
if(!$user || !$user->isAdmin())
130+
{
131+
throw new Exception('Must be super-admin', MIDAS_SIZEQUOTA_INVALID_POLICY);
132+
}
133+
$modelLoader = new MIDAS_ModelLoader();
134+
$folderModel = $modelLoader->loadModel('Folder');
135+
$folder = $folderModel->load($args['folder']);
136+
if(!$folder)
137+
{
138+
throw new Exception('Invalid folder id', MIDAS_SIZEQUOTA_INVALID_PARAMETER);
139+
}
140+
if($folder->getParentId() > 0)
141+
{
142+
throw new Exception('Must be a root folder', MIDAS_SIZEQUOTA_INVALID_PARAMETER);
143+
}
144+
$quota = array_key_exists('quota', $args) ? $args['quota'] : null;
145+
if($quota !== null && !preg_match('/^[0-9]*$/', $quota))
146+
{
147+
throw new Exception('Quota must be empty string or an integer if specified', MIDAS_SIZEQUOTA_INVALID_PARAMETER);
148+
}
149+
$quotaModel = $modelLoader->loadModel('FolderQuota', 'sizequota');
150+
return $quotaModel->setQuota($folder, $quota);
151+
}
152+
153+
}

modules/sizequota/models/base/FolderQuotaModelBase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public abstract function getQuota($folder);
4545
public function getUserQuota($user)
4646
{
4747
$quotaDao = $this->getQuota($user->getFolder());
48-
if(!quotaDao)
48+
if(!$quotaDao)
4949
{
5050
$modelLoader = new MIDAS_ModelLoader();
5151
$settingModel = $modelLoader->loadModel('Setting');
@@ -58,7 +58,7 @@ public function getUserQuota($user)
5858
public function getCommunityQuota($community)
5959
{
6060
$quotaDao = $this->getQuota($community->getFolder());
61-
if(!quotaDao)
61+
if(!$quotaDao)
6262
{
6363
$modelLoader = new MIDAS_ModelLoader();
6464
$settingModel = $modelLoader->loadModel('Setting');

0 commit comments

Comments
 (0)