diff --git a/ci4/app/Config/Auth.php b/ci4/app/Config/Auth.php index a1b84866..01d4e304 100644 --- a/ci4/app/Config/Auth.php +++ b/ci4/app/Config/Auth.php @@ -245,6 +245,7 @@ class Auth extends ShieldAuth 'required', 'max_length[254]', 'valid_email', + 'is_unique[auth_identities.secret]', ], ]; diff --git a/ci4/app/Config/Validation.php b/ci4/app/Config/Validation.php index a93a7146..cb05c77a 100755 --- a/ci4/app/Config/Validation.php +++ b/ci4/app/Config/Validation.php @@ -34,7 +34,7 @@ class Validation extends BaseConfig * @var array */ public array $templates = [ - 'list' => 'CodeIgniter\Validation\Views\list', + 'list' => 'CodeIgniter\Validation\Views\list', 'single' => 'CodeIgniter\Validation\Views\single', 'bootstrap_style' => 'themes/_commonPartialsBs/_form_validation_errors', ]; @@ -42,4 +42,6 @@ class Validation extends BaseConfig // -------------------------------------------------------------------- // Rules // -------------------------------------------------------------------- + + } diff --git a/ci4/app/Controllers/Configuracion/Users.php b/ci4/app/Controllers/Configuracion/Users.php index 7e7ab4dc..dcd2d6e0 100755 --- a/ci4/app/Controllers/Configuracion/Users.php +++ b/ci4/app/Controllers/Configuracion/Users.php @@ -20,7 +20,6 @@ class Users extends \App\Controllers\GoBaseController private ChatDeparmentUserModel $chat_department_user_model; - use \CodeIgniter\API\ResponseTrait; protected static $primaryModelName = 'App\Models\UserModel'; @@ -62,7 +61,7 @@ class Users extends \App\Controllers\GoBaseController $this->viewData['usingClientSideDataTable'] = true; $this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Users.user')]); $this->viewData['user_model'] = $this->user_model; - $this->viewData['userList2'] = auth()->getProvider()->findAll(); + $this->viewData['userList2'] = auth()->getProvider()->findAll(); parent::index(); } @@ -77,17 +76,18 @@ class Users extends \App\Controllers\GoBaseController // Obtener contraseña nueva si se ha introducido en texto plano if (empty($postData['new_pwd'])) { $postData['password'] = 'Safekat2024'; // Contraseña por defecto - }else{ + } else { $postData['password'] = $postData['new_pwd']; } + // Obtener los grupos a los que pertenece $currentGroups = $postData['group'] ?? []; $chatDepartments = $postData['chatDepartments'] ?? []; unset($postData['group']); unset($postData['chatDepartments']); - // Generar el nombre de usuario - $postData['username'] = strstr($postData['email'], '@', true); + // Marcar el username como NULL + $postData['username'] = null; $sanitizedData = $this->sanitized($postData, true); $noException = true; @@ -99,6 +99,7 @@ class Users extends \App\Controllers\GoBaseController if ($this->canValidate()) : try { + // Crear el usuario si pasa la validación $user = new User([ 'username' => $sanitizedData['username'], 'first_name' => $sanitizedData['first_name'], @@ -110,6 +111,7 @@ class Users extends \App\Controllers\GoBaseController ]); $users->save($user); $successfulResult = true; // Hacked + } catch (\Exception $e) { $noException = false; //$this->dealWithException($e); @@ -137,12 +139,11 @@ class Users extends \App\Controllers\GoBaseController ]; $this->group_user_model->insert($group_user_data); } - $this->chat_department_user_model->where("user_id",$id)->delete(); - foreach($chatDepartments as $chatDepartment) - { + $this->chat_department_user_model->where("user_id", $id)->delete(); + foreach ($chatDepartments as $chatDepartment) { $this->chat_department_user_model->insert([ "user_id" => $id, - "chat_department_id" => $this->chat_department_model->where("name",$chatDepartment)->first()["id"] + "chat_department_id" => $this->chat_department_model->where("name", $chatDepartment)->first()["id"] ]); } @@ -199,7 +200,6 @@ class Users extends \App\Controllers\GoBaseController unset($postData['group']); unset($postData['chatDepartments']); - // Obtener contraseña nueva si se ha introducido en texto plano // Obtener contraseña nueva si se ha introducido en texto plano if (!empty($postData['new_pwd'])) { $postData['password'] = $postData['new_pwd']; @@ -254,12 +254,11 @@ class Users extends \App\Controllers\GoBaseController ]; $this->group_user_model->insert($group_user_data); } - $this->chat_department_user_model->where("user_id",$id)->delete(); - foreach($chatDepartments as $chatDepartment) - { + $this->chat_department_user_model->where("user_id", $id)->delete(); + foreach ($chatDepartments as $chatDepartment) { $this->chat_department_user_model->insert([ "user_id" => $id, - "chat_department_id" => $this->chat_department_model->where("name",$chatDepartment)->first()["id"] + "chat_department_id" => $this->chat_department_model->where("name", $chatDepartment)->first()["id"] ]); } $id = $user->id ?? $id; @@ -284,7 +283,7 @@ class Users extends \App\Controllers\GoBaseController $this->viewData['formAction'] = route_to('updateUser', $id); $this->viewData['selectedGroups'] = $this->group_model->getUsersRoles($requestedId); $this->viewData['groups'] = $this->group_model->select('keyword, title')->findAll(); - $this->viewData['chatDepartments'] = $this->chat_department_model->select(["display","name","id as chatDepartmentId"])->findAll(); + $this->viewData['chatDepartments'] = $this->chat_department_model->select(["display", "name", "id as chatDepartmentId"])->findAll(); $this->viewData['chatDepartmentUser'] = $this->chat_department_user_model->getChatDepartmentUser($user->id); $this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('Users.user') . ' ' . lang('Basic.global.edit3'); @@ -308,7 +307,7 @@ class Users extends \App\Controllers\GoBaseController endif; $users = auth()->getProvider(); - $users->delete($user->id); + $users->delete($user->id, true); $message = "Usuario eliminado correctamente"; return $this->redirect2listView('successMessage', $message); diff --git a/ci4/app/Language/en/Users.php b/ci4/app/Language/en/Users.php index 116d67bc..9ef06c32 100755 --- a/ci4/app/Language/en/Users.php +++ b/ci4/app/Language/en/Users.php @@ -15,7 +15,7 @@ return [ 'createdAt' => 'Created At', 'dateBirth' => 'Date Birth', 'email' => 'Email', - 'emailConfirmed' => 'Email Confirmed', + 'emailConfirmed' => 'Email Confirmed', 'firstName' => 'First Name', 'group' => 'Group', 'idUser' => 'ID User', diff --git a/ci4/app/Models/UserModel.php b/ci4/app/Models/UserModel.php index d397eaf2..e68aa141 100644 --- a/ci4/app/Models/UserModel.php +++ b/ci4/app/Models/UserModel.php @@ -36,7 +36,8 @@ class UserModel extends ShieldUserModel "last_name" => "required|trim|max_length[150]", 'new_pwd' => 'permit_empty|min_length[8]', 'new_pwd_confirm' => 'permit_empty|required_with[new_pwd]|matches[new_pwd]', - "comments" => "permit_empty|trim|max_length[512]" + "comments" => "permit_empty|trim|max_length[512]", + 'email' => 'required|valid_email|is_unique[auth_identities.secret]', ]; protected $validationMessages = [ @@ -77,5 +78,11 @@ class UserModel extends ShieldUserModel } + // Método para comprobar si el email ya está registrado + public function isEmailUnique($email) + { + return $this->where('email', $email)->countAllResults() == 0; + } + }