From 5e7f1ba3c75196c3d4475cb2b3a7997b056e3a45 Mon Sep 17 00:00:00 2001 From: afornerot Date: Wed, 26 Feb 2020 12:03:57 +0100 Subject: [PATCH] export usergroup (ref #14) --- .../CoreBundle/Controller/GroupController.php | 113 ++++++++++++++++++ .../CoreBundle/Controller/UserController.php | 4 - .../CoreBundle/Resources/config/routing.yml | 8 +- .../Resources/views/Group/users.html.twig | 3 +- 4 files changed, 122 insertions(+), 6 deletions(-) diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/GroupController.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/GroupController.php index 9af8f737..1eb541dd 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/GroupController.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/GroupController.php @@ -11,6 +11,9 @@ use Symfony\Component\Form\FormError; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Cadoles\CoreBundle\Entity\Group; use Cadoles\CoreBundle\Entity\Usergroup; @@ -222,6 +225,7 @@ class GroupController extends Controller $em = $this->getDoctrine()->getManager(); $group=$this->getData($id); + $this->canManager($group,$access); $sub = $em->createQueryBuilder(); $sub->select("usergroup"); @@ -351,6 +355,7 @@ class GroupController extends Controller $em = $this->getDoctrine()->getManager(); $group=$this->getData($id); + $this->canManager($group,$access); $sub = $em->createQueryBuilder(); $sub->select("usergroup"); @@ -499,6 +504,7 @@ class GroupController extends Controller $groupid=$request->request->get('groupid'); $group=$em->getRepository("CadolesCoreBundle:Group")->find($groupid); + $this->canManager($group,$access); if($group->getFgAll()||$group->getFgtemplate()) throw $this->createNotFoundException('Permission denied'); @@ -563,6 +569,7 @@ class GroupController extends Controller $groupid=$request->request->get('groupid'); $group=$em->getRepository("CadolesCoreBundle:Group")->find($groupid); + $this->canManager($group,$access); if($group->getFgAll()||$group->getFgTemplate()) throw $this->createNotFoundException('Permission denied'); @@ -660,6 +667,9 @@ class GroupController extends Controller $userid=$request->request->get('user'); $groupid=$request->request->get('group'); + $group=$em->getRepository("CadolesCoreBundle:Group")->find($groupid); + $this->canManager($group,$access); + $data = $em->getRepository("CadolesCoreBundle:Usergroup")->findOneBy(array("user"=>$userid,"group"=>$groupid)); if($data) $data->setFgmanager(!$data->getFgmanager()); @@ -760,6 +770,7 @@ class GroupController extends Controller // Récupération de l'enregistrement courant $em = $this->getDoctrine()->getManager(); $data=$this->getData($id); + $this->canOwner($data,$access); // Création du formulaire $form = $this->createForm(GroupType::class,$data,array( @@ -837,6 +848,7 @@ class GroupController extends Controller // Récupération de l'enregistrement courant $data=$this->getData($id); + $this->canOwner($data,$access); // Vérifier que cet enregistrement est supprimable if($data->getFgAll()||$data->getFgTemplate()) @@ -917,6 +929,7 @@ class GroupController extends Controller { // Récupération de l'enregistrement courant $data=$this->getData($id); + $this->canManager($data,$access); // Affichage du formulaire return $this->render($this->labelentity.':users.html.twig', [ @@ -928,6 +941,106 @@ class GroupController extends Controller ]); } + public function usergroupexportAction($id,Request $request, $access="config") + { + // Récupération de l'enregistrement courant + $data=$this->getData($id); + $this->canManager($data,$access); + + $em = $this->getDoctrine()->getManager(); + $dir = $this->get('kernel')->getRootDir() . '/../uploads/export/'; + $file = "export-".$id.".csv"; + + $fs = new Filesystem(); + $fs->mkdir($dir); + + $csvh = fopen($dir.$file, 'w'); + $d = ';'; // this is the default but i like to be explicit + $e = '"'; // this is the default but i like to be explicit + + // Entête de colonne + $data=["id","Login","Nom","Prénom","Email","Téléphone",$this->getParameter("labelniveau01"),$this->getParameter("labelniveau02"),"Métier","Fonction","Nom Usage","Autres Prénom","Sexe","Adresse","Date Naissance","Pays Naissance","Ville Naissance"]; + fputcsv($csvh, $data, $d, $e); + + // Liste des utilisateurs en fonction du role de l'utilisateur en cours + $qb = $em->createQueryBuilder(); + if($this->isGranted('ROLE_ADMIN')||$access=="user") { + $users = $qb->select('user') + ->from('CadolesCoreBundle:User','user') + ->from('CadolesCoreBundle:UserGroup','usergroup') + ->where('usergroup.group=:group') + ->andWhere('usergroup.user=user') + ->setParameter("group", $id) + ->getQuery() + ->getResult(); + } + elseif($this->isGranted('ROLE_MODO')) { + $users = $qb->select('user') + ->from('CadolesCoreBundle:User','user') + ->from('CadolesCoreBundle:UserGroup','usergroup') + ->from('CadolesCoreBundle:Niveau01','niveau01') + ->from('CadolesCoreBundle:UserModo', 'usermodo') + ->where('usergroup.group=:group') + ->andWhere('usergroup.user=user') + ->andwhere('user.niveau01=niveau01.id') + ->andwhere("usermodo.niveau01 = user.niveau01") + ->andWhere("usermodo.user = :userid") + ->setParameter("group", $id) + ->setParameter("userid", $this->getUser()->getId()) + ->getQuery() + ->getResult(); + } + + foreach($users as $user) { + $data = [ + "id"=>$user->getId(), + "username"=>$user->getUsername(), + "name"=>$user->getLastname(), + "firstname"=>$user->getFirstname(), + "email"=>$user->getEmail(), + "phone"=>$user->getTelephonenumber(), + "niveau01"=>$user->getNiveau01()->getLabel(), + "niveau02"=>($user->getNiveau02()?$user->getNiveau02()->getLabel():""), + "job"=>$user->getJob(), + "position"=>$user->getPosition(), + "usualname"=>$user->getUsualname(), + "givensname"=>$user->getGivensname(), + "gender"=>$user->getGender(), + "postaladress"=>$user->getPostaladress(), + "birthdate"=>($user->getBirthdate()?$user->getBirthdate()->format("d/m/Y"):""), + "birthcountry"=>($user->getBirthcountry()?$user->getBirthcountry()->getLabel():""), + "birthplace"=>($user->getBirthplace()?$user->getBirthplace()->getLabel():""), + + ]; + + fputcsv($csvh, $data, $d, $e); + } + fclose($csvh); + + $response = new BinaryFileResponse($dir.$file); + $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT); + + return $response; + } + + protected function canManager($group,$access) { + if($access!="config") { + $em = $this->getDoctrine()->getManager(); + $ismanager=$em->getRepository("CadolesCoreBundle:Usergroup")->findOneBy(["group"=>$group,"user"=>$this->getUser(),"fgmanager"=>true]); + if(!$ismanager) + throw $this->createNotFoundException('Permission denied'); + } + } + + protected function canOwner($group,$access) { + if($access!="config") { + $em = $this->getDoctrine()->getManager(); + $isowner=($group->getOwner()==$this->getUser()); + if(!$isowner) + throw $this->createNotFoundException('Permission denied'); + } + } + protected function ctrlOwner($group) { $em = $this->getDoctrine()->getManager(); if($group->getOwner()) { diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php index 1f48c706..0203c579 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Controller/UserController.php @@ -804,10 +804,6 @@ class UserController extends Controller } foreach($users as $user) { - $groups=""; - foreach($user->getGroups() as $group) { - $groups.=$group->getGroup()->getLabel()." "; - } $data = [ "id"=>$user->getId(), "username"=>$user->getUsername(), diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml index cc071955..62ba36eb 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/config/routing.yml @@ -385,6 +385,10 @@ cadoles_core_config_group_ajax_switchmanager: path: /config/group/ajax/switchmanager defaults: { _controller: CadolesCoreBundle:Group:switchmanager, access: config } +cadoles_core_config_group_usergroup_export: + path: /config/group/users/export/{id} + defaults: { _controller: CadolesCoreBundle:Group:usergroupexport, access: config } + #-- Access user cadoles_core_user_group: path: /user/group @@ -434,7 +438,9 @@ cadoles_core_user_group_ajax_switchmanager: path: /user/group/ajax/switchmanager defaults: { _controller: CadolesCoreBundle:Group:switchmanager, access: user } - +cadoles_core_user_group_usergroup_export: + path: /user/group/users/export/{id} + defaults: { _controller: CadolesCoreBundle:Group:usergroupexport, access: user } #== Whitelist ============================================================================================================ diff --git a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/Group/users.html.twig b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/Group/users.html.twig index ecbf57f3..737f38a9 100644 --- a/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/Group/users.html.twig +++ b/src/ninegate-1.0/src/Cadoles/CoreBundle/Resources/views/Group/users.html.twig @@ -6,7 +6,8 @@ Fermer {% if moderegistration!="none" and masteridentity=="SQL"%} Inviter des personnes non listées ci-dessous - {%endif%} + {%endif%} + Exporter CSV