FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
user-del.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2017 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 "user-del-helper.php";
21 define("TITLE_USER_DEL", _("Delete A User"));
22 
23 use \Fossology\Lib\Auth\Auth;
24 
29 class user_del extends FO_Plugin
30 {
31  function __construct()
32  {
33  $this->Name = "user_del";
34  $this->Title = TITLE_USER_DEL;
35  $this->MenuList = "Admin::Users::Delete";
36  $this->DBaccess = PLUGIN_DB_ADMIN;
37  $this->dbManager = $GLOBALS['container']->get('db.manager');
38 
39  parent::__construct();
40  }
41 
47  function Delete($UserId)
48  {
49  global $PG_CONN;
50  /* See if the user already exists */
51  $sql = "SELECT * FROM users WHERE user_pk = '$UserId' LIMIT 1;";
52  $result = pg_query($PG_CONN, $sql);
53  DBCheckResult($result, $sql, __FILE__, __LINE__);
54  $row = pg_fetch_assoc($result);
55  pg_free_result($result);
56  if (empty($row['user_name'])) {
57  $text = _("User does not exist.");
58  return ($text);
59  }
60 
61  /* Delete the users group
62  * First look up the users group_pk
63  */
64  $sql = "SELECT group_pk FROM groups WHERE group_name = '$row[user_name]' LIMIT 1;";
65  $result = pg_query($PG_CONN, $sql);
66  DBCheckResult($result, $sql, __FILE__, __LINE__);
67  $GroupRow = pg_fetch_assoc($result);
68  pg_free_result($result);
69 
70  /* Delete all the group user members for this user_pk */
71  $sql = "DELETE FROM group_user_member WHERE user_fk = '$UserId'";
72  $result = pg_query($PG_CONN, $sql);
73  DBCheckResult($result, $sql, __FILE__, __LINE__);
74  pg_free_result($result);
75 
76  /* Delete the user */
77  $sql = "DELETE FROM users WHERE user_pk = '$UserId';";
78  $result = pg_query($PG_CONN, $sql);
79  DBCheckResult($result, $sql, __FILE__, __LINE__);
80  pg_free_result($result);
81 
82  /* Now delete their group */
83  DeleteGroup($GroupRow['group_pk']);
84 
85  /* Make sure it was deleted */
86  $sql = "SELECT * FROM users WHERE user_name = '$UserId' LIMIT 1;";
87  $result = pg_query($PG_CONN, $sql);
88  DBCheckResult($result, $sql, __FILE__, __LINE__);
89  $rowCount = pg_num_rows($result);
90  pg_free_result($result);
91  if ($rowCount != 0) {
92  $text = _("Failed to delete user.");
93  return ($text);
94  }
95 
96  return(NULL);
97  } // Delete()
98 
102  public function Output()
103  {
104  global $PG_CONN;
105  $V="";
106  /* If this is a POST, then process the request. */
107  $User = GetParm('userid',PARM_TEXT);
108  $Confirm = GetParm('confirm',PARM_INTEGER);
109  if (! empty($User)) {
110  if ($Confirm != 1) {
111  $rc = "Deletion not confirmed. Not deleted.";
112  } else {
113  $rc = deleteUser($User, $this->dbManager);
114  }
115  if (empty($rc)) {
116  /* Need to refresh the screen */
117  $text = _("User deleted.");
118  $this->vars['message'] = $text;
119  } else {
120  $this->vars['message'] = $rc;
121  }
122  }
123 
124  /* Get the user list */
125  $currentUserId = Auth::getUserId();
126  $sql = "SELECT user_pk,user_name,user_desc FROM users WHERE user_pk != '$currentUserId' AND user_pk != '1' ORDER BY user_name";
127  $result = pg_query($PG_CONN, $sql);
128  DBCheckResult($result, $sql, __FILE__, __LINE__);
129  if (pg_num_rows($result) == 0) {
130  $V .= _("No users to delete.");
131  } else {
132  /* Build HTML form */
133  $V .= _("Deleting a user removes the user entry from the FOSSology system. The user's name, account information, and password will be <font color='red'>permanently</font> removed. (There is no 'undo' to this delete.)<P />\n");
134  $V .= "<form name='formy' method='POST'>\n"; // no url = this url
135  $V .= _("To delete a user, enter the following information:<P />\n");
136  $V .= "<ol>\n";
137  $V .= _("<li>Select the user to delete.<br />");
138  $V .= "<select name='userid' class='ui-render-select2'>\n";
139  while ($row = pg_fetch_assoc($result)) {
140  $V .= "<option value='" . $row['user_pk'] . "'>";
141  $V .= $row['user_name'];
142  $V .= "</option>\n";
143  }
144  $V .= "</select>\n";
145 
146  $text = _("Confirm user deletion");
147  $V .= "<P /><li>$text: <input type='checkbox' name='confirm' value='1'>";
148  $V .= "</ol>\n";
149 
150  $text = _("Delete");
151  $V .= "<input type='submit' value='$text'>\n";
152  $V .= "</form>\n";
153  }
154  pg_free_result($result);
155 
156  return $V;
157  }
158 }
159 
160 $NewPlugin = new user_del();
const PARM_TEXT
Definition: common-parm.php:31
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
Delete($UserId)
Delete a user.
Definition: user-del.php:47
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
delete a user
Definition: user-del.php:29
const PARM_INTEGER
Definition: common-parm.php:25
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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
Output()
Generate the text for this plugin.
Definition: user-del.php:102
DeleteGroup($group_pk)
Delete a group.