Add password management methods to subusers_o
- Introduce `setPassword` method for securely updating subuser passwords with validation and hashing. - Enhance `add` method with stricter password validation and error handling. - Refactor exception handling and standardize imports for improved clarity.
This commit is contained in:
@@ -4,6 +4,7 @@ namespace objects;
|
||||
|
||||
use classes\db;
|
||||
use classes\object_property;
|
||||
use Exception;
|
||||
use traits\db_object_t;
|
||||
|
||||
class subusers_o extends db
|
||||
@@ -44,6 +45,16 @@ class subusers_o extends db
|
||||
$this->suspended_at = new object_property($this->table, $this->id, 'suspended_at', 'timestamp');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new subuser to the database. The password is optional, but if it is set, it must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number. The password will be hashed before being stored in the database.
|
||||
* @param string $username
|
||||
* @param string|null $password
|
||||
* @param string $name
|
||||
* @param string $email
|
||||
* @param int $phone_country_code
|
||||
* @param int $phone
|
||||
* @return $this
|
||||
*/
|
||||
public function add(string $username, ?string $password, string $name, string $email, int $phone_country_code, int $phone): subusers_o
|
||||
{
|
||||
global $db, $response;
|
||||
@@ -53,7 +64,7 @@ class subusers_o extends db
|
||||
if (!empty($password)) {
|
||||
// Validate the password (at least 8 characters, at least one uppercase letter, at least one lowercase letter, at least one number)
|
||||
if (!preg_match('/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/', $password)) {
|
||||
throw new \Exception('Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number.');
|
||||
throw new Exception('Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number.');
|
||||
}
|
||||
// Hash the password
|
||||
$password = password_hash($password, PASSWORD_DEFAULT);
|
||||
@@ -78,8 +89,21 @@ class subusers_o extends db
|
||||
// Set the values of the object properties
|
||||
$this->getObjectProperties();
|
||||
return $this;
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
$response->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the password for the subuser. The password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, and one number. The password will be hashed before being stored in the database.
|
||||
* @param string $password
|
||||
* @return self
|
||||
* @throws Exception If the password does not meet the requirements or if no subuser is selected.
|
||||
*/
|
||||
public function setPassword(string $password): self
|
||||
{
|
||||
self::requireSelected();
|
||||
$this->password->set((string)password_hash($password, PASSWORD_DEFAULT));
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user