FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AgentDecisionImporterPlugin.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace Fossology\DecisionImporter\UI;
14 
15 use Exception;
22 use Symfony\Component\HttpFoundation\File\UploadedFile;
23 use Symfony\Component\HttpFoundation\Request;
24 use Symfony\Component\HttpFoundation\Response;
25 
30 {
31  const NAME = 'ui_fodecisionimporter';
32 
34  private $uploadDao;
36  private $folderDao;
38  private $userDao;
39 
40  function __construct()
41  {
42  parent::__construct(self::NAME, array(
43  self::TITLE => _("Decision Dump Importer"),
44  self::PERMISSION => Auth::PERM_ADMIN,
45  self::REQUIRES_LOGIN => TRUE
46  ));
47  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
48  $this->folderDao = $GLOBALS['container']->get('dao.folder');
49  $this->userDao = $GLOBALS['container']->get('dao.user');
50  }
51 
52  function preInstall()
53  {
54  menu_insert("Browse-Pfile::Import&nbsp;FOSSology&nbsp;Dump", 0, self::NAME, $this->getTitle());
55  menu_insert("Main::Upload::Import FOSSology Dump", 0, self::NAME, $this->getTitle());
56  }
57 
64  protected function handle(Request $request): Response
65  {
66  if ($this->handleRequest($request) !== false) {
68  $showJobsPlugin = plugin_find('showjobs');
69  $showJobsPlugin->OutputOpen();
70  return $showJobsPlugin->getResponse();
71  } else {
72  return $this->showUiToChoose();
73  }
74  }
75 
82  public function handleRequest(Request $request)
83  {
85  $uploadedFile = $request->files->get("report");
86  $uploadId = intval($request->get("uploadselect"));
87  if (empty($uploadId) ||
88  $uploadedFile == null ||
89  empty($uploadedFile->getSize())) {
90  return false;
91  } else {
92  return $this->runImport($uploadId, $uploadedFile, $request);
93  }
94  }
95 
100  protected function showUiToChoose(): Response
101  {
102  $vars = array();
103  $groupId = Auth::getGroupId();
104 
105  $rootFolder = $this->folderDao->getRootFolder(Auth::getUserId());
106  $folder_pk = GetParm('folder', PARM_INTEGER);
107  if (empty($folder_pk)) {
108  $folder_pk = $rootFolder->getId();
109  }
110  $vars['folderId'] = $folder_pk;
111 
112  $folderUploads = $this->folderDao->getFolderUploads($folder_pk, $groupId);
113  $uploadsById = array();
114  foreach ($folderUploads as $uploadProgress) {
115  if ($uploadProgress->getGroupId() != $groupId) {
116  continue;
117  }
118  if (!$this->uploadDao->isEditable($uploadProgress->getId(), $groupId)) {
119  continue;
120  }
121  $display = $uploadProgress->getFilename() . _(" from ") . Convert2BrowserTime(date("Y-m-d H:i:s", $uploadProgress->getTimestamp()));
122  $uploadsById[$uploadProgress->getId()] = $display;
123  }
124  $vars['uploadList'] = $uploadsById;
125 
126  $vars['userid'] = Auth::getUserId();
127  $allUsers = $this->userDao->getAllUsers();
128  $usersById = [];
129  $usersById[$vars['userid']] = "-- ME --"; // Select current user by default
130  foreach ($allUsers as $user) {
131  if ($user['user_pk'] != $vars['userid']) {
132  $usersById[$user['user_pk']] = htmlentities($user['user_name']);
133  }
134  }
135  $vars['userList'] = $usersById;
136 
137  $uploadId = GetParm('upload', PARM_INTEGER);
138  if (empty($uploadId)) {
139  reset($uploadsById);
140  $uploadId = key($uploadsById);
141  }
142  $vars['uploadId'] = $uploadId;
143 
144  $folderStructure = $this->folderDao->getFolderStructure($rootFolder->getId());
145  $vars['folderStructure'] = $folderStructure;
146  $vars['baseUri'] = Traceback_uri() . "?mod=" . self::NAME . "&folder=";
147 
148  return $this->render('AgentDecisionImporterPlugin.html.twig', $this->mergeWithDefault($vars));
149  }
150 
159  protected function runImport(int $uploadId, UploadedFile $report, Request $request): array
160  {
162  $decisionImportAgent = plugin_find('agent_fodecisionimporter');
163 
164  $jqCmdArgs = $decisionImportAgent->addReport($report);
165  $jqCmdArgs .= $decisionImportAgent->setAdditionalJqCmdArgs($request);
166 
167  $userId = Auth::getUserId();
168  $groupId = Auth::getGroupId();
169  $dbManager = $this->getObject('db.manager');
170  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
171  . 'WHERE jq_job_fk=job_pk AND jq_type=$1 AND job_group_fk=$4 AND job_user_fk=$3 AND jq_args=$2 AND jq_endtime IS NULL';
172  $params = array($decisionImportAgent->AgentName, $uploadId, $userId, $groupId);
173  $statementName = __METHOD__;
174  if ($jqCmdArgs) {
175  $sql .= ' AND jq_cmd_args=$5';
176  $params[] = $jqCmdArgs;
177  $statementName .= '.args';
178  } else {
179  $sql .= ' AND jq_cmd_args IS NULL';
180  }
181 
182  $scheduled = $dbManager->getSingleRow($sql, $params, $statementName);
183  if (!empty($scheduled)) {
184  return array($scheduled['job_pk'], $scheduled['jq_pk']);
185  }
186 
187  $upload = $this->getUpload($uploadId, $groupId);
188  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
189  $error = "";
190  $jobQueueId = $decisionImportAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
191  if ($jobQueueId < 0) {
192  throw new Exception(_("Cannot schedule") . ": " . $error);
193  }
194  return array($jobId, $jobQueueId);
195  }
196 
204  protected function getUpload(int $uploadId, int $groupId): Upload
205  {
206  if ($uploadId <= 0) {
207  throw new Exception(_("parameter error: $uploadId"));
208  }
209  if (!$this->uploadDao->isAccessible($uploadId, $groupId)) {
210  throw new Exception(_("permission denied"));
211  }
212  $upload = $this->uploadDao->getUpload($uploadId);
213  if ($upload === null) {
214  throw new Exception(_('cannot find uploadId'));
215  }
216  return $upload;
217  }
218 }
219 
220 register_plugin(new AgentDecisionImporterPlugin());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
render($templateName, $vars=null, $headers=null)
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312