|
| 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 | +} |
0 commit comments