FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-perms.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library 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 GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this library; if not, write to the Free Software Foundation, Inc.0
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  ***********************************************************/
19 
32 function GetUploadsFromFolder($folder_pk)
33 {
34  $uploads = array();
35  if (empty($folder_pk)) {
36  return $uploads;
37  }
38  GetUploadsFromFolder_recurse($folder_pk, $uploads);
39  return $uploads;
40 } /* GetUploadsFromFolder */
41 
53 function GetUploadsFromFolder_recurse($folder_pk, &$uploads)
54 {
55  global $PG_CONN;
56 
57  $sql = "select * from foldercontents where parent_fk=$folder_pk";
58  $result = pg_query($PG_CONN, $sql);
59  DBCheckResult($result, $sql, __FILE__, __LINE__);
60  while ($row = pg_fetch_assoc($result)) {
61  switch ($row["foldercontents_mode"]) {
62  case 1: // Child is folder
63  GetUploadsFromFolder_recurse($row["child_id"], $uploads);
64  break;
65  case 2: // Child is upload
66  $uploads[] = $row["child_id"];
67  break;
68  default:
69  // Other modes not used at this time
70  }
71  }
72  pg_free_result($result);
73 } /* GetUploadsFromFolder_recurse */
74 
75 
86 function AddUserToGroupArray($GroupRow, &$GroupArray)
87 {
88  /* loop throught $GroupArray to see if the user is already present */
89  $found = false;
90  foreach ($GroupArray as &$Grec) {
91  if ($Grec['user_pk'] == $GroupRow['user_fk']) {
92  /* user already exists in $GroupArray, so make sure they have the highest
93  * permission granted to them.
94  */
95  if ($Grec['group_perm'] < $GroupRow['group_perm']) {
96  $Grec['group_perm'] = $GroupRow['group_perm'];
97  }
98  $found = true;
99  break;
100  }
101  }
102 
103  if (! $found) {
104  $NewGroup = array();
105  $NewGroup['user_pk'] = $GroupRow['user_fk'];
106  $NewGroup['group_pk'] = $GroupRow['group_pk'];
107  $NewGroup['group_name'] = $GroupRow['group_name'];
108  $NewGroup['group_perm'] = $GroupRow['group_perm'];
109  $GroupArray[] = $NewGroup;
110  }
111 }
112 
124 function GetGroupUsers($user_pk, $group_pk, &$GroupArray)
125 {
126  global $PG_CONN;
127  $GroupArray = array();
128 
129  $user_pk = GetArrayVal("UserId", $_SESSION);
130  if (empty($user_pk)) {
131  return $GroupArray;
132  }
133 
134  /****** For this group, get its users ******/
135  if (empty($user_pk)) {
136  $UserCondition = "";
137  } else {
138  $UserCondition = " and user_fk=$user_pk ";
139  }
140 
141  $sql = "select group_pk, group_name, group_perm, user_fk from group_user_member, groups where group_pk=$group_pk and group_pk=group_fk $UserCondition";
142  $result = pg_query($PG_CONN, $sql);
143  DBCheckResult($result, $sql, __FILE__, __LINE__);
144  while ($row = pg_fetch_assoc($result)) {
145  /* Add the user(s) to $GroupArray */
146  AddUserToGroupArray($row, $GroupArray);
147  }
148 }
149 
160 function GetUsersGroups($user_pk='')
161 {
162  global $PG_CONN;
163 
164  $GroupArray = array();
165 
166  if (empty($user_pk)) {
167  $user_pk = GetArrayVal("UserId", $_SESSION);
168  }
169  if (empty($user_pk)) {
170  return $GroupArray; /* user has no groups */
171  }
172  /* find all groups with this user */
173  $sql = "select group_fk as group_pk from group_user_member where user_fk=$user_pk";
174  $result = pg_query($PG_CONN, $sql);
175  DBCheckResult($result, $sql, __FILE__, __LINE__);
176  while ($row = pg_fetch_assoc($result)) {
177  /* Now find all the groups that contain this group */
178  GetGroupUsers($user_pk, $row['group_pk'], $GroupArray);
179  }
180  pg_free_result($result);
181  return $GroupArray;
182 }
183 
192 function GetGroupArray($user_pk)
193 {
194  global $PG_CONN;
195 
196  $GroupArray = array();
197 
198  if ($_SESSION[Auth::USER_LEVEL] == PLUGIN_DB_ADMIN) {
199  $sql = "select group_pk, group_name from groups";
200  } else {
201  $sql = "select group_pk, group_name from groups, group_user_member
202  where group_pk=group_fk and user_fk='$user_pk' and group_perm=1";
203  }
204  $result = pg_query($PG_CONN, $sql);
205  DBCheckResult($result, $sql, __FILE__, __LINE__);
206  if (pg_num_rows($result) > 0) {
207  while ($row = pg_fetch_assoc($result)) {
208  $GroupArray[$row['group_pk']] = $row['group_name'];
209  }
210  }
211  pg_free_result($result);
212 
213  natcasesort($GroupArray);
214  return $GroupArray;
215 }
216 
217 
223 function DeleteGroup($group_pk)
224 {
225  global $PG_CONN;
226 
227  $user_pk = Auth::getUserId();
228 
229  /* Make sure groupname looks valid */
230  if (empty($group_pk)) {
231  $text = _("Error: Group name must be specified.");
232  return ($text);
233  }
234 
235  /* See if the group already exists */
236  $sql = "SELECT group_pk FROM groups WHERE group_pk = '$group_pk'";
237  $result = pg_query($PG_CONN, $sql);
238  DBCheckResult($result, $sql, __FILE__, __LINE__);
239  if (pg_num_rows($result) < 1) {
240  pg_free_result($result);
241  $text = _("Group does not exist. Not deleted.");
242  return ($text);
243  }
244  pg_free_result($result);
245 
246  /* Make sure the user has permission to delete this group
247  * Look through all the group users (table group_user_member)
248  * and make sure the user has admin access.
249  */
250  if ($_SESSION[Auth::USER_LEVEL] != PLUGIN_DB_ADMIN) {
251  $sql = "SELECT * FROM group_user_member WHERE group_fk = '$group_pk' and user_fk='$user_pk' and group_perm=1";
252  $result = pg_query($PG_CONN, $sql);
253  DBCheckResult($result, $sql, __FILE__, __LINE__);
254  if (pg_num_rows($result) < 1) {
255  pg_free_result($result);
256  $text = _("Permission Denied.");
257  return ($text);
258  }
259  pg_free_result($result);
260  }
261 
262  /* Start transaction */
263  $sql = "begin";
264  $result = pg_query($PG_CONN, $sql);
265  DBCheckResult($result, $sql, __FILE__, __LINE__);
266  pg_free_result($result);
267 
268  /* Delete group records from perm_upload */
269  $sql = "delete from perm_upload where group_fk='$group_pk'";
270  $result = pg_query($PG_CONN, $sql);
271  DBCheckResult($result, $sql, __FILE__, __LINE__);
272  pg_free_result($result);
273 
274  /* Delete group records from group_user_member */
275  $sql = "delete from group_user_member where group_fk='$group_pk'";
276  $result = pg_query($PG_CONN, $sql);
277  DBCheckResult($result, $sql, __FILE__, __LINE__);
278  pg_free_result($result);
279 
280  /* Update new_upload_group_fk and new_upload_perm in users table */
281  $sql = "update users set new_upload_group_fk=NULL, new_upload_perm=NULL where new_upload_group_fk='$group_pk'";
282  $result = pg_query($PG_CONN, $sql);
283  DBCheckResult($result, $sql, __FILE__, __LINE__);
284  pg_free_result($result);
285 
286  /* Delete group records from groups table */
287  $sql = "delete from groups where group_pk='$group_pk'";
288  $result = pg_query($PG_CONN, $sql);
289  DBCheckResult($result, $sql, __FILE__, __LINE__);
290  pg_free_result($result);
291 
292  /* End transaction */
293  $sql = "commit";
294  $result = pg_query($PG_CONN, $sql);
295  DBCheckResult($result, $sql, __FILE__, __LINE__);
296  pg_free_result($result);
297 
298  return (null);
299 }
AddUserToGroupArray($GroupRow, &$GroupArray)
Check if User is already in the $GroupArray.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
GetUsersGroups($user_pk='')
Find all the groups a user belongs to.
GetGroupArray($user_pk)
Get array of groups that this user has admin access to use UserDao::getAdminGroupMap() ...
GetGroupUsers($user_pk, $group_pk, &$GroupArray)
Get all the users users of this group.
GetUploadsFromFolder_recurse($folder_pk, &$uploads)
GetUploadsFromFolder($folder_pk)
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
GetArrayVal($Key, $Arr)
Get the value from a array(map)
Definition: common-ui.php:143
DeleteGroup($group_pk)
Delete a group.