FOSSology  4.4.0
Open Source License Compliance by Open Source Software
change-license-bulk.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015, 2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 use Symfony\Component\HttpFoundation\JsonResponse;
15 use Symfony\Component\HttpFoundation\Request;
16 
18 {
19  const NAME = "change-license-bulk";
21  private $licenseDao;
23  private $dbManager;
25  private $uploadDao;
26 
27  function __construct()
28  {
29  parent::__construct(self::NAME, array(
30  self::TITLE => _("Private: schedule a bulk scan from post"),
31  self::PERMISSION => Auth::PERM_WRITE
32  ));
33 
34  $this->dbManager = $this->getObject('db.manager');
35  $this->licenseDao = $this->getObject('dao.license');
36  $this->uploadDao = $this->getObject('dao.upload');
37  }
38 
43  public function handle(Request $request)
44  {
45  $uploadTreeId = $request->get('uploadTreeId');
46  $uploadTreeId = strpos($uploadTreeId, ',') !== false
47  ? explode(',', $uploadTreeId)
48  : intval($uploadTreeId);
49 
50  if (empty($uploadTreeId)) {
51  return new JsonResponse(array("error" => 'bad request'), JsonResponse::HTTP_BAD_REQUEST);
52  }
53 
54  try {
55  $jobQueueId = $this->scheduleBulkScan($uploadTreeId, $request);
56  } catch (Exception $ex) {
57  $errorMsg = $ex->getMessage();
58  return new JsonResponse(array("error" => $errorMsg), JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
59  }
61 
62  return new JsonResponse(array("jqid" => $jobQueueId));
63  }
64 
71  private function scheduleBulkScan($uploadTreeId, Request $request)
72  {
73  if (is_array($uploadTreeId)) {
74  $jqId = array();
75  foreach ($uploadTreeId as $uploadTreePk) {
76  $jqId[] = $this->getJobQueueId($uploadTreePk, $request);
77  }
78  return $jqId;
79  } else {
80  return $this->getJobQueueId($uploadTreeId, $request);
81  }
82  }
89  private function getJobQueueId($uploadTreeId, Request $request)
90  {
91  $uploadEntry = $this->uploadDao->getUploadEntry($uploadTreeId);
92  $uploadId = intval($uploadEntry['upload_fk']);
93  $userId = Auth::getUserId();
94  $groupId = Auth::getGroupId();
95 
96  if ($uploadId <= 0 || !$this->uploadDao->isAccessible($uploadId, $groupId)) {
97  throw new Exception('permission denied');
98  }
99 
100  $bulkScope = $request->get('bulkScope');
101  switch ($bulkScope) {
102  case 'u':
103  $uploadTreeTable = $this->uploadDao->getUploadtreeTableName($uploadId);
104  $topBounds = $this->uploadDao->getParentItemBounds($uploadId, $uploadTreeTable);
105  $uploadTreeId = $topBounds->getItemId();
106  break;
107 
108  case 'f':
109  if (!Isdir($uploadEntry['ufile_mode']) &&
110  !Iscontainer($uploadEntry['ufile_mode']) &&
111  !Isartifact($uploadEntry['ufile_mode'])) {
112  $uploadTreeId = $uploadEntry['parent'] ?: $uploadTreeId;
113  }
114  break;
115 
116  default:
117  throw new InvalidArgumentException('bad scope request');
118  }
119 
120  $refText = $request->get('refText');
121  $actions = $request->get('bulkAction');
122  $ignoreIrrelevantFiles = (intval($request->get('ignoreIrre')) == 1);
123  $scanFindingsOnly = boolval(intval($request->get('scanOnlyFindings')));
124  $delimiters = $request->get('delimiters');
125 
126  $licenseRemovals = array();
127  foreach ($actions as $licenseAction) {
128  $licenseRemovals[$licenseAction['licenseId']] = array(($licenseAction['action']=='Remove'), $licenseAction['comment'], $licenseAction['reportinfo'], $licenseAction['acknowledgement']);
129  }
130  $bulkId = $this->licenseDao->insertBulkLicense($userId, $groupId,
131  $uploadTreeId, $licenseRemovals, $refText, $ignoreIrrelevantFiles,
132  $delimiters, $scanFindingsOnly);
133 
134  if ($bulkId <= 0) {
135  throw new Exception('cannot insert bulk reference');
136  }
137  $upload = $this->uploadDao->getUpload($uploadId);
138  $uploadName = $upload->getFilename();
139  $job_pk = JobAddJob($userId, $groupId, $uploadName, $uploadId);
141  $deciderPlugin = plugin_find("agent_deciderjob");
142  $dependecies = array(array('name' => 'agent_monk_bulk', 'args' => $bulkId));
143  $conflictStrategyId = intval($request->get('forceDecision'));
144  $errorMsg = '';
145  $jqId = $deciderPlugin->AgentAdd($job_pk, $uploadId, $errorMsg, $dependecies, $conflictStrategyId);
146 
147  if (!empty($errorMsg)) {
148  throw new Exception(str_replace('<br>', "\n", $errorMsg));
149  }
150  return $jqId;
151  }
152 }
153 
154 register_plugin(new ChangeLicenseBulk());
scheduleBulkScan($uploadTreeId, Request $request)
handle(Request $request)
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
ReportCachePurgeAll()
Purge all records from the report cache.
Isdir($mode)
Definition: common-dir.php:20
Isartifact($mode)
Definition: common-dir.php:29
Iscontainer($mode)
Definition: common-dir.php:38
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:33
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16