Add user email and phone support with security routes
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.
This commit is contained in:
@@ -32,12 +32,16 @@ class users_o extends db
|
||||
public user_price_overrides_o $price_overrides;
|
||||
public language_pack_en_us $language_pack;
|
||||
protected object_property $password;
|
||||
protected object_property $phone_country_code;
|
||||
protected object_property $phone;
|
||||
protected object_property $email;
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('users');
|
||||
}
|
||||
|
||||
|
||||
public function edit(int $id, string $customer_number, string|null $role, string|null $password, string|null $display_name): void
|
||||
{
|
||||
global $db;
|
||||
@@ -79,6 +83,9 @@ class users_o extends db
|
||||
$this->display_name = new object_property($this->table, $this->id, 'display_name', 'string', false);
|
||||
$this->password = new object_property($this->table, $this->id, 'password', 'string', true);
|
||||
$this->group_id = new object_property($this->table, $this->id, 'group_id', 'int', true);
|
||||
$this->phone_country_code = new object_property($this->table, $this->id, 'phone_country_code', 'int', false);
|
||||
$this->phone = new object_property($this->table, $this->id, 'phone', 'int', false);
|
||||
$this->email = new object_property($this->table, $this->id, 'email', 'string', false);
|
||||
$this->created_at = new object_property($this->table, $this->id, 'created_at', 'string', false);
|
||||
$this->updated_at = new object_property($this->table, $this->id, 'updated_at', 'string', false);
|
||||
$this->keys = (new user_key_value_pairs_o())->setUser($this->id);
|
||||
@@ -228,12 +235,22 @@ class users_o extends db
|
||||
|
||||
public function asArray(): array
|
||||
{
|
||||
$phone_country_code = $this->phone_country_code->value();
|
||||
$phone_country_code = $phone_country_code === null ? null : (int)$phone_country_code;
|
||||
$phone = $this->phone->value();
|
||||
$phone = $phone === null ? null : (int)$phone;
|
||||
// Create the array with the object properties
|
||||
$array = [
|
||||
'id' => (int)$this->id,
|
||||
'customer_number' => (int)$this->customer_number->value(),
|
||||
'customer_name' => $this->getCustomerName($this->customer_number->value()),
|
||||
'display_name' => $this->display_name->value(),
|
||||
'group_id' => (int)$this->group_id->value(),
|
||||
'phone' => [
|
||||
'country_code' => $phone_country_code,
|
||||
'number' => $phone,
|
||||
],
|
||||
'email' => $this->email->value(),
|
||||
'created_at' => $this->created_at->value(),
|
||||
'updated_at' => $this->updated_at->value(),
|
||||
];
|
||||
@@ -984,4 +1001,48 @@ class users_o extends db
|
||||
// Return the customer numbers
|
||||
return array_unique($customer_numbers);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the password matches the hashed password
|
||||
* @param string $password_to_check_against The password (plain text) to check against the hashed password
|
||||
* @return bool True if the password matches, false otherwise
|
||||
* @throws Exception If the user is not selected
|
||||
* @throws Exception If the user does not have a password
|
||||
*/
|
||||
public function passwordMatches(string $password_to_check_against): bool
|
||||
{
|
||||
// Require the user to be selected
|
||||
self::requireSelected();
|
||||
// Check if the user has a password
|
||||
if (empty($this->password->value())) {
|
||||
throw new Exception('The user does not have a password, unable to check if the password matches');
|
||||
}
|
||||
// Check if the password matches
|
||||
return password_verify(
|
||||
(string)$password_to_check_against,
|
||||
(string)$this->password->value()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the email for the user
|
||||
* @param string $email The email address to set
|
||||
* @throws Exception If the user is not selected
|
||||
* @throws Exception If the email address is invalid
|
||||
* @throws Exception If the email address is already in use
|
||||
*/
|
||||
public function setEmail(string $email): void
|
||||
{
|
||||
self::requireSelected();
|
||||
// Check if the email is valid
|
||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
throw new Exception('Invalid email address');
|
||||
}
|
||||
// Check if the email is already in use
|
||||
if (self::countRowsWhere(['email' => $email]) > 0) {
|
||||
throw new Exception('Email address already in use');
|
||||
}
|
||||
// Set the email
|
||||
$this->email->set($email);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user