Arreglado bug añadir usuarios con email repetido

This commit is contained in:
imnavajas
2024-11-18 22:00:37 +01:00
parent 006a9a1413
commit cb2ca522c6
12 changed files with 183 additions and 185 deletions

View File

@ -245,7 +245,6 @@ class Auth extends ShieldAuth
'required',
'max_length[254]',
'valid_email',
'is_unique[auth_identities.secret]',
],
];

View File

@ -97,7 +97,6 @@ $routes->group('configuracion', ['namespace' => 'App\Controllers\Configuracion']
$routes->group('users', ['namespace' => 'App\Controllers\Configuracion'], function ($routes) {
$routes->get('', 'Users::index', ['as' => 'userList']);
$routes->get('index', 'Users::index', ['as' => 'userIndex']);
$routes->get('list', 'Users::index', ['as' => 'userList2']);
$routes->get('add', 'Users::add', ['as' => 'newUser']);
$routes->post('add', 'Users::add', ['as' => 'createUser']);

View File

@ -87,7 +87,6 @@ class Users extends \App\Controllers\GoBaseController
unset($postData['chatDepartments']);
// Marcar el username como NULL
$postData['username'] = null;
$sanitizedData = $this->sanitized($postData, true);
$noException = true;
@ -99,35 +98,43 @@ 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'],
'last_name' => $sanitizedData['last_name'],
'email' => $sanitizedData['email'],
'password' => $sanitizedData['password'],
'status' => $sanitizedData['status'] ?? 0,
'active' => $sanitizedData['active'] ?? 0,
]);
$users->save($user);
$successfulResult = true; // Hacked
// The Email is unique
if ($this->user_model->isEmailUnique($sanitizedData['email'])) {
// Crear el usuario si pasa la validación
$user = new \CodeIgniter\Shield\Entities\User([
'username' => null, // If you don't have a username, be sure to set the value to null anyway, so that it passes CodeIgniter's empty data check
'first_name' => $sanitizedData['first_name'],
'last_name' => $sanitizedData['last_name'],
'cliente_id' => $sanitizedData['cliente_id'],
'comments' => $sanitizedData['comments'],
'email' => $sanitizedData['email'],
'password' => $sanitizedData['password'],
'status' => $sanitizedData['status'] ?? 0,
'active' => $sanitizedData['active'] ?? 0,
]);
// Add the user to the system
$users->save($user);
$successfulResult = true; // Hacked
} // Email is not unique!
else {
$this->viewData['errorMessage'] = "El correo '". $sanitizedData['email'] ."' ya está registrado en el sistema";
$this->session->setFlashdata('formErrors', $this->model->errors());
$successfulResult = false; // Hacked
}
} catch (\Exception $e) {
$noException = false;
//$this->dealWithException($e);
if (strpos($e->getMessage(), 'correo duplicado') !== false) {
$this->viewData['errorMessage'] = "El correo electrónico ya está registrado en el sistema";
$this->session->setFlashdata('formErrors', $this->model->errors());
}
$this->viewData['errorMessage'] = $e->getMessage();
}
else:
$this->viewData['errorMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Users.user'))]);
$this->session->setFlashdata('formErrors', $this->model->errors());
endif;
$thenRedirect = true; // Change this to false if you want your user to stay on the form after submission
endif;
if ($noException && $successfulResult) :
$id = $users->getInsertID();

View File

@ -30,14 +30,12 @@ class UserModel extends ShieldUserModel
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [
"first_name" => "required|trim|max_length[150]",
"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]",
'email' => 'required|valid_email|is_unique[auth_identities.secret]',
"comments" => "permit_empty|trim|max_length[512]"
];
protected $validationMessages = [
@ -81,7 +79,16 @@ 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;
$builder = $this->db
->table("auth_identities t1") // La tabla correcta
->select("t1.secret AS email")
->where('secret', $email);
// Obtener resultados
$result = $builder->get()->getRow();
// Devuelve true si no se encuentra el correo (es único), false en caso contrario
return $result === null;
}

View File

