falta lenguaje de user y usermodel. Repasar funcionalidades

This commit is contained in:
Jaime Jimenez
2023-05-09 07:39:50 +02:00
parent 50e8cb9fd5
commit 6c6b603fd7
7 changed files with 143 additions and 103 deletions

View File

@ -5,12 +5,15 @@ use App\Entities\Usuarios\UserEntity;
use App\Models\Usuarios\UserGroupModel;
use App\Models\Usuarios\GroupUserModel;
use App\Models\Usuarios\UserModel;
use App\Libraries\PasswordHash;
class Users extends \App\Controllers\GoBaseController {
private $group_model;
private $group_user_model;
private $user_model;
use \CodeIgniter\API\ResponseTrait;
@ -32,6 +35,7 @@ class Users extends \App\Controllers\GoBaseController {
$this->group_model = new UserGroupModel();
$this->group_user_model = new GroupUserModel();
$this->user_model = new UserModel();
$this->viewData['pageTitle'] = lang('Users.moduleTitle');
parent::initController($request, $response, $logger);
@ -43,6 +47,8 @@ class Users extends \App\Controllers\GoBaseController {
$this->viewData['usingClientSideDataTable'] = true;
$this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Users.user')]);
$this->viewData['groupsPerUser'] = $this->getGroupsPerUser();
parent::index();
}
@ -58,8 +64,21 @@ class Users extends \App\Controllers\GoBaseController {
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
if(!empty($postData['password'])){
$phpass = new PasswordHash(8, true);
$postData['password'] = $phpass->HashPassword($this->request->getPost('password'));
}
$currentGroups = $postData['group'];
unset($postData['group']);
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
if ($this->request->getPost('last_ip') == null ) {
$sanitizedData['last_ip'] = '::1';
}
$sanitizedData['token'] = md5(uniqid(rand(), true));
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
@ -74,7 +93,7 @@ class Users extends \App\Controllers\GoBaseController {
}
else:
$this->viewData['errorMessage'] = lang('Basic.global.formErr1', [mb_strtolower(lang('Users.user'))]);
$this->session->setFlashdata('formErrors', $this->model->errors());
$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
@ -82,6 +101,13 @@ class Users extends \App\Controllers\GoBaseController {
if ($noException && $successfulResult) :
$id = $this->model->db->insertID();
foreach($currentGroups as $group){
$group_user_data = [
'token_user' => $sanitizedData['token'],
'token_group' => $group
];
$this->group_user_model->insert($group_user_data);
}
$message = lang('Basic.global.saveSuccess', [mb_strtolower(lang('Users.user'))]).'.';
$message .= anchor(route_to('editUser', $id), lang('Basic.global.continueEditing').'?');
@ -102,8 +128,8 @@ class Users extends \App\Controllers\GoBaseController {
endif; // ($requestMethod === 'post')
$this->viewData['user'] = isset($sanitizedData) ? new UserEntity($sanitizedData) : new UserEntity();
$this->viewData['userGroupList'] = $this->getUserGroupListItems($user->group ?? null);
$this->viewData['paisList'] = $this->getPaisListItems();
$this->viewData['formAction'] = route_to('createUser');
$this->viewData['groups'] = $this->group_model->select('token,title')->findAll();
@ -133,16 +159,20 @@ class Users extends \App\Controllers\GoBaseController {
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
$currentGroups = $postData['group'];
unset($postData['group']);
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
if ($this->request->getPost('tfa') == null ) {
$sanitizedData['tfa'] = false;
}
if ($this->request->getPost('status') == null ) {
$sanitizedData['status'] = false;
if ($this->request->getPost('blocked') == null ) {
$sanitizedData['blocked'] = false;
}
if ($this->request->getPost('last_ip') == null ) {
$sanitizedData['last_ip'] = '::1';
}
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
@ -161,11 +191,25 @@ class Users extends \App\Controllers\GoBaseController {
endif;
$user->fill($sanitizedData);
$thenRedirect = true;
endif;
if ($noException && $successfulResult) :
$postData = $this->request->getPost();
$this->group_user_model->where('token_user', $user->token)->delete();
foreach($currentGroups as $group){
$group_user_data = [
'token_user' => $user->token,
'token_group' => $group
];
$this->group_user_model->insert($group_user_data);
}
$id = $user->id_user ?? $id;
$message = lang('Basic.global.updateSuccess', [mb_strtolower(lang('Users.user'))]).'.';
$message .= anchor(route_to('editUser', $id), lang('Basic.global.continueEditing').'?');
@ -185,9 +229,11 @@ class Users extends \App\Controllers\GoBaseController {
endif; // ($requestMethod === 'post')
$this->viewData['user'] = $user;
$this->viewData['userGroupList'] = $this->getUserGroupListItems($user->group ?? null);
$this->viewData['paisList'] = $this->getPaisListItems();
$this->viewData['formAction'] = route_to('updateUser', $id);
$this->viewData['selectedGroups'] = $this->group_user_model->select('token_group')->where('token_user', $user->token)->findAll();
$this->viewData['groups'] = $this->group_model->select('token,title')->findAll();
$this->viewData['boxTitle'] = lang('Basic.global.edit2').' '.lang('Users.user').' '.lang('Basic.global.edit3');
@ -246,17 +292,29 @@ class Users extends \App\Controllers\GoBaseController {
}
}
protected function getUserGroupListItems($selId = null) {
$data = [''=>lang('Basic.global.pleaseSelectA', [mb_strtolower(lang('UserGroups.userGroup'))])];
if (!empty($selId)) :
$userGroupModel = model('App\Models\Usuarios\UserGroupModel');
protected function getPaisListItems() {
$data = [''=>lang('Basic.global.pleaseSelectA', [mb_strtolower(lang('Pais.pais'))])];
$paisModel = model('App\Models\Configuracion\PaisModel');
$selOption = $userGroupModel->where('token', $selId)->findColumn('title');
if (!empty($selOption)) :
$data[$selId] = $selOption[0];
endif;
endif;
return $data;
$registers = $paisModel->findAll();
return $registers;
}
protected function getGroupsPerUser() {
$tokens = $this->user_model->select('token')->findAll();
$data = [];
foreach($tokens as $token){
$groups = $this->group_user_model->select('token_group')->where('token_user', $token->token)->findAll();
$groups_title = [];
foreach($groups as $group_token){
array_push($groups_title, $this->group_model->select('title')->where('token', $group_token)->first()->title);
}
$data[$token->token] = implode(',',$groups_title);
}
return $data;
}
}