FOSSology  4.4.0
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($searchQuery);
229  $ruleArray = $this->compatibilityDao->getAllRules($limit, $offset, $searchQuery);
230 
231  return new JsonResponse([
232  "draw" => $draw,
233  "recordsTotal" => $totalCount,
234  "recordsFiltered" => $totalCount,
235  "data" => $ruleArray,
236  ], JsonResponse::HTTP_OK);
237  }
238 
243  private function addRule()
244  {
245  $result = $this->compatibilityDao->insertEmptyRule();
246  if ($result > 0) {
247  return new JsonResponse(["lr_pk" => $result], JsonResponse::HTTP_OK);
248  }
249  return new JsonResponse(["error" => "Failed to add rule."], JsonResponse::HTTP_BAD_REQUEST);
250  }
251 
257  private function updateRules(Request $request)
258  {
259  $rules = [];
260  $update = [
261  "updated" => -1,
262  "inserted" => []
263  ];
264  $licFirstName = $request->get(self::FIRST_LIC_NAME_PARAM_NAME);
265  $insertFirstName = $request->get(self::INSERT_FIRST_LIC_NAME_PARAM_NAME);
266 
267  $licSecondName = $request->get(self::SECOND_LIC_NAME_PARAM_NAME);
268  $insertSecondName = $request->get(self::INSERT_SECOND_LIC_NAME_PARAM_NAME);
269 
270  $licFirstType = $request->get(self::FIRST_LIC_TYPE_PARAM_NAME);
271  $insertFirstType = $request->get(self::INSERT_FIRST_LIC_TYPE_PARAM_NAME);
272 
273  $licSecondType = $request->get(self::SECOND_LIC_TYPE_PARAM_NAME);
274  $insertSecondType = $request->get(self::INSERT_SECOND_LIC_TYPE_PARAM_NAME);
275 
276  $licText = $request->get(self::TEXT_PARAM_NAME);
277  $insertText = $request->get(self::INSERT_TEXT_PARAM_NAME);
278 
279  $licResult = $request->get(self::RESULT_PARAM_NAME);
280  $insertResult = $request->get(self::INSERT_RESULT_PARAM_NAME);
281 
282  if (!empty($licFirstName)) {
283  foreach ($licFirstName as $rulePk => $firstLic) {
284  if ($firstLic == "0") {
285  $rules[$rulePk]['firstLic'] = null;
286  } else {
287  $rules[$rulePk]['firstLic'] = $firstLic;
288  }
289  }
290  }
291  if (!empty($licSecondName)) {
292  foreach ($licSecondName as $rulePk => $secondLic) {
293  if ($secondLic == "0") {
294  $rules[$rulePk]['secondLic'] = null;
295  } else {
296  $rules[$rulePk]['secondLic'] = $secondLic;
297  }
298  }
299  }
300  if (!empty($licFirstType)) {
301  foreach ($licFirstType as $rulePk => $firstType) {
302  if ($firstType == "---") {
303  $rules[$rulePk]['firstType'] = null;
304  } else {
305  $rules[$rulePk]['firstType'] = $firstType;
306  }
307  }
308  }
309  if (!empty($licSecondType)) {
310  foreach ($licSecondType as $rulePk => $secondType) {
311  if ($secondType == "---") {
312  $rules[$rulePk]['secondType'] = null;
313  } else {
314  $rules[$rulePk]['secondType'] = $secondType;
315  }
316  }
317  }
318  if (!empty($licText)) {
319  foreach ($licText as $rulePk => $text) {
320  $rules[$rulePk]['comment'] = $text;
321  }
322  }
323  if (!empty($licResult)) {
324  foreach ($licResult as $rulePk => $result) {
325  $rules[$rulePk]['result'] = $result;
326  }
327  }
328 
329  if (! empty($rules)) {
330  try {
331  $update['updated'] = $this->compatibilityDao->updateRuleFromArray(
332  $rules);
333  } catch (\UnexpectedValueException $e) {
334  $update['updated'] = $e->getMessage();
335  }
336  }
337 
338  $update["inserted"] = $this->insertRules($insertFirstName,
339  $insertSecondName, $insertFirstType, $insertSecondType, $insertText,
340  $insertResult);
341  return $update;
342  }
343 
354  private function insertRules($firstNameArray, $secondNameArray,
355  $firstTypeArray, $secondTypeArray, $commentArray,
356  $resultArray)
357  {
358  $returnVal = [];
359  if ((!empty($firstNameArray) && !empty($secondNameArray)
360  && !empty($firstTypeArray) && !empty($secondTypeArray)
361  && !empty($commentArray))) {
362  for ($i = 0; $i < count($commentArray); $i++) {
363  if ($firstNameArray[$i] == "0") {
364  $firstNameArray[$i] = null;
365  }
366  if ($secondNameArray[$i] == "0") {
367  $secondNameArray[$i] = null;
368  }
369  if ($firstTypeArray[$i] == "---") {
370  $firstTypeArray[$i] = null;
371  }
372  if ($secondTypeArray[$i] == "---") {
373  $secondTypeArray[$i] = null;
374  }
375  $returnVal[] = $this->compatibilityDao->insertRule($firstNameArray[$i],
376  $secondNameArray[$i], $firstTypeArray[$i], $secondTypeArray[$i],
377  $commentArray[$i], $resultArray[$i]);
378  }
379  $returnVal['status'] = 0;
380  // Check if at least one value was inserted
381  if (count(array_filter($returnVal, function($val) {
382  return $val > 0; // No error
383  })) > 0) {
384  $returnVal['status'] |= 1;
385  }
386  // Check if an error occurred while insertion
387  if (in_array(-1, $returnVal)) {
388  $returnVal['status'] |= 1 << 1;
389  }
390  // Check if an exception occurred while insertion
391  if (in_array(-2, $returnVal)) {
392  $returnVal['status'] |= 1 << 2;
393  }
394  }
395  return $returnVal;
396  }
397 
403  private function deleteRules(Request $request)
404  {
405  $returnVal = [];
406  $rulePk = $request->get("rule");
407  $val = $this->compatibilityDao->deleteRule($rulePk);
408  $returnVal['status'] = $val ? 1 : -1;
409  return $returnVal;
410  }
411 }
412 
413 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.