FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseAcknowledgementDao.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
12 namespace Fossology\Lib\Dao;
13 
17 
23 {
26  private $dbManager;
27 
28  function __construct(DbManager $dbManager)
29  {
30  $this->dbManager = $dbManager;
31  }
32 
40  public function getAllAcknowledgements($skipNotSet = false)
41  {
42  $where = "";
43  if ($skipNotSet) {
44  $where = "WHERE name <> 'not-set' AND is_enabled = TRUE";
45  }
46  $sql = "SELECT la_pk, name, acknowledgement, is_enabled " .
47  "FROM license_std_acknowledgement $where " .
48  "ORDER BY la_pk ASC;";
49  return $this->dbManager->getRows($sql);
50  }
51 
59  function updateAcknowledgement($acknowledgementPk, $newName, $newAcknowledgement)
60  {
61  if (!Auth::isAdmin()) {
62  // Only admins can update the acknowledgements.
63  return false;
64  }
65  $this->isAcknowledgementIdValid($acknowledgementPk);
66 
67  $userFk = Auth::getUserId();
68 
69  $sql = "UPDATE license_std_acknowledgement " .
70  "SET name = $2, acknowledgement = $3, updated = NOW(), user_fk = $4 " .
71  "WHERE la_pk = $1 " .
72  "RETURNING 1 AS updated;";
73  $row = $this->dbManager->getSingleRow($sql,
74  [$acknowledgementPk, $newName,
75  StringOperation::replaceUnicodeControlChar($newAcknowledgement), $userFk]);
76  return $row['updated'] == 1;
77  }
78 
87  function insertAcknowledgement($name, $acknowledgement)
88  {
89  if (! Auth::isAdmin()) {
90  // Only admins can add acknowledgements.
91  return -1;
92  }
93 
94  $name = trim($name);
95  $acknowledgement = trim($acknowledgement);
96 
97  if (empty($name) || empty($acknowledgement)) {
98  // Cannot insert empty fields.
99  return -1;
100  }
101 
102  $userFk = Auth::getUserId();
103 
104  $params = [
105  'name' => $name,
106  'acknowledgement' => StringOperation::replaceUnicodeControlChar($acknowledgement),
107  'user_fk' => $userFk
108  ];
109  $statement = __METHOD__ . ".insertNewLicAcknowledgement";
110  $returning = "la_pk";
111  $returnVal = -1;
112  try {
113  $returnVal = $this->dbManager->insertTableRow("license_std_acknowledgement",
114  $params, $statement, $returning);
115  } catch (\Exception $e) {
116  $returnVal = -2;
117  }
118  return $returnVal;
119  }
120 
131  function updateAcknowledgementFromArray($acknowledgementArray)
132  {
133  if (!Auth::isAdmin()) {
134  // Only admins can update the acknowledgements.
135  return false;
136  }
137 
138  $userFk = Auth::getUserId();
139  $updated = 0;
140 
141  foreach ($acknowledgementArray as $acknowledgementPk => $acknowledgement) {
142  if (count($acknowledgement) < 1 ||
143  (! array_key_exists("name", $acknowledgement) &&
144  ! array_key_exists("acknowledgement", $acknowledgement))) {
145  throw new \UnexpectedValueException(
146  "At least name or acknowledgement is " . "required for entry " . $acknowledgementPk);
147  }
148  $this->isAcknowledgementIdValid($acknowledgementPk);
149  $statement = __METHOD__;
150  $params = [$acknowledgementPk, $userFk];
151  $updateStatement = [];
152  if (array_key_exists("name", $acknowledgement)) {
153  $params[] = $acknowledgement["name"];
154  $updateStatement[] = "name = $" . count($params);
155  $statement .= ".name";
156  }
157  if (array_key_exists("acknowledgement", $acknowledgement)) {
158  $params[] = StringOperation::replaceUnicodeControlChar($acknowledgement["acknowledgement"]);
159  $updateStatement[] = "acknowledgement = $" . count($params);
160  $statement .= ".acknowledgement";
161  }
162  $sql = "UPDATE license_std_acknowledgement " .
163  "SET updated = NOW(), user_fk = $2, " . join(",", $updateStatement) .
164  "WHERE la_pk = $1 " .
165  "RETURNING 1 AS updated;";
166  $retVal = $this->dbManager->getSingleRow($sql, $params, $statement);
167  $updated += intval($retVal);
168  }
169  return $updated;
170  }
171 
177  function getAcknowledgement($acknowledgementPk)
178  {
179  $this->isAcknowledgementIdValid($acknowledgementPk);
180  $sql = "SELECT acknowledgement FROM license_std_acknowledgement " . "WHERE la_pk = $1;";
181  $statement = __METHOD__ . ".getAcknowledgement";
182 
183  $acknowledgement = $this->dbManager->getSingleRow($sql, [$acknowledgementPk], $statement);
184  $acknowledgement = $acknowledgement['acknowledgement'];
185  if (strcasecmp($acknowledgement, "null") === 0) {
186  return null;
187  }
188  return $acknowledgement;
189  }
190 
197  function toggleAcknowledgement($acknowledgementPk)
198  {
199  if (! Auth::isAdmin()) {
200  // Only admins can update the acknowledgements.
201  return false;
202  }
203  $this->isAcknowledgementIdValid($acknowledgementPk);
204 
205  $userFk = Auth::getUserId();
206 
207  $sql = "UPDATE license_std_acknowledgement " .
208  "SET is_enabled = NOT is_enabled, user_fk = $2 " .
209  "WHERE la_pk = $1;";
210 
211  $this->dbManager->getSingleRow($sql, [$acknowledgementPk, $userFk]);
212  return true;
213  }
214 
221  private function isAcknowledgementIdValid($acknowledgementPk)
222  {
223  if (! is_int($acknowledgementPk)) {
224  throw new \UnexpectedValueException("Inavlid acknowledgement id");
225  }
226  $sql = "SELECT count(*) AS cnt FROM license_std_acknowledgement " .
227  "WHERE la_pk = $1;";
228 
229  $acknowledgementCount = $this->dbManager->getSingleRow($sql, [$acknowledgementPk]);
230  if ($acknowledgementCount['cnt'] < 1) {
231  // Invalid acknowledgement id
232  throw new \UnexpectedValueException("Inavlid acknowledgement id");
233  }
234  }
235 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static isAdmin()
Check if user is admin.
Definition: Auth.php:92
updateAcknowledgementFromArray($acknowledgementArray)
Update the acknowledgements based only on the values provided.
updateAcknowledgement($acknowledgementPk, $newName, $newAcknowledgement)
Fossology exception.
Definition: Exception.php:15
static replaceUnicodeControlChar($input, $replace="")
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16