Introduced support for user email and phone management, including phone country code and email validation. Added API endpoints for email change and password validation to enhance account security features. Updated Stripe processor logic for better external ID handling.
84 lines
3.7 KiB
PHP
84 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace routes;
|
|
|
|
use classes\authentication;
|
|
use objects\logs_o;
|
|
use traits\route_t;
|
|
|
|
class userSecurityRoute
|
|
{
|
|
use route_t;
|
|
|
|
public function run(): void
|
|
{
|
|
$this->post('/account/security/change-email', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
self::requirePermission('user_security_change_email');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
(new logs_o())->add('user_security', 'global', 0, 0, 'USER_SECURITY_CHANGE_EMAIL', 'User not logged in');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
// Require the email, and password parameters
|
|
self::requireParameters(['email', 'password']);
|
|
// Check if the email is valid
|
|
$email = (string)self::getParameter('email');
|
|
self::requireMinLength('email', 5);
|
|
self::requireMaxLength('email', 255);
|
|
self::requireType($email, self::type_string());
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
(new logs_o())->add('user_security', 'global', 0, $user->id, 'USER_SECURITY_CHANGE_EMAIL', 'Invalid email');
|
|
$response->error('Invalid email', 400);
|
|
}
|
|
// Validate the password
|
|
$password = (string)self::getParameter('password');
|
|
self::requireMinLength('password', 5);
|
|
self::requireMaxLength('password', 255);
|
|
self::requireType($password, self::type_string());
|
|
if (!$user->passwordMatches($password)) {
|
|
(new logs_o())->add('user_security', 'global', 0, $user->id, 'USER_SECURITY_CHANGE_EMAIL', 'Invalid password');
|
|
$response->error('Invalid password', 400);
|
|
} else {
|
|
// Change the email
|
|
$user->setEmail($email);
|
|
(new logs_o())->add('user_security', 'global', 0, $user->id, 'USER_SECURITY_CHANGE_EMAIL', 'Email changed');
|
|
$response->success(['message' => 'Email changed']);
|
|
}
|
|
},
|
|
[
|
|
'user_security_change_email' => 'Change the email address of the user',
|
|
]
|
|
);
|
|
|
|
$this->post('/account/security/validate-password', function () {
|
|
// Require the user to be logged in
|
|
global $response;
|
|
self::requirePermission('user_security_validate_password');
|
|
$user = (new authentication())->get_user();
|
|
if (!$user) {
|
|
(new logs_o())->add('user_security', 'global', 0, 0, 'USER_SECURITY_VALIDATE_PASSWORD', 'User not logged in');
|
|
$response->error('Invalid session', 400);
|
|
}
|
|
// Require the password parameter
|
|
self::requireParameters(['password']);
|
|
// Validate the password
|
|
$password = (string)self::getParameter('password');
|
|
self::requireMinLength('password', 5);
|
|
self::requireMaxLength('password', 255);
|
|
self::requireType($password, self::type_string());
|
|
if (!$user->passwordMatches($password)) {
|
|
(new logs_o())->add('user_security', 'global', 0, $user->id, 'USER_SECURITY_VALIDATE_PASSWORD', 'Invalid password');
|
|
$response->error('Invalid password', 400);
|
|
} else {
|
|
(new logs_o())->add('user_security', 'global', 0, $user->id, 'USER_SECURITY_VALIDATE_PASSWORD', 'Password validated');
|
|
$response->success(['message' => 'Password validated']);
|
|
}
|
|
},
|
|
[
|
|
'user_security_validate_password' => 'Validate the password of the user',
|
|
]
|
|
);
|
|
}
|
|
} |