FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
UploadPermissionDao.php
1 <?php
2 /*
3 Copyright (C) 2015-2018, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Dao;
20 
23 use Monolog\Logger;
24 
26 {
28  private $dbManager;
30  private $logger;
31 
32  public function __construct(DbManager $dbManager, Logger $logger)
33  {
34  $this->dbManager = $dbManager;
35  $this->logger = $logger;
36  }
37 
38  public function isAccessible($uploadId, $groupId)
39  {
40  $perm = $this->dbManager->getSingleRow('SELECT perm FROM perm_upload WHERE upload_fk=$1 AND group_fk=$2',
41  array($uploadId, $groupId), __METHOD__.'.group_perm');
42  if ($perm && $perm['perm'] > Auth::PERM_NONE) {
43  return true;
44  }
45 
46  if (!isset($_SESSION) || !array_key_exists(Auth::USER_LEVEL, $_SESSION) || $_SESSION[Auth::USER_LEVEL] === Auth::PERM_NONE) {
47  return false;
48  }
49 
50  $uploadPub = $this->dbManager->getSingleRow('SELECT public_perm FROM upload WHERE upload_pk=$1 AND public_perm>$2',
51  array($uploadId,Auth::PERM_NONE),__METHOD__.'.public_perm');
52  return !empty($uploadPub);
53  }
54 
55  public function isEditable($uploadId, $groupId)
56  {
57  if ($_SESSION[Auth::USER_LEVEL] == PLUGIN_DB_ADMIN) {
58  return true;
59  }
60 
61  $perm = $this->dbManager->getSingleRow('SELECT perm FROM perm_upload WHERE upload_fk=$1 AND group_fk=$2',
62  array($uploadId, $groupId), __METHOD__);
63  return $perm['perm']>=Auth::PERM_WRITE;
64  }
65 
66  public function makeAccessibleToGroup($uploadId, $groupId, $perm=null)
67  {
68  if (null === $perm) {
69  $perm = Auth::PERM_ADMIN;
70  }
71  $this->dbManager->getSingleRow("INSERT INTO perm_upload (perm, upload_fk, group_fk) "
72  . " VALUES($1,$2,$3)",
73  array($perm, $uploadId, $groupId), __METHOD__);
74  }
75 
76  public function makeAccessibleToAllGroupsOf($uploadId, $userId, $perm=null)
77  {
78  if (null === $perm) {
79  $perm = Auth::PERM_ADMIN;
80  }
81 
82  $this->dbManager->getSingleRow("INSERT INTO perm_upload (group_fk, perm, upload_fk)
83  SELECT DISTINCT(gum.group_fk), $perm perm, $uploadId upload_fk
84  FROM group_user_member gum
85  LEFT JOIN perm_upload ON perm_upload.group_fk=gum.group_fk
86  AND upload_fk=$uploadId
87  WHERE perm_upload IS NULL AND gum.user_fk=$userId",
88  array(), __METHOD__.'.insert');
89 
90  }
91 
92  public function updatePermissionId($permId, $permLevel)
93  {
94  if (empty($permLevel)) {
95  $this->dbManager->getSingleRow('DELETE FROM perm_upload WHERE perm_upload_pk=$1',
96  array($permId),
97  __METHOD__ . '.delete');
98  } else {
99  $this->dbManager->getSingleRow('UPDATE perm_upload SET perm=$2 WHERE perm_upload_pk=$1',
100  array($permId, $permLevel),
101  __METHOD__ . '.update');
102  }
103  }
104 
105  public function insertPermission($uploadId, $groupId, $permLevel)
106  {
107  $this->dbManager->getSingleRow("DELETE FROM perm_upload WHERE upload_fk=$1 AND group_fk=$2",
108  array($uploadId,$groupId),
109  __METHOD__.'.avoid_doublet');
110  if ($permLevel == Auth::PERM_NONE) {
111  return;
112  }
113  $this->dbManager->insertTableRow('perm_upload', array('perm'=>$permLevel,'upload_fk'=>$uploadId,'group_fk'=>$groupId));
114  }
115 
116  public function setPublicPermission($uploadId, $permLevel)
117  {
118  $this->dbManager->getSingleRow('UPDATE upload SET public_perm=$2 WHERE upload_pk=$1', array($uploadId, $permLevel));
119  }
120 
121  public function getPublicPermission($uploadId)
122  {
123  $row = $this->dbManager->getSingleRow('SELECT public_perm FROM upload WHERE upload_pk=$1 LIMIT 1',array($uploadId),__METHOD__);
124  return $row['public_perm'];
125  }
126 
127  public function getPermissionGroups($uploadId)
128  {
129  $this->dbManager->prepare($stmt=__METHOD__,
130  "SELECT perm_upload_pk, perm, group_pk, group_name
131  FROM groups, perm_upload
132  WHERE group_fk=group_pk AND upload_fk=$1
133  ORDER BY group_name");
134  $res = $this->dbManager->execute($stmt, array($uploadId));
135  $groupMap = array();
136  while ($row=$this->dbManager->fetchArray($res)) {
137  $groupMap[$row['group_pk']] = $row;
138  }
139  $this->dbManager->freeResult($res);
140  return $groupMap;
141  }
142 }
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28