mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
añadido opcion de crear contraseña en interfaz de administrador
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
<?php namespace App\Controllers\Configuracion;
|
||||
<?php namespace App\Controllers\Configuracion;
|
||||
|
||||
|
||||
use App\Entities\Usuarios\UserEntity;
|
||||
@ -8,15 +8,17 @@ use App\Models\Usuarios\GroupModel;
|
||||
use App\Models\UserModel;
|
||||
use App\Models\Usuarios\GroupsUsersModel;
|
||||
use CodeIgniter\Shield\Entities\User;
|
||||
use function PHPUnit\Framework\isNull;
|
||||
|
||||
class Users extends \App\Controllers\GoBaseController {
|
||||
class Users extends \App\Controllers\GoBaseController
|
||||
{
|
||||
|
||||
private $group_model;
|
||||
private $group_user_model;
|
||||
private $user_model;
|
||||
|
||||
|
||||
use \CodeIgniter\API\ResponseTrait;
|
||||
use \CodeIgniter\API\ResponseTrait;
|
||||
|
||||
protected static $primaryModelName = 'App\Models\UserModel';
|
||||
|
||||
@ -29,9 +31,9 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
|
||||
protected $indexRoute = 'userList';
|
||||
|
||||
|
||||
|
||||
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
|
||||
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
|
||||
{
|
||||
|
||||
$this->group_model = new GroupModel();
|
||||
$this->group_user_model = new GroupsUsersModel();
|
||||
@ -46,31 +48,39 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
];
|
||||
|
||||
parent::initController($request, $response, $logger);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function index() {
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
||||
$this->viewData['usingClientSideDataTable'] = true;
|
||||
$this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Users.user')]);
|
||||
$this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Users.user')]);
|
||||
$this->viewData['user_model'] = $this->user_model;
|
||||
|
||||
$this->viewData['userList2'] = $this->user_model->getUsersList();
|
||||
$this->viewData['userList2'] = auth()->getProvider()->findAll();
|
||||
|
||||
parent::index();
|
||||
|
||||
}
|
||||
|
||||
public function add() {
|
||||
public function add()
|
||||
{
|
||||
|
||||
if ($this->request->getPost()) :
|
||||
|
||||
$postData = $this->request->getPost();
|
||||
|
||||
$currentGroups = $postData['group']??[];
|
||||
// Obtener contraseña nueva si se ha introducido en texto plano
|
||||
if (empty($postData['password'])) {
|
||||
$postData['password'] = 'Safekat2024'; // Contraseña por defecto
|
||||
}
|
||||
|
||||
$currentGroups = $postData['group'] ?? [];
|
||||
unset($postData['group']);
|
||||
|
||||
$postData['username'] = strstr($postData['email'], '@', true);
|
||||
$sanitizedData = $this->sanitized($postData, true);
|
||||
$sanitizedData = $this->sanitized($postData, true);
|
||||
|
||||
$noException = true;
|
||||
|
||||
@ -78,40 +88,40 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
|
||||
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
|
||||
if ($this->canValidate()) :
|
||||
try {
|
||||
try {
|
||||
|
||||
$user = new User([
|
||||
'username' => $sanitizedData['username'],
|
||||
'first_name' => $sanitizedData['first_name'],
|
||||
'last_name' => $sanitizedData['last_name'],
|
||||
'email' => $sanitizedData['email'],
|
||||
'password' => 'Safekat2024',
|
||||
'status' => $sanitizedData['status']??0,
|
||||
'active' => $sanitizedData['active']??0,
|
||||
'email' => $sanitizedData['email'],
|
||||
'password' => $sanitizedData['password'],
|
||||
'status' => $sanitizedData['status'] ?? 0,
|
||||
'active' => $sanitizedData['active'] ?? 0,
|
||||
]);
|
||||
$users->save($user);
|
||||
$successfulResult = true; // Hacked
|
||||
} catch (\Exception $e) {
|
||||
$noException = false;
|
||||
//$this->dealWithException($e);
|
||||
} 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());
|
||||
}
|
||||
|
||||
}
|
||||
else:
|
||||
$this->viewData['errorMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Users.user'))]);
|
||||
|
||||
}
|
||||
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;
|
||||
|
||||
$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();
|
||||
$this->group_user_model->where('user_id', $id)->delete();
|
||||
foreach($currentGroups as $group){
|
||||
foreach ($currentGroups as $group) {
|
||||
$group_user_data = [
|
||||
'user_id' => $id,
|
||||
'group' => $group
|
||||
@ -119,8 +129,7 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
$this->group_user_model->insert($group_user_data);
|
||||
}
|
||||
|
||||
$message = lang('Basic.global.saveSuccess', [mb_strtolower(lang('Users.user'))]) . 'Downloads';
|
||||
$message .= anchor(route_to('editUser', $id), lang('Basic.global.continueEditing').'?');
|
||||
$message = lang('Basic.global.saveSuccess', [mb_strtolower(lang('Users.user'))]) . '.';
|
||||
$message = ucfirst(str_replace("'", "\'", $message));
|
||||
|
||||
if ($thenRedirect) :
|
||||
@ -141,88 +150,91 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
$this->viewData['clienteList'] = $this->getClienteListItems();
|
||||
$this->viewData['formAction'] = route_to('createUser');
|
||||
$this->viewData['groups'] = $this->group_model->select('keyword, title')->findAll();
|
||||
$this->viewData['boxTitle'] = lang('Basic.global.addNew') .lang('Users.user').' '.lang('Basic.global.addNewSuffix');
|
||||
|
||||
$this->viewData['boxTitle'] = lang('Basic.global.addNew') . lang('Users.user') . ' ' . lang('Basic.global.addNewSuffix');
|
||||
|
||||
|
||||
return $this->displayForm(__METHOD__);
|
||||
} // end function add()
|
||||
|
||||
public function edit($requestedId = null) {
|
||||
|
||||
if ($requestedId == null) :
|
||||
public function edit($requestedId = null)
|
||||
{
|
||||
if ($requestedId == null) {
|
||||
return $this->redirect2listView();
|
||||
endif;
|
||||
}
|
||||
|
||||
$id = filter_var($requestedId, FILTER_SANITIZE_URL);
|
||||
$user = $this->model->find($id);
|
||||
$users = auth()->getProvider();
|
||||
$user = $users->findById($id);
|
||||
|
||||
if ($user == false) :
|
||||
$message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Users.user')), $id]);
|
||||
return $this->redirect2listView('errorMessage', $message);
|
||||
endif;
|
||||
|
||||
|
||||
if ($this->request->getPost()) :
|
||||
|
||||
$postData = $this->request->getPost();
|
||||
|
||||
$currentGroups = $postData['group'];
|
||||
$currentGroups = $postData['group'] ?? [];
|
||||
unset($postData['group']);
|
||||
|
||||
// Obtener contraseña nueva si se ha introducido en texto plano
|
||||
if (empty($postData['password'])) {
|
||||
unset($postData['password']);
|
||||
}
|
||||
|
||||
$sanitizedData = $this->sanitized($postData, true);
|
||||
|
||||
if ($this->request->getPost('status') == 0 ) {
|
||||
if ($this->request->getPost('status') == 0) {
|
||||
$sanitizedData['status'] = null;
|
||||
}
|
||||
|
||||
$noException = true;
|
||||
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
|
||||
if ($successfulResult = $this->canValidate()) :
|
||||
|
||||
if ($this->canValidate()) :
|
||||
try {
|
||||
|
||||
if ($this->canValidate()) :
|
||||
try {
|
||||
if (in_array('cliente-editor', $currentGroups) || in_array('cliente-administrador', $currentGroups)) {
|
||||
if(!array_key_exists('cliente_id', $sanitizedData) || is_null($sanitizedData['cliente_id'])) {
|
||||
if (!array_key_exists('cliente_id', $sanitizedData) || is_null($sanitizedData['cliente_id'])) {
|
||||
$this->viewData['errorMessage'] = lang('Users.errors.cliente_sin_clienteID');
|
||||
$this->session->setFlashdata('formErrors', $this->model->errors());
|
||||
|
||||
$successfulResult = false;
|
||||
} else {
|
||||
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
|
||||
}
|
||||
else{
|
||||
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
$successfulResult = $this->model->skipValidation(true)->update($id, $sanitizedData);
|
||||
}
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$noException = false;
|
||||
$this->dealWithException($e);
|
||||
}
|
||||
else:
|
||||
$this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Users.user'))]);
|
||||
$this->session->setFlashdata('formErrors', $this->model->errors());
|
||||
|
||||
endif;
|
||||
} catch (\Exception $e) {
|
||||
$noException = false;
|
||||
$this->dealWithException($e);
|
||||
}
|
||||
else:
|
||||
$this->viewData['warningMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Users.user'))]);
|
||||
$this->session->setFlashdata('formErrors', $this->model->errors());
|
||||
|
||||
$user->fill($sanitizedData);
|
||||
$thenRedirect = false;
|
||||
endif;
|
||||
|
||||
$user->fill($sanitizedData);
|
||||
$users->save($user);
|
||||
$thenRedirect = false;
|
||||
|
||||
endif;
|
||||
if ($noException && $successfulResult) :
|
||||
|
||||
$this->group_user_model->where('user_id', $user->id)->delete();
|
||||
foreach($currentGroups as $group){
|
||||
foreach ($currentGroups as $group) {
|
||||
$group_user_data = [
|
||||
'user_id' => $user->id,
|
||||
'group' => $group
|
||||
];
|
||||
$this->group_user_model->insert($group_user_data);
|
||||
}
|
||||
|
||||
|
||||
$id = $user->id ?? $id;
|
||||
$message = lang('Basic.global.updateSuccess', [mb_strtolower(lang('Users.user'))]) . 'Downloads';
|
||||
$message .= anchor(route_to('editUser', $id), lang('Basic.global.continueEditing').'?');
|
||||
$message = lang('Basic.global.updateSuccess', [mb_strtolower(lang('Users.user'))]) . '.';
|
||||
$message = ucfirst(str_replace("'", "\'", $message));
|
||||
|
||||
if ($thenRedirect) :
|
||||
@ -234,7 +246,7 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
else:
|
||||
$this->session->setFlashData('sweet-success', $message);
|
||||
endif;
|
||||
|
||||
|
||||
endif; // $noException && $successfulResult
|
||||
endif; // ($requestMethod === 'post')
|
||||
|
||||
@ -243,13 +255,14 @@ 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['boxTitle'] = lang('Basic.global.edit2') .lang('Users.user').' '.lang('Basic.global.edit3');
|
||||
$this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('Users.user') . ' ' . lang('Basic.global.edit3');
|
||||
|
||||
return $this->displayForm(__METHOD__, $id);
|
||||
} // end function edit(...)
|
||||
|
||||
|
||||
public function delete($requestedId = null, bool $deletePermanently = true) {
|
||||
public function delete($requestedId = null, bool $deletePermanently = true)
|
||||
{
|
||||
|
||||
if ($requestedId == null) :
|
||||
return $this->redirect2listView();
|
||||
@ -264,26 +277,25 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
endif;
|
||||
|
||||
$users = auth()->getProvider();
|
||||
$users->delete($user->id, $deletePermanently);
|
||||
$users->delete($user->id);
|
||||
|
||||
$message = "Usuario eliminado correctamente";
|
||||
return $this->redirect2listView('successMessage', $message);
|
||||
|
||||
|
||||
|
||||
} // end function delete(...)
|
||||
|
||||
|
||||
|
||||
public function allItemsSelect() {
|
||||
|
||||
public function allItemsSelect()
|
||||
{
|
||||
if ($this->request->isAJAX()) {
|
||||
$onlyActiveOnes = true;
|
||||
$reqVal = $this->request->getPost('val') ?? 'id_user';
|
||||
$menu = $this->model->getAllForMenu($reqVal.', first_name', 'first_name', $onlyActiveOnes, false);
|
||||
$menu = $this->model->getAllForMenu($reqVal . ', first_name', 'first_name', $onlyActiveOnes, false);
|
||||
$nonItem = new \stdClass;
|
||||
$nonItem->id_user = '';
|
||||
$nonItem->first_name = '- '.lang('Basic.global.None').' -';
|
||||
array_unshift($menu , $nonItem);
|
||||
$nonItem->first_name = '- ' . lang('Basic.global.None') . ' -';
|
||||
array_unshift($menu, $nonItem);
|
||||
|
||||
$newTokenHash = csrf_hash();
|
||||
$csrfTokenName = csrf_token();
|
||||
@ -296,8 +308,9 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
return $this->failUnauthorized('Invalid request', 403);
|
||||
}
|
||||
}
|
||||
|
||||
public function menuItems() {
|
||||
|
||||
public function menuItems()
|
||||
{
|
||||
if ($this->request->isAJAX()) {
|
||||
$searchStr = goSanitize($this->request->getPost('searchTerm'))[0];
|
||||
$reqId = goSanitize($this->request->getPost('id'))[0];
|
||||
@ -308,8 +321,8 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
$menu = $this->model->getSelect2MenuItems($columns2select, $columns2select[1], $onlyActiveOnes, $searchStr);
|
||||
$nonItem = new \stdClass;
|
||||
$nonItem->id = '';
|
||||
$nonItem->text = '- '.lang('Basic.global.None').' -';
|
||||
array_unshift($menu , $nonItem);
|
||||
$nonItem->text = '- ' . lang('Basic.global.None') . ' -';
|
||||
array_unshift($menu, $nonItem);
|
||||
|
||||
$newTokenHash = csrf_hash();
|
||||
$csrfTokenName = csrf_token();
|
||||
@ -323,10 +336,11 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
}
|
||||
}
|
||||
|
||||
public function getMenuComerciales(){
|
||||
public function getMenuComerciales()
|
||||
{
|
||||
if ($this->request->isAJAX()) {
|
||||
$comerciales = $this->model->getComerciales();
|
||||
|
||||
|
||||
$newTokenHash = csrf_hash();
|
||||
$csrfTokenName = csrf_token();
|
||||
$data = [
|
||||
@ -339,15 +353,16 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function getPaisListItems() {
|
||||
$data = [''=>lang('Basic.global.pleaseSelectA', [mb_strtolower(lang('Pais.pais'))])];
|
||||
|
||||
protected function getPaisListItems()
|
||||
{
|
||||
$data = ['' => lang('Basic.global.pleaseSelectA', [mb_strtolower(lang('Pais.pais'))])];
|
||||
$paisModel = model('App\Models\Configuracion\PaisModel');
|
||||
|
||||
$registers = $paisModel->findAll();
|
||||
|
||||
return $registers;
|
||||
}
|
||||
return $registers;
|
||||
}
|
||||
|
||||
protected function getClienteListItems($selId = null)
|
||||
{
|
||||
@ -362,5 +377,5 @@ class Users extends \App\Controllers\GoBaseController {
|
||||
endif;
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user