FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadBrowseProxy.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Proxy;
9 
14 
16 {
17  const PRIO_COLUMN = 'priority';
18 
19  protected $groupId;
20  protected $userPerm;
22  protected $dbManager;
23 
24  public function __construct($groupId, $userPerm, DbManager $dbManager, $doSanity=true)
25  {
26  $this->groupId = $groupId;
27  $this->userPerm = $userPerm;
28  $this->dbManager = $dbManager;
29  if ($doSanity) {
30  $this->sanity();
31  }
32  }
33 
34  public function sanity()
35  {
36  $params = array($this->groupId, UploadStatus::OPEN, Auth::PERM_READ);
37  $sql = 'INSERT INTO upload_clearing (upload_fk,group_fk,status_fk,'.self::PRIO_COLUMN.') '
38  . ' SELECT upload_pk,$1,$2,upload_pk as '.self::PRIO_COLUMN
39  . ' FROM upload LEFT JOIN upload_clearing ON upload_pk=upload_fk AND group_fk=$1'
40  . ' WHERE upload_clearing.upload_fk IS NULL'
41  . ' AND (public_perm>=$3 OR EXISTS(SELECT * FROM perm_upload WHERE perm_upload.upload_fk = upload_pk AND group_fk=$1))';
42  $this->dbManager->getSingleRow($sql, $params);
43  }
44 
45  public function updateTable($columnName, $uploadId, $value)
46  {
47  if ($columnName == 'status_fk') {
48  $this->changeStatus($uploadId, $value);
49  } else if ($columnName == 'assignee' && $this->userPerm) {
50  $sql = "UPDATE upload_clearing SET assignee=$1 WHERE group_fk=$2 AND upload_fk=$3";
51  $this->dbManager->getSingleRow($sql, array($value, $this->groupId, $uploadId), $sqlLog = __METHOD__);
52  $this->setAssigneeEvent($uploadId);
53  } else {
54  throw new \Exception('invalid column');
55  }
56  }
57 
58  protected function changeStatus($uploadId, $newStatus)
59  {
60  if ($newStatus == UploadStatus::REJECTED && $this->userPerm) {
61  $this->setStatusAndComment($uploadId, $newStatus, $commentText = '');
62  } else if ($newStatus == UploadStatus::REJECTED) {
63  throw new \Exception('missing permission');
64  } else if ($this->userPerm) {
65  $sql = "UPDATE upload_clearing SET status_fk=$1 WHERE group_fk=$2 AND upload_fk=$3";
66  $this->dbManager->getSingleRow($sql, array($newStatus, $this->groupId, $uploadId), __METHOD__ . '.advisor');
67  } else {
68  $sql = "UPDATE upload_clearing SET status_fk=$1 WHERE group_fk=$2 AND upload_fk=$3 AND status_fk<$4";
69  $params = array($newStatus, $this->groupId, $uploadId, UploadStatus::REJECTED);
70  $this->dbManager->getSingleRow($sql, $params, __METHOD__ . '.user');
71  }
72  if ($newStatus == UploadStatus::CLOSED || $newStatus == UploadStatus::REJECTED) {
73  $this->setCloseEvent($uploadId);
74  }
75  }
76 
77  public function setStatusAndComment($uploadId, $statusId, $commentText)
78  {
79  $sql = "UPDATE upload_clearing SET status_fk=$1, status_comment=$2 WHERE group_fk=$3 AND upload_fk=$4";
80  $this->dbManager->getSingleRow($sql, array($statusId, $commentText, $this->groupId, $uploadId), __METHOD__);
81  if ($statusId == UploadStatus::CLOSED || $statusId == UploadStatus::REJECTED) {
82  $this->setCloseEvent($uploadId);
83  }
84  }
85 
92  private function setAssigneeEvent($uploadId)
93  {
94  $sql = "SELECT 1 as exists FROM upload_events WHERE upload_fk = $1 " .
95  "AND event_type = " . UploadEvents::ASSIGNEE_EVENT;
96  $row = $this->dbManager->getSingleRow($sql, [$uploadId],
97  __METHOD__ . ".exists");
98  if (empty($row) || empty($row["exists"])) {
99  $sql = "INSERT INTO upload_events (upload_fk, event_type) VALUES ($1, " .
101  $this->dbManager->getSingleRow($sql, [$uploadId],
102  __METHOD__ . ".insert");
103  }
104  }
105 
112  private function setCloseEvent($uploadId)
113  {
114  $sql = "SELECT 1 as exists FROM upload_events WHERE upload_fk = $1 " .
115  "AND event_type = " . UploadEvents::UPLOAD_CLOSED_EVENT;
116  $row = $this->dbManager->getSingleRow($sql, [$uploadId],
117  __METHOD__ . ".exists");
118  if (empty($row) || empty($row["exists"])) {
119  $sql = "INSERT INTO upload_events (upload_fk, event_type) VALUES ($1, " .
121  $this->dbManager->getSingleRow($sql, [$uploadId],
122  __METHOD__ . ".insert");
123  }
124  }
125 
126  public function moveUploadToInfinity($uploadId, $top)
127  {
128  $fun = $top ? 'MAX('.self::PRIO_COLUMN.')+1' : 'MIN('.self::PRIO_COLUMN.')-1';
129  $sql = "UPDATE upload_clearing SET ".self::PRIO_COLUMN."=(SELECT $fun FROM upload_clearing WHERE group_fk=$1)"
130  . " WHERE group_fk=$1 AND upload_fk=$2";
131  $this->dbManager->getSingleRow($sql,
132  array($this->groupId,$uploadId),
133  __METHOD__.($top?'+':'-'));
134  }
135 
136  public function moveUploadBeyond($moveUpload, $beyondUpload)
137  {
138  $this->dbManager->begin();
139  $this->dbManager->prepare($stmt = __METHOD__ . '.get.single.Upload',
140  $sql='SELECT upload_fk,'.self::PRIO_COLUMN.' FROM upload_clearing WHERE group_fk=$1 AND upload_fk=$2');
141  $movePoint = $this->dbManager->getSingleRow($sql, array($this->groupId,$moveUpload), $stmt);
142  $beyondPoint = $this->dbManager->getSingleRow($sql, array($this->groupId,$beyondUpload), $stmt);
143 
144  if ($movePoint[self::PRIO_COLUMN] > $beyondPoint[self::PRIO_COLUMN]) {
145  $farPoint = $this->dbManager->getSingleRow("SELECT MAX(".self::PRIO_COLUMN.") m FROM upload_clearing WHERE group_fk=$1 AND ".self::PRIO_COLUMN."<$2",
146  array($this->groupId,$beyondPoint[self::PRIO_COLUMN]), 'get.upload.with.lower.priority');
147  $farPrio = $farPoint['m']!==null ? $farPoint['m'] : $beyondPoint[self::PRIO_COLUMN]-1;
148  } else {
149  $farPoint = $this->dbManager->getSingleRow("SELECT MIN(".self::PRIO_COLUMN.") m FROM upload_clearing WHERE group_fk=$1 AND ".self::PRIO_COLUMN.">$2",
150  array($this->groupId,$beyondPoint[self::PRIO_COLUMN]), 'get.upload.with.higher.priority');
151  $farPrio = $farPoint['m']!==null ? $farPoint['m'] : $beyondPoint[self::PRIO_COLUMN]+1;
152  }
153 
154  $newPriority = ($farPrio + $beyondPoint[self::PRIO_COLUMN]) / 2;
155  $this->dbManager->getSingleRow('UPDATE upload_clearing SET '.self::PRIO_COLUMN.'=$1 WHERE group_fk=$2 AND upload_fk=$3',
156  array($newPriority, $this->groupId, $moveUpload),
157  __METHOD__.'.update.priority');
158  $this->dbManager->commit();
159  }
160 
165  public function getFolderPartialQuery(& $params)
166  {
167  if (count($params)!=1) {
168  throw new \Exception('expected argument to be array with exactly one element for folderId');
169  }
170  if (! is_array($params[0])) {
171  $params[0] = [$params[0]];
172  }
173  $params[0] = '{' . implode(',', $params[0]) . '}';
174  $params[] = $this->groupId;
175  $params[] = Auth::PERM_READ;
176  return 'upload
177  INNER JOIN upload_clearing ON upload_pk = upload_clearing.upload_fk AND group_fk=$2
178  INNER JOIN uploadtree ON upload_pk = uploadtree.upload_fk AND upload.pfile_fk = uploadtree.pfile_fk
179  WHERE upload_pk IN (SELECT child_id FROM foldercontents WHERE foldercontents_mode&2 != 0 AND parent_fk = ANY($1::int[]) )
180  AND (public_perm>=$3
181  OR EXISTS(SELECT * FROM perm_upload WHERE perm_upload.upload_fk = upload_pk AND group_fk=$2))
182  AND parent IS NULL
183  AND lft IS NOT NULL';
184  }
185 
191  public function getStatus($uploadId)
192  {
193  $row = $this->dbManager->getSingleRow("SELECT status_fk FROM upload_clearing WHERE upload_fk=$1 AND group_fk=$2",
194  array($uploadId, $this->groupId));
195  if (false === $row) {
196  throw new \Exception("cannot find uploadId=$uploadId");
197  }
198  return $row['status_fk'];
199  }
200 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
This class contains the events for the upload_events table.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16