fix deprecations: use fqcn for submit in controllers

This commit is contained in:
nobohan 2018-04-03 17:14:52 +02:00
parent 24357ce3d6
commit 7922f8f181
4 changed files with 232 additions and 227 deletions

View File

@ -4,10 +4,12 @@ namespace Chill\MainBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Chill\MainBundle\Entity\Center;
use Chill\MainBundle\Form\CenterType;
/**
* Center controller.
*
@ -67,7 +69,7 @@ class CenterController extends Controller
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
$form->add('submit', SubmitType::class, array('label' => 'Create'));
return $form;
}
@ -141,7 +143,7 @@ class CenterController extends Controller
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
$form->add('submit', SubmitType::class, array('label' => 'Update'));
return $form;
}

View File

@ -3,7 +3,7 @@
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
@ -28,44 +28,44 @@ use Chill\MainBundle\Form\Type\Export\ExportType;
use Chill\MainBundle\Form\Type\Export\FormatterType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Chill\MainBundle\Form\Type\Export\PickCenterType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* ExportController is the controller use for exporting data.
*
*
*
*
*/
class ExportController extends Controller
{
/**
* Render the list of available exports
*
*
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function indexAction(Request $request)
{
$exportManager = $this->get('chill.main.export_manager');
$exports = $exportManager->getExports(true);
return $this->render('ChillMainBundle:Export:layout.html.twig', array(
'exports' => $exports
));
}
/**
* handle the step to build a query for an export
*
*
* This action has three steps :
*
*
* 1.'export', the export form. When the form is posted, the data is stored
* in the session (if valid), and then a redirection is done to next step.
* 2. 'formatter', the formatter form. When the form is posted, the data is
* stored in the session (if valid), and then a redirection is done to next step.
* 3. 'generate': gather data from session from the previous steps, and
* 3. 'generate': gather data from session from the previous steps, and
* make a redirection to the "generate" action with data in query (HTTP GET)
*
*
* @param string $request
* @param Request $alias
* @return \Symfony\Component\HttpFoundation\Response
@ -75,13 +75,13 @@ class ExportController extends Controller
// first check for ACL
$exportManager = $this->get('chill.main.export_manager');
$export = $exportManager->getExport($alias);
if ($exportManager->isGrantedForElement($export) === FALSE) {
throw $this->createAccessDeniedException('The user does not have access to this export');
}
$step = $request->query->getAlpha('step', 'centers');
switch ($step) {
case 'centers':
return $this->selectCentersStep($request, $alias);
@ -98,91 +98,91 @@ class ExportController extends Controller
throw $this->createNotFoundException("The given step '$step' is invalid");
}
}
public function selectCentersStep(Request $request, $alias)
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$form = $this->createCreateFormExport($alias, 'centers');
$export = $exportManager->getExport($alias);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('logger')->debug('form centers is valid', array(
'location' => __METHOD__));
$data = $form->getData();
// check ACL
if ($exportManager->isGrantedForElement($export, NULL,
$exportManager->getPickedCenters($data['centers'])) === FALSE) {
throw $this->createAccessDeniedException('you do not have '
. 'access to this export for those centers');
}
$this->get('session')->set('centers_step_raw',
$this->get('session')->set('centers_step_raw',
$request->request->all());
$this->get('session')->set('centers_step', $data);
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('centers'),
'alias' => $alias
));
}
}
return $this->render('ChillMainBundle:Export:new_centers_step.html.twig',
array(
'form' => $form->createView(),
'export' => $export
));
}
/**
* Render the export form
*
*
* When the method is POST, the form is stored if valid, and a redirection
* is done to next step.
*
*
* @param string $alias
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function exportFormStep(Request $request, $alias)
{
$exportManager = $this->get('chill.main.export_manager');
// check we have data from the previous step (export step)
$data = $this->get('session')->get('centers_step', null);
if ($data === null) {
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('export', true),
'alias' => $alias
));
}
$export = $exportManager->getExport($alias);
$form = $this->createCreateFormExport($alias, 'export', $data);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$this->get('logger')->debug('form export is valid', array(
'location' => __METHOD__));
// store data for reusing in next steps
$data = $form->getData();
$this->get('session')->set('export_step_raw',
$this->get('session')->set('export_step_raw',
$request->request->all());
$this->get('session')->set('export_step', $data);
//redirect to next step
return $this->redirect(
$this->generateUrl('chill_main_export_new', array(
@ -194,17 +194,17 @@ class ExportController extends Controller
'location' => __METHOD__));
}
}
return $this->render('ChillMainBundle:Export:new.html.twig', array(
'form' => $form->createView(),
'export_alias' => $alias,
'export' => $export
));
}
/**
* create a form to show on different steps.
*
* create a form to show on different steps.
*
* @param string $alias
* @param string $step, can either be 'export', 'formatter', 'generate_export' or 'generate_formatter' (last two are used by generate action)
* @param array $data the data from previous step. Required for steps 'formatter' and 'generate_formatter'
@ -215,26 +215,26 @@ class ExportController extends Controller
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$isGenerate = strpos($step, 'generate_') === 0;
$builder = $this->get('form.factory')
->createNamedBuilder(null, FormType::class, array(), array(
'method' => $isGenerate ? 'GET' : 'POST',
'csrf_protection' => $isGenerate ? false : true,
'csrf_protection' => $isGenerate ? false : true,
));
if ($step === 'centers' or $step === 'generate_centers') {
$builder->add('centers', PickCenterType::class, array(
'export_alias' => $alias
'export_alias' => $alias
));
}
if ($step === 'export' or $step === 'generate_export') {
$builder->add('export', ExportType::class, array(
'export_alias' => $alias,
'picked_centers' => $exportManager->getPickedCenters($data['centers'])
));
}
if ($step === 'formatter' or $step === 'generate_formatter') {
$builder->add('formatter', FormatterType::class, array(
'formatter_alias' => $exportManager
@ -244,19 +244,19 @@ class ExportController extends Controller
->getUsedAggregatorsAliases($data['export'])
));
}
$builder->add('submit', 'submit', array(
$builder->add('submit', SubmitType::class, array(
'label' => 'Generate'
));
return $builder->getForm();
}
/**
* get the next step. If $reverse === true, the previous step is returned.
*
*
* This method provides a centralized way of handling next/previous step.
*
*
* @param string $step the current step
* @param boolean $reverse set to true to get the previous step
* @return string the next/current step
@ -265,32 +265,32 @@ class ExportController extends Controller
private function getNextStep($step, $reverse = false)
{
switch($step) {
case 'centers':
case 'centers':
if ($reverse !== false) {
throw new \LogicException("there is no step before 'export'");
}
return 'export';
case 'export':
return $reverse ? 'centers' : 'formatter';
case 'formatter' :
case 'formatter' :
return $reverse ? 'export' : 'generate';
case 'generate' :
case 'generate' :
if ($reverse === false) {
throw new \LogicException("there is no step after 'generate'");
}
return 'formatter';
default:
throw new \LogicException("the step $step is not defined.");
}
}
/**
* Render the form for formatter.
*
* If the form is posted and valid, store the data in session and
* Render the form for formatter.
*
* If the form is posted and valid, store the data in session and
* redirect to the next step.
*
*
* @param Request $request
* @param string $alias
* @return \Symfony\Component\HttpFoundation\Response
@ -298,12 +298,12 @@ class ExportController extends Controller
protected function formatterFormStep(Request $request, $alias)
{
$export = $this->get('chill.main.export_manager')->getExport($alias);
// check we have data from the previous step (export step)
$data = $this->get('session')->get('export_step', null);
if ($data === null) {
return $this->redirectToRoute('chill_main_export_new', array(
'step' => $this->getNextStep('formatter', true),
'alias' => $alias
@ -311,39 +311,39 @@ class ExportController extends Controller
}
$form = $this->createCreateFormExport($alias, 'formatter', $data);
if ($request->getMethod() === 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$dataFormatter = $form->getData();
$this->get('session')->set('formatter_step', $dataFormatter);
$this->get('session')->set('formatter_step_raw',
$this->get('session')->set('formatter_step_raw',
$request->request->all());
//redirect to next step
return $this->redirect($this->generateUrl('chill_main_export_new',
return $this->redirect($this->generateUrl('chill_main_export_new',
array(
'alias' => $alias,
'alias' => $alias,
'step' => $this->getNextStep('formatter')
)));
}
}
return $this->render('ChillMainBundle:Export:new_formatter_step.html.twig',
array(
'form' => $form->createView(),
'export' => $export
));
}
/**
* Gather data stored in session from previous steps, and redirect
* to the `generate` action, compiling data from previous step in the URL
* (to obtain a GET HTTP query).
*
*
* The data from previous steps is removed from session.
*
*
* @param Request $request
* @param string $alias
* @return \Symfony\Component\HttpFoundation\RedirectResponse
@ -353,19 +353,19 @@ class ExportController extends Controller
$dataCenters = $this->get('session')->get('centers_step_raw', null);
$dataFormatter = $this->get('session')->get('formatter_step_raw', null);
$dataExport = $this->get('session')->get('export_step_raw', null);
if ($dataFormatter === NULL) {
return $this->redirectToRoute('chill_main_export_new', array(
'alias' => $alias, 'step' => $this->getNextStep('generate', true)
));
}
// remove data from session
$this->get('session')->remove('export_step_raw');
$this->get('session')->remove('export_step');
$this->get('session')->remove('formatter_step_raw');
$this->get('session')->remove('formatter_step');
$redirectParameters = array_merge(
$dataFormatter,
$dataExport,
@ -373,16 +373,16 @@ class ExportController extends Controller
array('alias' => $alias)
);
unset($redirectParameters['_token']);
return $this->redirectToRoute('chill_main_export_download',
return $this->redirectToRoute('chill_main_export_download',
$redirectParameters);
}
/**
* Generate a report.
*
*
* This action must work with GET queries.
*
*
* @param Request $request
* @param string $alias
* @return \Symfony\Component\HttpFoundation\Response
@ -391,26 +391,26 @@ class ExportController extends Controller
{
/* @var $exportManager \Chill\MainBundle\Export\ExportManager */
$exportManager = $this->get('chill.main.export_manager');
$formCenters = $this->createCreateFormExport($alias, 'generate_centers');
$formCenters->handleRequest($request);
$dataCenters = $formCenters->getData();
$formExport = $this->createCreateFormExport($alias, 'generate_export', $dataCenters);
$formExport->handleRequest($request);
$dataExport = $formExport->getData();
$formFormatter = $this->createCreateFormExport($alias, 'generate_formatter',
$formFormatter = $this->createCreateFormExport($alias, 'generate_formatter',
$dataExport);
$formFormatter->handleRequest($request);
$dataFormatter = $formFormatter->getData();
$r = $exportManager->generate($alias, $dataCenters['centers'],
$dataExport['export'], $dataFormatter['formatter']);
return $r;
}
public function downloadResultAction(Request $request, $alias)
{
return $this->render("ChillMainBundle:Export:download.html.twig", [

View File

@ -4,6 +4,7 @@ namespace Chill\MainBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Chill\MainBundle\Entity\RoleScope;
use Chill\MainBundle\Entity\PermissionsGroup;
use Chill\MainBundle\Form\PermissionsGroupType;
@ -32,7 +33,7 @@ class PermissionsGroupController extends Controller
'entities' => $entities,
));
}
/**
* Creates a new PermissionsGroup entity.
*
@ -48,7 +49,7 @@ class PermissionsGroupController extends Controller
$em->persist($permissionsGroup);
$em->flush();
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
array('id' => $permissionsGroup->getId())));
}
@ -72,7 +73,7 @@ class PermissionsGroupController extends Controller
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
$form->add('submit', SubmitType::class, array('label' => 'Create'));
return $form;
}
@ -105,26 +106,26 @@ class PermissionsGroupController extends Controller
if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
}
$translatableStringHelper = $this->get('chill.main.helper.translatable_string');
$roleScopes = $permissionsGroup->getRoleScopes()->toArray();
// sort $roleScopes by name
usort($roleScopes,
function(RoleScope $a, RoleScope $b) use ($translatableStringHelper) {
if ($a->getScope() === NULL) {
return 1;
}
}
if ($b->getScope() === NULL) {
return +1;
}
return strcmp(
$translatableStringHelper->localize($a->getScope()->getName()),
$translatableStringHelper->localize($b->getScope()->getName())
);
});
// sort role scope by title
/* @var $roleProvider \Chill\MainBundle\Security\RoleProvider */
$roleProvider = $this->get('chill.main.role_provider');
@ -135,17 +136,17 @@ class PermissionsGroupController extends Controller
$roleScopesSorted[$title][] = $roleScope;
}
ksort($roleScopesSorted);
return $this->render('ChillMainBundle:PermissionsGroup:show.html.twig', array(
'entity' => $permissionsGroup,
'role_scopes_sorted' => $roleScopesSorted,
'expanded_roles' => $this->getExpandedRoles($roleScopes)
));
}
/**
* expand roleScopes to be easily shown in template
*
*
* @param array $roleScopes
* @return array
*/
@ -154,10 +155,10 @@ class PermissionsGroupController extends Controller
$expandedRoles = array();
foreach ($roleScopes as $roleScope) {
if (!array_key_exists($roleScope->getRole(), $expandedRoles)) {
$expandedRoles[$roleScope->getRole()] =
$expandedRoles[$roleScope->getRole()] =
array_map(
function(RoleInterface $role) {
return $role->getRole();
},
$this->get('security.role_hierarchy')
@ -186,15 +187,15 @@ class PermissionsGroupController extends Controller
// create all the forms
$editForm = $this->createEditForm($permissionsGroup);
$deleteRoleScopesForm = array();
foreach ($permissionsGroup->getRoleScopes() as $roleScope) {
$deleteRoleScopesForm[$roleScope->getId()] = $this->createDeleteRoleScopeForm(
$permissionsGroup, $roleScope);
}
$addRoleScopesForm = $this->createAddRoleScopeForm($permissionsGroup);
// sort role scope by title
/* @var $roleProvider \Chill\MainBundle\Security\RoleProvider */
$roleProvider = $this->get('chill.main.role_provider');
@ -211,9 +212,9 @@ class PermissionsGroupController extends Controller
'role_scopes_sorted' => $roleScopesSorted,
'edit_form' => $editForm->createView(),
'expanded_roles' => $this->getExpandedRoles($permissionsGroup->getRoleScopes()->toArray()),
'delete_role_scopes_form' => array_map( function($form) {
return $form->createView();
'delete_role_scopes_form' => array_map( function($form) {
return $form->createView();
}, $deleteRoleScopesForm),
'add_role_scopes_form' => $addRoleScopesForm->createView()
));
@ -233,11 +234,11 @@ class PermissionsGroupController extends Controller
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
$form->add('submit', SubmitType::class, array('label' => 'Update'));
return $form;
}
/**
* Edits an existing PermissionsGroup entity.
*
@ -259,20 +260,20 @@ class PermissionsGroupController extends Controller
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->flush();
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit', array('id' => $id)));
}
$deleteRoleScopesForm = array();
foreach ($permissionsGroup->getRoleScopes() as $roleScope) {
$deleteRoleScopesForm[$roleScope->getId()] = $this->createDeleteRoleScopeForm(
$permissionsGroup, $roleScope);
}
$addRoleScopesForm = $this->createAddRoleScopeForm($permissionsGroup);
// sort role scope by title
/* @var $roleProvider \Chill\MainBundle\Security\RoleProvider */
$roleProvider = $this->get('chill.main.role_provider');
@ -283,100 +284,100 @@ class PermissionsGroupController extends Controller
$roleScopesSorted[$title][] = $roleScope;
}
ksort($roleScopesSorted);
return $this->render('ChillMainBundle:PermissionsGroup:edit.html.twig', array(
'entity' => $permissionsGroup,
'role_scopes_sorted' => $roleScopesSorted,
'edit_form' => $editForm->createView(),
'expanded_roles' => $this->getExpandedRoles($permissionsGroup->getRoleScopes()->toArray()),
'delete_role_scopes_form' => array_map( function($form) {
return $form->createView();
'delete_role_scopes_form' => array_map( function($form) {
return $form->createView();
}, $deleteRoleScopesForm),
'add_role_scopes_form' => $addRoleScopesForm->createView()
));
}
/**
* get a role scope by his parameters. The role scope is persisted if it
* get a role scope by his parameters. The role scope is persisted if it
* doesn't exists in database.
*
*
* @param Scope $scope
* @param string $role
* @return RoleScope
*/
protected function getPersistentRoleScopeBy($role, Scope $scope = null)
protected function getPersistentRoleScopeBy($role, Scope $scope = null)
{
$em = $this->getDoctrine()->getManager();
$roleScope = $em->getRepository('ChillMainBundle:RoleScope')
->findOneBy(array('role' => $role, 'scope' => $scope));
if ($roleScope === NULL) {
$roleScope = (new RoleScope())
->setRole($role)
->setScope($scope)
;
$em->persist($roleScope);
}
return $roleScope;
}
/**
* remove an association between permissionsGroup and roleScope
*
*
* @param int $pgid permissionsGroup id
* @param int $rsid roleScope id
* @return redirection to edit form
*/
public function deleteLinkRoleScopeAction($pgid, $rsid)
public function deleteLinkRoleScopeAction($pgid, $rsid)
{
$em = $this->getDoctrine()->getManager();
$permissionsGroup = $em->getRepository('ChillMainBundle:PermissionsGroup')->find($pgid);
$roleScope = $em->getRepository('ChillMainBundle:RoleScope')->find($rsid);
if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
}
if (!$roleScope) {
throw $this->createNotFoundException('Unable to find RoleScope entity');
}
try {
$permissionsGroup->removeRoleScope($roleScope);
} catch (\RuntimeException $ex) {
$this->addFlash('notice',
$this->addFlash('notice',
$this->get('translator')->trans("The role '%role%' and circle "
. "'%scope%' is not associated with this permission group", array(
'%role%' => $this->get('translator')->trans($roleScope->getRole()),
'%scope%' => $this->get('chill.main.helper.translatable_string')
->localize($roleScope->getScope()->getName())
)));
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
array('id' => $pgid)));
}
$em->flush();
$this->addFlash('notice',
$this->addFlash('notice',
$this->get('translator')->trans("The role '%role%' on circle "
. "'%scope%' has been removed", array(
'%role%' => $this->get('translator')->trans($roleScope->getRole()),
'%scope%' => $this->get('chill.main.helper.translatable_string')
->localize($roleScope->getScope()->getName())
)));
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
array('id' => $pgid)));
}
/**
*
*
* @param Request $request
* @param int $id
* @return Respon
@ -391,26 +392,26 @@ class PermissionsGroupController extends Controller
if (!$permissionsGroup) {
throw $this->createNotFoundException('Unable to find PermissionsGroup entity.');
}
$form = $this->createAddRoleScopeForm($permissionsGroup);
$form->handleRequest($request);
if ($form->isValid()) {
$roleScope = $this->getPersistentRoleScopeBy(
$form['composed_role_scope']->getData()->getRole(),
$form['composed_role_scope']->getData()->getScope()
);
$permissionsGroup->addRoleScope($roleScope);
$violations = $this->get('validator')->validate($permissionsGroup);
if ($violations->count() === 0) {
$em->flush();
$this->addFlash('notice',
$this->addFlash('notice',
$this->get('translator')->trans("The permissions have been added"));
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
return $this->redirect($this->generateUrl('admin_permissionsgroup_edit',
array('id' => $id)));
} else {
foreach($violations as $error) {
@ -423,17 +424,17 @@ class PermissionsGroupController extends Controller
$this->addFlash('error', $error->getMessage());
}
}
$editForm = $this->createEditForm($permissionsGroup);
$deleteRoleScopesForm = array();
foreach ($permissionsGroup->getRoleScopes() as $roleScope) {
$deleteRoleScopesForm[$roleScope->getId()] = $this->createDeleteRoleScopeForm(
$permissionsGroup, $roleScope);
}
$addRoleScopesForm = $this->createAddRoleScopeForm($permissionsGroup);
// sort role scope by title
/* @var $roleProvider \Chill\MainBundle\Security\RoleProvider */
$roleProvider = $this->get('chill.main.role_provider');
@ -450,15 +451,15 @@ class PermissionsGroupController extends Controller
'edit_form' => $editForm->createView(),
'role_scopes_sorted' => $roleScopesSorted,
'expanded_roles' => $this->getExpandedRoles($permissionsGroup->getRoleScopes()->toArray()),
'delete_role_scopes_form' => array_map( function($form) {
return $form->createView();
'delete_role_scopes_form' => array_map( function($form) {
return $form->createView();
}, $deleteRoleScopesForm),
'add_role_scopes_form' => $addRoleScopesForm->createView()
));
}
/**
* Creates a form to delete a link to roleScope.
*
@ -470,17 +471,17 @@ class PermissionsGroupController extends Controller
RoleScope $roleScope)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('admin_permissionsgroup_delete_role_scope',
->setAction($this->generateUrl('admin_permissionsgroup_delete_role_scope',
array('pgid' => $permissionsGroup->getId(), 'rsid' => $roleScope->getId())))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->add('submit', SubmitType::class, array('label' => 'Delete'))
->getForm()
;
}
/**
* creates a form to add a role scope to permissionsgroup
*
*
* @param PermissionsGroup $permissionsGroup
* @return \Symfony\Component\Form\Form The form
*/
@ -491,10 +492,10 @@ class PermissionsGroupController extends Controller
array('id' => $permissionsGroup->getId())))
->setMethod('PUT')
->add('composed_role_scope', 'composed_role_scope')
->add('submit', 'submit', array('label' => 'Add permission'))
->add('submit', SubmitType::class, array('label' => 'Add permission'))
->getForm()
;
}
}

