FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ScanOptions.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2017 Siemens AG
4  SPDX-FileCopyrightText: © 2021 Orange by Piotr Pszczola <piotr.pszczola@orange.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 namespace Fossology\UI\Api\Models;
14 
18 use Symfony\Component\HttpFoundation\Request;
19 
20 if (!class_exists("AgentAdder", false)) {
21  require_once dirname(__DIR__, 2) . "/agent-add.php";
22 }
23 require_once dirname(__DIR__, 4) . "/lib/php/common-folders.php";
24 
30 {
35  private $analysis;
40  private $reuse;
45  private $decider;
50  private $scancode;
59  {
60  $this->analysis = $analysis;
61  $this->reuse = $reuse;
62  $this->decider = $decider;
63  $this->scancode = $scancode;
64  }
65 
70  public function getArray()
71  {
72  return [
73  "analysis" => $this->analysis,
74  "reuse" => $this->reuse,
75  "decide" => $this->decider,
76  "scancode" => $this->scancode
77  ];
78  }
79 
91  public function scheduleAgents($folderId, $uploadId , $newUpload = true)
92  {
93  $uploadsAccessible = FolderListUploads_perm($folderId, Auth::PERM_WRITE);
94 
95  if (! $newUpload) {
96  $found = false;
97  foreach ($uploadsAccessible as $singleUpload) {
98  if ($singleUpload['upload_pk'] == $uploadId) {
99  $found = true;
100  break;
101  }
102  }
103  if ($found === false) {
104  throw new HttpNotFoundException(
105  "Folder id $folderId does not have upload id " .
106  "$uploadId or you do not have write access to the folder.");
107  }
108  }
109 
110  $paramAgentRequest = new Request();
111  $agentsToAdd = $this->prepareAgents($paramAgentRequest);
112  $this->prepareReuser($paramAgentRequest);
113  $this->prepareDecider($paramAgentRequest);
114  $this->prepareScancode($paramAgentRequest);
115  $returnStatus = (new \AgentAdder())->scheduleAgents($uploadId, $agentsToAdd, $paramAgentRequest);
116  if (is_numeric($returnStatus)) {
117  return new Info(201, $returnStatus, InfoType::INFO);
118  } else {
119  throw new HttpForbiddenException($returnStatus);
120  }
121  }
122 
128  private function prepareAgents(Request &$request)
129  {
130  $agentsToAdd = [];
131  foreach ($this->analysis->getArray() as $agent => $set) {
132  if ($set === true) {
133  if ($agent == "copyright_email_author") {
134  $agentsToAdd[] = "agent_copyright";
135  $request->request->set("Check_agent_copyright", 1);
136  } elseif ($agent == "patent") {
137  $agentsToAdd[] = "agent_ipra";
138  $request->request->set("Check_agent_ipra", 1);
139  } elseif ($agent == "package") {
140  $agentsToAdd[] = "agent_pkgagent";
141  $request->request->set("Check_agent_pkgagent", 1);
142  } else {
143  $agentsToAdd[] = "agent_$agent";
144  $request->request->set("Check_agent_$agent", 1);
145  }
146  }
147  }
148  return $agentsToAdd;
149  }
150 
155  private function prepareReuser(Request &$request)
156  {
157  if ($this->reuse->getReuseUpload() == 0) {
158  // No upload to reuse
159  return;
160  }
161  $reuserRules = [];
162  if ($this->reuse->getReuseMain() === true) {
163  $reuserRules[] = 'reuseMain';
164  }
165  if ($this->reuse->getReuseEnhanced() === true) {
166  $reuserRules[] = 'reuseEnhanced';
167  }
168  if ($this->reuse->getReuseReport() === true) {
169  $reuserRules[] = 'reuseConf';
170  }
171  if ($this->reuse->getReuseCopyright() === true) {
172  $reuserRules[] = 'reuseCopyright';
173  }
174  $userDao = $GLOBALS['container']->get("dao.user");
175  $reuserSelector = $this->reuse->getReuseUpload() . "," . $userDao->getGroupIdByName($this->reuse->getReuseGroup());
176  $request->request->set('uploadToReuse', $reuserSelector);
177  $request->request->set('reuseMode', $reuserRules);
178  }
179 
184  private function prepareDecider(Request &$request)
185  {
186  $deciderRules = [];
187  if ($this->decider->getNomosMonk() === true) {
188  $deciderRules[] = 'nomosInMonk';
189  }
190  if ($this->decider->getBulkReused() === true) {
191  $deciderRules[] = 'reuseBulk';
192  }
193  if ($this->decider->getNewScanner() === true) {
194  $deciderRules[] = 'wipScannerUpdates';
195  }
196  if ($this->decider->getOjoDecider() === true) {
197  $deciderRules[] = 'ojoNoContradiction';
198  }
199  if (! empty($this->decider->getConcludeLicenseType())) {
200  $deciderRules[] = 'licenseTypeConc';
201  $request->request->set('licenseTypeConc',
202  $this->decider->getConcludeLicenseType());
203  }
204  $request->request->set('deciderRules', $deciderRules);
205  if ($this->analysis->getNomos()) {
206  $request->request->set('Check_agent_nomos', 1);
207  }
208  }
209 
214  private function prepareScancode(Request &$request)
215  {
216  $scancodeRules = [];
217  if ($this->scancode->getScanLicense() === true) {
218  $scancodeRules[] = 'license';
219  }
220  if ($this->scancode->getScanCopyright() === true) {
221  $scancodeRules[] = 'copyright';
222  }
223  if ($this->scancode->getScanEmail() === true) {
224  $scancodeRules[] = 'email';
225  }
226  if ($this->scancode->getScanUrl() === true) {
227  $scancodeRules[] = 'url';
228  }
229  $request->request->set('scancodeFlags', $scancodeRules);
230  }
231 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Info model to contain general error and return values.
Definition: Info.php:19
Model to hold add settings for new scan.
Definition: ScanOptions.php:30
scheduleAgents($folderId, $uploadId, $newUpload=true)
Definition: ScanOptions.php:91
__construct($analysis, $reuse, $decider, $scancode)
Definition: ScanOptions.php:58
FolderListUploads_perm($ParentFolder, $perm)
Returns an array of uploads in a folder.