FOSSology  4.4.0
Open Source License Compliance by Open Source Software
user-add.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
11 use Symfony\Component\HttpFoundation\Request;
12 
13 define("TITLE_USER_ADD", _("Add A User"));
14 
15 class user_add extends DefaultPlugin
16 {
17  const NAME = "user_add";
18 
20  private $dbManager;
21 
22  function __construct()
23  {
24  parent::__construct(self::NAME, array(
25  self::TITLE => TITLE_USER_ADD,
26  self::MENU_LIST => 'Admin::Users::Add',
27  self::REQUIRES_LOGIN => true,
28  self::PERMISSION => Auth::PERM_ADMIN
29  ));
30  $this->dbManager = $this->getObject('db.manager');
31  }
32 
38  public function add(Request $request)
39  {
40  /* Get the parameters */
41  $User = str_replace("'", "''", $request->get('username'));
42  $User = trim($User);
43  $Pass = $request->get('pass1');
44  $Pass2 = $request->get('pass2');
45  $options = array('cost' => 10);
46  $Hash = password_hash($Pass, PASSWORD_DEFAULT, $options);
47  $Desc = str_replace("'", "''", $request->get('description'));
48  $Perm = $request->get('permission');
49  $Folder = $request->get('folder');
50  $Email_notify = $request->get('enote');
51  $Email = str_replace("'", "''", $request->get('email'));
52  $Upload_visibility = $request->get('public');
53  $agentList = is_null($request->get('user_agent_list')) ? userAgents() : $request->get('user_agent_list');
54  $default_bucketpool_fk = $request->get('default_bucketpool_fk');
55 
56  /* Make sure username looks valid */
57  if (empty($User)) {
58  $text = _("Username must be specified. Not added.");
59  return ($text);
60  }
61  /* limit the user name size to 64 characters when creating an account */
62  if (strlen($User) > 64) {
63  $text = _("Username exceed 64 characters. Not added.");
64  return ($text);
65  }
66  /* Make sure password matches */
67  if ($Pass != $Pass2) {
68  $text = _("Passwords did not match. Not added.");
69  return ($text);
70  }
71 
72  /* Make sure password matches policy */
73  $policyRegex = generate_password_policy();
74  $result = preg_match('/^' . $policyRegex . '$/m', $Pass);
75  if ($result !== 1) {
76  $text = _("Password does not match policy.");
77  $text .= "<br />" . generate_password_policy_string();
78  return ($text);
79  }
80 
81  /* Make sure email looks valid (If email field not empty) */
82  if (! empty($Email) && ! filter_var($Email, FILTER_VALIDATE_EMAIL)) {
83  $text = _("Invalid email address. Not added.");
84  return ($text);
85  }
86 
87  /* Make sure email is unique (If email field not empty) */
88  $email_count = 0;
89  if (! empty($Email)) {
90  $email_count = $this->dbManager->getSingleRow(
91  "SELECT COUNT(*) as count FROM users WHERE user_email = $1 LIMIT 1;",
92  array($Email))["count"];
93  }
94  if ($email_count > 0) {
95  $text = _("Email address already exists. Not added.");
96  return ($text);
97  }
98 
99  /* See if the user already exists (better not!) */
100  $row = $this->dbManager->getSingleRow("SELECT * FROM users WHERE LOWER(user_name) = LOWER($1) LIMIT 1;",
101  array($User), $stmt = __METHOD__ . ".getUserIfExisting");
102  if (! empty($row['user_name'])) {
103  $text = _("User already exists. Not added.");
104  return ($text);
105  }
106 
107  /* check email notification, if empty (box not checked), or if no email
108  * specified for the user set to 'n'.
109  */
110  if (empty($Email_notify) || empty($Email)) {
111  $Email_notify = '';
112  }
113 
114  if (empty($Upload_visibility)) {
115  $Upload_visibility = null;
116  }
117 
118  $ErrMsg = add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify, $Upload_visibility,
119  $agentList, $Folder, $default_bucketpool_fk);
120 
121  return ($ErrMsg);
122  } // Add()
123 
124 
125  public function handle(Request $request)
126  {
127  /* If this is a POST, then process the request. */
128  $User = $request->get('username');
129  if (! empty($User)) {
130  $rc = $this->add($request);
131  if (empty($rc)) {
132  $text = _("User");
133  $text1 = _("added");
134  $vars['message'] = "$text $User $text1.";
135  } else {
136  $vars['message'] = $rc;
137  }
138  }
139 
140  $vars['userName'] = htmlentities($request->get('username'), ENT_QUOTES);
141  $vars['userDescription'] = htmlentities($request->get('description'), ENT_QUOTES);
142  $vars['userEmail'] = htmlentities($request->get('email'), ENT_QUOTES);
143  $vars['accessLevel'] = [
147  PLUGIN_DB_CADMIN,
149  ];
150  $vars['folderListOption'] = FolderListOption(-1, 0);
151  $vars['passOptional'] = " (Optional)";
152  if (passwordPolicyEnabled()) {
153  $vars['passOptional'] = "";
154  }
155  $vars['passwordPolicy'] = generate_password_policy_string();
156  if ($vars['passwordPolicy'] == "No policy defined.") {
157  $vars['passwordPolicy'] = "";
158  }
159  $vars['agentSelector'] = AgentCheckBoxMake(-1, array("agent_unpack", "agent_adj2nest", "wget_agent"));
160 
161  $default_bucketpool_fk = 0;
162  $vars['bucketPool'] = SelectBucketPool($default_bucketpool_fk);
163  $vars['formName'] = "user_add";
164  $vars['policyDisabled'] = passwordPolicyEnabled() ? "false" : "true";
165  $vars['policyRegex'] = generate_password_policy();
166  $scripts = "<script src='scripts/togglePasswords.js' type='text/javascript'></script>";
167  $vars['scripts'] = $scripts;
168  return $this->render('user_add.html.twig', $this->mergeWithDefault($vars));
169  }
170 }
171 
172 register_plugin(new user_add());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)
add(Request $request)
Add a user.
Definition: user-add.php:38
handle(Request $request)
Definition: user-add.php:125
userAgents($agents=null)
Read the UI form and format the user selected agents into a comma separated list.
AgentCheckBoxMake($upload_pk, $SkipAgents=array(), $specified_username="")
Generate a checkbox list of available agents.
generate_password_policy_string()
passwordPolicyEnabled()
generate_password_policy()
SelectBucketPool($selected, $active='Y')
Return a select list containing all the active bucketpool's.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify, $Upload_visibility, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
Definition: libfossology.h:36
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:34
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16