@ -20,6 +20,7 @@ class UserModel extends \App\Models\BaseModel
protected $allowedFields = [
"username",
"email",
"first_name",
"last_name",
"client_id",

View File

@ -21,7 +21,7 @@
name="save"
value="<?= lang("Basic.global.Save") ?>"
>
<?= anchor(route_to("userIndex"), lang("Basic.global.Cancel"), ["class" => "btn btn-secondary"]) ?>
<?= anchor(route_to("userList"), lang("Basic.global.Cancel"), ["class" => "btn btn-secondary"]) ?>
</div>
</form>
</div><!-- /.card-body -->

View File

@ -1,122 +1,59 @@
<?=$this->include('themes/_commonPartialsBs/datatables') ?>
<?=$this->extend('themes/vuexy/main/defaultlayout') ?>
<?=$this->section('content'); ?>
<div class="row">
<div class="col-md-12">
<?= $this->include('themes/_commonPartialsBs/datatables') ?>
<?= $this->extend('themes/vuexy/main/defaultlayout') ?>
<?= $this->section('content'); ?>
<div class="row">
<div class="col-md-12">
<div class="card card-info">
<div class="card-header">
<h3 class="card-title"><?=lang('Users.userList') ?></h3>
<?=anchor(route_to('newUser'), lang('Basic.global.addNew').' '.lang('Users.user'), ['class'=>'btn btn-primary float-end']); ?>
</div><!--//.card-header -->
<div class="card-body">
<?= view('themes/_commonPartialsBs/_alertBoxes'); ?>
<div class="card card-info">
<div class="card-header">
<h3 class="card-title"><?= lang('Users.userList') ?></h3>
<?= anchor(route_to('newUser'), lang('Basic.global.addNew') . ' ' . lang('Users.user'), ['class' => 'btn btn-primary float-end']); ?>
</div><!--//.card-header -->
<div class="card-body">
<?= view('themes/_commonPartialsBs/_alertBoxes'); ?>
<table id="tableOfUsers" class="table table-striped table-hover using-data-table" style="width: 100%;">
<thead>
<tr>
<th><?= lang('Users.firstName') ?></th>
<th><?= lang('Users.lastName') ?></th>
<th><?= lang('Users.email') ?></th>
<th><?= lang('Users.lastAccess') ?></th>
<?php /*
<th><?= lang('Users.mobile') ?></th>
<th><?= lang('Users.email') ?></th>
<th><?= lang('Users.address') ?></th>
<th><?= lang('Users.city') ?></th>
<th><?= lang('Users.state') ?></th>
<th><?= lang('Users.country') ?></th>
<th><?= lang('Users.zipCode') ?></th>
<th><?= lang('Users.lastIp') ?></th>
<th><?= lang('Users.picture') ?></th>
<th><?= lang('Users.language') ?></th>
<th><?= lang('Users.blocked') ?></th>
<th><?= lang('Users.emailConfirmed') ?></th>
*/ ?>
<th class="text-nowrap"><?= lang('Basic.global.Action') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($userList2 as $item ) : ?>
<tr>
<td class="align-middle">
<?= empty($item->first_name) || strlen($item->first_name) < 51 ? esc($item->first_name) : character_limiter(esc($item->first_name), 50) ?>
</td>
<td class="align-middle">
<?= empty($item->last_name) || strlen($item->last_name) < 51 ? esc($item->last_name) : character_limiter(esc($item->last_name), 50) ?>
</td>
<table id="tableOfUsers" class="table table-striped table-hover using-data-table"
style="width: 100%;">
<thead>
<tr>
<th><?= lang('Users.firstName') ?></th>
<th><?= lang('Users.lastName') ?></th>
<th><?= lang('Users.email') ?></th>
<th><?= lang('Users.lastAccess') ?></th>
<th class="text-nowrap"><?= lang('Basic.global.Action') ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($userList2 as $item) : ?>
<tr>
<td class="align-middle">
<?= empty($item->email) ? "" : character_limiter(esc(lang($item->email)), 50) ?>
<?= empty($item->first_name) || strlen($item->first_name) < 51 ? esc($item->first_name) : character_limiter(esc($item->first_name), 50) ?>
</td>
<td class="align-middle text-nowrap">
<?= empty($item->last_active) ? '' : date('d/m/Y H:m:s', strtotime($item->last_active)) ?>
<td class="align-middle">
<?= empty($item->last_name) || strlen($item->last_name) < 51 ? esc($item->last_name) : character_limiter(esc($item->last_name), 50) ?>
</td>
<td class="align-middle">
<?= empty(auth()->getProvider()->findById($item->id)->email) ? "" : character_limiter(esc(auth()->getProvider()->findById($item->id)->email), 50) ?>
</td>
<td class="align-middle text-nowrap">
<?= empty($item->last_active) ? '' : date('d/m/Y H:m:s', strtotime($item->last_active)) ?>
</td>
<?php /*
<td class="align-middle">
<?= empty($user_model->getGroupsTitles($item->token)) || strlen($user_model->getGroupsTitles($item->token)) < 51 ? esc($user_model->getGroupsTitles($item->token)) : character_limiter($user_model->getGroupsTitles($item->token), 50) ?>
</td>
<td class="align-middle text-center text-nowrap">
<?= anchor(route_to('editUser', $item->id), "<i class='ti ti-pencil ti-sm mx-2'></i>", ['class' => 'text-body', 'data-id' => $item->id,]); ?>
<?= anchor('#confirm2delete', "<i class='ti ti-trash ti-sm mx-2'></i>", ['class' => 'text-body', 'data-href' => route_to('deleteUser', $item->id), 'data-bs-toggle' => 'modal', 'data-bs-target' => '#confirm2delete']); ?>
</td>
</tr>
<td class="align-middle">
<?= empty($item->mobile) || strlen($item->mobile) < 51 ? esc($item->mobile) : character_limiter(esc($item->mobile), 50) ?>
</td>
<td class="align-middle">
<?= esc($item->email) ?>
</td>
<?php endforeach; ?>
</tbody>
</table>
</div><!--//.card-body -->
<div class="card-footer">
<td class="align-middle">
<?= empty($item->address) || strlen($item->address) < 51 ? esc($item->address) : character_limiter(esc($item->address), 50) ?>
</td>
<td class="align-middle">
<?= empty($item->city) || strlen($item->city) < 51 ? esc($item->city) : character_limiter(esc($item->city), 50) ?>
</td>
<td class="align-middle">
<?= empty($item->state) || strlen($item->state) < 51 ? esc($item->state) : character_limiter(esc($item->state), 50) ?>
</td>
<td class="align-middle">
<?= esc($item->country) ?>
</td>
<td class="align-middle">
<?= empty($item->zip_code) || strlen($item->zip_code) < 51 ? esc($item->zip_code) : character_limiter(esc($item->zip_code), 50) ?>
</td>
<td class="align-middle">
<?= empty($item->last_ip) || strlen($item->last_ip) < 51 ? esc($item->last_ip) : character_limiter(esc($item->last_ip), 50) ?>
</td>
<td class="align-middle text-nowrap">
<?= empty($item->last_access) ? '' : date('d/m/Y H:m:s', strtotime($item->last_access)) ?>
</td>
<td class="align-middle">
<?= empty($item->picture) || strlen($item->picture) < 51 ? esc($item->picture) : character_limiter(esc($item->picture), 50) ?>
</td>
<td class="align-middle">
<?= esc($item->language) ?>
</td>
<td class="align-middle text-nowrap">
<?= empty($item->blocked) ? '' : date('d/m/Y H:m:s', strtotime($item->blocked)) ?>
</td>
<td class="align-middle">
<?= esc($item->email_confirmed) ?>
</td>
*/ ?>
</div><!--//.card-footer -->
</div><!--//.card -->
</div><!--//.col -->
</div><!--//.row -->
<td class="align-middle text-center text-nowrap">
<?=anchor(route_to('editUser', $item->id), "<i class='ti ti-pencil ti-sm mx-2'></i>", ['class'=>'text-body', 'data-id'=>$item->id,]); ?>
<?=anchor('#confirm2delete', "<i class='ti ti-trash ti-sm mx-2'></i>", ['class'=>'text-body', 'data-href'=>route_to('deleteUser', $item->id), 'data-bs-toggle'=>'modal', 'data-bs-target'=>'#confirm2delete']); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div><!--//.card-body -->
<div class="card-footer">
</div><!--//.card-footer -->
</div><!--//.card -->
</div><!--//.col -->
</div><!--//.row -->
<?=$this->endSection() ?>
<?= $this->endSection() ?>