FOSSology  4.7.1
Open Source License Compliance by Open Source Software
AdminLicenseCompatibilityRules.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
12 namespace Fossology\UI\Page;
13 
18 use Symfony\Component\HttpFoundation\JsonResponse;
19 use Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
21 
27 {
28 
33  const NAME = "admin_license_compatibility_rules";
34 
39  const UPDATE_PARAM_NAME = "formUpdated";
40 
45  const RULE_ID_PARAM_NAME = "licenseRuleLrPK";
46 
51  const INSERT_FIRST_LIC_TYPE_PARAM_NAME = "insertFirstLicType";
52 
57  const FIRST_LIC_TYPE_PARAM_NAME = "firstLicenseType";
58 
63  const INSERT_SECOND_LIC_TYPE_PARAM_NAME = "insertSecondLicType";
64 
69  const SECOND_LIC_TYPE_PARAM_NAME = "secondLicenseType";
70 
75  const INSERT_FIRST_LIC_NAME_PARAM_NAME = "insertFirstLicName";
76 
81  const FIRST_LIC_NAME_PARAM_NAME = "firstLicenseName";
82 
87  const INSERT_SECOND_LIC_NAME_PARAM_NAME = "insertSecondLicName";
88 
93  const SECOND_LIC_NAME_PARAM_NAME = "secondLicenseName";
94 
99  const INSERT_TEXT_PARAM_NAME = "insertLicRuleText";
100 
105  const TEXT_PARAM_NAME = "licenseRuleText";
106 
111  const INSERT_RESULT_PARAM_NAME = "insertLicCompatibilityResult";
112 
117  const RESULT_PARAM_NAME = "licCompatibilityResult";
118 
124 
129  private $licenseDao;
130 
131  function __construct()
132  {
133  parent::__construct(self::NAME,
134  array(
135  self::TITLE => "License Compatibility Rules",
136  self::MENU_LIST => "Admin::License Admin::Compatibility Rules",
137  self::REQUIRES_LOGIN => true,
138  self::PERMISSION => Auth::PERM_ADMIN
139  ));
140  $this->compatibilityDao = $this->getObject('dao.compatibility');
141  $this->licenseDao = $this->getObject('dao.license');
142  }
143 
148  protected function handle(Request $request)
149  {
150  if ($request->get('action') === 'fetchLicenseData') {
151  return $this->fetchLicenseData();
152  }
153  if ($request->get("action", "") === "fetchRules") {
154  return $this->fetchRules($request);
155  }
156 
157  if ($request->get(self::UPDATE_PARAM_NAME, 0) == 1) {
158  return new JsonResponse($this->updateRules($request),
159  JsonResponse::HTTP_OK);
160  } elseif ($request->get("action", 0) === "deleterule") {
161  return new JsonResponse($this->deleteRules($request),
162  JsonResponse::HTTP_OK);
163  } elseif ($request->get("action", 0) === "addrule") {
164  return $this->addRule();
165  }
166  $vars = [];
167  $vars["firstTypeParam"] = self::INSERT_FIRST_LIC_TYPE_PARAM_NAME;
168  $vars["secondTypeParam"] = self::INSERT_SECOND_LIC_TYPE_PARAM_NAME;
169  $vars["firstNameParam"] = self::INSERT_FIRST_LIC_NAME_PARAM_NAME;
170  $vars["secondNameParam"] = self::INSERT_SECOND_LIC_NAME_PARAM_NAME;
171  $vars["desc"] = self::INSERT_TEXT_PARAM_NAME;
172  $vars["result"] = self::INSERT_RESULT_PARAM_NAME;
173 
174  $vars["updateParam"] = self::UPDATE_PARAM_NAME;
175  $vars["firstLicTypeParam"] = self::FIRST_LIC_TYPE_PARAM_NAME;
176  $vars["secondLicTypeParam"] = self::SECOND_LIC_TYPE_PARAM_NAME;
177  $vars["ruleIdParam"] = self::RULE_ID_PARAM_NAME;
178  $vars["firstLicNameParam"] = self::FIRST_LIC_NAME_PARAM_NAME;
179  $vars["secondLicNameParam"] = self::SECOND_LIC_NAME_PARAM_NAME;
180  $vars["textParam"] = self::TEXT_PARAM_NAME;
181  $vars["resultParam"] = self::RESULT_PARAM_NAME;
182 
183  return $this->render('admin_license_compatibility_rules.html.twig',
184  $this->mergeWithDefault($vars));
185  }
186 
191  private function fetchLicenseData()
192  {
193  global $SysConf;
194 
195  $licenseArray = $this->licenseDao->getLicenseArray(0);
196  $licenseArray = array_column($licenseArray, 'shortname', 'id');
197  $licenseList = [0 => "---"];
198  $licenseList += $licenseArray;
199 
200  $licenseTypes = $SysConf['SYSCONFIG']['LicenseTypes'];
201  $licenseTypes = explode(',', $licenseTypes);
202  $licenseTypes = array_map('trim', $licenseTypes);
203  $licenseTypes = ["---", ...$licenseTypes];
204  $licenseTypeList = array_combine($licenseTypes, $licenseTypes);
205 
206  return new JsonResponse([
207  'licenseArray' => $licenseList,
208  'licenseTypes' => $licenseTypeList,
209  ], JsonResponse::HTTP_OK);
210  }
211 
217  private function fetchRules(Request $request)
218  {
219  $offset = intval($request->query->get('start', 0));
220  $limit = intval($request->query->get('length', 10));
221  $draw = intval($request->query->get('draw', 1));
222  $searchQuery = $_GET['search']['value'] ?? '';
223 
224  if (!empty($searchQuery)) {
225  $searchQuery = '%' . $searchQuery . '%';
226  }
227 
228  $totalCount = $this->compatibilityDao->getTotalRulesCount();
229  $filteredCount = empty($searchQuery) ? $totalCount : $this->compatibilityDao->getTotalRulesCount($searchQuery);
230  $ruleArray = $this->compatibilityDao->getAllRules($limit, $offset, $searchQuery);
231 
232  return new JsonResponse([
233  "draw" => $draw,
234  "recordsTotal" => $totalCount,
235  "recordsFiltered" => $filteredCount,
236  "data" => $ruleArray,
237  ], JsonResponse::HTTP_OK);
238  }
239 
244  private function addRule()
245  {
246  $result = $this->compatibilityDao->insertEmptyRule();
247  if ($result > 0) {
248  return new JsonResponse(["lr_pk" => $result], JsonResponse::HTTP_OK);
249  }
250  return new JsonResponse(["error" => "Failed to add rule."], JsonResponse::HTTP_BAD_REQUEST);
251  }
252 
258  private function updateRules(Request $request)
259  {
260  $rules = [];
261  $update = [
262  "updated" => -1,
263  "inserted" => []
264  ];
265  $licFirstName = $request->get(self::FIRST_LIC_NAME_PARAM_NAME);
266  $insertFirstName = $request->get(self::INSERT_FIRST_LIC_NAME_PARAM_NAME);
267 
268  $licSecondName = $request->get(self::SECOND_LIC_NAME_PARAM_NAME);
269  $insertSecondName = $request->get(self::INSERT_SECOND_LIC_NAME_PARAM_NAME);
270 
271  $licFirstType = $request->get(self::FIRST_LIC_TYPE_PARAM_NAME);
272  $insertFirstType = $request->get(self::INSERT_FIRST_LIC_TYPE_PARAM_NAME);
273 
274  $licSecondType = $request->get(self::SECOND_LIC_TYPE_PARAM_NAME);
275  $insertSecondType = $request->get(self::INSERT_SECOND_LIC_TYPE_PARAM_NAME);
276 
277  $licText = $request->get(self::TEXT_PARAM_NAME);
278  $insertText = $request->get(self::INSERT_TEXT_PARAM_NAME);
279 
280  $licResult = $request->get(self::RESULT_PARAM_NAME);
281  $insertResult = $request->get(self::INSERT_RESULT_PARAM_NAME);
282 
283  if (!empty($licFirstName)) {
284  foreach ($licFirstName as $rulePk => $firstLic) {
285  if ($firstLic == "0") {
286  $rules[$rulePk]['firstLic'] = null;
287  } else {
288  $rules[$rulePk]['firstLic'] = $firstLic;
289  }
290  }
291  }
292  if (!empty($licSecondName)) {
293  foreach ($licSecondName as $rulePk => $secondLic) {
294  if ($secondLic == "0") {
295  $rules[$rulePk]['secondLic'] = null;
296  } else {
297  $rules[$rulePk]['secondLic'] = $secondLic;
298  }
299  }
300  }
301  if (!empty($licFirstType)) {
302  foreach ($licFirstType as $rulePk => $firstType) {
303  if ($firstType == "---") {
304  $rules[$rulePk]['firstType'] = null;
305  } else {
306  $rules[$rulePk]['firstType'] = $firstType;
307  }
308  }
309  }
310  if (!empty($licSecondType)) {
311  foreach ($licSecondType as $rulePk => $secondType) {
312  if ($secondType == "---") {
313  $rules[$rulePk]['secondType'] = null;
314  } else {
315  $rules[$rulePk]['secondType'] = $secondType;
316  }
317  }
318  }
319  if (!empty($licText)) {
320  foreach ($licText as $rulePk => $text) {
321  $rules[$rulePk]['comment'] = $text;
322  }
323  }
324  if (!empty($licResult)) {
325  foreach ($licResult as $rulePk => $result) {
326  $rules[$rulePk]['result'] = $result;
327  }
328  }
329 
330  if (! empty($rules)) {
331  try {
332  $update['updated'] = $this->compatibilityDao->updateRuleFromArray(
333  $rules);
334  } catch (\UnexpectedValueException $e) {
335  $update['updated'] = $e->getMessage();
336  }
337  }
338 
339  $update["inserted"] = $this->insertRules($insertFirstName,
340  $insertSecondName, $insertFirstType, $insertSecondType, $insertText,
341  $insertResult);
342  return $update;
343  }
344 
355  private function insertRules($firstNameArray, $secondNameArray,
356  $firstTypeArray, $secondTypeArray, $commentArray,
357  $resultArray)
358  {
359  $returnVal = [];
360  if ((!empty($firstNameArray) && !empty($secondNameArray)
361  && !empty($firstTypeArray) && !empty($secondTypeArray)
362  && !empty($commentArray))) {
363  for ($i = 0; $i < count($commentArray); $i++) {
364  if ($firstNameArray[$i] == "0") {
365  $firstNameArray[$i] = null;
366  }
367  if ($secondNameArray[$i] == "0") {
368  $secondNameArray[$i] = null;
369  }
370  if ($firstTypeArray[$i] == "---") {
371  $firstTypeArray[$i] = null;
372  }
373  if ($secondTypeArray[$i] == "---") {
374  $secondTypeArray[$i] = null;
375  }
376  $returnVal[] = $this->compatibilityDao->insertRule($firstNameArray[$i],
377  $secondNameArray[$i], $firstTypeArray[$i], $secondTypeArray[$i],
378  $commentArray[$i], $resultArray[$i]);
379  }
380  $returnVal['status'] = 0;
381  // Check if at least one value was inserted
382  if (count(array_filter($returnVal, function($val) {
383  return $val > 0; // No error
384  })) > 0) {
385  $returnVal['status'] |= 1;
386  }
387  // Check if an error occurred while insertion
388  if (in_array(-1, $returnVal)) {
389  $returnVal['status'] |= 1 << 1;
390  }
391  // Check if an exception occurred while insertion
392  if (in_array(-2, $returnVal)) {
393  $returnVal['status'] |= 1 << 2;
394  }
395  }
396  return $returnVal;
397  }
398 
404  private function deleteRules(Request $request)
405  {
406  $returnVal = [];
407  $rulePk = $request->get("rule");
408  $val = $this->compatibilityDao->deleteRule($rulePk);
409  $returnVal['status'] = $val ? 1 : -1;
410  return $returnVal;
411  }
412 }
413 
414 register_plugin(new AdminLicenseCompatibilityRules());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)
deleteRules(Request $request)
Delete a rule from the UI.
updateRules(Request $request)
Update the already existing rules.
fetchRules(Request $request)
Fetch the compatibility rules based on search query and pagination.
insertRules($firstNameArray, $secondNameArray, $firstTypeArray, $secondTypeArray, $commentArray, $resultArray)
Insert new rules from the UI.