FOSSology  4.4.0
Open Source License Compliance by Open Source Software
RestHelper.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2017 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
12 namespace Fossology\UI\Api\Helper;
13 
26 
32 {
37  const VALID_SCOPES = ["read", "write"];
42  const SCOPE_DB_MAP = ["read" => "r", "write" => "w"];
47  const TOKEN_KEY_LENGTH = 40;
52  private $uploadDao;
57  private $dbHelper;
67  private $folderDao;
72  private $userDao;
77  private $jobDao;
82  private $showJobDao;
87  private $authHelper;
88 
98  {
99  $this->uploadPermissionDao = $uploadPermissionDao;
100  $this->uploadDao = $uploadDao;
101  $this->userDao = $userDao;
102  $this->folderDao = $folderDao;
103  $this->dbHelper = $dbHelper;
104  $this->authHelper = $authHelper;
105  $this->jobDao = $jobDao;
106  $this->showJobDao = $showJobDao;
107  }
108 
112  public function getUserId()
113  {
114  $session = $this->authHelper->getSession();
115  return $session->get(Auth::USER_ID);
116  }
117 
121  public function getGroupId()
122  {
123  $session = $this->authHelper->getSession();
124  return $session->get(Auth::GROUP_ID);
125  }
126 
130  public function getUploadDao()
131  {
132  return $this->uploadDao;
133  }
134 
138  public function getUserDao()
139  {
140  return $this->userDao;
141  }
142 
146  public function getFolderDao()
147  {
148  return $this->folderDao;
149  }
150 
154  public function getUploadPermissionDao()
155  {
157  }
158 
162  public function getAuthHelper()
163  {
164  return $this->authHelper;
165  }
166 
170  public function getDbHelper()
171  {
172  return $this->dbHelper;
173  }
174 
178  public function getJobDao()
179  {
180  return $this->jobDao;
181  }
182 
186  public function getShowJobDao()
187  {
188  return $this->showJobDao;
189  }
190 
200  public function copyUpload($uploadId, $newFolderId, $isCopy)
201  {
202  if (! is_numeric($newFolderId) || $newFolderId <= 0) {
203  throw new HttpBadRequestException("Folder id should be a positive integer");
204  }
205  if (!$this->folderDao->isFolderAccessible($newFolderId, $this->getUserId())) {
206  throw new HttpForbiddenException("Folder is not accessible.");
207  }
208  if (!$this->uploadPermissionDao->isAccessible($uploadId, $this->getGroupId())) {
209  throw new HttpForbiddenException("Upload is not accessible.");
210  }
211  $uploadContentId = $this->folderDao->getFolderContentsId($uploadId,
212  $this->folderDao::MODE_UPLOAD);
214  $contentMove = $this->getPlugin('content_move');
215 
216  $errors = $contentMove->copyContent([$uploadContentId], $newFolderId, $isCopy);
217  if (empty($errors)) {
218  $action = $isCopy ? "copied" : "moved";
219  $info = new Info(202, "Upload $uploadId will be $action to folder $newFolderId",
220  InfoType::INFO);
221  } else {
222  $info = new Info(202, "Exceptions occurred: $errors",
223  InfoType::ERROR);
224  }
225  return $info;
226  }
227 
239  public function getPlugin($pluginName)
240  {
241  $plugin = plugin_find($pluginName);
242  if (! $plugin) {
243  throw new \UnexpectedValueException(
244  "Unable to find plugin " . $pluginName);
245  }
246  return $plugin;
247  }
248 
264  public function validateTokenRequest($tokenExpire, $tokenName, $tokenScope)
265  {
266  $tokenValidity = $this->authHelper->getMaxTokenValidity();
267 
268  if (strtotime($tokenExpire) < strtotime("tomorrow") ||
269  ! preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",
270  $tokenExpire) ||
271  strtotime($tokenExpire) > strtotime("+$tokenValidity days")) {
272  throw new HttpBadRequestException(
273  "The token should have at least 1 day and max $tokenValidity days " .
274  "of validity and should follow YYYY-MM-DD format.");
275  } elseif (! in_array($tokenScope, RestHelper::SCOPE_DB_MAP)) {
276  throw new HttpBadRequestException(
277  "Invalid token scope, allowed only " .
278  join(",", RestHelper::VALID_SCOPES));
279  } elseif (empty($tokenName) || strlen($tokenName) > 40) {
280  throw new HttpBadRequestException(
281  "The token name must be a valid string of max 40 character length");
282  }
283  }
284 
300  public function validateNewOauthClient($userId, $clientName, $clientScope,
301  $clientId)
302  {
303  if (!in_array($clientScope, RestHelper::SCOPE_DB_MAP)) {
304  throw new HttpBadRequestException("Invalid client scope, allowed only " .
305  join(",", RestHelper::VALID_SCOPES));
306  }
307  if (empty($clientName) || strlen($clientName) > 40) {
308  throw new HttpBadRequestException(
309  "The client name must be a valid string of max 40 character length");
310  }
311  $sql = "SELECT 1 FROM personal_access_tokens " .
312  "WHERE user_fk = $1 AND client_id = $2;";
313  $rows = $this->dbHelper->getDbManager()->getSingleRow($sql, [
314  $userId,
315  $clientId
316  ], __METHOD__);
317  if (!empty($rows)) {
318  throw new HttpBadRequestException("Client already added for the user.");
319  }
320  }
321 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Provides helper methods for REST api.
Definition: AuthHelper.php:38
Provides helper methods to access database for REST api.
Definition: DbHelper.php:38
Provides various DAO helper functions for REST api.
Definition: RestHelper.php:32
validateTokenRequest($tokenExpire, $tokenName, $tokenScope)
Check if the token request contains valid parameters.
Definition: RestHelper.php:264
getPlugin($pluginName)
A safe wrapper around plugin_find.
Definition: RestHelper.php:239
validateNewOauthClient($userId, $clientName, $clientScope, $clientId)
Check if the new oauth client is valid.
Definition: RestHelper.php:300
__construct(UploadPermissionDao $uploadPermissionDao, UploadDao $uploadDao, UserDao $userDao, FolderDao $folderDao, DbHelper $dbHelper, AuthHelper $authHelper, JobDao $jobDao, ShowJobsDao $showJobDao)
RestHelper constructor.
Definition: RestHelper.php:94
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
REST api helper classes.