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();
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 
127  private function prepareAgents()
128  {
129  $agentsToAdd = [];
130  foreach ($this->analysis->getArray() as $agent => $set) {
131  if ($set === true) {
132  if ($agent == "copyright_email_author") {
133  $agentsToAdd[] = "agent_copyright";
134  } else {
135  $agentsToAdd[] = "agent_$agent";
136  }
137  }
138  }
139  return $agentsToAdd;
140  }
141 
146  private function prepareReuser(Request &$request)
147  {
148  if ($this->reuse->getReuseUpload() == 0) {
149  // No upload to reuse
150  return;
151  }
152  $reuserRules = [];
153  if ($this->reuse->getReuseMain() === true) {
154  $reuserRules[] = 'reuseMain';
155  }
156  if ($this->reuse->getReuseEnhanced() === true) {
157  $reuserRules[] = 'reuseEnhanced';
158  }
159  if ($this->reuse->getReuseReport() === true) {
160  $reuserRules[] = 'reuseConf';
161  }
162  if ($this->reuse->getReuseCopyright() === true) {
163  $reuserRules[] = 'reuseCopyright';
164  }
165  $userDao = $GLOBALS['container']->get("dao.user");
166  $reuserSelector = $this->reuse->getReuseUpload() . "," . $userDao->getGroupIdByName($this->reuse->getReuseGroup());
167  $request->request->set('uploadToReuse', $reuserSelector);
168  $request->request->set('reuseMode', $reuserRules);
169  }
170 
175  private function prepareDecider(Request &$request)
176  {
177  $deciderRules = [];
178  if ($this->decider->getNomosMonk() === true) {
179  $deciderRules[] = 'nomosInMonk';
180  }
181  if ($this->decider->getBulkReused() === true) {
182  $deciderRules[] = 'reuseBulk';
183  }
184  if ($this->decider->getNewScanner() === true) {
185  $deciderRules[] = 'wipScannerUpdates';
186  }
187  if ($this->decider->getOjoDecider() === true) {
188  $deciderRules[] = 'ojoNoContradiction';
189  }
190  $request->request->set('deciderRules', $deciderRules);
191  if ($this->analysis->getNomos()) {
192  $request->request->set('Check_agent_nomos', 1);
193  }
194  }
195 
200  private function prepareScancode(Request &$request)
201  {
202  $scancodeRules = [];
203  if ($this->scancode->getScanLicense() === true) {
204  $scancodeRules[] = 'license';
205  }
206  if ($this->scancode->getScanCopyright() === true) {
207  $scancodeRules[] = 'copyright';
208  }
209  if ($this->scancode->getScanEmail() === true) {
210  $scancodeRules[] = 'email';
211  }
212  if ($this->scancode->getScanUrl() === true) {
213  $scancodeRules[] = 'url';
214  }
215  $request->request->set('scancodeFlags', $scancodeRules);
216  }
217 }
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.