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(self::UPDATE_PARAM_NAME, 0) == 1) {
151  return new JsonResponse($this->updateRules($request),
152  JsonResponse::HTTP_OK);
153  } elseif ($request->get("action", 0) === "deleterule") {
154  return new JsonResponse($this->deleteRules($request),
155  JsonResponse::HTTP_OK);
156  }
157  global $SysConf;
158  $vars = [];
159  $vars["firstTypeParam"] = self::INSERT_FIRST_LIC_TYPE_PARAM_NAME;
160  $vars["secondTypeParam"] = self::INSERT_SECOND_LIC_TYPE_PARAM_NAME;
161  $vars["firstNameParam"] = self::INSERT_FIRST_LIC_NAME_PARAM_NAME;
162  $vars["secondNameParam"] = self::INSERT_SECOND_LIC_NAME_PARAM_NAME;
163  $vars["desc"] = self::INSERT_TEXT_PARAM_NAME;
164  $vars["result"] = self::INSERT_RESULT_PARAM_NAME;
165 
166  $vars["updateParam"] = self::UPDATE_PARAM_NAME;
167  $vars["firstLicTypeParam"] = self::FIRST_LIC_TYPE_PARAM_NAME;
168  $vars["secondLicTypeParam"] = self::SECOND_LIC_TYPE_PARAM_NAME;
169  $vars["ruleIdParam"] = self::RULE_ID_PARAM_NAME;
170  $vars["firstLicNameParam"] = self::FIRST_LIC_NAME_PARAM_NAME;
171  $vars["secondLicNameParam"] = self::SECOND_LIC_NAME_PARAM_NAME;
172  $vars["textParam"] = self::TEXT_PARAM_NAME;
173  $vars["resultParam"] = self::RESULT_PARAM_NAME;
174 
175  $licenseType = $SysConf['SYSCONFIG']['LicenseTypes'];
176  $licenseType = explode(',', $licenseType);
177  $licenseType = array_map('trim', $licenseType);
178  $licenseType = ["---", ...$licenseType];
179  $vars['licenseTypes'] = array_combine($licenseType, $licenseType);
180 
181  $vars['ruleArray'] = $this->compatibilityDao->getAllRules();
182 
183  $licenseArray = $this->licenseDao->getLicenseArray(0);
184  $licenseArray = array_column($licenseArray, 'shortname', 'id');
185 
186  $licenseList = [0 => "---"];
187  $licenseList += $licenseArray;
188  $vars['licenseArray'] = $licenseList;
189  return $this->render('admin_license_compatibility_rules.html.twig',
190  $this->mergeWithDefault($vars));
191  }
192 
198  private function updateRules(Request $request)
199  {
200  $rules = [];
201  $update = [
202  "updated" => -1,
203  "inserted" => []
204  ];
205  $licFirstName = $request->get(self::FIRST_LIC_NAME_PARAM_NAME);
206  $insertFirstName = $request->get(self::INSERT_FIRST_LIC_NAME_PARAM_NAME);
207 
208  $licSecondName = $request->get(self::SECOND_LIC_NAME_PARAM_NAME);
209  $insertSecondName = $request->get(self::INSERT_SECOND_LIC_NAME_PARAM_NAME);
210 
211  $licFirstType = $request->get(self::FIRST_LIC_TYPE_PARAM_NAME);
212  $insertFirstType = $request->get(self::INSERT_FIRST_LIC_TYPE_PARAM_NAME);
213 
214  $licSecondType = $request->get(self::SECOND_LIC_TYPE_PARAM_NAME);
215  $insertSecondType = $request->get(self::INSERT_SECOND_LIC_TYPE_PARAM_NAME);
216 
217  $licText = $request->get(self::TEXT_PARAM_NAME);
218  $insertText = $request->get(self::INSERT_TEXT_PARAM_NAME);
219 
220  $licResult = $request->get(self::RESULT_PARAM_NAME);
221  $insertResult = $request->get(self::INSERT_RESULT_PARAM_NAME);
222 
223  if (!empty($licFirstName)) {
224  foreach ($licFirstName as $rulePk => $firstLic) {
225  if ($firstLic == "0") {
226  $rules[$rulePk]['firstLic'] = null;
227  } else {
228  $rules[$rulePk]['firstLic'] = $firstLic;
229  }
230  }
231  }
232  if (!empty($licSecondName)) {
233  foreach ($licSecondName as $rulePk => $secondLic) {
234  if ($secondLic == "0") {
235  $rules[$rulePk]['secondLic'] = null;
236  } else {
237  $rules[$rulePk]['secondLic'] = $secondLic;
238  }
239  }
240  }
241  if (!empty($licFirstType)) {
242  foreach ($licFirstType as $rulePk => $firstType) {
243  if ($firstType == "---") {
244  $rules[$rulePk]['firstType'] = null;
245  } else {
246  $rules[$rulePk]['firstType'] = $firstType;
247  }
248  }
249  }
250  if (!empty($licSecondType)) {
251  foreach ($licSecondType as $rulePk => $secondType) {
252  if ($secondType == "---") {
253  $rules[$rulePk]['secondType'] = null;
254  } else {
255  $rules[$rulePk]['secondType'] = $secondType;
256  }
257  }
258  }
259  if (!empty($licText)) {
260  foreach ($licText as $rulePk => $text) {
261  $rules[$rulePk]['comment'] = $text;
262  }
263  }
264  if (!empty($licResult)) {
265  foreach ($licResult as $rulePk => $result) {
266  $rules[$rulePk]['result'] = $result;
267  }
268  }
269 
270  if (! empty($rules)) {
271  try {
272  $update['updated'] = $this->compatibilityDao->updateRuleFromArray(
273  $rules);
274  } catch (\UnexpectedValueException $e) {
275  $update['updated'] = $e->getMessage();
276  }
277  }
278 
279  $update["inserted"] = $this->insertRules($insertFirstName,
280  $insertSecondName, $insertFirstType, $insertSecondType, $insertText,
281  $insertResult);
282  return $update;
283  }
284 
295  private function insertRules($firstNameArray, $secondNameArray,
296  $firstTypeArray, $secondTypeArray, $commentArray,
297  $resultArray)
298  {
299  $returnVal = [];
300  if ((!empty($firstNameArray) && !empty($secondNameArray)
301  && !empty($firstTypeArray) && !empty($secondTypeArray)
302  && !empty($commentArray))) {
303  for ($i = 0; $i < count($commentArray); $i++) {
304  if ($firstNameArray[$i] == "0") {
305  $firstNameArray[$i] = null;
306  }
307  if ($secondNameArray[$i] == "0") {
308  $secondNameArray[$i] = null;
309  }
310  if ($firstTypeArray[$i] == "---") {
311  $firstTypeArray[$i] = null;
312  }
313  if ($secondTypeArray[$i] == "---") {
314  $secondTypeArray[$i] = null;
315  }
316  $returnVal[] = $this->compatibilityDao->insertRule($firstNameArray[$i],
317  $secondNameArray[$i], $firstTypeArray[$i], $secondTypeArray[$i],
318  $commentArray[$i], $resultArray[$i]);
319  }
320  $returnVal['status'] = 0;
321  // Check if at least one value was inserted
322  if (count(array_filter($returnVal, function($val) {
323  return $val > 0; // No error
324  })) > 0) {
325  $returnVal['status'] |= 1;
326  }
327  // Check if an error occurred while insertion
328  if (in_array(-1, $returnVal)) {
329  $returnVal['status'] |= 1 << 1;
330  }
331  // Check if an exception occurred while insertion
332  if (in_array(-2, $returnVal)) {
333  $returnVal['status'] |= 1 << 2;
334  }
335  }
336  return $returnVal;
337  }
338 
344  private function deleteRules(Request $request)
345  {
346  $returnVal = [];
347  $rulePk = $request->get("rule");
348  $val = $this->compatibilityDao->deleteRule($rulePk);
349  $returnVal['status'] = $val ? 1 : -1;
350  return $returnVal;
351  }
352 }
353 
354 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.
insertRules($firstNameArray, $secondNameArray, $firstTypeArray, $secondTypeArray, $commentArray, $resultArray)
Insert new rules from the UI.