From f58d3b142bbb7f42f625f50bbb35ec676146448c Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Sat, 18 Dec 2021 18:55:42 +0100 Subject: [PATCH] The user model can validate usernames --- application/models/Users_model.php | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/application/models/Users_model.php b/application/models/Users_model.php index b19bb43e..e0e2bf07 100644 --- a/application/models/Users_model.php +++ b/application/models/Users_model.php @@ -46,7 +46,7 @@ class Users_model extends EA_Model { 'notes' => 'notes', 'roleId' => 'id_roles', ]; - + /** * Save (insert or update) a user. * @@ -198,7 +198,7 @@ class Users_model extends EA_Model { } $user = $this->db->get_where('users', ['id' => $user_id])->row_array(); - + $this->cast($user); $user['settings'] = $this->db->get_where('user_settings', ['id_users' => $user_id])->row_array(); @@ -240,7 +240,7 @@ class Users_model extends EA_Model { // Check if the required field is part of the user data. $user = $query->row_array(); - + $this->cast($user); if ( ! array_key_exists($field, $user)) @@ -278,7 +278,7 @@ class Users_model extends EA_Model { foreach ($users as &$user) { $this->cast($user); - + $user['settings'] = $this->db->get_where('user_settings', ['id_users' => $user['id']])->row_array(); unset( @@ -417,4 +417,22 @@ class Users_model extends EA_Model { { // Users do not currently have any related resources. } + + /** + * Validate the username. + * + * @param string $username Username. + * @param int|null $user_id Exclude user ID. + * + * @return bool Returns the validation result. + */ + public function validate_username(string $username, int $user_id = NULL): bool + { + if ( ! empty($user_id)) + { + $this->db->where('id_users !=', $user_id); + } + + return $this->db->get_where('user_settings', ['username' => $username])->num_rows() === 0; + } }