generated from jric11/baseProject
Codex's updates...
This commit is contained in:
+64
-133
@@ -25,6 +25,7 @@ class Users extends Base {
|
||||
|
||||
date_default_timezone_set(self::TIMEZONE);
|
||||
|
||||
$this->current_user_serial = (int) ($_SESSION[self::USER_SERIAL] ?? 0);
|
||||
$this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
|
||||
$this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";
|
||||
$this->pagination_page = $_SESSION[self::PAGINATION_PAGE] ?? 1;
|
||||
@@ -100,9 +101,13 @@ class Users extends Base {
|
||||
case "add":
|
||||
|
||||
$user_name = filter_input(INPUT_POST, "user_name") ?: "";
|
||||
$user_client_name = filter_input(INPUT_POST, "user_client_name") ?: "";
|
||||
$user_first_name = filter_input(INPUT_POST, "user_first_name") ?: "";
|
||||
$user_email = filter_input(INPUT_POST, "user_email") ?: "";
|
||||
$user_role = filter_input(INPUT_POST, "user_role") ?: "";
|
||||
if (!in_array($user_role, ['User', 'Guest', 'Administrator'], true) || !filter_var($user_email, FILTER_VALIDATE_EMAIL) || strlen($user_email) > 100 || !preg_match('/\A[A-Za-z0-9_.-]{3,64}\z/', $user_name)) {
|
||||
http_response_code(422); exit('Enter a valid username, email address and role.');
|
||||
}
|
||||
|
||||
|
||||
$user_name = strtoupper(preg_replace("/\s+/", " ", $user_name));
|
||||
|
||||
@@ -110,31 +115,31 @@ class Users extends Base {
|
||||
$user_temp_password = password_hash($user_random_password, PASSWORD_DEFAULT);
|
||||
|
||||
$user_change_password = (int) true;
|
||||
$user_active = (int) true;
|
||||
$user_active = 'active';
|
||||
|
||||
// Add the User
|
||||
|
||||
$connection = $this->connect();
|
||||
|
||||
$SQL = "insert into users
|
||||
|
||||
|
||||
( user_name,
|
||||
user_email,
|
||||
user_client_name,
|
||||
user_password,
|
||||
user_first_name,
|
||||
user_password_hash,
|
||||
user_role,
|
||||
user_change_password,
|
||||
user_active,
|
||||
user_status,
|
||||
user_creator,
|
||||
user_created )
|
||||
|
||||
values ( :user_name,
|
||||
:user_email,
|
||||
:user_client_name,
|
||||
:user_password,
|
||||
:user_first_name,
|
||||
:user_password_hash,
|
||||
:user_role,
|
||||
:user_change_password,
|
||||
:user_active,
|
||||
:user_status,
|
||||
:user_creator,
|
||||
:user_created )";
|
||||
|
||||
@@ -142,11 +147,11 @@ class Users extends Base {
|
||||
|
||||
$statement->bindValue(":user_name", $user_name);
|
||||
$statement->bindValue(":user_email", $user_email);
|
||||
$statement->bindValue(":user_client_name", $user_client_name);
|
||||
$statement->bindValue(":user_password", $user_temp_password);
|
||||
$statement->bindValue(":user_first_name", $user_first_name);
|
||||
$statement->bindValue(":user_password_hash", $user_temp_password);
|
||||
$statement->bindValue(":user_role", $user_role);
|
||||
$statement->bindValue(":user_change_password", $user_change_password, PDO::PARAM_BOOL);
|
||||
$statement->bindValue(":user_active", $user_active, PDO::PARAM_BOOL);
|
||||
$statement->bindValue(":user_status", $user_active);
|
||||
$statement->bindValue(":user_creator", $this->current_user_name);
|
||||
$statement->bindValue(":user_created", $this->getTimestamp());
|
||||
|
||||
@@ -158,7 +163,7 @@ class Users extends Base {
|
||||
|
||||
// Send User Credentials
|
||||
|
||||
(new Email())->emailNewUserCredentials($user, $user_random_password);
|
||||
(new AuthService())->requestReset($user_email);
|
||||
|
||||
break;
|
||||
|
||||
@@ -226,9 +231,13 @@ class Users extends Base {
|
||||
$user_serial = filter_input(INPUT_POST, "user_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||||
$user_name = filter_input(INPUT_POST, "user_name") ?: "";
|
||||
$user_email = filter_input(INPUT_POST, "user_email") ?: "";
|
||||
$user_client_name = filter_input(INPUT_POST, "user_client_name") ?: "";
|
||||
$user_first_name = filter_input(INPUT_POST, "user_first_name") ?: "";
|
||||
$user_role = filter_input(INPUT_POST, "user_role") ?: "";
|
||||
$user_active = filter_input(INPUT_POST, "user_active", FILTER_VALIDATE_BOOLEAN) ?: false;
|
||||
if (!in_array($user_role, ['User', 'Guest', 'Administrator'], true) || !filter_var($user_email, FILTER_VALIDATE_EMAIL) || strlen($user_email) > 100 || !preg_match('/\A[A-Za-z0-9_.-]{3,64}\z/', $user_name)) {
|
||||
http_response_code(422); exit('Enter a valid username, email address and role.');
|
||||
}
|
||||
|
||||
$user_active = filter_input(INPUT_POST, 'user_active', FILTER_VALIDATE_BOOLEAN) ? 'active' : 'inactive';
|
||||
|
||||
$users = $this->getUser($user_serial);
|
||||
|
||||
@@ -238,12 +247,13 @@ class Users extends Base {
|
||||
|
||||
// Update the User
|
||||
|
||||
$SQL = "update users
|
||||
$SQL = "update users
|
||||
set user_name = :user_name,
|
||||
user_email = :user_email,
|
||||
user_client_name = :user_client_name,
|
||||
user_first_name = :user_first_name,
|
||||
user_role = :user_role,
|
||||
user_active = :user_active,
|
||||
user_auth_version = user_auth_version + 1,
|
||||
user_status = :user_status,
|
||||
user_changer = :user_changer,
|
||||
user_changed = :user_changed
|
||||
where user_serial = :user_serial";
|
||||
@@ -253,9 +263,9 @@ class Users extends Base {
|
||||
$statement->bindValue(":user_serial", $user_serial);
|
||||
$statement->bindValue(":user_name", $user_name);
|
||||
$statement->bindValue(":user_email", $user_email);
|
||||
$statement->bindValue(":user_client_name", $user_client_name);
|
||||
$statement->bindValue(":user_first_name", $user_first_name);
|
||||
$statement->bindValue(":user_role", $user_role);
|
||||
$statement->bindValue(":user_active", $user_active, PDO::PARAM_BOOL);
|
||||
$statement->bindValue(":user_status", $user_active);
|
||||
$statement->bindValue(":user_changer", $this->current_user_name);
|
||||
$statement->bindValue(":user_changed", $this->getTimestamp());
|
||||
|
||||
@@ -274,6 +284,7 @@ class Users extends Base {
|
||||
// ====================
|
||||
|
||||
public function getUser($user_serial = 0) {
|
||||
$user_serial = (int) $user_serial;
|
||||
|
||||
$SQL = "select view_users.*
|
||||
from view_users
|
||||
@@ -287,12 +298,9 @@ class Users extends Base {
|
||||
// ====================
|
||||
|
||||
public function getUserByUserName($user_name) {
|
||||
|
||||
$SQL = "select view_users.*
|
||||
from view_users
|
||||
where user_name = '{$user_name}'";
|
||||
|
||||
return $this->getTable("user", $SQL);
|
||||
$stmt = $this->connect()->prepare('SELECT * FROM view_users WHERE user_name = :name');
|
||||
$stmt->execute([':name' => $user_name]);
|
||||
return $this->getTableXML('users', $stmt);
|
||||
}
|
||||
|
||||
// =====================
|
||||
@@ -337,14 +345,14 @@ class Users extends Base {
|
||||
$SQL = "select count(*) as count
|
||||
from view_users
|
||||
where user_name regexp :user_name
|
||||
or user_client_name regexp :user_client_name
|
||||
or user_first_name regexp :user_first_name
|
||||
or user_email regexp :user_email
|
||||
or user_role regexp :user_role";
|
||||
|
||||
$statement = $connection->prepare($SQL);
|
||||
|
||||
$statement->bindValue(":user_name", $find_words);
|
||||
$statement->bindValue(":user_client_name", $find_words);
|
||||
$statement->bindValue(":user_first_name", $find_words);
|
||||
$statement->bindValue(":user_email", $find_words);
|
||||
$statement->bindValue(":user_role", $find_words);
|
||||
|
||||
@@ -379,7 +387,7 @@ class Users extends Base {
|
||||
$SQL = "select view_users.*
|
||||
from view_users
|
||||
where user_name regexp :user_name
|
||||
or user_client_name regexp :user_client_name
|
||||
or user_first_name regexp :user_first_name
|
||||
or user_email regexp :user_email
|
||||
or user_role regexp :user_role
|
||||
limit {$this->pagination_size}
|
||||
@@ -388,7 +396,7 @@ class Users extends Base {
|
||||
$statement = $connection->prepare($SQL);
|
||||
|
||||
$statement->bindValue(":user_name", $find_words);
|
||||
$statement->bindValue(":user_client_name", $find_words);
|
||||
$statement->bindValue(":user_first_name", $find_words);
|
||||
$statement->bindValue(":user_email", $find_words);
|
||||
$statement->bindValue(":user_role", $find_words);
|
||||
|
||||
@@ -485,111 +493,33 @@ class Users extends Base {
|
||||
// ===============
|
||||
|
||||
public function changePassword() {
|
||||
|
||||
$step = filter_input(INPUT_POST, "step") ?: "prompt";
|
||||
|
||||
switch ($step) {
|
||||
|
||||
case "prompt":
|
||||
|
||||
$popovers = (new Popovers())->getPopovers();
|
||||
$XML = $this->mergeXML($popovers, "<XML/>");
|
||||
|
||||
$content = $this->applyXSL($XML, $this->getXSL("changePassword"));
|
||||
$this->displayContent($content);
|
||||
|
||||
break;
|
||||
|
||||
case "change":
|
||||
|
||||
$user_serial = $_SESSION[self::USER_SERIAL];
|
||||
$user_new_password = filter_input(INPUT_POST, "user_new_password") ?: "";
|
||||
$user_verify_password = filter_input(INPUT_POST, "user_verify_password") ?: "";
|
||||
|
||||
if (!$user_new_password == $user_verify_password) {
|
||||
$this->initializeSession();
|
||||
$params = ['PASSWORD_ERROR' => ''];
|
||||
if (($_POST['step'] ?? '') === 'change') {
|
||||
$this->requireCsrf();
|
||||
try {
|
||||
$auth = new AuthService();
|
||||
if (!$auth->allowAttempt('change-password', (string) $_SESSION[self::USER_SERIAL], 10, 900)) {
|
||||
throw new InvalidArgumentException('Too many attempts. Please try again later.');
|
||||
}
|
||||
|
||||
$user_new_hashed_password = password_hash($user_new_password, PASSWORD_DEFAULT);
|
||||
|
||||
// Update User Password
|
||||
|
||||
$connection = $this->connect();
|
||||
|
||||
$SQL = "update users
|
||||
set user_password = :user_password,
|
||||
user_change_password = :user_change_password,
|
||||
user_changer = :user_changer,
|
||||
user_changed = :user_changed
|
||||
where user_serial = :user_serial";
|
||||
|
||||
$statement = $connection->prepare($SQL);
|
||||
|
||||
$statement->bindValue(":user_serial", $user_serial);
|
||||
$statement->bindValue(":user_password", $user_new_hashed_password);
|
||||
$statement->bindValue(":user_change_password", 0);
|
||||
$statement->bindValue(":user_changer", $this->current_user_name);
|
||||
$statement->bindValue(":user_changed", $this->getTimestamp());
|
||||
|
||||
if (!$statement->execute()) {
|
||||
$this->debug($statement->errorInfo()[2]);
|
||||
if (!$auth->changePassword((int) $_SESSION[self::USER_SERIAL], $_POST['user_current_password'] ?? '', $_POST['user_new_password'] ?? '', $_POST['user_verify_password'] ?? '')) {
|
||||
throw new InvalidArgumentException('Your current password is incorrect or your session has expired.');
|
||||
}
|
||||
|
||||
(new Journal())->journalEntry("{$_SESSION[self::USER_NAME]} changed their password.");
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$this->displayHome();
|
||||
$this->logoutSession();
|
||||
header('Location: main.php', true, 303);
|
||||
exit;
|
||||
} catch (InvalidArgumentException $e) { $params['PASSWORD_ERROR'] = $e->getMessage(); }
|
||||
}
|
||||
$this->displayContent($this->applyXSL('<XML/>', $this->getXSL('changePassword'), $params));
|
||||
}
|
||||
|
||||
// ==============
|
||||
// Reset Password
|
||||
// ==============
|
||||
|
||||
public function resetPassword() {
|
||||
|
||||
$reset = false;
|
||||
|
||||
$user_serial = filter_input(INPUT_POST, "user_serial") ?: "";
|
||||
|
||||
$user = $this->getUser($user_serial);
|
||||
|
||||
$user_name = $user->record->user_name;
|
||||
|
||||
$user_random_password = $this->generateRandomPassword();
|
||||
$user_temp_password = password_hash($user_random_password, PASSWORD_DEFAULT);
|
||||
|
||||
// Update User Password
|
||||
|
||||
$connection = $this->connect();
|
||||
|
||||
$SQL = "update users
|
||||
set user_password = :user_password,
|
||||
user_change_password = :user_change_password
|
||||
where user_serial = :user_serial";
|
||||
|
||||
$statement = $connection->prepare($SQL);
|
||||
|
||||
$statement->bindValue(":user_serial", $user_serial);
|
||||
$statement->bindValue(":user_password", $user_temp_password);
|
||||
$statement->bindValue(":user_change_password", 1);
|
||||
|
||||
if (!$statement->execute()) {
|
||||
$this->debug($statement->errorInfo()[2]);
|
||||
} else {
|
||||
$reset = true;
|
||||
}
|
||||
|
||||
// Send User Credentials
|
||||
|
||||
(new Email())->emailUserPasswordResetLink($user, $user_random_password);
|
||||
|
||||
(new Journal())->journalEntry("{$_SESSION[self::USER_NAME]} reset {$user_name}'s password.");
|
||||
|
||||
echo json_encode($reset);
|
||||
$this->requireCsrf();
|
||||
$stmt = $this->connect()->prepare('SELECT user_email FROM users WHERE user_serial = :id');
|
||||
$stmt->execute([':id' => (int) ($_POST['user_serial'] ?? 0)]);
|
||||
$email = $stmt->fetchColumn();
|
||||
if ($email) (new AuthService())->requestReset($email);
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode(true);
|
||||
}
|
||||
|
||||
// ===============================================
|
||||
@@ -599,7 +529,7 @@ class Users extends Base {
|
||||
public function updateLogin() {
|
||||
|
||||
$SQL = "update users
|
||||
set user_login = :user_login
|
||||
set user_last_login = :user_login
|
||||
where user_serial = :user_serial";
|
||||
|
||||
$statement = $this->connect()->prepare($SQL);
|
||||
@@ -617,10 +547,11 @@ class Users extends Base {
|
||||
public function updateActiveStatus() {
|
||||
|
||||
$user_serial = filter_input(INPUT_POST, "user_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||||
$user_active = filter_input(INPUT_POST, "user_active", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||||
$user_active = filter_input(INPUT_POST, 'user_active', FILTER_VALIDATE_BOOLEAN) ? 'active' : 'inactive';
|
||||
|
||||
$SQL = "update users
|
||||
set user_active = :user_active,
|
||||
set user_status = :user_status,
|
||||
user_auth_version = user_auth_version + 1,
|
||||
user_changed = :user_changed,
|
||||
user_changer = :user_changer
|
||||
where user_serial = :user_serial";
|
||||
@@ -628,7 +559,7 @@ class Users extends Base {
|
||||
$statement = $this->connect()->prepare($SQL);
|
||||
|
||||
$statement->bindValue(":user_serial", $user_serial);
|
||||
$statement->bindValue(":user_active", $user_active);
|
||||
$statement->bindValue(":user_status", $user_active);
|
||||
$statement->bindValue(":user_changed", $this->getTimestamp());
|
||||
$statement->bindValue(":user_changer", $this->current_user_name);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user