FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
RestHelper.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************
3 Copyright (C) 2017 Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***************************************************************/
18 
23 namespace Fossology\UI\Api\Helper;
24 
35 
41 {
46  const VALID_SCOPES = ["read", "write"];
51  const SCOPE_DB_MAP = ["read" => "r", "write" => "w"];
56  const TOKEN_KEY_LENGTH = 40;
61  private $uploadDao;
66  private $dbHelper;
76  private $folderDao;
81  private $userDao;
86  private $jobDao;
91  private $showJobDao;
96  private $authHelper;
97 
107  {
108  $this->uploadPermissionDao = $uploadPermissionDao;
109  $this->uploadDao = $uploadDao;
110  $this->userDao = $userDao;
111  $this->folderDao = $folderDao;
112  $this->dbHelper = $dbHelper;
113  $this->authHelper = $authHelper;
114  $this->jobDao = $jobDao;
115  $this->showJobDao = $showJobDao;
116  }
117 
121  public function getUserId()
122  {
123  $session = $this->authHelper->getSession();
124  return $session->get(Auth::USER_ID);
125  }
126 
130  public function getGroupId()
131  {
132  $session = $this->authHelper->getSession();
133  return $session->get(Auth::GROUP_ID);
134  }
135 
139  public function getUploadDao()
140  {
141  return $this->uploadDao;
142  }
143 
147  public function getUserDao()
148  {
149  return $this->userDao;
150  }
151 
155  public function getFolderDao()
156  {
157  return $this->folderDao;
158  }
159 
163  public function getUploadPermissionDao()
164  {
166  }
167 
171  public function getAuthHelper()
172  {
173  return $this->authHelper;
174  }
175 
179  public function getDbHelper()
180  {
181  return $this->dbHelper;
182  }
183 
187  public function getJobDao()
188  {
189  return $this->jobDao;
190  }
191 
195  public function getShowJobDao()
196  {
197  return $this->showJobDao;
198  }
199 
207  public function copyUpload($uploadId, $newFolderId, $isCopy)
208  {
209  if (is_numeric($newFolderId) && $newFolderId > 0) {
210  if (!$this->folderDao->isFolderAccessible($newFolderId, $this->getUserId())) {
211  return new Info(403, "Folder is not accessible.",
212  InfoType::ERROR);
213  }
214  if (!$this->uploadPermissionDao->isAccessible($uploadId, $this->getGroupId())) {
215  return new Info(403, "Upload is not accessible.",
216  InfoType::ERROR);
217  }
218  $uploadContentId = $this->folderDao->getFolderContentsId($uploadId);
219  $contentMove = $this->getPlugin('content_move');
220 
221  $errors = $contentMove->copyContent([$uploadContentId], $newFolderId, $isCopy);
222  if (empty($errors)) {
223  $action = $isCopy ? "copied" : "moved";
224  $info = new Info(202, "Upload $uploadId will be $action to folder $newFolderId",
225  InfoType::INFO);
226  } else {
227  $info = new Info(202, "Exceptions occurred: $errors",
228  InfoType::ERROR);
229  }
230  return $info;
231  } else {
232  return new Info(400, "Bad Request. Folder id should be a positive integer",
233  InfoType::ERROR);
234  }
235  }
236 
248  public function getPlugin($pluginName)
249  {
250  $plugin = plugin_find($pluginName);
251  if (! $plugin) {
252  throw new \UnexpectedValueException(
253  "Unable to find plugin " . $pluginName);
254  }
255  return $plugin;
256  }
257 
273  public function validateTokenRequest($tokenExpire, $tokenName, $tokenScope)
274  {
275  $requestValid = true;
276  $tokenValidity = $this->authHelper->getMaxTokenValidity();
277 
278  if (strtotime($tokenExpire) < strtotime("tomorrow") ||
279  ! preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",
280  $tokenExpire) ||
281  strtotime($tokenExpire) > strtotime("+$tokenValidity days")) {
282  $requestValid = new Info(400,
283  "The token should have at least 1 day and max $tokenValidity days " .
284  "of validity and should follow YYYY-MM-DD format.", InfoType::ERROR);
285  } elseif (! in_array($tokenScope, RestHelper::VALID_SCOPES)) {
286  $requestValid = new Info(400,
287  "Invalid token scope, allowed only " .
288  join(",", RestHelper::VALID_SCOPES), InfoType::ERROR);
289  } elseif (empty($tokenName) || strlen($tokenName) > 40) {
290  $requestValid = new Info(400,
291  "The token name must be a valid string of max 40 character length",
292  InfoType::ERROR);
293  }
294  return $requestValid;
295  }
296 }
__construct(UploadPermissionDao $uploadPermissionDao, UploadDao $uploadDao, UserDao $userDao, FolderDao $folderDao, DbHelper $dbHelper, AuthHelper $authHelper, JobDao $jobDao, ShowJobsDao $showJobDao)
RestHelper constructor.
Definition: RestHelper.php:103
getPlugin($pluginName)
A safe wrapper around plugin_find.
Definition: RestHelper.php:248
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:40
REST api helper classes.
Provides helper methods for REST api.
Definition: AuthHelper.php:39
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
Info model to contain general error and return values.
Definition: Info.php:29
Provides helper methods to access database for REST api.
Definition: DbHelper.php:43
copyUpload($uploadId, $newFolderId, $isCopy)
Definition: RestHelper.php:207
validateTokenRequest($tokenExpire, $tokenName, $tokenScope)
Check if the token request contains valid parameters.
Definition: RestHelper.php:273