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 
103  public function getAllRules($limit = 10, $offset = 0, $searchTerm = '')
104  {
105  $sql = "SELECT lr_pk, first_rf_fk, second_rf_fk, first_type, second_type,
106  comment, compatibility
107  FROM license_rules";
108  if (!empty($searchTerm)) {
109  $sql .= " WHERE comment ILIKE '$searchTerm'";
110  }
111  $sql .= " ORDER BY lr_pk LIMIT $limit OFFSET $offset;";
112  return $this->dbManager->getRows($sql);
113  }
114 
120  public function getTotalRulesCount($searchTerm = '')
121  {
122  $query = "SELECT COUNT(*) as count FROM license_rules";
123  if (!empty($searchTerm)) {
124  $query .= " WHERE comment ILIKE '$searchTerm'";
125  }
126  $count = $this->dbManager->getSingleRow($query);
127  return $count ? reset($count) : 0;
128  }
129 
134  public function insertEmptyRule()
135  {
136  if (!Auth::isAdmin()) {
137  return -1;
138  }
139  $params = [
140  'first_rf_fk' => null,
141  'second_rf_fk' => null,
142  'first_type' => null,
143  'second_type' => null,
144  'comment' => '',
145  'compatibility' => false
146  ];
147 
148  $statement = __METHOD__ . ".insertEmptyLicCompatibilityRule";
149  $returning = "lr_pk";
150  $returnVal = -1;
151 
152  try {
153  $returnVal = $this->dbManager->insertTableRow("license_rules", $params, $statement, $returning);
154  } catch (\Exception $_) {
155  $returnVal = -2;
156  }
157  return $returnVal;
158  }
159 
170  function insertRule($firstName, $secondName, $firstType, $secondType, $comment,
171  $result)
172  {
173  if (! Auth::isAdmin()) {
174  // Only admins can add rules.
175  return -1;
176  }
177  $comment = trim($comment);
178  if (empty($comment)) {
179  // Cannot insert empty fields.
180  return -1;
181  }
182  $params = [
183  'first_rf_fk' => $firstName,
184  'second_rf_fk' => $secondName,
185  'first_type' => $firstType,
186  'second_type' => $secondType,
187  'comment' => $comment,
188  'compatibility' => $result
189  ];
190  $statement = __METHOD__ . ".insertNewLicCompatibilityRule";
191  $returning = "lr_pk";
192  $returnVal = -1;
193  try {
194  $returnVal = $this->dbManager->insertTableRow("license_rules",
195  $params, $statement, $returning);
196  } catch (\Exception $_) {
197  $returnVal = -2;
198  }
199  return $returnVal;
200  }
201 
208  function updateRuleFromArray($ruleArray)
209  {
210  if (!Auth::isAdmin()) {
211  // Only admins can update the rules.
212  return -1;
213  }
214  $updated = 0;
215  foreach ($ruleArray as $rulePk => $rule) {
216  if (count($rule) < 1) {
217  throw new \UnexpectedValueException("Rule has no values");
218  }
219  $this->isRuleIdValid($rulePk);
220  $statement = __METHOD__;
221  $params = [$rulePk];
222  $updateStatement = [];
223  if (array_key_exists("firstLic", $rule)) {
224  $params[] = $rule["firstLic"];
225  $updateStatement[] = "first_rf_fk = $" . count($params);
226  $statement .= ".first_rf_fk";
227  }
228  if (array_key_exists("secondLic", $rule)) {
229  $params[] = $rule["secondLic"];
230  $updateStatement[] = "second_rf_fk = $" . count($params);
231  $statement .= ".second_rf_fk";
232  }
233  if (array_key_exists("firstType", $rule)) {
234  $params[] = $rule["firstType"];
235  $updateStatement[] = "first_type = $" . count($params);
236  $statement .= ".first_type";
237  }
238  if (array_key_exists("secondType", $rule)) {
239  $params[] = $rule["secondType"];
240  $updateStatement[] = "second_type = $" . count($params);
241  $statement .= ".second_type";
242  }
243  if (array_key_exists("comment", $rule)) {
244  $params[] = $rule["comment"];
245  $updateStatement[] = "comment = $" . count($params);
246  $statement .= ".comment";
247  }
248  if (array_key_exists("result", $rule)) {
249  $params[] = $rule["result"];
250  $updateStatement[] = "compatibility = $" . count($params);
251  $statement .= ".compatibility";
252  }
253  $sql = "UPDATE license_rules " .
254  "SET " . join(",", $updateStatement) .
255  " WHERE lr_pk = $1 " .
256  "RETURNING 1 AS updated;";
257  $retVal = $this->dbManager->getSingleRow($sql, $params, $statement);
258  $updated += intval($retVal["updated"]);
259  }
260  return $updated;
261  }
262 
268  private function isRuleIdValid($rulePk)
269  {
270  if (! is_int($rulePk)) {
271  throw new \UnexpectedValueException("Invalid rule id $rulePk");
272  }
273  $sql = "SELECT count(*) AS cnt FROM license_rules " .
274  "WHERE lr_pk = $1;";
275 
276  $ruleCount = $this->dbManager->getSingleRow($sql, [$rulePk]);
277  if ($ruleCount['cnt'] < 1) {
278  // Invalid rule id
279  throw new \UnexpectedValueException("Invalid rule id $rulePk");
280  }
281  }
282 
288  function deleteRule($rulePk)
289  {
290  if (!Auth::isAdmin()) {
291  // Only admins can delete the rules.
292  return false;
293  }
294 
295  $stmt = __METHOD__ . "DeletionOfRule";
296  $sql = "DELETE FROM license_rules " .
297  "WHERE lr_pk = $1 " .
298  "RETURNING lr_pk;";
299 
300  $retVal = $this->dbManager->getSingleRow($sql, [$rulePk], $stmt);
301  if ($retVal["lr_pk"] == $rulePk) {
302  return true;
303  } else {
304  return false;
305  }
306  }
307 
312  public function getDefaultCompatibility()
313  {
314  return $this->defaultCompatibility;
315  }
316 }
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.
getTotalRulesCount($searchTerm='')
Get the total count of license compatibility rules.
insertRule($firstName, $secondName, $firstType, $secondType, $comment, $result)
Insert new rule in the database.
insertEmptyRule()
Insert a new empty rule in the database.
getAllRules($limit=10, $offset=0, $searchTerm='')
Get all the existing license compatibility rules from the database.
isRuleIdValid($rulePk)
Check if rule ID exists in DB.
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