2022-07-21 16:15:47 +02:00
< ? php
2022-09-23 16:14:15 +02:00
namespace App\Controller ;
2022-07-21 16:15:47 +02:00
use App\Entity\User as Entity ;
use App\Entity\UserGroup ;
use App\Entity\UserModo ;
use App\Form\UserType as Form ;
2022-09-23 16:14:15 +02:00
use Doctrine\Persistence\ManagerRegistry ;
use Ramsey\Uuid\Uuid ;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ;
use Symfony\Component\HttpFoundation\JsonResponse ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
2022-07-21 16:15:47 +02:00
class UserController extends AbstractController
{
2022-09-23 16:14:15 +02:00
private $data = 'user' ;
private $entity = " App \ Entity \ User " ;
private $twig = 'User/' ;
private $route = 'app_admin_user' ;
2022-10-02 14:50:48 +02:00
public function list ( $access , Request $request , ManagerRegistry $em ) : Response
2022-09-23 16:14:15 +02:00
{
2022-10-02 14:50:48 +02:00
if ( 'all' == $access && ! $request -> getSession () -> get ( 'showannuaire' )) {
2022-07-21 16:15:47 +02:00
throw $this -> createAccessDeniedException ( 'Permission denied' );
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
return $this -> render ( $this -> twig . 'list.html.twig' , [
'useheader' => true ,
'usemenu' => false ,
2022-10-02 14:50:48 +02:00
'usesidebar' => ( 'all' != $access ),
2022-09-23 16:14:15 +02:00
'access' => $access ,
2022-10-02 14:50:48 +02:00
'isactive' => ( 'true' == $em -> getRepository ( $this -> entity ) -> getPreference ( $this -> getUser (), 'userisactive' , true )),
2022-07-21 16:15:47 +02:00
]);
}
2022-09-23 16:14:15 +02:00
public function tablelist ( $access , Request $request , ManagerRegistry $em ) : Response
{
$query = $request -> query -> all ();
$start = $query [ 'start' ];
$length = $query [ 'length' ];
$search = $query [ 'search' ];
$draw = $query [ 'draw' ];
$ordercolumn = $query [ 'order' ][ 0 ][ 'column' ];
$orderdir = $query [ 'order' ][ 0 ][ 'dir' ];
$niveau01 = null ;
$niveau02 = null ;
2022-09-27 11:52:49 +02:00
$niveau03 = null ;
$niveau04 = null ;
2022-10-02 14:50:48 +02:00
$isactive = true ;
2022-07-21 16:15:47 +02:00
// Nombre total d'enregistrement
2022-09-23 16:14:15 +02:00
switch ( $access ) {
case 'admin' :
2022-10-02 14:50:48 +02:00
$isactive = $em -> getRepository ( $this -> entity ) -> getPreference ( $this -> getUser (), 'userisactive' , true );
$total = $em -> getManager () -> createQueryBuilder ()
-> select ( 'COUNT(entity)' )
-> from ( $this -> entity , 'entity' )
-> where ( 'entity.isactive = :isactive' )
-> setParameter ( 'isactive' , $isactive )
-> getQuery () -> getSingleScalarResult ();
2022-09-23 16:14:15 +02:00
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 'modo' :
2022-10-02 14:50:48 +02:00
$isactive = $em -> getRepository ( $this -> entity ) -> getPreference ( $this -> getUser (), 'userisactive' , true );
2022-07-21 16:15:47 +02:00
$total = $em -> getManager () -> createQueryBuilder ()
-> select ( 'COUNT(entity)' )
2022-09-23 16:14:15 +02:00
-> from ( $this -> entity , 'entity' )
-> from ( " App \ Entity \ UserModo " , 'usermodo' )
-> where ( 'usermodo.niveau01 = entity.niveau01' )
-> andWhere ( 'usermodo.user = :user' )
2022-10-04 09:47:41 +02:00
-> andwhere ( 'entity.isactive = :isactive' )
2022-10-02 14:50:48 +02:00
-> setParameter ( 'isactive' , $isactive )
2022-09-23 16:14:15 +02:00
-> setParameter ( 'user' , $this -> getUser ())
-> getQuery () -> getSingleScalarResult ();
break ;
2022-07-21 16:15:47 +02:00
default :
2022-10-04 11:55:50 +02:00
$isactive = ( 'manager' == $access ? $em -> getRepository ( $this -> entity ) -> getPreference ( $this -> getUser (), 'userisactive' , true ) : true );
2022-10-04 11:32:05 +02:00
2022-09-23 16:14:15 +02:00
$niveau01 = $this -> getUser () -> getNiveau01 ();
$niveau02 = $this -> getUser () -> getNiveau02 ();
2022-09-27 11:52:49 +02:00
$niveau03 = $this -> getUser () -> getNiveau03 ();
$niveau04 = $this -> getUser () -> getNiveau04 ();
2022-07-21 16:15:47 +02:00
2022-10-02 14:50:48 +02:00
$qb = $em -> getManager () -> createQueryBuilder ()
-> select ( 'COUNT(entity)' )
-> from ( $this -> entity , 'entity' )
-> where ( 'entity.isvisible=true' )
2022-10-04 09:47:41 +02:00
-> andwhere ( 'entity.isactive = :isactive' )
2022-10-02 14:50:48 +02:00
-> setParameter ( 'isactive' , $isactive );
2022-09-23 16:14:15 +02:00
switch ( $request -> getSession () -> get ( 'scopeannu' )) {
2022-09-27 11:52:49 +02:00
case 1 :
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'entity.niveau01 = :niveau01' ) -> setParameter ( 'niveau01' , $niveau01 );
break ;
2022-07-21 16:15:47 +02:00
2022-09-27 11:52:49 +02:00
case 2 :
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'entity.niveau02 = :niveau02' ) -> setParameter ( 'niveau02' , $niveau02 );
break ;
2022-09-27 11:52:49 +02:00
case 3 :
$qb -> andWhere ( 'entity.niveau03 = :niveau03' ) -> setParameter ( 'niveau03' , $niveau03 );
break ;
case 4 :
$qb -> andWhere ( 'entity.niveau04 = :niveau04' ) -> setParameter ( 'niveau04' , $niveau04 );
break ;
2022-07-21 16:15:47 +02:00
}
$total = $qb -> getQuery () -> getSingleScalarResult ();
2022-09-23 16:14:15 +02:00
break ;
2022-07-21 16:15:47 +02:00
}
// Nombre d'enregistrement filtré
2022-09-23 16:14:15 +02:00
if ( ! $search || '' == $search [ 'value' ]) {
2022-07-21 16:15:47 +02:00
$totalf = $total ;
2022-09-23 16:14:15 +02:00
} else {
switch ( $access ) {
case 'admin' :
$totalf = $em -> getManager () -> createQueryBuilder ()
2022-07-21 16:15:47 +02:00
-> select ( 'COUNT(entity)' )
2022-09-23 16:14:15 +02:00
-> from ( $this -> entity , 'entity' )
2022-07-21 16:15:47 +02:00
-> from ( 'App:Niveau01' , 'niveau01' )
-> where ( 'entity.niveau01=niveau01.id' )
2022-10-02 14:50:48 +02:00
-> andwhere ( 'entity.isactive = :isactive' )
2022-07-21 16:15:47 +02:00
-> andWhere ( 'entity.username LIKE :value OR entity.firstname LIKE :value OR entity.lastname LIKE :value OR entity.email LIKE :value OR entity.roles LIKE :value OR niveau01.label LIKE :value' )
2022-10-02 14:50:48 +02:00
-> setParameter ( 'isactive' , $isactive )
2022-09-23 16:14:15 +02:00
-> setParameter ( 'value' , '%' . $search [ 'value' ] . '%' )
2022-07-21 16:15:47 +02:00
-> getQuery ()
-> getSingleScalarResult ();
2022-09-23 16:14:15 +02:00
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 'modo' :
$totalf = $em -> getManager () -> createQueryBuilder ()
2022-07-21 16:15:47 +02:00
-> select ( 'COUNT(entity)' )
2022-09-23 16:14:15 +02:00
-> from ( $this -> entity , 'entity' )
2022-07-21 16:15:47 +02:00
-> from ( 'App:Niveau01' , 'niveau01' )
2022-09-23 16:14:15 +02:00
-> from ( 'App:UserModo' , 'usermodo' )
2022-07-21 16:15:47 +02:00
-> where ( 'entity.niveau01=niveau01.id' )
2022-10-02 14:50:48 +02:00
-> andwhere ( 'entity.isactive = :isactive' )
2022-07-21 16:15:47 +02:00
-> andWhere ( 'entity.username LIKE :value OR entity.firstname LIKE :value OR entity.lastname LIKE :value OR entity.email LIKE :value OR entity.roles LIKE :value OR niveau01.label LIKE :value' )
2022-09-23 16:14:15 +02:00
-> andWhere ( 'usermodo.niveau01 = entity.niveau01' )
-> andWhere ( 'usermodo.user = :userid' )
-> setParameter ( 'value' , '%' . $search [ 'value' ] . '%' )
2022-10-02 14:50:48 +02:00
-> setParameter ( 'isactive' , $isactive )
2022-09-23 16:14:15 +02:00
-> setParameter ( 'userid' , $this -> getUser () -> getId ())
2022-07-21 16:15:47 +02:00
-> getQuery ()
2022-09-23 16:14:15 +02:00
-> getSingleScalarResult ();
break ;
2022-07-21 16:15:47 +02:00
default :
2022-09-23 16:14:15 +02:00
$qb = $em -> getManager () -> createQueryBuilder ()
2022-07-21 16:15:47 +02:00
-> select ( 'COUNT(entity)' )
2022-09-23 16:14:15 +02:00
-> from ( $this -> entity , 'entity' )
2022-07-21 16:15:47 +02:00
-> from ( 'App:Niveau01' , 'niveau01' )
-> where ( 'entity.niveau01=niveau01.id' )
2022-10-02 14:50:48 +02:00
-> andwhere ( 'entity.isactive = :isactive' )
2022-07-21 16:15:47 +02:00
-> andWhere ( 'entity.isvisible=true' )
-> andWhere ( 'entity.username LIKE :value OR entity.firstname LIKE :value OR entity.lastname LIKE :value OR entity.email LIKE :value OR entity.roles LIKE :value OR niveau01.label LIKE :value' )
2022-10-02 14:50:48 +02:00
-> setParameter ( 'value' , '%' . $search [ 'value' ] . '%' )
-> setParameter ( 'isactive' , $isactive );
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
switch ( $request -> getSession () -> get ( 'scopeannu' )) {
2022-09-27 11:52:49 +02:00
case 1 :
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'entity.niveau01 = :niveau01' ) -> setParameter ( 'niveau01' , $niveau01 );
break ;
2022-09-27 11:52:49 +02:00
case 2 :
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'entity.niveau02 = :niveau02' ) -> setParameter ( 'niveau02' , $niveau02 );
break ;
2022-09-27 11:52:49 +02:00
case 3 :
$qb -> andWhere ( 'entity.niveau03 = :niveau03' ) -> setParameter ( 'niveau03' , $niveau03 );
break ;
case 4 :
$qb -> andWhere ( 'entity.niveau04 = :niveau04' ) -> setParameter ( 'niveau04' , $niveau04 );
break ;
2022-09-23 16:14:15 +02:00
}
$totalf = $qb -> getQuery () -> getSingleScalarResult ();
break ;
2022-07-21 16:15:47 +02:00
}
}
// Construction du tableau de retour
2022-09-23 16:14:15 +02:00
$output = [
2022-07-21 16:15:47 +02:00
'draw' => $draw ,
'recordsFiltered' => $totalf ,
'recordsTotal' => $total ,
2022-09-23 16:14:15 +02:00
'data' => [],
];
2022-07-21 16:15:47 +02:00
// Parcours des Enregistrement
$qb = $em -> getManager () -> createQueryBuilder ();
2022-09-23 16:14:15 +02:00
switch ( $access ) {
case 'admin' :
$qb -> select ( 'entity' ) -> from ( $this -> entity , 'entity' ) -> from ( 'App:Niveau01' , 'niveau01' );
2022-07-21 16:15:47 +02:00
$qb -> where ( 'entity.niveau01=niveau01.id' );
2022-10-02 14:50:48 +02:00
$qb -> andwhere ( 'entity.isactive = :isactive' );
$qb -> setParameter ( 'isactive' , $isactive );
2022-09-23 16:14:15 +02:00
break ;
case 'modo' :
$qb -> select ( 'entity' ) -> from ( $this -> entity , 'entity' ) -> from ( 'App:Niveau01' , 'niveau01' ) -> from ( 'App:UserModo' , 'usermodo' );
2022-07-21 16:15:47 +02:00
$qb -> where ( 'entity.niveau01=niveau01.id' );
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'usermodo.niveau01 = entity.niveau01' );
$qb -> andWhere ( 'usermodo.user = :userid' );
2022-10-02 14:50:48 +02:00
$qb -> andwhere ( 'entity.isactive = :isactive' );
$qb -> setParameter ( 'isactive' , $isactive );
2022-09-23 16:14:15 +02:00
$qb -> setParameter ( 'userid' , $this -> getUser () -> getId ());
break ;
2022-07-21 16:15:47 +02:00
default :
2022-09-23 16:14:15 +02:00
$qb -> select ( 'entity' ) -> from ( $this -> entity , 'entity' ) -> from ( 'App:Niveau01' , 'niveau01' );
2022-07-21 16:15:47 +02:00
$qb -> where ( 'entity.niveau01=niveau01.id' );
$qb -> andWhere ( 'entity.isvisible=true' );
2022-10-02 14:50:48 +02:00
$qb -> andwhere ( 'entity.isactive = :isactive' );
$qb -> setParameter ( 'isactive' , $isactive );
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
switch ( $request -> getSession () -> get ( 'scopeannu' )) {
2022-09-27 11:52:49 +02:00
case 1 :
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'entity.niveau01 = :niveau01' ) -> setParameter ( 'niveau01' , $niveau01 );
break ;
2022-07-21 16:15:47 +02:00
2022-09-27 11:52:49 +02:00
case 2 :
2022-09-23 16:14:15 +02:00
$qb -> andWhere ( 'entity.niveau02 = :niveau02' ) -> setParameter ( 'niveau02' , $niveau02 );
break ;
2022-09-27 11:52:49 +02:00
case 3 :
$qb -> andWhere ( 'entity.niveau03 = :niveau03' ) -> setParameter ( 'niveau03' , $niveau03 );
break ;
case 4 :
$qb -> andWhere ( 'entity.niveau04 = :niveau04' ) -> setParameter ( 'niveau04' , $niveau04 );
break ;
2022-09-23 16:14:15 +02:00
}
break ;
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
if ( $search && '' != $search [ 'value' ]) {
$qb -> andWhere ( 'entity.username LIKE :value OR entity.firstname LIKE :value OR entity.lastname LIKE :value OR entity.email LIKE :value OR entity.roles LIKE :value OR niveau01.label LIKE :value' )
-> setParameter ( 'value' , '%' . $search [ 'value' ] . '%' );
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
if ( $ordercolumn ) {
2022-10-04 11:32:05 +02:00
if ( 'admin' == $access || 'modo' == $access || 'manager' == $access ) {
2022-09-23 16:14:15 +02:00
$ordercolumn = $ordercolumn - 1 ;
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
switch ( $ordercolumn ) {
case 1 :
$qb -> orderBy ( 'entity.username' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 2 :
$qb -> orderBy ( 'entity.lastname' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 3 :
$qb -> orderBy ( 'entity.firstname' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 4 :
$qb -> orderBy ( 'entity.email' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 5 :
$qb -> orderBy ( 'entity.telephonenumber' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 6 :
$qb -> orderBy ( 'niveau01.label' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
case 8 :
$qb -> orderBy ( 'entity.visitedate' , $orderdir );
break ;
case 9 :
$qb -> orderBy ( 'entity.roles' , $orderdir );
break ;
2022-07-21 16:15:47 +02:00
}
}
2022-09-23 16:14:15 +02:00
$datas = $qb -> setFirstResult ( $start ) -> setMaxResults ( $length ) -> getQuery () -> getResult ();
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
foreach ( $datas as $data ) {
2022-07-21 16:15:47 +02:00
// Action
2022-09-23 16:14:15 +02:00
$action = '' ;
switch ( $access ) {
case 'admin' :
$action .= " <a href=' " . $this -> generateUrl ( $this -> route . '_update' , [ 'id' => $data -> getId ()]) . " '><i class='fa fa-file fa-fw fa-2x'></i></a> " ;
break ;
case 'modo' :
$action .= " <a href=' " . $this -> generateUrl ( str_replace ( '_admin_' , '_modo_' , $this -> route ) . '_update' , [ 'id' => $data -> getId ()]) . " '><i class='fa fa-file fa-fw fa-2x'></i></a> " ;
break ;
2022-10-04 11:32:05 +02:00
case 'manager' :
2022-10-04 11:55:50 +02:00
if ( $this -> getUser () -> getNiveau03 () && $this -> getUser () -> getNiveau03 () == $data -> getNiveau03 ()) {
2022-10-04 11:32:05 +02:00
$action .= " <a href=' " . $this -> generateUrl ( str_replace ( '_admin_' , '_manager_' , $this -> route ) . '_update' , [ 'id' => $data -> getId ()]) . " '><i class='fa fa-file fa-fw fa-2x'></i></a> " ;
2022-10-04 11:55:50 +02:00
} elseif ( ! $this -> getUser () -> getNiveau03 () && $this -> getUser () -> getNiveau02 () && $this -> getUser () -> getNiveau02 () == $data -> getNiveau02 ()) {
2022-10-04 11:32:05 +02:00
$action .= " <a href=' " . $this -> generateUrl ( str_replace ( '_admin_' , '_manager_' , $this -> route ) . '_update' , [ 'id' => $data -> getId ()]) . " '><i class='fa fa-file fa-fw fa-2x'></i></a> " ;
2022-10-04 11:55:50 +02:00
} elseif ( ! $this -> getUser () -> getNiveau02 () && $this -> getUser () -> getNiveau01 () && $this -> getUser () -> getNiveau01 () == $data -> getNiveau01 ()) {
2022-10-04 11:32:05 +02:00
$action .= " <a href=' " . $this -> generateUrl ( str_replace ( '_admin_' , '_manager_' , $this -> route ) . '_update' , [ 'id' => $data -> getId ()]) . " '><i class='fa fa-file fa-fw fa-2x'></i></a> " ;
}
break ;
2022-07-21 16:15:47 +02:00
}
// Groupes
2022-09-23 16:14:15 +02:00
$groups = '' ;
foreach ( $data -> getGroups () as $usergroup ) {
$groups .= $usergroup -> getGroup () -> getLabel () . '<br>' ;
2022-07-21 16:15:47 +02:00
}
// Roles
2022-09-23 16:14:15 +02:00
$roles = '' ;
foreach ( $data -> getRoles () as $role ) {
$roles .= $role . '<br>' ;
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
$tmp = [];
2022-10-04 11:32:05 +02:00
if ( 'admin' == $access || 'modo' == $access || 'manager' == $access ) {
2022-09-23 16:14:15 +02:00
array_push ( $tmp , $action );
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
array_push ( $tmp , " <img src=' " . $this -> generateUrl ( 'app_minio_image' , [ 'file' => 'avatar/' . $data -> getAvatar ()]) . " ' class='avatar'> " );
2022-08-24 14:40:10 +02:00
2022-09-23 16:14:15 +02:00
array_push ( $tmp , $data -> getUsername ());
array_push ( $tmp , $data -> getLastname ());
array_push ( $tmp , $data -> getFirstname ());
array_push ( $tmp , " <a href='mailto: " . $data -> getEmail () . " '> " . $data -> getEmail () . '</a>' );
array_push ( $tmp , $data -> getTelephonenumber ());
array_push ( $tmp , $data -> getNiveau01 () -> getLabel ());
array_push ( $tmp , $data -> getNiveau02 () ? $data -> getNiveau02 () -> getLabel () : '' );
2022-09-27 11:52:49 +02:00
array_push ( $tmp , $data -> getNiveau03 () ? $data -> getNiveau03 () -> getLabel () : '' );
array_push ( $tmp , $data -> getNiveau04 () ? $data -> getNiveau04 () -> getLabel () : '' );
2022-09-23 16:14:15 +02:00
array_push ( $tmp , $data -> getVisitedate () ? $data -> getVisitedate () -> format ( 'd/m/Y H:i' ) . '<br>nb = ' . $data -> getVisitecpt () : '' );
array_push ( $tmp , $roles );
array_push ( $tmp , $groups );
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
array_push ( $output [ 'data' ], $tmp );
2022-07-21 16:15:47 +02:00
}
// Retour
return new JsonResponse ( $output );
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function selectlist ( $access , Request $request , ManagerRegistry $em ) : Response
2022-07-21 16:15:47 +02:00
{
2022-09-23 16:14:15 +02:00
$output = [];
$page_limit = $request -> query -> get ( 'page_limit' );
$q = $request -> query -> get ( 'q' );
2022-07-21 16:15:47 +02:00
$qb = $em -> getManager () -> createQueryBuilder ();
2022-09-23 16:14:15 +02:00
$qb -> select ( 'entity' ) -> from ( $this -> entity , 'entity' )
2022-07-21 16:15:47 +02:00
-> where ( 'entity.username LIKE :value' )
2022-10-02 14:50:48 +02:00
-> orWhere ( 'entity.lastname LIKE :value' )
-> orWhere ( 'entity.firstname LIKE :value' )
2022-09-23 16:14:15 +02:00
-> setParameter ( 'value' , '%' . $q . '%' )
2022-07-21 16:15:47 +02:00
-> orderBy ( 'entity.username' );
2022-09-23 16:14:15 +02:00
$datas = $qb -> setFirstResult ( 0 ) -> setMaxResults ( $page_limit ) -> getQuery () -> getResult ();
foreach ( $datas as $data ) {
2022-10-02 14:50:48 +02:00
array_push ( $output , [ 'id' => $data -> getId (), 'text' => $data -> getFullname ()]);
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
$ret_string [ 'results' ] = $output ;
$response = new JsonResponse ( $ret_string );
2022-07-21 16:15:47 +02:00
return $response ;
}
2022-09-23 16:14:15 +02:00
public function submit ( $access , Request $request , ManagerRegistry $em ) : Response
2022-07-21 16:15:47 +02:00
{
// Vérifier que l'on puisse créer
2022-09-23 16:14:15 +02:00
if ( 'SQL' != $this -> getParameter ( 'appMasteridentity' ) && $this -> getParameter ( 'appSynchroPurgeUser' )) {
2022-07-21 16:15:47 +02:00
throw $this -> createNotFoundException ( 'Permission denied' );
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
// Controler les permissions
2022-09-23 16:14:15 +02:00
$this -> cansubmit ( $access , $em );
2022-07-21 16:15:47 +02:00
// Initialisation de l'enregistrement
$data = new Entity ();
2022-09-23 16:14:15 +02:00
$data -> setAvatar ( 'noavatar.png' );
2022-07-21 16:15:47 +02:00
$data -> setIsvisible ( true );
2022-10-02 14:50:48 +02:00
$data -> setIsactive ( true );
2022-07-21 16:15:47 +02:00
$data -> setApikey ( Uuid :: uuid4 ());
2022-10-04 11:32:05 +02:00
// If manager set same niveau to usertosubmit
2022-10-04 11:55:50 +02:00
if ( 'manager' == $access ) {
2022-10-04 11:32:05 +02:00
$data -> setNiveau01 ( $this -> getUser () -> getNiveau01 ());
$data -> setNiveau02 ( $this -> getUser () -> getNiveau02 ());
$data -> setNiveau03 ( $this -> getUser () -> getNiveau03 ());
2022-10-04 11:55:50 +02:00
$data -> setRoles ([ 'ROLE_USER' ]);
2022-10-04 11:32:05 +02:00
}
2022-07-21 16:15:47 +02:00
// Création du formulaire
2022-09-23 16:14:15 +02:00
$form = $this -> createForm ( Form :: class , $data , [
'mode' => 'submit' ,
'access' => $access ,
'userid' => $this -> getUser () -> getId (),
'appMasteridentity' => $this -> GetParameter ( 'appMasteridentity' ),
'appNiveau01label' => $this -> GetParameter ( 'appNiveau01label' ),
2022-09-27 11:52:49 +02:00
'appNiveau02use' => $this -> GetParameter ( 'appNiveau02use' ),
2022-09-23 16:14:15 +02:00
'appNiveau02label' => $this -> GetParameter ( 'appNiveau02label' ),
2022-09-27 11:52:49 +02:00
'appNiveau03use' => $this -> GetParameter ( 'appNiveau03use' ),
'appNiveau03label' => $this -> GetParameter ( 'appNiveau03label' ),
'appNiveau04use' => $this -> GetParameter ( 'appNiveau04use' ),
'appNiveau04label' => $this -> GetParameter ( 'appNiveau04label' ),
'appNiveauupdatable' => $this -> GetParameter ( 'appNiveauupdatable' ),
2022-10-04 11:32:05 +02:00
'appUserviewisvisible' => $this -> GetParameter ( 'appUserviewisvisible' ),
2022-09-23 16:14:15 +02:00
]);
2022-07-21 16:15:47 +02:00
// Récupération des data du formulaire
$form -> handleRequest ( $request );
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// Sur validation
2022-09-23 16:14:15 +02:00
if ( $form -> get ( 'submit' ) -> isClicked () && $form -> isValid ()) {
$data = $form -> getData ();
2022-07-21 16:15:47 +02:00
// S'assurer que les modos ne donne pas des ROLE_ADMIN ou ROLE_USER au user qu'il submit
2022-09-23 16:14:15 +02:00
if ( 'modo' == $access ) {
$roles = $data -> getRoles ();
$roles = array_diff ( $roles , [ 'ROLE_ADMIN' , 'ROLE_MODO' ]);
2022-07-21 16:15:47 +02:00
$data -> setRoles ( $roles );
}
// On récupère les groupes et on cacule ceux à ajouter ou à supprimer
2022-09-23 16:14:15 +02:00
$lstgroups = array_filter ( explode ( ',' , $form -> get ( 'linkgroups' ) -> getData ()));
$lstmodos = array_filter ( explode ( ',' , $form -> get ( 'linkmodos' ) -> getData ()));
2022-07-21 16:15:47 +02:00
// Sauvegarde
$em -> getManager () -> persist ( $data );
$em -> getManager () -> flush ();
// Ajout des groupes
2022-09-23 16:14:15 +02:00
foreach ( $lstgroups as $idgroup ) {
$group = $em -> getRepository ( " App \ Entity \ Group " ) -> find ( $idgroup );
$usergroup = $em -> getRepository ( 'App\Entity\UserGroup' ) -> findBy ([ 'user' => $data , 'group' => $group ]);
if ( ! $usergroup ) {
$usergroup = new UserGroup ();
2022-07-21 16:15:47 +02:00
$usergroup -> setUser ( $data );
$usergroup -> setGroup ( $group );
$usergroup -> setApikey ( Uuid :: uuid4 ());
$usergroup -> setRolegroup ( 0 );
$em -> getManager () -> persist ( $usergroup );
$em -> getManager () -> flush ();
}
}
// Ajout des modos
2022-09-23 16:14:15 +02:00
foreach ( $lstmodos as $idmodo ) {
$niveau01 = $em -> getRepository ( " App \ Entity \N iveau01 " ) -> find ( $idmodo );
$usermodo = $em -> getRepository ( 'App\Entity\UserModo' ) -> findBy ([ 'user' => $data , 'niveau01' => $niveau01 ]);
if ( ! $usermodo ) {
$usermodo = new UserModo ();
2022-07-21 16:15:47 +02:00
$usermodo -> setUser ( $data );
2022-09-23 14:53:47 +02:00
$usermodo -> setNiveau01 ( $niveau01 );
2022-07-21 16:15:47 +02:00
$em -> getManager () -> persist ( $usermodo );
$em -> getManager () -> flush ();
}
}
// Retour à la liste
2022-09-23 16:14:15 +02:00
return $this -> redirectToRoute ( str_replace ( '_admin_' , '_' . $access . '_' , $this -> route ));
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// Affichage du formulaire
return $this -> render ( $this -> twig . 'edit.html.twig' , [
2022-09-23 16:14:15 +02:00
'useheader' => true ,
'usemenu' => false ,
'usesidebar' => true ,
'access' => $access ,
'mode' => 'submit' ,
'form' => $form -> createView (),
$this -> data => $data ,
'listgroups' => $this -> getListGroups ( 'admin' , $em ),
'listmodos' => $this -> getListModos ( $em ),
2022-07-21 16:15:47 +02:00
]);
}
2022-09-23 16:14:15 +02:00
public function profil ( $access , Request $request , ManagerRegistry $em ) : Response
2022-07-21 16:15:47 +02:00
{
2022-09-23 16:14:15 +02:00
$id = $this -> getUser () -> getId ();
return $this -> update ( $access , $id , $request , $em );
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
public function update ( $access , $id , Request $request , ManagerRegistry $em ) : Response
2022-07-21 16:15:47 +02:00
{
// Initialisation de l'enregistrement
2022-09-23 16:14:15 +02:00
$data = $em -> getRepository ( $this -> entity ) -> find ( $id );
if ( ! $data ) {
throw $this -> createNotFoundException ( 'Unable to find entity.' );
}
2022-07-21 16:15:47 +02:00
// Controler les permissions
2022-09-23 16:14:15 +02:00
$this -> canupdate ( $access , $data , $em );
2022-07-21 16:15:47 +02:00
// Récupération de l'ancien password
2022-09-23 16:14:15 +02:00
$oldpassword = $data -> getPassword ();
2022-07-21 16:15:47 +02:00
// Récuparation des groupes associés
2022-09-23 16:14:15 +02:00
$oldlstgroups = [];
foreach ( $data -> getGroups () as $group ) {
2022-07-21 16:15:47 +02:00
$oldlstgroups [] = $group -> getGroup () -> getId ();
}
// Récuparation des modos associés
2022-09-23 16:14:15 +02:00
$oldlstmodos = [];
foreach ( $data -> getModos () as $modo ) {
2022-07-21 16:15:47 +02:00
$oldlstmodos [] = $modo -> getNiveau01 () -> getId ();
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
// Création du formulaire
2022-09-23 16:14:15 +02:00
$form = $this -> createForm ( Form :: class , $data , [
'mode' => 'update' ,
'access' => $access ,
'userid' => $this -> getUser () -> getId (),
'appMasteridentity' => $this -> GetParameter ( 'appMasteridentity' ),
'appNiveau01label' => $this -> GetParameter ( 'appNiveau01label' ),
2022-09-27 11:52:49 +02:00
'appNiveau02use' => $this -> GetParameter ( 'appNiveau02use' ),
2022-09-23 16:14:15 +02:00
'appNiveau02label' => $this -> GetParameter ( 'appNiveau02label' ),
2022-09-27 11:52:49 +02:00
'appNiveau03use' => $this -> GetParameter ( 'appNiveau03use' ),
'appNiveau03label' => $this -> GetParameter ( 'appNiveau03label' ),
'appNiveau04use' => $this -> GetParameter ( 'appNiveau04use' ),
'appNiveau04label' => $this -> GetParameter ( 'appNiveau04label' ),
'appNiveauupdatable' => $this -> GetParameter ( 'appNiveauupdatable' ),
2022-10-04 11:32:05 +02:00
'appUserviewisvisible' => $this -> GetParameter ( 'appUserviewisvisible' ),
2022-09-23 16:14:15 +02:00
]);
2022-07-21 16:15:47 +02:00
// Récupération des data du formulaire
$form -> handleRequest ( $request );
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// Sur validation
2022-09-23 16:14:15 +02:00
if ( $form -> get ( 'submit' ) -> isClicked () && $form -> isValid ()) {
$data = $form -> getData ();
2022-07-21 16:15:47 +02:00
// S'assurer que les modos ne donne pas des ROLE_ADMIN ou ROLE_USER au user qu'il update
2022-09-23 16:14:15 +02:00
if ( 'modo' == $access ) {
$roles = $data -> getRoles ();
$roles = array_diff ( $roles , [ 'ROLE_ADMIN' , 'ROLE_MODO' ]);
2022-07-21 16:15:47 +02:00
$data -> setRoles ( $roles );
}
// Si pas de changement de password on replace l'ancien
2022-09-23 16:14:15 +02:00
if ( '' == $data -> getPassword ()) {
$data -> setPassword ( $oldpassword );
2022-07-21 16:15:47 +02:00
}
// Sinon on encode le nouveau
else {
$data -> setPassword ( $data -> getPassword ());
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// Sauvegarde
$em -> getManager () -> flush ();
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// On récupère les groupes et on cacule ceux à ajouter ou à supprimer
2022-09-23 16:14:15 +02:00
$lstgroups = array_filter ( explode ( ',' , $form -> get ( 'linkgroups' ) -> getData ()));
$removegroups = array_diff ( $oldlstgroups , $lstgroups );
$addgroups = array_diff ( $lstgroups , $oldlstgroups );
2022-07-21 16:15:47 +02:00
// Ajout des nouveaux groupes
2022-09-23 16:14:15 +02:00
foreach ( $addgroups as $idgroup ) {
$group = $em -> getRepository ( " App \ Entity \ Group " ) -> find ( $idgroup );
$usergroup = $em -> getRepository ( 'App\Entity\UserGroup' ) -> findOneBy ([ 'user' => $data , 'group' => $group ]);
if ( ! $usergroup ) {
$usergroup = new UserGroup ();
2022-07-21 16:15:47 +02:00
$usergroup -> setUser ( $data );
$usergroup -> setGroup ( $group );
$usergroup -> setApikey ( Uuid :: uuid4 ());
$usergroup -> setRolegroup ( 0 );
$em -> getManager () -> persist ( $usergroup );
$em -> getManager () -> flush ();
}
}
// Suppression des groupes obsolètes
2022-09-23 16:14:15 +02:00
foreach ( $removegroups as $idgroup ) {
$group = $em -> getRepository ( " App \ Entity \ Group " ) -> find ( $idgroup );
$usergroup = $em -> getRepository ( 'App\Entity\UserGroup' ) -> findOneBy ([ 'user' => $data , 'group' => $group ]);
if ( $usergroup ) {
2022-07-21 16:15:47 +02:00
$em -> getManager () -> remove ( $usergroup );
$em -> getManager () -> flush ();
}
}
// On récupère les modos et on cacule ceux à ajouter ou à supprimer
2022-09-23 16:14:15 +02:00
$linkmodos = array_filter ( explode ( ',' , $form -> get ( 'linkmodos' ) -> getData ()));
$removemodos = array_diff ( $oldlstmodos , $linkmodos );
$addmodos = array_diff ( $linkmodos , $oldlstmodos );
2022-07-21 16:15:47 +02:00
// Ajout des nouveaux modos
2022-09-23 16:14:15 +02:00
foreach ( $addmodos as $idmodo ) {
$niveau01 = $em -> getRepository ( " App \ Entity \N iveau01 " ) -> find ( $idmodo );
$usermodo = $em -> getRepository ( 'App\Entity\UserModo' ) -> findOneBy ([ 'user' => $data , 'niveau01' => $niveau01 ]);
if ( ! $usermodo ) {
$usermodo = new UserModo ();
2022-07-21 16:15:47 +02:00
$usermodo -> setUser ( $data );
$usermodo -> setNiveau01 ( $niveau01 );
$em -> getManager () -> persist ( $usermodo );
$em -> getManager () -> flush ();
}
}
// Suppression des modos obsolètes
2022-09-23 16:14:15 +02:00
foreach ( $removemodos as $idmodo ) {
$niveau01 = $em -> getRepository ( " App \ Entity \N iveau01 " ) -> find ( $idmodo );
$usermodo = $em -> getRepository ( 'App\Entity\UserModo' ) -> findOneBy ([ 'user' => $data , 'niveau01' => $niveau01 ]);
if ( $usermodo ) {
2022-07-21 16:15:47 +02:00
$em -> getManager () -> remove ( $usermodo );
$em -> getManager () -> flush ();
}
}
// Retour à la liste
2022-10-02 14:50:48 +02:00
if ( 'all' == $access ) {
2022-09-23 16:14:15 +02:00
return $this -> redirectToRoute ( 'app_home' );
} else {
return $this -> redirectToRoute ( str_replace ( '_admin_' , '_' . $access . '_' , $this -> route ));
}
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
// Affichage du formulaire
return $this -> render ( $this -> twig . 'edit.html.twig' , [
2022-09-23 16:14:15 +02:00
'useheader' => true ,
'usemenu' => false ,
'usesidebar' => ( 'admin' == $access ),
'access' => $access ,
'mode' => 'update' ,
'form' => $form -> createView (),
$this -> data => $data ,
'listgroups' => $this -> getListGroups ( $access , $em ),
'listmodos' => $this -> getListModos ( $em ),
2022-10-02 14:50:48 +02:00
'maxsize' => ( 'all' == $access ? 1200 : null ),
2022-07-21 16:15:47 +02:00
]);
}
2022-09-23 16:14:15 +02:00
public function delete ( $access , $id , Request $request , ManagerRegistry $em ) : Response
2022-07-21 16:15:47 +02:00
{
// Récupération de l'enregistrement courant
2022-09-23 16:14:15 +02:00
$data = $em -> getRepository ( $this -> entity ) -> find ( $id );
if ( ! $data ) {
throw $this -> createNotFoundException ( 'Unable to find entity.' );
}
2022-07-21 16:15:47 +02:00
// Controler les permissions
2022-09-23 16:14:15 +02:00
$this -> candelete ( $access , $data , $em );
2022-07-21 16:15:47 +02:00
// Tentative de suppression
2022-09-23 16:14:15 +02:00
try {
2022-07-21 16:15:47 +02:00
$em -> getManager () -> remove ( $data );
$em -> getManager () -> flush ();
2022-09-23 16:14:15 +02:00
} catch ( \Exception $e ) {
$request -> getSession () -> getFlashBag () -> add ( 'error' , $e -> getMessage ());
return $this -> redirectToRoute ( str_replace ( '_admin_' , '_' . $access . '_' , $this -> route ) . '_update' , [ 'id' => $id ]);
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
return $this -> redirectToRoute ( str_replace ( '_admin_' , '_' . $access . '_' , $this -> route ));
}
protected function getListGroups ( $access , $em )
2022-07-21 16:15:47 +02:00
{
2022-09-23 16:14:15 +02:00
$qb = $em -> getManager () -> createQueryBuilder ();
$qb -> select ( 'b' ) -> from ( 'App:Group' , 'b' );
if ( 'admin' != $access ) {
$qb -> where ( 'b.isopen=true AND b.isworkgroup=true' );
}
$qb -> andWhere ( 'b.ldapfilter IS NULL' );
$qb -> andWhere ( 'b.attributes IS NULL' );
$qb -> andWhere ( 'b.id>0' );
$datas = $qb -> getQuery () -> getResult ();
2022-07-21 16:15:47 +02:00
return $datas ;
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
protected function getListModos ( $em )
{
2022-09-23 16:14:15 +02:00
$qb = $em -> getManager () -> createQueryBuilder ();
$qb -> select ( 'b' ) -> from ( 'App:Niveau01' , 'b' );
$datas = $qb -> getQuery () -> getResult ();
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
return $datas ;
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
private function cansubmit ( $access , $em )
{
switch ( $access ) {
case 'admin' : return true ;
break ;
case 'modo' : return true ;
break ;
2022-10-04 11:32:05 +02:00
case 'manager' : return true ;
break ;
2022-07-21 16:15:47 +02:00
}
throw $this -> createAccessDeniedException ( 'Permission denied' );
}
2022-09-23 16:14:15 +02:00
private function canupdate ( $access , $entity , $em )
{
switch ( $access ) {
case 'admin' : return true ;
break ;
2022-10-04 11:55:50 +02:00
2022-09-23 16:14:15 +02:00
case 'modo' :
$usermodo = $em -> getRepository ( " App \ Entity \ UserModo " ) -> findOneBy ([ 'user' => $this -> getUser (), 'niveau01' => $entity -> getNiveau01 ()]);
if ( ! $usermodo ) {
throw $this -> createAccessDeniedException ( 'Permission denied' );
}
2022-07-21 16:15:47 +02:00
return true ;
2022-09-23 16:14:15 +02:00
break ;
2022-10-04 11:32:05 +02:00
case 'manager' :
2022-10-04 11:55:50 +02:00
if ( $this -> getUser () -> getNiveau03 () && $this -> getUser () -> getNiveau03 () == $entity -> getNiveau03 ()) {
2022-10-04 11:32:05 +02:00
return true ;
2022-10-04 13:46:14 +02:00
} elseif ( ! $this -> getUser () -> getNiveau03 () && $this -> getUser () -> getNiveau02 () && $this -> getUser () -> getNiveau02 () == $entity -> getNiveau02 ()) {
2022-10-04 11:32:05 +02:00
return true ;
2022-10-04 13:46:14 +02:00
} elseif ( ! $this -> getUser () -> getNiveau02 () && $this -> getUser () -> getNiveau01 () && $this -> getUser () -> getNiveau01 () == $entity -> getNiveau01 ()) {
2022-10-04 11:32:05 +02:00
return true ;
}
2022-10-04 11:55:50 +02:00
2022-10-04 11:32:05 +02:00
throw $this -> createAccessDeniedException ( 'Permission denied' );
break ;
2022-10-02 14:50:48 +02:00
case 'all' :
2022-09-23 16:14:15 +02:00
if ( $this -> getUser () -> getId () != $entity -> getId ()) {
throw $this -> createAccessDeniedException ( 'Permission denied' );
}
2022-07-21 16:15:47 +02:00
return true ;
2022-09-23 16:14:15 +02:00
break ;
2022-07-21 16:15:47 +02:00
}
throw $this -> createAccessDeniedException ( 'Permission denied' );
2022-09-23 16:14:15 +02:00
}
private function candelete ( $access , $entity , $em )
{
switch ( $access ) {
case 'admin' : return true ;
break ;
case 'modo' :
$usermodo = $em -> getRepository ( " App \ Entity \ UserModo " ) -> findOneBy ([ 'user' => $this -> getUser (), 'niveau01' => $entity -> getNiveau01 ()]);
if ( ! $usermodo ) {
throw $this -> createAccessDeniedException ( 'Permission denied' );
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
if ( $entity -> hasRole ( 'ROLE_ADMIN' ) || $entity -> hasRole ( 'ROLE_MODO' )) {
throw $this -> createAccessDeniedException ( 'Permission denied' );
}
2022-07-21 16:15:47 +02:00
return true ;
2022-09-23 16:14:15 +02:00
break ;
2022-10-04 13:46:14 +02:00
case 'manager' :
if ( $this -> getUser () -> getNiveau03 () && $this -> getUser () -> getNiveau03 () == $entity -> getNiveau03 ()) {
return true ;
} elseif ( ! $this -> getUser () -> getNiveau03 () && $this -> getUser () -> getNiveau02 () && $this -> getUser () -> getNiveau02 () == $entity -> getNiveau02 ()) {
return true ;
} elseif ( ! $this -> getUser () -> getNiveau02 () && $this -> getUser () -> getNiveau01 () && $this -> getUser () -> getNiveau01 () == $entity -> getNiveau01 ()) {
return true ;
}
throw $this -> createAccessDeniedException ( 'Permission denied' );
break ;
2022-10-04 15:58:21 +02:00
2022-10-02 14:50:48 +02:00
case 'all' :
2022-09-23 16:14:15 +02:00
if ( $this -> getUser () -> getId () != $entity -> getId ()) {
throw $this -> createAccessDeniedException ( 'Permission denied' );
}
2022-07-21 16:15:47 +02:00
return true ;
2022-09-23 16:14:15 +02:00
break ;
2022-07-21 16:15:47 +02:00
}
throw $this -> createAccessDeniedException ( 'Permission denied' );
2022-09-23 16:14:15 +02:00
}
2022-07-21 16:15:47 +02:00
2022-09-23 16:14:15 +02:00
public function preference ( $access , Request $request , ManagerRegistry $em ) : Response
2022-07-21 16:15:47 +02:00
{
2022-09-23 16:14:15 +02:00
$key = $request -> request -> get ( 'key' );
$id = $request -> request -> get ( 'id' );
$value = $request -> request -> get ( 'value' );
2022-07-21 16:15:47 +02:00
// Récupérer les préférences de l'utilisateur
2022-09-23 16:14:15 +02:00
$preference = $this -> getUser () -> getPreference ();
2022-07-21 16:15:47 +02:00
// Mise à jour de la préférence
2022-09-23 16:14:15 +02:00
$toupdate = false ;
if ( ! is_array ( $preference )) {
$toupdate = true ;
$preference = [];
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
if ( ! array_key_exists ( $key , $preference )) {
$toupdate = true ;
$preference [ $key ] = [];
}
if ( ! array_key_exists ( $id , $preference [ $key ])) {
$toupdate = true ;
$preference [ $key ][ $id ] = $value ;
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
if ( $preference [ $key ][ $id ] != $value ) {
$toupdate = true ;
$preference [ $key ][ $id ] = $value ;
2022-07-21 16:15:47 +02:00
}
// Mise à jour des préferences
2022-09-23 16:14:15 +02:00
if ( $toupdate ) {
2022-07-21 16:15:47 +02:00
$this -> getUser () -> setPreference ( $preference );
2022-09-23 16:14:15 +02:00
$em -> getManager () -> flush ();
2022-07-21 16:15:47 +02:00
}
2022-09-23 16:14:15 +02:00
2022-07-21 16:15:47 +02:00
return new Response ();
}
2022-09-23 16:14:15 +02:00
}