FOSSology  4.4.0
Open Source License Compliance by Open Source Software
UploadPermissionDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015-2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Dao;
9 
12 use Monolog\Logger;
13 
15 {
17  private $dbManager;
19  private $logger;
20 
21  public function __construct(DbManager $dbManager, Logger $logger)
22  {
23  $this->dbManager = $dbManager;
24  $this->logger = $logger;
25  }
26 
27  public function isAccessible($uploadId, $groupId)
28  {
29  $perm = $this->dbManager->getSingleRow('SELECT perm FROM perm_upload WHERE upload_fk=$1 AND group_fk=$2',
30  array($uploadId, $groupId), __METHOD__.'.group_perm');
31  if ($perm && $perm['perm'] > Auth::PERM_NONE) {
32  return true;
33  }
34 
35  if (!isset($_SESSION) || !array_key_exists(Auth::USER_LEVEL, $_SESSION) || $_SESSION[Auth::USER_LEVEL] === Auth::PERM_NONE) {
36  return false;
37  }
38 
39  $uploadPub = $this->dbManager->getSingleRow('SELECT public_perm FROM upload WHERE upload_pk=$1 AND public_perm>$2',
40  array($uploadId,Auth::PERM_NONE),__METHOD__.'.public_perm');
41  return !empty($uploadPub);
42  }
43 
44  public function isEditable($uploadId, $groupId)
45  {
46  if ($_SESSION[Auth::USER_LEVEL] == PLUGIN_DB_ADMIN) {
47  return true;
48  }
49 
50  $perm = $this->dbManager->getSingleRow('SELECT perm FROM perm_upload WHERE upload_fk=$1 AND group_fk=$2',
51  array($uploadId, $groupId), __METHOD__);
52  if (! empty($perm) && array_key_exists('perm', $perm)) {
53  return $perm['perm']>=Auth::PERM_WRITE;
54  }
55  return false;
56  }
57 
58  public function makeAccessibleToGroup($uploadId, $groupId, $perm=null)
59  {
60  if (null === $perm) {
61  $perm = Auth::PERM_ADMIN;
62  }
63  $this->dbManager->getSingleRow("INSERT INTO perm_upload (perm, upload_fk, group_fk) "
64  . " VALUES($1,$2,$3)",
65  array($perm, $uploadId, $groupId), __METHOD__);
66  }
67 
68  public function makeAccessibleToAllGroupsOf($uploadId, $userId, $perm=null)
69  {
70  if (null === $perm) {
71  $perm = Auth::PERM_ADMIN;
72  }
73 
74  $this->dbManager->getSingleRow("INSERT INTO perm_upload (group_fk, perm, upload_fk)
75  SELECT DISTINCT(gum.group_fk), $perm perm, $uploadId upload_fk
76  FROM group_user_member gum
77  LEFT JOIN perm_upload ON perm_upload.group_fk=gum.group_fk
78  AND upload_fk=$uploadId
79  WHERE perm_upload IS NULL AND gum.user_fk=$userId",
80  array(), __METHOD__.'.insert');
81 
82  }
83 
84  public function updatePermissionId($permId, $permLevel)
85  {
86  if (empty($permLevel)) {
87  $this->dbManager->getSingleRow('DELETE FROM perm_upload WHERE perm_upload_pk=$1',
88  array($permId),
89  __METHOD__ . '.delete');
90  } else {
91  $this->dbManager->getSingleRow('UPDATE perm_upload SET perm=$2 WHERE perm_upload_pk=$1',
92  array($permId, $permLevel),
93  __METHOD__ . '.update');
94  }
95  }
96 
97  public function insertPermission($uploadId, $groupId, $permLevel)
98  {
99  $this->dbManager->getSingleRow("DELETE FROM perm_upload WHERE upload_fk=$1 AND group_fk=$2",
100  array($uploadId,$groupId),
101  __METHOD__.'.avoid_doublet');
102  if ($permLevel == Auth::PERM_NONE) {
103  return;
104  }
105  $this->dbManager->insertTableRow('perm_upload', array('perm'=>$permLevel,'upload_fk'=>$uploadId,'group_fk'=>$groupId));
106  }
107 
108  public function setPublicPermission($uploadId, $permLevel)
109  {
110  $this->dbManager->getSingleRow('UPDATE upload SET public_perm=$2 WHERE upload_pk=$1', array($uploadId, $permLevel));
111  }
112 
113  public function getPublicPermission($uploadId)
114  {
115  $row = $this->dbManager->getSingleRow('SELECT public_perm FROM upload WHERE upload_pk=$1 LIMIT 1',array($uploadId),__METHOD__);
116  return $row['public_perm'];
117  }
118 
119  public function getPermissionGroups($uploadId)
120  {
121  $this->dbManager->prepare($stmt=__METHOD__,
122  "SELECT perm_upload_pk, perm, group_pk, group_name
123  FROM groups, perm_upload
124  WHERE group_fk=group_pk AND upload_fk=$1
125  ORDER BY group_name");
126  $res = $this->dbManager->execute($stmt, array($uploadId));
127  $groupMap = array();
128  while ($row=$this->dbManager->fetchArray($res)) {
129  $groupMap[$row['group_pk']] = $row;
130  }
131  $this->dbManager->freeResult($res);
132  return $groupMap;
133  }
134 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16