<?php
namespace App\Controller\Seguridades;
use App\Controller\InformacionGeneralController;
use App\Entity\Catalogos\Cantones;
use App\Entity\Catalogos\Iconos;
use App\Entity\Catalogos\Parroquias;
use App\Entity\Persona\CargosRol;
use App\Entity\Persona\Persona;
use App\Entity\Seguridades\{Grupo, GrupoCargo, MailSaliente, Modulo, Pagina,PermisosGrupo,PermisosUsuario, Secciones, Usuario, UsuarioGrupo};
use App\Form\Seguridades\Persona\{CargosRolType, PersonaType};
use App\Form\Seguridades\EditProfileType;
use App\Form\Seguridades\Entidades\{CamposType, GruposType, MailSalienteType, ModuleType,PagesType,SectionsType};
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\{CacheService,AdminService,LoginService, RegistroService};
use Exception;
class AdminController extends AbstractController
{
private $em;
private $cache;
public function __construct(EntityManagerInterface $em, CacheService $cache){
$this->em = $em;
$this->cache = $cache;
}
public function home(UserInterface $user,AdminService $adminService){
if($user->getIdPersona() == null):
return $this->redirectToRoute('addPerson');
endif;
$adminService->addAccess($user,'home', $this->em);
$informacionGeneralController = new InformacionGeneralController($this->em);
/* $permissions = $adminService->getPermissionsSec($user,$em,'profile');
dump($permissions); */
return $this->render('Seguridades/admin/home/index.html.twig',[
'informacionGeneral' => $informacionGeneralController->getData($user),
]);
}
public function addPersonInUserGmail(UserInterface $user,AdminService $adminService){
$form = $adminService->addPersonInUserGmailForm();
$informacionGeneralController = new InformacionGeneralController($this->em);
return $this->render('Seguridades/admin/perfil/addPerson.html.twig', [
'fecha'=> new \DateTime(),
'form'=> $form->createView(),
'informacionGeneral' => $informacionGeneralController->getData($user),
]);
}
public function addPersonExternal(AdminService $adminService){
$form = $adminService->addPersonInUserGmailForm();
return $this->render('Seguridades/admin/addPerson/index.html.twig', [
'fecha'=> new \DateTime(),
'form'=> $form->createView(),
]);
}
public function menucliente() {
return $this->render('Administration/cliente/index.html.twig');
}
public function profile(UserInterface $user, AdminService $adminService, EntityManagerInterface $em)
{
if($user->getIdPersona() == null):
return $this->redirectToRoute('addPerson');
endif;
//$adminService->addAccess($user,'profile', $em);
//$informacionGeneralController = new InformacionGeneralController($em);
/* $permissions = $adminService->getPermissionsSec($user,$em,'profile');
if(count($permissions)==0): return $this->redirectToRoute('paginaRestringida'); endif; */
//Comprueba si necesita cambiar su password
if($adminService->checkPassChange($user)):return $this->redirectToRoute('change_pass');endif;
$form = $adminService->addPersonInUserGmailForm($user);
$informacionGeneralController = new InformacionGeneralController($this->em);
return $this->render('Seguridades/admin/perfil/index.html.twig', [
'fecha'=> new \DateTime(),
'informacionGeneral' => $informacionGeneralController->getData($user),
'form'=> $form->createView(),
]);
}
public function paginaRestringida(UserInterface $user){
return $this->render('Seguridades/admin/accesoDenegado.html.twig',[
'nombres' => $user->getIdPersona()->getNombres()
]);
}
//pantalla que obliga al usuario a cambiar su contraseña
public function changePass(UserInterface $user, AdminService $adminService)
{
$form = $adminService->formSetPass();
return $this->render('Seguridades/usuario/changePass/setPass.html.twig',[
'form'=> $form->createView(),
]);
}
public function changePassAdmin(UserInterface $user,AdminService $adminService, EntityManagerInterface $em)
{
$adminService->addAccess($user,'changePassAdmin', $em);
$informacionGeneralController = new InformacionGeneralController($em);
$form = $adminService->formeditPass($user);
return $this->render('Seguridades/admin/changePass/index.html.twig',[
'form'=> $form->createView(),
'informacionGeneral' => $informacionGeneralController->getData($user),
]);
}
public function catalogs(UserInterface $user, EntityManagerInterface $em, AdminService $adminSer)
{
$adminSer->addAccess($user,'catalogsAdministration', $em);
$informacionGeneralController = new InformacionGeneralController($em);
$permissions = $adminSer->getPermissionsSec($user,$em,'usersGroups');
return $this->render('Seguridades/admin/catalogs/index.html.twig',[
'permisos'=>$permissions,
'catalogos' =>(isset($permissions[0]) && $permissions[0]['section'] == 'Administración de Catálogos' && $permissions[0]['see'])?true:false,
'informacionGeneral' => $informacionGeneralController->getData($user),
]);
}
public function usersGroups(UserInterface $user, AdminService $adminSer, EntityManagerInterface $em){
$adminSer->addAccess($user,'usersGroups', $em);
$informacionGeneralController = new InformacionGeneralController($em);
$permissions = $adminSer->getPermissionsSec($user,$em,'usersGroups');
$groupForm = $this->createForm(GruposType::class,new Grupo());
$personForm = $this->createForm(PersonaType::class,new Persona());
$cargosForm = $this->createForm(CargosRolType::class,new CargosRol());
$this->cache->delete('idCargo');
return $this->render('Seguridades/admin/usersGroups/usersGroups.html.twig',[
'fecha'=> new \DateTime(),
'informacionGeneral' => $informacionGeneralController->getData($user),
'groups'=> $groupForm->createView(),
'person'=> $personForm->createView(),
'cargosRol'=> $cargosForm->createView(),
'permisos'=>$permissions,
'grupos' =>(isset($permissions[0]) && $permissions[0]['section'] == 'Grupos' && $permissions[0]['see'])?true:false,
'cargos' =>(isset($permissions[1]) && $permissions[1]['section'] == 'Cargos' && $permissions[1]['see'])?true:false,
'usuarios' =>(isset($permissions[2]) && $permissions[2]['section'] == 'Usuarios' && $permissions[2]['see'])?true:false
]);
}
public function system(UserInterface $user, AdminService $adminSer, EntityManagerInterface $em){
$adminSer->addAccess($user,'system', $em);
$informacionGeneralController = new InformacionGeneralController($em);
$permissions = $adminSer->getPermissionsSec($user,$em,'system');
$moduleForm = $this->createForm(ModuleType::class, new Modulo());
$pageForm = $this->createForm(PagesType::class, new Pagina());
$sectionForm = $this->createForm(SectionsType::class, new Secciones());
return $this->render('Seguridades/admin/system/system.html.twig',[
'moduleForm'=>$moduleForm->createView(),
'pageForm'=>$pageForm->createView(),
'sectionForm'=>$sectionForm->createView(),
'fecha'=> new \DateTime(),
'informacionGeneral' => $informacionGeneralController->getData($user),
'paginaActual' => 'Sistema',
'permisos'=>$permissions,
'modules'=>(isset($permissions[0]) && $permissions[0]['section'] =='Módulos' && $permissions[0]['see'])?true:false,
'pages'=>(isset($permissions[1]) && $permissions[1]['section'] =='Páginas' && $permissions[1]['see'])?true:false,
'sections'=>(isset($permissions[2]) && $permissions[2]['section'] =='Secciones' && $permissions[2]['see'])?true:false
]);
}
public function permissions(UserInterface $user, EntityManagerInterface $em, AdminService $adminSer){
$adminSer->addAccess($user,'permissions', $em);
$informacionGeneralController = new InformacionGeneralController($em);
$permissions = $adminSer->getPermissionsSec($user,$em,'permissions');
return $this->render('Seguridades/admin/permissions/permissions.html.twig',[
'informacionGeneral' => $informacionGeneralController->getData($user),
'permisos'=>$permissions,
'grUs'=>(isset($permissions[0]) && $permissions[0]['section'] =='Grupo / Usuario' && $permissions[0]['see'])?true:false,
'per'=>(isset($permissions[1]) && $permissions[1]['section'] =='Permisos' && $permissions[1]['see'])?true:false,
]);
}
public function mailAdministration(UserInterface $user, EntityManagerInterface $em, AdminService $adminSer){
$adminSer->addAccess($user,'mailAdministration', $em);
$informacionGeneralController = new InformacionGeneralController($em);
$permissions = $this->getPermissionsMailAdministration($user,$adminSer,$em);
$mailSalienteForm = $this->createForm(MailSalienteType::class, new MailSaliente());
return $this->render('Seguridades/admin/mailAdministration/mailAdministration.html.twig',[
'informacionGeneral' => $informacionGeneralController->getData($user),
'permisos'=>$permissions,
'parametersMail'=>$permissions[0]['see'],
'mailSalienteForm'=>$mailSalienteForm->createView(),
]);
}
private function getPermissionsMailAdministration(UserInterface $user,AdminService $adminSer,EntityManagerInterface $em){
$permissions = $adminSer->getPermissionsSec($user,$em,'mailAdministration');
if(count($permissions)==0): return $this->redirectToRoute('paginaRestringida'); endif;
$parametersMail = ['see'=>false,'read'=>false,'edit'=>false];
foreach($permissions as $per){
switch ($per['section']){
case 'Parámetros Correo': $parametersMail = $per;break;
}
}
return [$parametersMail];
}
/**
* @Route("/comprobChPss", name="setPass")
*/
public function setPass(Request $request, EntityManagerInterface $em, UserInterface $user){
$loginService = new LoginService();
$pass = $request->request->get('pass');
try{
$us = $em->getRepository(Usuario::class)->find($user->getId());
$newPass = $loginService->encode($pass);
$us->setPassword($newPass);
$us->setNuevaClave(false);
$em->persist($us);$em->flush();
return $this->json(true);
}catch(Exception $e){
return $this->json(false);
}
}
/**
* @Route("/verifyPass", name="verifyPass")
*/
public function verifyPass(Request $request, UserInterface $user){
$passAc = $user->getPassword();
$passNw = $request->request->get('passNw');
return $this->json(password_verify($passNw,$passAc));
}
/**
* @Route("/verifyOldPass", name="verifyOldPass")
*/
public function verifyOldPass(Request $request, UserInterface $user){
$pass = $request->request->get('pass');
$oldPass = $user->getPassword();
return $this->json(password_verify($pass,$oldPass));
}
/**
* @Route("/getIcons", name="getIcons")
*/
public function getIcons(EntityManagerInterface $em){
return $this->json($em->getRepository(Iconos::class)->findBy(['activo'=>true],['id'=>'ASC']));
}
/**
* @Route("/validateCI", name="validateCI")
*/
public function validateCI(Request $request, RegistroService $regroService){
$ci = $request->request->get('ci');
return $this->json($regroService->ValidateCI($ci));
}
/**
* @Route("/getCantones", name="getCantones")
*/
public function getCantones(Request $request){
$id = $request->request->get('idProvincia');
$cantones = $this->em->getRepository(Cantones::class)->findBy(['idProvincias' => $id],['nombre'=>'ASC']);
return $this->json($cantones??[]);
}
/**
* @Route("/getParroquias", name="getParroquias")
*/
public function getParroquias(Request $request){
$id = $request->request->get('idCanton');
$parroquias = $this->em->getRepository(Parroquias::class)->findBy(['idCantones' => $id],['nombre'=>'ASC']);
return $this->json($parroquias??[]);
}
}