FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
user-del-helper.php
1 <?php
2 /***********************************************************
3 Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4 Copyright (C) 2017-2018 Siemens AG
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
20 require_once __DIR__ . "/../../lib/php/common-db.php";
21 require_once __DIR__ . "/../../lib/php/common-perms.php";
22 
29 function deleteUser($UserId, $dbManager)
30 {
31  global $PG_CONN;
32 
33  // Prepare all statements
34  $userSelectStatement = __METHOD__ . ".getUser";
35  $dbManager->prepare($userSelectStatement,
36  "SELECT * FROM users WHERE user_pk = $1 LIMIT 1;");
37 
38  $selectGroupStatement = __METHOD__ . ".getGroup";
39  $dbManager->prepare($selectGroupStatement,
40  "SELECT group_pk FROM groups WHERE group_name = $1 LIMIT 1;");
41 
42  $deleteGroupUserStatement = __METHOD__ . ".deleteGroupUser";
43  $dbManager->prepare($deleteGroupUserStatement,
44  "DELETE FROM group_user_member WHERE user_fk = $1;");
45 
46  $deleteUserStatement = __METHOD__ . ".deleteUser";
47  $dbManager->prepare($deleteUserStatement,
48  "DELETE FROM users WHERE user_pk = $1;");
49 
50  $userCheckStatement = __METHOD__ . ".getUserbyName";
51  $dbManager->prepare($userCheckStatement,
52  "SELECT count(*) AS cnt FROM users WHERE user_name = $1 LIMIT 1;");
53 
54  /* See if the user already exists */
55  $result = $dbManager->execute($userSelectStatement, [$UserId]);
56  $row = $dbManager->fetchArray($result);
57  $dbManager->freeResult($result);
58  if (empty($row['user_name'])) {
59  $text = _("User does not exist.");
60  return ($text);
61  }
62 
63  /* Delete the users group
64  * First look up the users group_pk
65  */
66  $result = $dbManager->execute($selectGroupStatement, [$row['user_name']]);
67  $GroupRow = $dbManager->fetchArray($result);
68  $dbManager->freeResult($result);
69 
70  /* Delete all the group user members for this user_pk */
71  $dbManager->freeResult($dbManager->execute($deleteGroupUserStatement, [$UserId]));
72 
73  /* Delete the user */
74  $dbManager->freeResult($dbManager->execute($deleteUserStatement, [$UserId]));
75 
76  /* Now delete their group */
77  DeleteGroup($GroupRow['group_pk'], $PG_CONN);
78 
79  /* Make sure it was deleted */
80  $result = $dbManager->execute($userCheckStatement, [$UserId]);
81  $rowCount = count($dbManager->fetchArray($result)['cnt']);
82  $dbManager->freeResult($result);
83  if ($rowCount != 0) {
84  $text = _("Failed to delete user.");
85  return ($text);
86  }
87 
88  return(null);
89 } // Delete()
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DeleteGroup($group_pk)
Delete a group.