Arreglados gestion de usuarios y roles

This commit is contained in:
imnavajas
2024-05-07 17:19:01 +02:00
parent 6fb370ee30
commit bdf605b26f
30 changed files with 1194 additions and 1210 deletions

View File

@ -1,40 +0,0 @@
<?php
namespace App\Controllers\Catalogo;
use App\Controllers\BaseController;
class Catalogo extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Catalogo Libros';
}
public function importar()
{
echo 'Importar Catalogo Libros';
}
public function nuevo()
{
echo 'Nuevo Catalogo Libros';
}
public function categorias()
{
echo 'Categorias de Catalogo de Libros';
}
public function delete()
{
}
}

View File

@ -189,6 +189,12 @@ class Group extends \App\Controllers\GoBaseController
endif;
if ($noException && $successfulResult) :
// IMN hacer las configuraciones hard coded
helper('rbac');
generate_php_roles_constant();
generate_php_permissions_matrix_constant();
$id = $groupEntity->id ?? $id;
$message = lang('Basic.global.updateSuccess', [lang('Basic.global.record')]) . '.';

View File

@ -4,8 +4,10 @@
use App\Entities\Usuarios\UserEntity;
use App\Models\Usuarios\GroupModel;
use App\Models\Usuarios\GroupsUsersModel;
use App\Models\UserModel;
use App\Models\Usuarios\GroupsUsersModel;
use CodeIgniter\Shield\Entities\User;
class Users extends \App\Controllers\GoBaseController {
@ -43,49 +45,40 @@ class Users extends \App\Controllers\GoBaseController {
public function index() {
$this->viewData['usingClientSideDataTable'] = true;
$this->viewData['pageSubTitle'] = lang('Basic.global.ManageAllRecords', [lang('Users.user')]);
$this->viewData['user_model'] = $this->user_model;
parent::index();
}
public function add() {
public function add() {
if ($this->request->getPost()) :
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
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));
$sanitizedData = $this->sanitized($postData, true);
$noException = true;
$users = auth()->getProvider();
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
try {
$successfulResult = $this->model->skipValidation(true)->save($sanitizedData);
$user = new User([
'username' => strstr($sanitizedData['email'], '@', true),
'email' => $sanitizedData['email'],
'password' => 'Safekat2024',
]);
$users->save($user);
$successfulResult = true; // Hacked
} catch (\Exception $e) {
$noException = false;
$this->dealWithException($e);
@ -99,11 +92,12 @@ class Users extends \App\Controllers\GoBaseController {
endif;
if ($noException && $successfulResult) :
$id = $this->model->db->insertID();
$id = $users->getInsertID();
$this->group_user_model->where('user_id', $user->id)->delete();
foreach($currentGroups as $group){
$group_user_data = [
'token_user' => $sanitizedData['token'],
'token_group' => $group
'user_id' => $user->id,
'group' => $group
];
$this->group_user_model->insert($group_user_data);
}
@ -127,11 +121,8 @@ class Users extends \App\Controllers\GoBaseController {
endif; // ($requestMethod === 'post')
$this->viewData['user'] = isset($sanitizedData) ? new UserEntity($sanitizedData) : new UserEntity();
$this->viewData['paisList'] = $this->getPaisListItems();
$this->viewData['formAction'] = route_to('createUser');
$this->viewData['groups'] = $this->group_model->select('token,title')->findAll();
$this->viewData['groups'] = $this->group_model->select('keyword, title')->findAll();
$this->viewData['boxTitle'] = lang('Basic.global.addNew') .lang('Users.user').' '.lang('Basic.global.addNewSuffix');
@ -143,6 +134,7 @@ class Users extends \App\Controllers\GoBaseController {
if ($requestedId == null) :
return $this->redirect2listView();
endif;
$id = filter_var($requestedId, FILTER_SANITIZE_URL);
$user = $this->model->find($id);
@ -150,37 +142,22 @@ class Users extends \App\Controllers\GoBaseController {
$message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Users.user')), $id]);
return $this->redirect2listView('errorMessage', $message);
endif;
if ($this->request->getPost()) :
$nullIfEmpty = true; // !(phpversion() >= '8.1');
$postData = $this->request->getPost();
$currentGroups = $postData['group'];
unset($postData['group']);
$sanitizedData = $this->sanitized($postData, true);
if(!empty($postData['password'])){
$phpass = new PasswordHash(8, true);
$postData['password'] = $phpass->HashPassword($this->request->getPost('password'));
}
$sanitizedData = $this->sanitized($postData, $nullIfEmpty);
if ($this->request->getPost('tfa') == null ) {
$sanitizedData['tfa'] = false;
}
if ($this->request->getPost('blocked') == null ) {
$sanitizedData['blocked'] = false;
}
if ($this->request->getPost('last_ip') == null ) {
$sanitizedData['last_ip'] = '::1';
if ($this->request->getPost('status') == 0 ) {
$sanitizedData['status'] = null;
}
$noException = true;
if ($successfulResult = $this->canValidate()) : // if ($successfulResult = $this->validate($this->formValidationRules) ) :
if ($this->canValidate()) :
try {
@ -196,22 +173,21 @@ class Users extends \App\Controllers\GoBaseController {
endif;
$user->fill($sanitizedData);
$thenRedirect = false;
endif;
if ($noException && $successfulResult) :
$postData = $this->request->getPost();
$this->group_user_model->where('token_user', $user->token)->delete();
$this->group_user_model->where('user_id', $user->id)->delete();
foreach($currentGroups as $group){
$group_user_data = [
'token_user' => $user->token,
'token_group' => $group
'user_id' => $user->id,
'group' => $group
];
$this->group_user_model->insert($group_user_data);
}
$id = $user->id_user ?? $id;
$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 = ucfirst(str_replace("'", "\'", $message));
@ -230,18 +206,38 @@ class Users extends \App\Controllers\GoBaseController {
endif; // ($requestMethod === 'post')
$this->viewData['user'] = $user;
$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['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');
return $this->displayForm(__METHOD__, $id);
} // end function edit(...)
public function delete($requestedId = null, bool $deletePermanently = true) {
if ($requestedId == null) :
return $this->redirect2listView();
endif;
$id = filter_var($requestedId, FILTER_SANITIZE_URL);
$user = $this->model->find($id);
if ($user == false) :
$message = lang('Basic.global.notFoundWithIdErr', [mb_strtolower(lang('Users.user')), $id]);
return $this->redirect2listView('errorMessage', $message);
endif;
$users = auth()->getProvider();
$users->delete($user->id, $deletePermanently);
$message = "Usuario eliminado correctamente";
return $this->redirect2listView('successMessage', $message);
} // end function delete(...)

View File

@ -1,32 +0,0 @@
<?php
namespace App\Controllers\EnviosLogistica;
use App\Controllers\BaseController;
class Logistica extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Logistica';
}
public function etiquetas()
{
echo 'Etiquetas';
}
public function tracking()
{
echo 'Traking';
}
}

View File

@ -1,36 +0,0 @@
<?php
namespace App\Controllers\Importacion;
use App\Controllers\BaseController;
class Importar extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Importar';
}
// public function delete_files()
// {
//
// }
//
// public function pedidos_maquetacion()
// {
//
// }
//
// public function pedidos_prestashop()
// {
//
// }
}

View File

@ -1,22 +0,0 @@
<?php
namespace App\Controllers\Informes;
use App\Controllers\BaseController;
class Informe extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Informe';
}
}

View File

@ -22,74 +22,5 @@ class My extends BaseController
return redirect()->to('profile');
}
public function notification()
{
$session = session();
$data['title'] = [
'module' => lang("App.notification_title_my"),
'page' => lang("App.notification_subtitle_my"),
'icon' => 'fas fa-bell'
];
$data['breadcrumb'] = [
['title' => lang("App.menu_dashboard"), 'route' => "/home", 'active' => false],
['title' => lang("App.notification_title_my"), 'route' => "", 'active' => true]
];
$data['btn_add'] = [
'title' => lang("App.notification_btn_add"),
'route' => '/notification/add',
'class' => 'btn btn-lg btn-primary float-md-right',
'icon' => 'fas fa-plus'
];
echo view(getenv('theme.path').'main/header');
echo view(getenv('theme.path').'form/my/notification',$data);
echo view(getenv('theme.path').'main/footer');
}
public function notification_view($id)
{
$session = session();
$data['obj'] = $this->notification_model->where('token', $id)->first();
if($data['obj']==null){
return redirect()->to('/my/notification');
}else{
if(!$data['obj']['is_read']){
$this->notification_model->save(['id_notification' => $data['obj']['id_notification'],'is_read' => true]);
$notification = $session->get('notification')??[];
foreach ($notification as $key => $value){
if($notification[$key]['token'] == $id){
$notification[$key]['is_read'] = '1';
}
}
$pulse = $this->notification_model->where('user_recipient',$session->get('token'))->where('is_read',false)->countAllResults() ?? 0;
$session->set('pulse', $pulse);
$session->set('notification',$notification);
}
}
$data['title'] = [
'module' => lang("App.notification_title_my"),
'page' => lang("App.notification_subtitle_view"),
'icon' => 'far fa-envelope-open'
];
$data['breadcrumb'] = [
['title' => lang("App.menu_dashboard"), 'route' => "/home", 'active' => false],
['title' => lang("App.notification_title_my"), 'route' => "/my/notification", 'active' => false],
['title' => lang("App.notification_subtitle_view"), 'route' => "", 'active' => true]
];
$data['btn_return'] = [
'title' => lang("App.global_come_back"),
'route' => '/my/notification',
'class' => 'btn btn-dark mr-1',
'icon' => 'fas fa-angle-left'
];
echo view(getenv('theme.path').'main/header');
echo view(getenv('theme.path').'form/my/view',$data);
echo view(getenv('theme.path').'main/footer');
}
}

View File

@ -1,26 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
class Compras extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Compras';
}
public function ajustes()
{
echo 'Ajustes de Compras';
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
class Digitalizacion extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Digitalización';
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
class Impresion extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Impresion';
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
use function App\Controllers\ServiciosDigitalizacion\base_url;
use function App\Controllers\ServiciosDigitalizacion\service;
use function App\Controllers\ServiciosDigitalizacion\view;
class Maquetacion extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Maquetacion';
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
use function App\Controllers\ServiciosDigitalizacion\base_url;
use function App\Controllers\ServiciosDigitalizacion\service;
use function App\Controllers\ServiciosDigitalizacion\view;
class Mensajes extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Mensajes';
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
class Productos extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Productos';
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace App\Controllers\Servicios;
use App\Controllers\BaseController;
class Proveedor extends BaseController
{
function __construct()
{
}
public function index()
{
echo 'Proveedor';
}
}

View File

@ -10,6 +10,7 @@ use App\Models\Presupuestos\PresupuestoModel;
use App\Models\Usuarios\GroupModel;
use App\Models\Usuarios\PermisosModel;
use App\Services\PresupuestoService;
use CodeIgniter\Shield\Entities\User;
class Test extends BaseController
{
@ -21,8 +22,11 @@ class Test extends BaseController
{
helper('rbac');
var_dump(auth()->user()->can('plantilla-tarifa.menu'));
//$user = auth()->user();
generate_php_permissions_constant();
//echo generate_php_permissions_matrix_constant();