21 use Symfony\Component\HttpFoundation\Request;
25 const NAME =
"user_edit";
40 function __construct()
42 parent::__construct(self::NAME, array(
43 self::TITLE => _(
"Edit User Account"),
44 self::MENU_LIST =>
'Admin::Users::Edit User Account',
45 self::REQUIRES_LOGIN =>
true,
50 $this->authHelper = $this->
getObject(
'helper.authHelper');
51 $this->userDao = $this->
getObject(
'dao.user');
66 $user_pk = Auth::getUserId();
72 $vars = array(
'refreshUri' =>
Traceback_uri() .
"?mod=" . self::NAME);
76 $newClient = $this->addNewClient($request);
77 }
catch (\Exception $e) {
78 $newClient = $e->getMessage();
83 $newToken = $this->generateNewToken($request);
84 }
catch (\Exception $e) {
85 $vars[
'message'] = $e->getMessage();
89 $user_pk_to_modify = intval($request->get(
'user_pk'));
90 if (! ($SessionIsAdmin || empty($user_pk_to_modify) ||
91 $user_pk == $user_pk_to_modify)) {
92 $vars[
'content'] = _(
"Your request is not valid.");
93 return $this->
render(
'include/base.html.twig', $this->mergeWithDefault($vars));
100 $BtnText = $request->get(
'UpdateBtn');
101 if (! empty($BtnText)) {
105 $rv = $this->
UpdateUser($UserRec, $SessionIsAdmin);
108 $vars[
'message'] =
"User $UserRec[user_name] updated.";
111 $UserRec = $this->
CreateUserRec($request, $UserRec[
'user_pk']);
112 if ($user_pk == $user_pk_to_modify) {
113 $_SESSION[
'User'] = $UserRec[
'user_name'];
116 if (empty($UserRec[
'user_name']) || $_SESSION[
'User'] != $UserRec[
'user_name']) {
117 $UserRec = $this->
CreateUserRec($request, $UserRec[
'user_pk']);
119 $vars[
'message'] = $rv;
122 $NewUserpk = intval($request->get(
'newuser'));
127 $vars = array_merge($vars, $this->
DisplayForm($UserRec, $SessionIsAdmin));
128 $vars[
'userId'] = $UserRec[
'user_pk'];
129 $vars[
'newToken'] = $newToken;
130 $vars[
'newClient'] = $newClient;
135 $vars[
'maxTokenDate'] = $this->authHelper->getMaxTokenValidity();
136 $vars[
'writeAccess'] = ($_SESSION[Auth::USER_LEVEL] >= 3);
138 $vars[
'policyDisabled'] =
"true";
139 $vars[
'formName'] =
"user_edit";
140 $vars[
'passwordPolicy'] =
"";
142 if ($policy !=
"No policy defined.") {
143 $vars[
'passwordPolicy'] = $policy;
145 $restToken = Auth::getRestTokenType();
146 if ($restToken == Auth::TOKEN_OAUTH) {
147 $restToken =
"oauth";
148 } elseif ($restToken == Auth::TOKEN_BOTH) {
151 $restToken =
"token";
153 $vars[
'resttoken'] = $restToken;
155 return $this->
render(
'user_edit.html.twig', $this->mergeWithDefault($vars));
169 $vars = array(
'isSessionAdmin' => $SessionIsAdmin,
170 'userId' => $UserRec[
'user_pk']);
171 $vars[
'userDescReadOnly'] = $SysConf[
'SYSCONFIG'][
'UserDescReadOnly'];
176 if ($SessionIsAdmin) {
177 $stmt = __METHOD__ .
'.asSessionAdmin';
178 $sql =
"SELECT * FROM users ORDER BY user_name";
182 while ($row = $this->
dbManager->fetchArray($res)) {
183 $allUsers[$row[
'user_pk']] = htmlentities($row[
'user_name']);
186 $vars[
'allUsers'] = $allUsers;
189 $vars[
'userName'] = $UserRec[
'user_name'];
190 $vars[
'userDescription'] = $UserRec[
'user_desc'];
191 $vars[
'userEMail'] = $UserRec[
"user_email"];
192 $vars[
'eMailNotification'] = ($UserRec[
'email_notify'] ==
'y');
194 if ($SessionIsAdmin) {
195 $vars[
'allAccessLevels'] = array(
197 PLUGIN_DB_READ => _(
"Read-only (read, but no writes or downloads)"),
198 PLUGIN_DB_WRITE => _(
"Read-Write (read, download, or edit information)"),
199 PLUGIN_DB_CADMIN => _(
"Clearing Administrator (read, download, edit information and edit decisions)"),
200 PLUGIN_DB_ADMIN => _(
"Full Administrator (all access including adding and deleting users)")
202 $vars[
'accessLevel'] = $UserRec[
'user_perm'];
204 $vars[
'allUserStatuses'] = array(
205 "active" => _(
"Active"),
206 "inactive" => _(
"Inactive")
209 $vars[
'userStatus'] = $UserRec[
'user_status'];
211 $SelectedFolderPk = $UserRec[
'root_folder_fk'];
212 $vars[
'folderListOption'] =
FolderListOption($ParentFolder = -1, $Depth = 0, $IncludeTop = 1, $SelectedFolderPk);
215 $SelectedDefaultFolderPk = $UserRec[
'default_folder_fk'];
216 $vars[
'folderListOption2'] =
FolderListOption($ParentFolder = $UserRec[
'root_folder_fk'], $Depth = 0, $IncludeTop = 1, $SelectedDefaultFolderPk);
218 $vars[
'isBlankPassword'] = ($UserRec[
'_blank_pass'] ==
'on');
220 "agent_adj2nest",
"wget_agent"), $UserRec[
'user_name']);
223 $vars[
'uploadVisibility'] = $UserRec[
'upload_visibility'];
242 if (empty($UserRec[
'user_pk'])) {
243 $Errors .=
"<li>" . _(
"Consistency error (User_pk missing). Please start over.") .
"</li>";
247 if (empty($UserRec[
'user_name'])) {
248 $Errors .=
"<li>" . _(
"Username must be specified.") .
"</li>";
252 $CheckUserRec =
GetSingleRec(
"users",
"WHERE user_name='$UserRec[user_name]'");
253 if ((!empty($CheckUserRec)) and ( $CheckUserRec[
'user_pk'] != $UserRec[
'user_pk'])) {
254 $Errors .=
"<li>" . _(
"Username is not unique.") .
"</li>";
258 if ($UserRec[
'_pass1'] != $UserRec[
'_pass2']) {
259 $Errors .=
"<li>" . _(
"Passwords do not match.") .
"</li>";
262 $oldEmail = $this->
dbManager->getSingleRow(
263 "SELECT user_email FROM users WHERE user_pk = $1;",
264 array($UserRec[
'user_pk']), __METHOD__.
"oldEmail");
265 if (strcmp($oldEmail[
'user_email'],$UserRec[
'user_email']) != 0) {
267 $Check = preg_replace(
"/[^a-zA-Z0-9@_.+-]/",
"", $UserRec[
'user_email']);
268 if ($Check != $UserRec[
'user_email']) {
269 $Errors .=
"<li>" . _(
"Invalid email address.") .
"</li>";
274 if (!empty($UserRec[
'user_email'])) {
275 $email_count = $this->
dbManager->getSingleRow(
276 "SELECT COUNT(*) as count FROM users WHERE user_email = $1 LIMIT 1;",
277 array($UserRec[
'user_email']), __METHOD__.
"email_count")[
"count"];
279 if ($email_count > 0) {
280 $Errors .=
"<li>" . _(
"Email address already exists.") .
"</li>";
286 $Errors .=
"<li>" . _(
"Password policy enabled, can't have a blank password.") .
"</li>";
290 if (!empty($UserRec[
'_blank_pass']) && ( !empty($UserRec[
'_pass1']) || ! empty($UserRec[
'_pass2']))) {
291 $Errors .=
"<li>" . _(
"You cannot specify both a password and a blank password.") .
"</li>";
295 if (!empty($UserRec[
'_pass1']) && !empty($UserRec[
'_pass2'])) {
297 $result = preg_match(
'/^' . $policyRegex .
'$/m', $UserRec[
'_pass1']);
299 $Errors .=
"<li>" . _(
"Password does not match policy.");
306 if (!empty($UserRec[
'group_fk'])) {
307 $group_map = $this->userDao->getUserGroupMap($UserRec[
'user_pk']);
308 if (!in_array($UserRec[
'group_fk'], array_keys($group_map))) {
309 $Errors .=
"<li>" . _(
"User is not member of provided group.") .
315 if ((!Auth::isAdmin()) && ($UserRec[
'user_name'] != $_SESSION[
'User'])) {
316 $Errors .=
"<li>" . _(
"Only admin can change the username.") .
"</li>";
320 if (!empty($Errors)) {
321 return _(
"Errors") .
":<ol>$Errors </ol>";
326 if (!empty($UserRec[
'_blank_pass']) ) {
327 $UserRec[
'user_seed'] =
'';
328 $options = array(
'cost' => 10);
329 $UserRec[
'user_pass'] = password_hash(
"", PASSWORD_DEFAULT, $options);
330 }
else if (empty($UserRec[
'_pass1'])) {
331 unset( $UserRec[
'user_pass']);
332 unset( $UserRec[
'user_seed']);
336 $sql =
"UPDATE users SET ";
338 foreach ($UserRec as $key=>$val) {
339 if ($key[0] ==
'_' || $key ==
"user_pk") {
342 if (!$SessionIsAdmin && ($key ==
"user_perm" || $key ==
"root_folder_fk" || $key ==
"user_status")) {
348 $sql .=
"$key='" . pg_escape_string($val) .
"'";
351 $sql .=
" WHERE user_pk=$UserRec[user_pk]";
354 pg_free_result($result);
368 if (empty($user_pk)) {
369 throw new Exception(
"Invalid access. Your session has expired.",1);
372 $UserRec =
GetSingleRec(
"users",
"WHERE user_pk=$user_pk");
373 if (empty($UserRec)) {
374 throw new Exception(
"Invalid user. ",1);
404 if (!empty($user_pk)) {
406 $UserRec[
'_pass1'] =
"";
407 $UserRec[
'_pass2'] =
"";
408 $UserRec[
'_blank_pass'] = password_verify(
'', $UserRec[
'user_pass']) ?
"on" :
"";
411 $UserRec[
'user_pk'] = intval($request->get(
'user_pk'));
412 $UserRec[
'user_name'] = stripslashes($request->get(
'user_name'));
413 $UserRec[
'root_folder_fk'] = intval($request->get(
'root_folder_fk'));
414 $UserRec[
'upload_visibility'] = stripslashes($request->get(
'public'));
415 $UserRec[
'default_folder_fk'] = intval($request->get(
'default_folder_fk'));
416 $UserRec[
'user_desc'] = stripslashes($request->get(
'user_desc'));
417 $defaultGroup = $request->get(
'default_group_fk',
null);
418 if ($defaultGroup !==
null) {
419 $UserRec[
'group_fk'] = intval($defaultGroup);
422 $UserRec[
'_pass1'] = stripslashes($request->get(
'_pass1'));
423 $UserRec[
'_pass2'] = stripslashes($request->get(
'_pass2'));
424 if (!empty($UserRec[
'_pass1'])) {
425 $UserRec[
'user_seed'] =
'Seed';
426 $options = array(
'cost' => 10);
427 $UserRec[
'user_pass'] = password_hash($UserRec[
'_pass1'], PASSWORD_DEFAULT, $options);
428 $UserRec[
'_blank_pass'] =
"";
430 $UserRec[
'user_pass'] =
"";
431 $UserRec[
'_blank_pass'] = stripslashes($request->get(
"_blank_pass"));
432 if (empty($UserRec[
'_blank_pass'])) {
433 $StoredUserRec = $this->
GetUserRec($UserRec[
'user_pk']);
434 $options = array(
'cost' => 10);
435 $UserRec[
'_blank_pass'] = password_verify($StoredUserRec[
'user_pass'], password_hash(
"", PASSWORD_DEFAULT, $options)) ?
"on" :
"";
439 $UserRec[
'user_perm'] = intval($request->get(
'user_perm'));
440 $UserRec[
'user_status'] = stripslashes($request->get(
'user_status'));
441 $UserRec[
'user_email'] = stripslashes($request->get(
'user_email'));
442 $UserRec[
'email_notify'] = stripslashes($request->get(
'email_notify'));
443 if (!empty($UserRec[
'email_notify'])) {
444 $UserRec[
'email_notify'] =
'y';
446 $UserRec[
'user_agent_list'] = is_null($request->get(
'user_agent_list')) ?
userAgents() : $request->get(
'user_agent_list');
447 $UserRec[
'default_bucketpool_fk'] = intval($request->get(
"default_bucketpool_fk"));
463 function generateNewToken(Request $request)
467 $user_pk = Auth::getUserId();
468 $tokenName = $request->get(
'pat_name');
469 $tokenExpiry = $request->get(
'pat_expiry');
470 if ($_SESSION[Auth::USER_LEVEL] < 3) {
473 $tokenScope = $request->get(
'pat_scope');
475 $tokenScope = array_search($tokenScope, RestHelper::SCOPE_DB_MAP);
476 if ($tokenScope ===
false) {
477 throw new \UnexpectedValueException(
"Invalid token scope " .
478 $request->get(
'pat_scope') .
".");
480 $tokenScope = RestHelper::SCOPE_DB_MAP[$tokenScope];
482 $restHelper = $container->get(
'helper.restHelper');
484 $restHelper->validateTokenRequest($tokenExpiry, $tokenName, $tokenScope);
486 throw new \UnexpectedValueException($e->getMessage());
490 $restDbHelper = $container->get(
'helper.dbHelper');
492 openssl_random_pseudo_bytes(RestHelper::TOKEN_KEY_LENGTH / 2));
494 $jti = $restDbHelper->insertNewTokenKey($user_pk, $tokenExpiry,
495 $tokenScope, $tokenName, $key);
499 openssl_random_pseudo_bytes(RestHelper::TOKEN_KEY_LENGTH / 2));
501 $jti = $restDbHelper->insertNewTokenKey($user_pk, $tokenExpiry,
502 $tokenScope, $tokenName, $key);
510 throw new \UnexpectedValueException($e->getMessage());
512 return $this->authHelper->generateJwtToken($tokenExpiry,
513 $jti[
'created_on'], $jti[
'jti'], $tokenScope, $key);
525 $user_pk = Auth::getUserId();
526 $sql =
"SELECT pat_pk, user_fk, expire_on, token_scope, token_name, created_on, active " .
527 "FROM personal_access_tokens " .
528 "WHERE user_fk = $1 AND active = true AND client_id IS NULL;";
529 $rows = $this->
dbManager->getRows($sql, [$user_pk],
530 __METHOD__ .
".getActiveTokens");
532 foreach ($rows as $row) {
534 $this->authHelper->isTokenActive($row, $row[
"pat_pk"]);
539 "id" => $row[
"pat_pk"] .
"." . $user_pk,
540 "name" => $row[
"token_name"],
541 "created" => $row[
"created_on"],
542 "expire" => $row[
"expire_on"],
543 "scope" => $row[
"token_scope"]
545 $response[] = $entry;
547 array_multisort(array_column($response,
"created"), SORT_ASC, $response);
557 $user_pk = Auth::getUserId();
559 $sql =
"SELECT pat_pk, user_fk, expire_on, token_scope, token_name, created_on " .
560 "FROM personal_access_tokens " .
561 "WHERE user_fk = $1 AND active = false " .
562 "AND expire_on >= (SELECT CURRENT_DATE - ($2)::integer) " .
563 "AND client_id IS NULL;";
564 $rows = $this->
dbManager->getRows($sql, [$user_pk, $retentionPeriod],
565 __METHOD__ .
".getExpiredTokens");
567 foreach ($rows as $row) {
569 "id" => $row[
"pat_pk"] .
"." . $user_pk,
570 "name" => $row[
"token_name"],
571 "created" => $row[
"created_on"],
572 "expire" => $row[
"expire_on"],
573 "scope" => $row[
"token_scope"]
575 $response[] = $entry;
577 array_multisort(array_column($response,
"created"), SORT_ASC, $response);
588 $groups = $this->userDao->getUserGroupMap($userRec[
'user_pk']);
589 $userDefaults = $this->userDao->getUserAndDefaultGroupByUserName($userRec[
'user_name']);
591 foreach ($groups as $groupId => $groupName) {
592 $options .=
"<option value='$groupId' ";
593 if ($groupId == $userDefaults[
'group_fk']) {
594 $options .=
"selected='selected'";
596 $options .=
">$groupName</option>";
611 private function addNewClient(Request $request)
615 $user_pk = Auth::getUserId();
618 if ($_SESSION[Auth::USER_LEVEL] < 3) {
624 $restHelper = $container->get(
'helper.restHelper');
626 $restHelper->validateNewOauthClient($user_pk, $clientName, $clientScope,
629 throw new \UnexpectedValueException($e->getMessage());
632 $restHelper->getDbHelper()->addNewClient($clientName, $user_pk,
633 $clientId, $clientScope);
634 return "Client \"$clientName\" added with ID \"$clientId\"";
646 $user_pk = Auth::getUserId();
647 $sql =
"SELECT pat_pk, user_fk, token_scope, token_name, " .
648 "created_on, active, client_id " .
649 "FROM personal_access_tokens " .
650 "WHERE user_fk = $1 AND active = true AND token_key IS NULL;";
651 $rows = $this->
dbManager->getRows($sql, [$user_pk],
652 __METHOD__ .
".getActiveClients");
654 foreach ($rows as $row) {
656 "id" => $row[
"pat_pk"] .
"." . $user_pk,
657 "name" => $row[
"token_name"],
658 "created" => $row[
"created_on"],
659 "clientid" => $row[
"client_id"],
660 "scope" => $row[
"token_scope"]
662 $response[] = $entry;
664 array_multisort(array_column($response,
"created"), SORT_ASC, $response);
674 $user_pk = Auth::getUserId();
675 $sql =
"SELECT pat_pk, user_fk, token_scope, token_name, " .
676 "created_on, active, client_id " .
677 "FROM personal_access_tokens " .
678 "WHERE user_fk = $1 AND active = false AND token_key IS NULL;";
679 $rows = $this->
dbManager->getRows($sql, [$user_pk],
680 __METHOD__ .
".getRevokedClients");
682 foreach ($rows as $row) {
684 "id" => $row[
"pat_pk"] .
"." . $user_pk,
685 "name" => $row[
"token_name"],
686 "created" => $row[
"created_on"],
687 "clientid" => $row[
"client_id"],
688 "scope" => $row[
"token_scope"]
690 $response[] = $entry;
692 array_multisort(array_column($response,
"created"), SORT_ASC, $response);
703 return $SysConf[
'SYSCONFIG'][
'PATMaxPostExpiryRetention'];
Contains the constants and helpers for authentication of user.
Exception when a token has duplicate key for same user.
Exception when a token has duplicate name for same user.
render($templateName, $vars=null, $headers=null)
Provides helper methods for REST api.
Provides helper methods to access database for REST api.
Provides various DAO helper functions for REST api.
CreateUserRec(Request $request, $user_pk="")
Create a user record.
GetUserRec($user_pk)
Get a user record.
getListOfExpiredClients()
getListOfActiveClients()
Get a list of active clients for current user.
IsSessionAdmin($UserRec)
Determine if the session user is an admin.
UpdateUser($UserRec, $SessionIsAdmin)
Validate and update the user data.
getListOfActiveTokens()
Get a list of active tokens for current user.
handle(Request $request)
Allow user to change their account settings (users db table).
DisplayForm($UserRec, $SessionIsAdmin)
Display the user record edit form.
getUserGroupSelect($userRec)
getMaxExpiredTokenRetentionPeriod()
getMaxExpiredTokenRetentionPeriod() get the refresh time from DB. @Returns number of days to retain e...
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()
generate_password_policy()
SelectBucketPool($selected, $active='Y')
Return a select list containing all the active bucketpool's.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
GetSingleRec($Table, $Where="")
Retrieve a single database record.
FolderListOption($ParentFolder, $Depth, $IncludeTop=1, $SelectId=-1, $linkParent=false, $OldParent=0)
Create the folder tree, using OPTION tags.
Traceback_uri()
Get the URI without query to this location.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
#define PERM_READ
Read-only permission.
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
fo_dbManager * dbManager
fo_dbManager object