FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CompatibilityDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Dao;
9 
15 use Monolog\Logger;
16 
22 {
24  private $dbManager;
26  private $logger;
28  private $licenseDao;
30  private $agentDao;
32  private $defaultCompatibility;
34  private $agentName;
35 
36  public function __construct(DbManager $dbManager, Logger $logger,LicenseDao $licenseDao, AgentDao $agentDao)
37  {
38  $this->dbManager = $dbManager;
39  $this->logger = $logger;
40  $this->licenseDao = $licenseDao;
41  $this->agentDao = $agentDao;
42  $this->agentName = "compatibility";
43  $sql = "SELECT compatibility FROM license_rules
44  WHERE first_rf_fk IS NULL AND second_rf_fk IS NULL
45  AND first_type IS NULL AND second_type IS NULL;";
46  $result = $this->dbManager->getSingleRow($sql, [], __METHOD__ .
47  "defaultComp");
48  if (!empty($result) && array_key_exists("compatibility", $result)) {
49  $this->defaultCompatibility = $result["compatibility"];
50  } else {
51  $this->defaultCompatibility = false;
52  }
53  }
54 
63  public function getCompatibilityForFile($itemTreeBounds, $shortname)
64  {
65  $uploadTreeTableName = $itemTreeBounds->getUploadTreeTableName();
66  $uploadTreePk = $itemTreeBounds->getItemId();
67  $stmt = __METHOD__.$uploadTreeTableName;
68  $sql = "SELECT pfile_fk FROM $uploadTreeTableName UT
69  WHERE UT.uploadtree_pk = $1";
70  $result = $this->dbManager->getSingleRow($sql, [$uploadTreePk], $stmt);
71  $pfileId = $result["pfile_fk"];
72  $uploadId = $itemTreeBounds->getUploadId();
74  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
75  $scanJobProxy->createAgentStatus([$this->agentName]);
76  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
77  if (!array_key_exists($this->agentName, $selectedScanners)) {
78  throw new InvalidAgentStageException("Agent " . $this->agentName .
79  " has not been scheduled/completed on the upload.");
80  }
81  $latestAgentId = $selectedScanners[$this->agentName];
82  $license = $this->licenseDao->getLicenseByShortName($shortname, $groupId=null);
83  if ($license === null) {
84  return true;
85  }
86  $licenseId = $license->getId();
87  $stmt = __METHOD__ . "getCompResult";
88  $sql = "SELECT result FROM comp_result
89  WHERE pfile_fk = $1 AND agent_fk= $2 AND result = FALSE AND
90  (first_rf_fk = $3 OR second_rf_fk = $3);";
91  $res = $this->dbManager->getSingleRow($sql,
92  [$pfileId, $latestAgentId, $licenseId], $stmt);
93  return empty($res); // If not empty, there is at least 1 false result.
94  }
95 
100  public function getAllRules()
101  {
102  $sql = "SELECT lr_pk, first_rf_fk, second_rf_fk, first_type, second_type,
103  comment, compatibility
104  FROM license_rules ORDER BY lr_pk;";
105  return $this->dbManager->getRows($sql);
106  }
107 
118  function insertRule($firstName, $secondName, $firstType, $secondType, $comment,
119  $result)
120  {
121  if (! Auth::isAdmin()) {
122  // Only admins can add rules.
123  return -1;
124  }
125  $comment = trim($comment);
126  if (empty($comment)) {
127  // Cannot insert empty fields.
128  return -1;
129  }
130  $params = [
131  'first_rf_fk' => $firstName,
132  'second_rf_fk' => $secondName,
133  'first_type' => $firstType,
134  'second_type' => $secondType,
135  'comment' => $comment,
136  'compatibility' => $result
137  ];
138  $statement = __METHOD__ . ".insertNewLicCompatibilityRule";
139  $returning = "lr_pk";
140  $returnVal = -1;
141  try {
142  $returnVal = $this->dbManager->insertTableRow("license_rules",
143  $params, $statement, $returning);
144  } catch (\Exception $_) {
145  $returnVal = -2;
146  }
147  return $returnVal;
148  }
149 
156  function updateRuleFromArray($ruleArray)
157  {
158  if (!Auth::isAdmin()) {
159  // Only admins can update the rules.
160  return -1;
161  }
162  $updated = 0;
163  foreach ($ruleArray as $rulePk => $rule) {
164  if (count($rule) < 1) {
165  throw new \UnexpectedValueException("Rule has no values");
166  }
167  $this->isRuleIdValid($rulePk);
168  $statement = __METHOD__;
169  $params = [$rulePk];
170  $updateStatement = [];
171  if (array_key_exists("firstLic", $rule)) {
172  $params[] = $rule["firstLic"];
173  $updateStatement[] = "first_rf_fk = $" . count($params);
174  $statement .= ".first_rf_fk";
175  }
176  if (array_key_exists("secondLic", $rule)) {
177  $params[] = $rule["secondLic"];
178  $updateStatement[] = "second_rf_fk = $" . count($params);
179  $statement .= ".second_rf_fk";
180  }
181  if (array_key_exists("firstType", $rule)) {
182  $params[] = $rule["firstType"];
183  $updateStatement[] = "first_type = $" . count($params);
184  $statement .= ".first_type";
185  }
186  if (array_key_exists("secondType", $rule)) {
187  $params[] = $rule["secondType"];
188  $updateStatement[] = "second_type = $" . count($params);
189  $statement .= ".second_type";
190  }
191  if (array_key_exists("comment", $rule)) {
192  $params[] = $rule["comment"];
193  $updateStatement[] = "comment = $" . count($params);
194  $statement .= ".comment";
195  }
196  if (array_key_exists("result", $rule)) {
197  $params[] = $rule["result"];
198  $updateStatement[] = "compatibility = $" . count($params);
199  $statement .= ".compatibility";
200  }
201  $sql = "UPDATE license_rules " .
202  "SET " . join(",", $updateStatement) .
203  " WHERE lr_pk = $1 " .
204  "RETURNING 1 AS updated;";
205  $retVal = $this->dbManager->getSingleRow($sql, $params, $statement);
206  $updated += intval($retVal["updated"]);
207  }
208  return $updated;
209  }
210 
216  private function isRuleIdValid($rulePk)
217  {
218  if (! is_int($rulePk)) {
219  throw new \UnexpectedValueException("Invalid rule id $rulePk");
220  }
221  $sql = "SELECT count(*) AS cnt FROM license_rules " .
222  "WHERE lr_pk = $1;";
223 
224  $ruleCount = $this->dbManager->getSingleRow($sql, [$rulePk]);
225  if ($ruleCount['cnt'] < 1) {
226  // Invalid rule id
227  throw new \UnexpectedValueException("Invalid rule id $rulePk");
228  }
229  }
230 
236  function deleteRule($rulePk)
237  {
238  if (!Auth::isAdmin()) {
239  // Only admins can delete the rules.
240  return false;
241  }
242 
243  $stmt = __METHOD__ . "DeletionOfRule";
244  $sql = "DELETE FROM license_rules " .
245  "WHERE lr_pk = $1 " .
246  "RETURNING lr_pk;";
247 
248  $retVal = $this->dbManager->getSingleRow($sql, [$rulePk], $stmt);
249  if ($retVal["lr_pk"] == $rulePk) {
250  return true;
251  } else {
252  return false;
253  }
254  }
255 
260  public function getDefaultCompatibility()
261  {
262  return $this->defaultCompatibility;
263  }
264 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static isAdmin()
Check if user is admin.
Definition: Auth.php:92
deleteRule($rulePk)
Delete a license compatibility rule.
updateRuleFromArray($ruleArray)
Update the existing rules.
insertRule($firstName, $secondName, $firstType, $secondType, $comment, $result)
Insert new rule in the database.
isRuleIdValid($rulePk)
Check if rule ID exists in DB.
getAllRules()
Get all the existing rules present in the database.
Fossology exception.
Definition: Exception.php:15
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16