FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
user-add.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
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 
20 
21 define("TITLE_USER_ADD", _("Add A User"));
22 
23 class user_add extends FO_Plugin
24 {
25 
27  private $dbManager;
28 
29  function __construct()
30  {
31  $this->Name = "user_add";
32  $this->Title = TITLE_USER_ADD;
33  $this->MenuList = "Admin::Users::Add";
34  $this->DBaccess = PLUGIN_DB_ADMIN;
35  parent::__construct();
36  $this->dbManager = $GLOBALS['container']->get('db.manager');
37  }
38 
44  function Add()
45  {
46 
47  global $PG_CONN;
48 
49  if (! $PG_CONN) {
50  DBconnect();
51  if (! $PG_CONN) {
52  $text = _("NO DB connection!");
53  echo "<pre>$text\n</pre>";
54  }
55  }
56 
57  /* Get the parameters */
58  $User = str_replace("'", "''", GetParm('username', PARM_TEXT));
59  $User = trim($User);
60  $Pass = GetParm('pass1', PARM_TEXT);
61  $Pass2 = GetParm('pass2', PARM_TEXT);
62  $Seed = rand() . rand();
63  $Hash = sha1($Seed . $Pass);
64  $Desc = str_replace("'", "''", GetParm('description', PARM_TEXT));
65  $Perm = GetParm('permission', PARM_INTEGER);
66  $Folder = GetParm('folder', PARM_INTEGER);
67  $Email_notify = GetParm('enote', PARM_TEXT);
68  $Email = str_replace("'", "''", GetParm('email', PARM_TEXT));
69  $agentList = userAgents();
70  $default_bucketpool_fk = GetParm('default_bucketpool_fk', PARM_INTEGER);
71 
72  /* Make sure username looks valid */
73  if (empty($User)) {
74  $text = _("Username must be specified. Not added.");
75  return ($text);
76  }
77  /* limit the user name size to 64 characters when creating an account */
78  if (strlen($User) > 64) {
79  $text = _("Username exceed 64 characters. Not added.");
80  return ($text);
81  }
82  /* Make sure password matches */
83  if ($Pass != $Pass2) {
84  $text = _("Passwords did not match. Not added.");
85  return ($text);
86  }
87 
88  if (empty($Email)) {
89  $text = _("Email must be specified. Not added.");
90  return ($text);
91  }
92 
93  /* Make sure email looks valid */
94  if (! filter_var($Email, FILTER_VALIDATE_EMAIL)) {
95  $text = _("Invalid email address. Not added.");
96  return ($text);
97  }
98 
99  /* Make sure email is unique */
100  $email_count = $this->dbManager->getSingleRow(
101  "SELECT COUNT(*) as count FROM users WHERE user_email = $1 LIMIT 1;",
102  array($Email))["count"];
103  if ($email_count > 0) {
104  $text = _("Email address already exists. Not added.");
105  return ($text);
106  }
107 
108  /* See if the user already exists (better not!) */
109  $row = $this->dbManager->getSingleRow("SELECT * FROM users WHERE LOWER(user_name) = LOWER($1) LIMIT 1;",
110  array($User), $stmt = __METHOD__ . ".getUserIfExisting");
111  if (! empty($row['user_name'])) {
112  $text = _("User already exists. Not added.");
113  return ($text);
114  }
115 
116  /* check email notification, if empty (box not checked), or if no email
117  * specified for the user set to 'n'.
118  */
119  if (empty($Email_notify) || empty($Email)) {
120  $Email_notify = '';
121  }
122 
123  $ErrMsg = add_user($User, $Desc, $Seed, $Hash, $Perm, $Email, $Email_notify,
124  $agentList, $Folder, $default_bucketpool_fk);
125 
126  return ($ErrMsg);
127  } // Add()
128 
129 
130  public function Output()
131  {
132  /* If this is a POST, then process the request. */
133  $User = GetParm('username', PARM_TEXT);
134  if (! empty($User)) {
135  $rc = $this->Add();
136  if (empty($rc)) {
137  $text = _("User");
138  $text1 = _("added");
139  $this->vars['message'] = "$text $User $text1.";
140  } else {
141  $this->vars['message'] = $rc;
142  }
143  }
144 
145  $V = "<form name='formy' method='POST'>\n";
146  $V.= _("To create a new user, enter the following information:<P />\n");
147  $Style = "<tr><td colspan=2 style='background:black;'></td></tr><tr>";
148  $V.= "<table style='border:1px solid black; text-align:left; background:lightyellow;' width='75%'>";
149  $Val = htmlentities(GetParm('username', PARM_TEXT), ENT_QUOTES);
150  $text = _("Username");
151  $V.= "$Style<th width='25%' >$text</th>";
152  $V.= "<td><input type='text' value='$Val' name='username' size=20></td>\n";
153  $V.= "</tr>\n";
154  $Val = htmlentities(GetParm('description', PARM_TEXT), ENT_QUOTES);
155  $text = _("Description, full name, contact, etc. (optional)");
156  $V.= "$Style<th>$text</th>\n";
157  $V.= "<td><input type='text' name='description' value='$Val' size=60></td>\n";
158  $V.= "</tr>\n";
159  $Val = htmlentities(GetParm('email', PARM_TEXT), ENT_QUOTES);
160  $text = _("Email address");
161  $V .= "$Style<th>$text</th>\n";
162  $V.= "<td><input type='text' name='email' value='$Val' size=60></td>\n";
163  $V.= "</tr>\n";
164  $text = _("Access level");
165  $V.= "$Style<th>$text</th>";
166  $V.= "<td><select name='permission'>\n";
167  $text = _("None (very basic, no database access)");
168  $V.= "<option value='" . PLUGIN_DB_NONE . "'>$text</option>\n";
169  $text = _("Read-only (read, but no writes or downloads)");
170  $V.= "<option selected value='" . PLUGIN_DB_READ . "'>$text</option>\n";
171  $text = _("Read-Write (read, download, or edit information)");
172  $V.= "<option value='" . PLUGIN_DB_WRITE . "'>$text</option>\n";
173  $text = _("Clearing Administrator (read, download, edit information and edit decisions)");
174  $V.= "<option value='" . PLUGIN_DB_CADMIN . "'>$text</option>\n";
175  $text = _("Full Administrator (all access including adding and deleting users)");
176  $V.= "<option value='" . PLUGIN_DB_ADMIN . "'>$text</option>\n";
177  $V.= "</select></td>\n";
178  $V.= "</tr>\n";
179  $text = _("User root folder");
180  $V.= "$Style<th>$text";
181  $V.= "</th>";
182  $V.= "<td><select name='folder' class='ui-render-select2'>";
183  $V.= FolderListOption(-1, 0);
184  $V.= "</select></td>\n";
185  $V.= "</tr>\n";
186  $text = _("Password (optional)");
187  $V.= "$Style<th>$text</th><td><input type='password' name='pass1' size=20></td>\n";
188  $V.= "</tr>\n";
189  $text = _("Re-enter password");
190  $V.= "$Style<th>$text</th><td><input type='password' name='pass2' size=20></td>\n";
191  $V.= "</tr>\n";
192  $text = _("E-mail Notification");
193  $text1 = _("Check to enable email notification when upload scan completes .");
194  $V .= "$Style<th>$text</th><td><input type='checkbox'" .
195  "name='enote' value='y' checked='checked'>" .
196  "$text1</td>\n";
197  $V.= "</tr>\n";
198  $text = _("Agents selected by default when uploading");
199  $V .= "$Style<th>$text\n</th><td> ";
200  $V.= AgentCheckBoxMake(-1, array("agent_unpack", "agent_adj2nest", "wget_agent"));
201 
202  $V .= "</td>\n";
203  $text = _("Default bucketpool");
204  $V.= "$Style<th>$text</th>";
205  $V.= "<td>";
206  $default_bucketpool_fk = 0;
207  $V.= SelectBucketPool($default_bucketpool_fk);
208  $V.= "</td>";
209  $V .= "</tr>\n";
210  $V.= "</table border=0><P />";
211 
212  $text = _("Add User");
213  $V.= "<input type='submit' value='$text'>\n";
214  $V.= "</form>\n";
215  return $V;
216  }
217 }
218 $NewPlugin = new user_add;
SelectBucketPool($selected, $active='Y')
Return a select list containing all the active bucketpool&#39;s.
userAgents()
Read the UI form and format the user selected agents into a comma separated list. ...
const PARM_TEXT
Definition: common-parm.php:31
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:44
AgentCheckBoxMake($upload_pk, $SkipAgents=array(), $specified_username="")
Generate a checkbox list of available agents.
add_user($User, $Desc, $Seed, $Hash, $Perm, $Email, $Email_notify, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
Add()
Add a user.
Definition: user-add.php:44
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
Definition: libfossology.h:48
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
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
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695