View File

@ -4,6 +4,7 @@ namespace Chill\MainBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\UserType;
@ -11,13 +12,14 @@ use Chill\MainBundle\Entity\GroupCenter;
use Chill\MainBundle\Form\Type\ComposedGroupCenterType;
use Chill\MainBundle\Form\UserPasswordType;
/**
* User controller.
*
*/
class UserController extends Controller
{
const FORM_GROUP_CENTER_COMPOSED = 'composed_groupcenter';
/**
@ -46,10 +48,10 @@ class UserController extends Controller
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$user->setPassword($this->get('security.password_encoder')
->encodePassword($user, $form['plainPassword']['password']->getData()));
$em->persist($user);
$em->flush();
@ -77,7 +79,7 @@ class UserController extends Controller
'is_creation' => true
));
$form->add('submit', 'submit', array('label' => 'Create'));
$form->add('submit', SubmitType::class, array('label' => 'Create'));
return $form;
}
@ -136,15 +138,15 @@ class UserController extends Controller
'entity' => $user,
'edit_form' => $editForm->createView(),
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user)->createView(),
'delete_groupcenter_form' => array_map(
'delete_groupcenter_form' => array_map(
function(\Symfony\Component\Form\Form $form) {
return $form->createView();
},
iterator_to_array($this->getDeleteLinkGroupCenterByUser($user), true))
));
}
/**
* Displays a form to edit the user password.
*
@ -166,25 +168,25 @@ class UserController extends Controller
'edit_form' => $editForm->createView()
));
}
/**
*
*
*
*
* @param User $user
* @return \Symfony\Component\Form\Form
*/
private function createEditPasswordForm(User $user)
{
return $this->createForm(new UserPasswordType(), $user, array(
'action' =>
'action' =>
$this->generateUrl('admin_user_update_password', array('id' => $user->getId())),
'method' => 'PUT'
))
->add('submit', 'submit', array('label' => 'Change password'))
->add('submit', SubmitType::class, array('label' => 'Change password'))
;
}
public function deleteLinkGroupCenterAction($uid, $gcid)
public function deleteLinkGroupCenterAction($uid, $gcid)
{
$em = $this->getDoctrine()->getManager();
@ -193,32 +195,32 @@ class UserController extends Controller
if (!$user) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$groupCenter = $em->getRepository('ChillMainBundle:GroupCenter')
->find($gcid);
if (!$groupCenter) {
throw $this->createNotFoundException('Unable to find groupCenter entity');
}
try {
$user->removeGroupCenter($groupCenter);
} catch (\RuntimeException $ex) {
$this->addFlash('error', $this->get('translator')->trans($ex-getMessage()));
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $uid)));
}
$em->flush();
$this->addFlash('success', $this->get('translator')
->trans('The permissions where removed.'));
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $uid)));
}
public function addLinkGroupCenterAction(Request $request, $uid)
public function addLinkGroupCenterAction(Request $request, $uid)
{
$em = $this->getDoctrine()->getManager();
@ -227,21 +229,21 @@ class UserController extends Controller
if (!$user) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$form = $this->createAddLinkGroupCenterForm($user);
$form->handleRequest($request);
if ($form->isValid()) {
$groupCenter = $this->getPersistedGroupCenter(
$form[self::FORM_GROUP_CENTER_COMPOSED]->getData());
$user->addGroupCenter($groupCenter);
if ($this->get('validator')->validate($user)->count() === 0) {
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('The '
. 'permissions have been successfully added to the user'));
return $this->redirect($this->generateUrl('admin_user_edit',
array('id' => $uid)));
} else {
@ -249,35 +251,35 @@ class UserController extends Controller
$this->addFlash('error', $error->getMessage());
}
}
return $this->render('ChillMainBundle:User:edit.html.twig', array(
'entity' => $user,
'edit_form' => $this->createEditForm($user)->createView(),
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user)->createView(),
'delete_groupcenter_form' => array_map(
'delete_groupcenter_form' => array_map(
function(\Symfony\Component\Form\Form $form) {
return $form->createView();
},
iterator_to_array($this->getDeleteLinkGroupCenterByUser($user), true))
));
}
private function getPersistedGroupCenter(GroupCenter $groupCenter)
{
$em = $this->getDoctrine()->getManager();
$groupCenterManaged = $em->getRepository('ChillMainBundle:GroupCenter')
->findOneBy(array(
'center' => $groupCenter->getCenter(),
'permissionsGroup' => $groupCenter->getPermissionsGroup()
));
if (!$groupCenterManaged) {
$em->persist($groupCenter);
return $groupCenter;
}
return $groupCenterManaged;
}
@ -295,11 +297,11 @@ class UserController extends Controller
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
$form->add('submit', SubmitType::class, array('label' => 'Update'));
return $form;
}
/**
* Edits an existing User entity.
*
@ -327,15 +329,15 @@ class UserController extends Controller
'entity' => $user,
'edit_form' => $editForm->createView(),
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user)->createView(),
'delete_groupcenter_form' => array_map(
'delete_groupcenter_form' => array_map(
function(\Symfony\Component\Form\Form $form) {
return $form->createView();
},
iterator_to_array($this->getDeleteLinkGroupCenterByUser($user), true))
));
}
/**
* Edits the user password
*
@ -355,20 +357,20 @@ class UserController extends Controller
if ($editForm->isValid()) {
$password = $editForm->getData()->getPassword();
// logging for debug !! WARNING print the new password !!
$this->get('logger')->debug('update password for an user',
array('method' => __METHOD__, 'password' => $password,
$this->get('logger')->debug('update password for an user',
array('method' => __METHOD__, 'password' => $password,
'user' => $user->getUsername()));
// logging for prod
$this->get('logger')->info('update password for an user',
$this->get('logger')->info('update password for an user',
array('method' => __METHOD__, 'user' => $user->getUsername()));
$user->setPassword($this->get('security.password_encoder')
->encodePassword($user, $password));
$em->flush();
$this->addFlash('success', $this->get('translator')->trans('Password successfully updated!'));
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $id)));
@ -380,7 +382,7 @@ class UserController extends Controller
));
}
/**
* Creates a form to delete a link to a GroupCenter
*
@ -391,34 +393,34 @@ class UserController extends Controller
private function createDeleteLinkGroupCenterForm(User $user, GroupCenter $groupCenter)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('admin_user_delete_group_center',
->setAction($this->generateUrl('admin_user_delete_group_center',
array('uid' => $user->getId(), 'gcid' => $groupCenter->getId())))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->add('submit', SubmitType::class, array('label' => 'Delete'))
->getForm()
;
}
/**
* create a form to add a link to a groupcenter
*
*
* @param User $user
* @return \Symfony\Component\Form\Form
*/
private function createAddLinkGroupCenterForm(User $user)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('admin_user_add_group_center',
->setAction($this->generateUrl('admin_user_add_group_center',
array('uid' => $user->getId())))
->setMethod('POST')
->add(self::FORM_GROUP_CENTER_COMPOSED, new ComposedGroupCenterType())
->add('submit', 'submit', array('label' => 'Add a new groupCenter'))
->add('submit', SubmitType::class, array('label' => 'Add a new groupCenter'))
->getForm()
;
}
/**
*
*
* @param User $user
*/
private function getDeleteLinkGroupCenterByUser(User $user)