FOSSology  4.7.1
Open Source License Compliance by Open Source Software
UploadTreeController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Samuel Dushimimana <dushsam100@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace Fossology\UI\Api\Controllers;
14 
36 use Psr\Http\Message\ServerRequestInterface;
37 use Symfony\Component\HttpFoundation\Response;
38 
44 {
46  private $clearingDao;
47 
52  private $licenseDao;
53 
58  private $highlightDao;
59 
61  private $clearingDecisionEventProcessor;
62 
67  private $decisionTypes;
68 
69 
70  public function __construct($container)
71  {
72  parent::__construct($container);
73  $this->clearingDao = $this->container->get('dao.clearing');
74  $this->licenseDao = $this->container->get('dao.license');
75  $this->highlightDao = $this->container->get("dao.highlight");
76  $this->clearingDecisionEventProcessor = $this->container->get('businessrules.clearing_decision_processor');
77  $this->decisionTypes = $this->container->get('decision.types');
78  }
79 
89  public function viewLicenseFile($request, $response, $args)
90  {
91  $uploadId = intval($args['id']);
92  $itemId = intval($args['itemId']);
93 
94  $this->uploadAccessible($uploadId);
95  $this->isItemExists($uploadId, $itemId);
96 
98  $view = $this->restHelper->getPlugin('view');
99 
100  $inputFile = @fopen(RepPathItem($itemId), "rb");
101  if (empty($inputFile)) {
102  global $Plugins;
103  $reunpackPlugin = &$Plugins[plugin_find_id("ui_reunpack")];
104  $state = $reunpackPlugin->CheckStatus($uploadId, "reunpack", "ununpack");
105  if ($state != 0 && $state != 2) {
106  $errorMess = _("Reunpack job is running: you can see it in");
107  } else {
108  $errorMess = _("File contents are not available in the repository.");
109  }
110  throw new HttpInternalServerErrorException($errorMess);
111  }
112  rewind($inputFile);
113 
114  $res = $view->getText($inputFile, 0, 0, -1, null, false, true);
115  $response->getBody()->write($res);
116  return $response->withHeader("Content-Type", "text/plain")
117  ->withHeader("Cache-Control", "max-age=1296000, must-revalidate")
118  ->withHeader("Etag", md5($response->getBody()));
119  }
120 
130  public function setClearingDecision($request, $response, $args)
131  {
132  $uploadId = intval($args['id']);
133  $this->uploadAccessible($uploadId);
134 
135  $body = $this->getParsedBody($request);
136  $decisionType = $body['decisionType'];
137  $global = $body['globalDecision'];
138 
139  // check if the given globalDecision value is a boolean
140  if ($global !== null && !is_bool($global)) {
141  throw new HttpBadRequestException("GlobalDecision should be a boolean");
142  }
143 
144  $uploadTreeId = intval($args['itemId']);
145 
146  // check if the given key exists in the known decision types
147  if (!array_key_exists($decisionType, $this->decisionTypes->getMap())) {
148  throw new HttpBadRequestException(
149  "Decision Type should be one of the following keys: " .
150  implode(", ", array_keys($this->decisionTypes->getMap())));
151  }
152  $this->isItemExists($uploadId, $uploadTreeId);
153 
155  $viewLicensePlugin = $this->restHelper->getPlugin('view-license');
156  $_GET['clearingTypes'] = $decisionType;
157  $_GET['globalDecision'] = $global ? 1 : 0;
158  $viewLicensePlugin->updateLastItem($this->restHelper->getUserId(), $this->restHelper->getGroupId(), $uploadTreeId, $uploadTreeId);
159  $returnVal = new Info(200, "Successfully set decision", InfoType::INFO);
160  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
161  }
162 
172  public function getNextPreviousItem($request, $response, $args)
173  {
174  $uploadTreeId = intval($args['itemId']);
175  $uploadId = intval($args['id']);
176  $query = $request->getQueryParams();
177  $uploadDao = $this->restHelper->getUploadDao();
178  $selection = "";
179 
180  $this->uploadAccessible($uploadId);
181  $this->isItemExists($uploadId, $uploadTreeId);
182 
183  if ($query['selection'] !== null) {
184  $selection = $query['selection'];
185  if ($selection != "withLicenses" && $selection != "noClearing") {
186  throw new HttpBadRequestException(
187  "selection should be either 'withLicenses' or 'noClearing'");
188  }
189  }
190 
191  $options = array('skipThese' => $selection == "withLicenses" ? "noLicense" : ($selection == "noClearing" ? "alreadyCleared" : ""), 'groupId' => $this->restHelper->getGroupId());
192 
193  $prevItem = $uploadDao->getPreviousItem($uploadId, $uploadTreeId, $options);
194  $prevItemId = $prevItem ? $prevItem->getId() : null;
195 
196  $nextItem = $uploadDao->getNextItem($uploadId, $uploadTreeId, $options);
197  $nextItemId = $nextItem ? $nextItem->getId() : null;
198 
199  $res = [
200  "prevItemId" => $prevItemId,
201  "nextItemId" => $nextItemId
202  ];
203  return $response->withJson($res, 200);
204  }
205 
215  public function getBulkHistory($request, $response, $args)
216  {
217  $uploadTreeId = intval($args['itemId']);
218  $uploadId = intval($args['id']);
219  $uploadDao = $this->restHelper->getUploadDao();
220  $clearingDao = $this->container->get('dao.clearing');
221 
222  $this->uploadAccessible($uploadId);
223  $this->isItemExists($uploadId, $uploadTreeId);
224 
225  $uploadTreeTableName = $uploadDao->getUploadtreeTableName($uploadId);
226  $itemTreeBounds = $uploadDao->getItemTreeBounds($uploadTreeId, $uploadTreeTableName);
227 
228  $res = $clearingDao->getBulkHistory($itemTreeBounds, $this->restHelper->getGroupId());
229  $updatedRes = [];
230 
231  foreach ($res as $value) {
232  $obj = new BulkHistory(
233  intval($value['bulkId']),
234  intval($value['id']),
235  $value['text'],
236  $value['matched'],
237  $value['tried'],
238  $value['addedLicenses'],
239  $value['removedLicenses']);
240  $updatedRes[] = $obj->getArray();
241  }
242  return $response->withJson($updatedRes, 200);
243  }
244 
254  public function getClearingHistory($request, $response, $args)
255  {
256  $itemId = intval($args['itemId']);
257  $uploadId = intval($args['id']);
258  $uploadDao = $this->restHelper->getUploadDao();
259  $clearingDao = $this->container->get('dao.clearing');
260 
261  $this->uploadAccessible($uploadId);
262  $this->isItemExists($uploadId, $itemId);
263 
264  $itemTreeBounds = $uploadDao->getItemTreeBoundsFromUploadId($itemId, $uploadId);
265  $clearingDecWithLicenses = $clearingDao->getFileClearings($itemTreeBounds, $this->restHelper->getGroupId(), false, true);
266 
267  $data = [];
268  $scope = new DecisionScopes();
269 
270  foreach ($clearingDecWithLicenses as $clearingDecision) {
271  $removedLicenses = [];
272  $addedLicenses = [];
273 
274  foreach ($clearingDecision->getClearingLicenses() as $lic) {
275  $shortName = $lic->getShortName();
276  $lic->isRemoved() ? $removedLicenses[] = $shortName : $addedLicenses[] = $shortName;
277  }
278  ksort($removedLicenses, SORT_STRING);
279  ksort($addedLicenses, SORT_STRING);
280  $obj = new ClearingHistory(
281  date('Y-m-d', $clearingDecision->getTimeStamp()),
282  $clearingDecision->getUserName(),
283  $scope->getTypeName($clearingDecision->getScope()),
284  $this->decisionTypes->getConstantNameFromKey($clearingDecision->getType()),
285  $addedLicenses, $removedLicenses);
286  $data[] = $obj->getArray();
287  }
288  return $response->withJson($data, 200);
289  }
290 
300  public function getHighlightEntries($request, $response, $args)
301  {
302  $uploadTreeId = intval($args['itemId']);
303  $uploadId = intval($args['id']);
304  $query = $request->getQueryParams();
305  $uploadDao = $this->restHelper->getUploadDao();
306 
307  $this->uploadAccessible($uploadId);
308  $this->isItemExists($uploadId, $uploadTreeId);
309 
310  $agentId = $query['agentId'] ?? null;
311  $highlightId = $query['highlightId'] ?? null;
312  $licenseId = $query['licenseId'] ?? null;
313  $clearingId = $query['clearingId'] ?? null;
314 
315  if ($licenseId !== null && !$this->dbHelper->doesIdExist("license_ref", "rf_pk", $licenseId)) {
316  throw new HttpNotFoundException("License does not exist");
317  }
318  if ($highlightId !== null && !$this->dbHelper->doesIdExist("highlight", "fl_fk", $highlightId)) {
319  throw new HttpNotFoundException("Highlight does not exist");
320  }
321  if ($agentId !== null && !$this->dbHelper->doesIdExist("agent", "agent_pk", $agentId)) {
322  throw new HttpNotFoundException("Agent does not exist");
323  }
324  if ($clearingId !== null && !$this->dbHelper->doesIdExist("clearing_event", "clearing_event_pk", $clearingId)) {
325  throw new HttpNotFoundException("Clearing does not exist");
326  }
327 
328  $uploadTreeTableName = $uploadDao->getUploadtreeTableName($uploadId);
329  $itemTreeBounds = $uploadDao->getItemTreeBounds($uploadTreeId, $uploadTreeTableName);
331  $viewLicensePlugin = $this->restHelper->getPlugin('view-license');
332  $res = $viewLicensePlugin->getSelectedHighlighting($itemTreeBounds, $licenseId,
333  $agentId, $highlightId, $clearingId, $uploadId);
334 
335  $transformedRes = [];
336  foreach ($res as $value) {
337  $transformedRes[] = $value->getArray();
338  }
339  return $response->withJson($transformedRes, 200);
340  }
341 
351  public function getTreeView($request, $response, $args)
352  {
353  $uploadTreeId = intval($args['itemId']);
354  $uploadId = intval($args['id']);
355  $query = $request->getQueryParams();
356 
357  $queryKeys = array_keys($query);
358  $allowedKeys = ['showQuick', 'agentId', 'flatten', 'scanLicenseFilter', 'editedLicenseFilter', 'sort', 'page', 'limit', 'tagId', 'search', 'filterOpen'];
359  $diff = array_diff($queryKeys, $allowedKeys);
360  if (count($diff) > 0) {
361  throw new HttpBadRequestException("Invalid query parameter(s) : " . implode(",", $diff));
362  }
363 
364  $agentId = $query['agentId'] ?? null;
365  $flatten = $query['flatten'] ?? null;
366  $scanFilter = $query['scanLicenseFilter'] ?? null;
367  $editedFilter = $query['editedLicenseFilter'] ?? null;
368  $sortDir = $query['sort'] ?? null;
369  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
370  $page = $query['page'] ?? null;
371  $limit = $query['limit'] ?? null;
372  } else {
373  $page = $request->getHeaderLine('page');
374  $limit = $request->getHeaderLine('limit');
375  }
376  $tagId = $query['tagId'] ?? null;
377  $sSearch = $query['search'] ?? null;
378  $openCBoxFilter = $query['filterOpen'] ?? null;
379  $show = ($query['showQuick'] !== null && $query['showQuick'] !== 'false') ? true : null;
380  $licenseDao = $this->container->get('dao.license');
381 
382  $uploadDao = $this->restHelper->getUploadDao();
383 
384  $this->uploadAccessible($uploadId);
385  $this->isItemExists($uploadId, $uploadTreeId);
386 
387  if ($agentId !== null && !$this->dbHelper->doesIdExist("agent", "agent_pk", $agentId)) {
388  throw new HttpNotFoundException("Agent does not exist");
389  }
390  if ($tagId !== null && !$this->dbHelper->doesIdExist("tag", "tag_pk", $tagId)) {
391  throw new HttpNotFoundException("Tag does not exist");
392  }
393  if ($openCBoxFilter !== null && ($openCBoxFilter !== 'true' && $openCBoxFilter !== 'false')) {
394  throw new HttpBadRequestException("openCBoxFilter must be a boolean value");
395  }
396  if ($flatten !== null && ($flatten !== 'true' && $flatten !== 'false')) {
397  throw new HttpBadRequestException("flatten must be a boolean value");
398  }
399  if ($sortDir != null && !($sortDir == "asc" || $sortDir == "desc")) {
400  throw new HttpBadRequestException("sortDirection must be asc or desc");
401  }
402  if ($page != null && (!is_numeric($page) || intval($page) < 1)) {
403  throw new HttpBadRequestException("page should be positive integer Greater or Equal to 1");
404  }
405  if ($show != null && $show != 'true' && $show != 'false') {
406  throw new HttpBadRequestException("show must be a boolean value");
407  }
408  if ($limit != null && (!is_numeric($limit) || intval($limit) < 1)) {
409  throw new HttpBadRequestException("limit should be positive integer Greater or Equal to 1");
410  }
411 
412  if ($editedFilter !== null) {
413  $license = $licenseDao->getLicenseByShortName($editedFilter, $this->restHelper->getGroupId());
414  if ($license === null) {
415  throw new HttpNotFoundException("Edited License filter $editedFilter does not exist");
416  }
417  $editedFilter = $license->getId();
418  }
419 
420  if ($scanFilter !== null) {
421  $license = $licenseDao->getLicenseByShortName($scanFilter, $this->restHelper->getGroupId());
422  if ($license === null) {
423  throw new HttpNotFoundException("Scan License filter $scanFilter does not exist");
424  }
425  $scanFilter = $license->getId();
426  }
427 
428  if ($page == null) {
429  $page = 1;
430  }
431  if ($limit == null) {
432  $limit = 50;
433  }
434 
435  if ($show) {
436  $uploadTreeId = $uploadDao->getFatItemId($uploadTreeId, $uploadId, $uploadDao->getUploadtreeTableName($uploadId));
437  }
438 
440  $ajaxExplorerPlugin = $this->restHelper->getPlugin('ajax_explorer');
441  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
442  $symfonyRequest->request->set('agentId', $agentId);
443  $symfonyRequest->request->set('tag', $tagId);
444  $symfonyRequest->request->set('item', $uploadTreeId);
445  $symfonyRequest->request->set('upload', $uploadId);
446  $symfonyRequest->request->set('fromRest', true);
447  $symfonyRequest->request->set('flatten', ($flatten !== null && $flatten !== 'false') ? true : null);
448  $symfonyRequest->request->set('openCBoxFilter', $openCBoxFilter);
449  $symfonyRequest->request->set('show', $show ? "quick" : null);
450  $symfonyRequest->request->set('iSortingCols', "1");
451  $symfonyRequest->request->set('bSortable_0', "true");
452  $symfonyRequest->request->set('iSortCol_0', "0");
453  $symfonyRequest->request->set('sSortDir_0', $sortDir != null ? $sortDir : 'asc');
454  $symfonyRequest->request->set('iDisplayStart', (intval($page) - 1) * intval($limit));
455  $symfonyRequest->request->set('iDisplayLength', intval($limit));
456  $symfonyRequest->request->set("sSearch", $sSearch);
457  $symfonyRequest->request->set("conFilter", $editedFilter);
458  $symfonyRequest->request->set("scanFilter", $scanFilter);
459 
460  $res = $ajaxExplorerPlugin->handle($symfonyRequest);
461 
462  return $response->withJson(json_decode($res->getContent(), true)["aaData"], 200);
463  }
464 
474  public function getLicenseDecisions($request, $response, $args)
475  {
476  $uploadTreeId = intval($args['itemId']);
477  $uploadPk = intval($args['id']);
478  $uploadDao = $this->restHelper->getUploadDao();
479  $licenses = [];
480 
481  $this->uploadAccessible($uploadPk);
482  $this->isItemExists($uploadPk, $uploadTreeId);
483 
484  $itemTreeBounds = $uploadDao->getItemTreeBoundsFromUploadId($uploadTreeId, $uploadPk);
485  if ($itemTreeBounds->containsFiles()) {
486  throw new HttpBadRequestException("Item expected to be a file, container sent.");
487  }
488 
489  list ($addedClearingResults, $removedLicenses) = $this->clearingDecisionEventProcessor->getCurrentClearings(
490  $itemTreeBounds, $this->restHelper->getGroupId(), LicenseMap::CONCLUSION);
491  $licenseEventTypes = new ClearingEventTypes();
492 
493  $mergedArray = [];
494 
495  foreach ($addedClearingResults as $item) {
496  $mergedArray[] = ['item' => $item, 'isRemoved' => false];
497  }
498 
499  foreach ($removedLicenses as $item) {
500  $mergedArray[] = ['item' => $item, 'isRemoved' => true];
501  }
502 
503  $mainLicIds = $this->clearingDao->getMainLicenseIds($uploadPk, $this->restHelper->getGroupId());
504 
505  foreach ($mergedArray as $item) {
506  $clearingResult = $item['item'];
507  $licenseShortName = $clearingResult->getLicenseShortName();
508  $licenseId = $clearingResult->getLicenseId();
509 
510  $types = $this->getAgentInfo($clearingResult);
511  $reportInfo = "";
512  $comment = "";
513  $acknowledgement = "";
514 
515  if ($clearingResult->hasClearingEvent()) {
516  $licenseDecisionEvent = $clearingResult->getClearingEvent();
517  $types[] = $this->getEventInfo($licenseDecisionEvent, $licenseEventTypes);
518  $reportInfo = $licenseDecisionEvent->getReportinfo();
519  $comment = $licenseDecisionEvent->getComment();
520  $acknowledgement = $licenseDecisionEvent->getAcknowledgement();
521  }
522 
523  $obligations = $this->licenseDao->getLicenseObligations([$licenseId], false);
524  $obligations = array_merge($obligations, $this->licenseDao->getLicenseObligations([$licenseId], true));
525  $obligationList = [];
526  foreach ($obligations as $obligation) {
527  $obligationList[] = new Obligation(
528  $obligation['ob_pk'],
529  $obligation['ob_topic'],
530  $obligation['ob_type'],
531  $obligation['ob_text'],
532  $obligation['ob_classification'],
533  $obligation['ob_comment']
534  );
535  }
536  $license = $this->licenseDao->getLicenseById($licenseId,
537  $this->restHelper->getGroupId());
538  $licenseObj = new LicenseDecision(
539  $license->getId(),
540  $licenseShortName,
541  $license->getFullName(),
542  $item['isRemoved'] ? '-' : (!empty($reportInfo) ? $reportInfo : $license->getText()),
543  $license->getUrl(),
544  $types,
545  $item['isRemoved'] ? '-' : $acknowledgement,
546  $item['isRemoved'] ? '-' : $comment,
547  in_array($license->getId(), $mainLicIds),
548  $obligationList,
549  $license->getRisk(),
550  $item['isRemoved']
551  );
552  $licenses[] = $licenseObj->getArray();
553  }
554  return $response->withJson($licenses, 200);
555  }
556 
561  private function getAgentInfo(ClearingResult $licenseDecisionResult)
562  {
563  $agentResults = array();
564  foreach ($licenseDecisionResult->getAgentDecisionEvents() as $agentDecisionEvent) {
565  $agentId = $agentDecisionEvent->getAgentId();
566  $matchId = $agentDecisionEvent->getMatchId();
567  $highlightRegion = $this->highlightDao->getHighlightRegion($matchId);
568  $page = null;
569  $percentage = $agentDecisionEvent->getPercentage();
570  if ($highlightRegion[0] != "" && $highlightRegion[1] != "") {
571  $page = $this->highlightDao->getPageNumberOfHighlightEntry($matchId);
572  }
573  $result = array(
574  'name' => $agentDecisionEvent->getAgentName(),
575  'clearingId' => null,
576  'agentId' => $agentId,
577  'highlightId' => $matchId,
578  'page' => intval($page),
579  'percentage' => $percentage
580  );
581  $agentResults[] = $result;
582  }
583  return $agentResults;
584  }
585 
586  private function getEventInfo($licenseDecisionEvent, $licenseEventTypes)
587  {
588  $type = $licenseEventTypes->getTypeName($licenseDecisionEvent->getEventType());
589  $eventId = null;
590  if ($licenseDecisionEvent->getEventType() == ClearingEventTypes::BULK) {
591  $eventId = $licenseDecisionEvent->getEventId();
592  }
593  return array(
594  'name' => $type,
595  'clearingId' => $eventId,
596  'agentId' => null,
597  'highlightId' => null,
598  'page' => null,
599  'percentage' => null
600  );
601  }
602 
612  public function handleAddEditAndDeleteLicenseDecision($request, $response, $args)
613  {
614  $body = $this->getParsedBody($request);
615  $uploadTreeId = intval($args['itemId']);
616  $uploadId = intval($args['id']);
617  $uploadDao = $this->restHelper->getUploadDao();
618  $errors = [];
619  $success = [];
620 
621  $this->uploadAccessible($uploadId);
622  $this->isItemExists($uploadId, $uploadTreeId);
623 
624  if (empty($body)) {
625  throw new HttpBadRequestException("Request body is missing or empty.");
626  }
627  if (!is_array($body)) {
628  throw new HttpBadRequestException("Request body should be an array.");
629  }
631  $concludeLicensePlugin = $this->restHelper->getPlugin('conclude-license');
632  $res = $concludeLicensePlugin->getCurrentSelectedLicensesTableData($uploadDao->getItemTreeBoundsFromUploadId($uploadTreeId, $uploadId), $this->restHelper->getGroupId(), true);
633 
634  $existingLicenseIds = [];
635  foreach ($res as $license) {
636  $currId = $license['DT_RowId'];
637  $currId = explode(',', $currId)[1];
638  $existingLicenseIds[] = intval($currId);
639  }
640 
641  foreach (array_keys($body) as $index) {
642  $licenseReq = $body[$index];
643 
644  $shortName = $licenseReq['shortName'];
645  if (empty($shortName)) {
646  $error = new Info(400, "Short name missing from the request #" . ($index + 1), InfoType::ERROR);
647  $errors[] = $error->getArray();
648  continue;
649  }
650 
651  $existingLicense = $this->licenseDao->getLicenseByShortName($shortName, $this->restHelper->getGroupId());
652  if ($existingLicense === null) {
653  $error = new Info(404, "License file with short name '$shortName' not found.",
654  InfoType::ERROR);
655  $errors[] = $error->getArray();
656  continue;
657  }
658 
659  if (!isset($licenseReq['add'])) {
660  $error = new Info(400, "'add' property missing from the request #" . ($index + 1), InfoType::ERROR);
661  $errors[] = $error->getArray();
662  continue;
663  }
664 
665  if ($licenseReq['add']) {
666  $columnsToUpdate = [];
667  if (isset($licenseReq['text'])) {
668  $columnsToUpdate[] = [
669  'columnId' => 'reportinfo',
670  'value' => $licenseReq['text']
671  ];
672  }
673  if (isset($licenseReq['ack'])) {
674  $columnsToUpdate[] = [
675  'columnId' => 'acknowledgement',
676  'value' => $licenseReq['ack']
677  ];
678  }
679  if (isset($licenseReq['comment'])) {
680  $columnsToUpdate[] = [
681  'columnId' => 'comment',
682  'value' => $licenseReq['comment']
683  ];
684  }
685 
686  if (in_array($existingLicense->getId(), $existingLicenseIds)) {
687  $valText = "";
688  foreach (array_keys($columnsToUpdate) as $colIdx) {
689  $this->clearingDao->updateClearingEvent($uploadTreeId, $this->restHelper->getUserId(), $this->restHelper->getGroupId(), $existingLicense->getId(), $columnsToUpdate[$colIdx]['columnId'], $columnsToUpdate[$colIdx]['value']);
690  if ($colIdx == count($columnsToUpdate) - 1 && count($columnsToUpdate) > 1) {
691  $valText .= " and ";
692  } else if ($colIdx > 0 && count($columnsToUpdate) > 1) {
693  $valText .= ", ";
694  }
695  $valText .= $columnsToUpdate[$colIdx]['columnId'];
696  }
697  $val = new Info(200, "Successfully updated " . $shortName . "'s license " . $valText, InfoType::INFO);
698  } else {
699  $this->clearingDao->insertClearingEvent($uploadTreeId, $this->restHelper->getUserId(), $this->restHelper->getGroupId(), $existingLicense->getId(), false);
700  foreach (array_keys($columnsToUpdate) as $colIdx) {
701  $this->clearingDao->updateClearingEvent($uploadTreeId, $this->restHelper->getUserId(), $this->restHelper->getGroupId(), $existingLicense->getId(), $columnsToUpdate[$colIdx]['columnId'], $columnsToUpdate[$colIdx]['value']);
702  }
703  $val = new Info(200, "Successfully added " . $shortName . " as a new license decision.", InfoType::INFO);
704  }
705  } else {
706  if (!in_array($existingLicense->getId(), $existingLicenseIds)) {
707  $error = new Info(404, $shortName . " license does not exist on this item", InfoType::ERROR);
708  $errors[] = $error->getArray();
709  continue;
710  }
711  $this->clearingDao->insertClearingEvent($uploadTreeId, $this->restHelper->getUserId(), $this->restHelper->getGroupId(), $existingLicense->getId(), true);
712  $val = new Info(200, "Successfully deleted " . $shortName . " from license decision list.", InfoType::INFO);
713  }
714  $success[] = $val->getArray();
715  }
716 
717  return $response->withJson([
718  'success' => $success,
719  'errors' => $errors
720  ], 200);
721  }
722 
732  public function scheduleBulkScan($request, $response, $args)
733  {
734  $body = $this->getParsedBody($request);
735  $uploadTreeId = intval($args['itemId']);
736  $uploadId = intval($args['id']);
737  $licenseDao = $this->container->get('dao.license');
738 
739  $this->uploadAccessible($uploadId);
740  $this->isItemExists($uploadId, $uploadTreeId);
741 
742  $isValid = true;
743 
744  // Validation errors
745  $errors = [];
746 
747  // Verify if each element from $body was given in the request
748  $requiredFields = ['bulkActions', 'refText', 'bulkScope', 'forceDecision', 'ignoreIrre', 'delimiters', 'scanOnlyFindings'];
749 
750  foreach ($requiredFields as $field) {
751  if (!array_key_exists($field, $body)) {
752  $isValid = false;
753  $errors[] = "$field should be given";
754  }
755  }
756  // Additional validations for specific fields
757  if ($isValid) {
758  // Check if refText is not equal to ""
759  if ($body['refText'] === "") {
760  $isValid = false;
761  $errors[] = "refText should not be empty";
762  }
763  // Check if forceDecision and ignoreIrre are true or false
764  if (!in_array($body['forceDecision'], [true, false]) || !in_array($body['ignoreIrre'], [true, false]) || !in_array($body['scanOnlyFindings'], [true, false])) {
765  $isValid = false;
766  $errors[] = "forceDecision, ignoreIrre and scanOnlyFindings should be either true or false";
767  }
768  // Check if delimiters is a string
769  if (!is_string($body['delimiters'])) {
770  $isValid = false;
771  $errors[] = "delimiters should be a string";
772  }
773  // check if bulkScope value is either folder or upload
774  if (!in_array($body['bulkScope'], ["folder", "upload"])) {
775  $isValid = false;
776  $errors[] = "bulkScope should be either folder or upload";
777  }
778  // check if the bulkActions property is an array and if each element has a valid license id
779  if (!is_array($body['bulkActions'])) {
780  $isValid = false;
781  $errors[] = "bulkActions should be an array";
782  } else {
783  foreach ($body['bulkActions'] as &$license) {
784  $existingLicense = $licenseDao->getLicenseByShortName($license['licenseShortName'],
785  $this->restHelper->getGroupId());
786  if ($existingLicense == null) {
787  $isValid = false;
788  $errors[] = "License with short name " . $license['licenseShortName'] . " does not exist";
789  } else {
790  $licenseAction = $license['licenseAction'] ?? null;
791  if ($licenseAction !== null && !in_array($licenseAction, ["ADD", "REMOVE"])) {
792  $isValid = false;
793  $errors[] = "License action should be either ADD or REMOVE";
794  }
795  $license['licenseId'] = $existingLicense->getId();
796  $license['reportinfo'] = $license['licenseText'] ?? '';
797  $license['action'] = $licenseAction === 'REMOVE' ? 'Remove' : 'Add';
798  }
799  }
800  }
801  }
802 
803  $errorMess = "";
804  if (!$isValid) {
805  foreach ($errors as $error) {
806  $errorMess .= $error . "\n";
807  }
808  throw new HttpBadRequestException($errorMess);
809  }
810 
811  $symfonyRequest = new \Symfony\Component\HttpFoundation\Request();
812  $symfonyRequest->request->set('bulkAction', $body['bulkActions']);
813  $symfonyRequest->request->set('refText', $body['refText']);
814  $symfonyRequest->request->set('bulkScope', $body['bulkScope'] == "folder" ? 'f' : 'u');
815  $symfonyRequest->request->set('uploadTreeId', $uploadTreeId);
816  $symfonyRequest->request->set('forceDecision', $body['forceDecision'] ? 1 : 0);
817  $symfonyRequest->request->set('ignoreIrre', $body['ignoreIrre'] ? 1 : 0);
818  $symfonyRequest->request->set('delimiters', $body['delimiters']);
819  $symfonyRequest->request->set('scanOnlyFindings', $body['scanOnlyFindings'] ? 1 : 0);
820 
822  $changeLicenseBulk = $this->restHelper->getPlugin('change-license-bulk');
823  $res = $changeLicenseBulk->handle($symfonyRequest);
824  $status = $res->getStatusCode();
825 
826  if ($status != 200) {
827  if ($status == Response::HTTP_BAD_REQUEST) {
828  throw new HttpBadRequestException(
829  json_decode($res->getContent(), true)["error"]);
830  }
831  throw new HttpInternalServerErrorException(
832  json_decode($res->getContent(), true)["error"]);
833  }
834 
835  $info = new Info(201, json_decode($res->getContent(), true)["jqid"], InfoType::INFO);
836  return $response->withJson($info->getArray(), $info->getCode());
837  }
838 }
Utility functions to process ClearingDecision.
Wrapper class for license map.
Definition: LicenseMap.php:19
Base controller for REST calls.
isItemExists(int $uploadId, int $itemId)
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
getAgentInfo(ClearingResult $licenseDecisionResult)
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Definition: ApiVersion.php:29
Different type of infos provided by REST.
Definition: InfoType.php:16
Info model to contain general error and return values.
Definition: Info.php:19
RepPathItem($Item, $Repo="files")
Given an uploadtree_pk, retrieve the pfile path.
Definition: common-repo.php:94