mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
338 lines
12 KiB
PHP
338 lines
12 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\MainBundle\Controller;
|
|
|
|
use Chill\MainBundle\CRUD\Controller\AbstractCRUDController;
|
|
use Chill\MainBundle\CRUD\Controller\CRUDController;
|
|
use Chill\MainBundle\Pagination\PaginatorInterface;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\Form\Form;
|
|
use Symfony\Component\Form\FormInterface;
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\MainBundle\Form\UserType;
|
|
use Chill\MainBundle\Entity\GroupCenter;
|
|
use Chill\MainBundle\Form\Type\ComposedGroupCenterType;
|
|
use Chill\MainBundle\Form\UserCurrentLocationType;
|
|
use Chill\MainBundle\Form\UserPasswordType;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
|
|
|
|
|
|
class UserController extends CRUDController
|
|
{
|
|
const FORM_GROUP_CENTER_COMPOSED = 'composed_groupcenter';
|
|
|
|
private LoggerInterface $logger;
|
|
|
|
private ValidatorInterface $validator;
|
|
|
|
private UserPasswordEncoderInterface $passwordEncoder;
|
|
|
|
public function __construct(
|
|
LoggerInterface $chillLogger,
|
|
ValidatorInterface $validator,
|
|
UserPasswordEncoderInterface $passwordEncoder
|
|
) {
|
|
$this->logger = $chillLogger;
|
|
$this->validator = $validator;
|
|
$this->passwordEncoder = $passwordEncoder;
|
|
}
|
|
|
|
protected function createFormFor(string $action, $entity, string $formClass = null, array $formOptions = []): FormInterface
|
|
{
|
|
// for "new", add special config
|
|
if ('new' === $action) {
|
|
return $this->createForm(UserType::class, $entity, array(
|
|
'is_creation' => true
|
|
));
|
|
}
|
|
|
|
// default behaviour
|
|
return parent::createFormFor($action, $entity, $formClass, $formOptions);
|
|
}
|
|
|
|
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
|
|
{
|
|
// for "new", encode the password
|
|
if ('new' === $action) {
|
|
$entity->setPassword($this->passwordEncoder
|
|
->encodePassword($entity, $form['plainPassword']->getData()));
|
|
}
|
|
|
|
// default behaviour
|
|
parent::onPrePersist($action, $entity, $form, $request);
|
|
}
|
|
|
|
protected function orderQuery(string $action, $query, Request $request, PaginatorInterface $paginator)
|
|
{
|
|
$query->addOrderBy('e.usernameCanonical', 'ASC');
|
|
|
|
return parent::orderQuery($action, $query, $request, $paginator);
|
|
}
|
|
|
|
protected function generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = [])
|
|
{
|
|
// add mini-forms for edit action
|
|
if ("edit" === $action) {
|
|
return array_merge(
|
|
$defaultTemplateParameters,
|
|
[
|
|
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($entity, $request)->createView(),
|
|
'delete_groupcenter_form' => array_map(
|
|
function (\Symfony\Component\Form\Form $form) {
|
|
return $form->createView();
|
|
},
|
|
iterator_to_array($this->getDeleteLinkGroupCenterByUser($entity, $request), true)
|
|
)
|
|
]
|
|
);
|
|
}
|
|
|
|
// default behaviour
|
|
return parent::generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters);
|
|
}
|
|
|
|
/**
|
|
* Displays a form to edit the user password.
|
|
*
|
|
* @Route("/{_locale}/admin/user/{id}/edit_password", name="admin_user_edit_password")
|
|
*/
|
|
public function editPasswordAction(User $user, Request $request)
|
|
{
|
|
$editForm = $this->createEditPasswordForm($user);
|
|
$editForm->handleRequest($request);
|
|
|
|
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
|
$password = $editForm->get('new_password')->getData();
|
|
|
|
// logging for prod
|
|
$this->logger->info('update password for an user', [
|
|
'by' => $this->getUser()->getUsername(),
|
|
'user' => $user->getUsername()
|
|
]);
|
|
|
|
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
|
|
|
|
$this->getDoctrine()->getManager()->flush();
|
|
$this->addFlash('success', $this->get('translator')->trans('Password successfully updated!'));
|
|
|
|
return $this->redirect(
|
|
$request->query->has('returnPath') ? $request->query->get('returnPath') :
|
|
$this->generateUrl('chill_crud_admin_user_edit', ['id' => $user->getId()])
|
|
);
|
|
}
|
|
|
|
return $this->render('@ChillMain/User/edit_password.html.twig', [
|
|
'entity' => $user,
|
|
'edit_form' => $editForm->createView()
|
|
]);
|
|
}
|
|
|
|
private function createEditPasswordForm(User $user): FormInterface
|
|
{
|
|
return $this->createForm(
|
|
UserPasswordType::class,
|
|
null,
|
|
[
|
|
'user' => $user
|
|
]
|
|
)
|
|
->add('submit', SubmitType::class, array('label' => 'Change password'))
|
|
->remove('actual_password');
|
|
}
|
|
|
|
/**
|
|
* @Route("/{_locale}/admin/main/user/{uid}/delete_link_groupcenter/{gcid}",
|
|
* name="admin_user_delete_groupcenter")
|
|
*/
|
|
public function deleteLinkGroupCenterAction($uid, $gcid, Request $request): RedirectResponse
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$user = $em->getRepository('ChillMainBundle:User')->find($uid);
|
|
|
|
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('chill_crud_admin_user_edit', array('id' => $uid)));
|
|
}
|
|
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', $this->get('translator')
|
|
->trans('The permissions where removed.'));
|
|
|
|
return $this->redirect($this->generateUrl('chill_crud_admin_user_edit', array('id' => $uid)));
|
|
}
|
|
|
|
/**
|
|
* @Route("/{_locale}/admin/main/user/{uid}/add_link_groupcenter",
|
|
* name="admin_user_add_groupcenter")
|
|
*/
|
|
public function addLinkGroupCenterAction(Request $request, $uid): Response
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$user = $em->getRepository('ChillMainBundle:User')->find($uid);
|
|
|
|
if (!$user) {
|
|
throw $this->createNotFoundException('Unable to find User entity.');
|
|
}
|
|
|
|
$form = $this->createAddLinkGroupCenterForm($user, $request);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isValid()) {
|
|
$groupCenter = $this->getPersistedGroupCenter(
|
|
$form[self::FORM_GROUP_CENTER_COMPOSED]->getData()
|
|
);
|
|
$user->addGroupCenter($groupCenter);
|
|
|
|
if ($this->validator->validate($user)->count() === 0) {
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', $this->get('translator')->trans('The '
|
|
. 'permissions have been successfully added to the user'));
|
|
|
|
$returnPathParams = $request->query->has('returnPath') ?
|
|
['returnPath' => $request->query->get('returnPath')] : [];
|
|
|
|
return $this->redirect($this->generateUrl(
|
|
'chill_crud_admin_user_edit',
|
|
\array_merge(['id' => $uid], $returnPathParams)
|
|
));
|
|
}
|
|
|
|
foreach ($this->validator->validate($user) as $error) {
|
|
$this->addFlash('error', $error->getMessage());
|
|
}
|
|
}
|
|
|
|
return $this->render('@ChillMain/User/edit.html.twig', [
|
|
'entity' => $user,
|
|
'edit_form' => $this->createEditForm($user)->createView(),
|
|
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user, $request)->createView(),
|
|
'delete_groupcenter_form' => array_map(
|
|
static fn (Form $form) => $form->createView(),
|
|
iterator_to_array($this->getDeleteLinkGroupCenterByUser($user, $request), 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;
|
|
}
|
|
|
|
/**
|
|
* Creates a form to delete a link to a GroupCenter
|
|
*
|
|
* @param mixed $permissionsGroup The entity id
|
|
*/
|
|
private function createDeleteLinkGroupCenterForm(User $user, GroupCenter $groupCenter, $request): FormInterface
|
|
{
|
|
$returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : [];
|
|
|
|
return $this->createFormBuilder()
|
|
->setAction($this->generateUrl(
|
|
'admin_user_delete_groupcenter',
|
|
array_merge($returnPathParams, ['uid' => $user->getId(), 'gcid' => $groupCenter->getId()])
|
|
))
|
|
->setMethod('DELETE')
|
|
->add('submit', SubmitType::class, array('label' => 'Delete'))
|
|
->getForm();
|
|
}
|
|
|
|
/**
|
|
* Create a form to add a link to a groupcenter.
|
|
*/
|
|
private function createAddLinkGroupCenterForm(User $user, Request $request): FormInterface
|
|
{
|
|
$returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : [];
|
|
|
|
return $this->createFormBuilder()
|
|
->setAction($this->generateUrl(
|
|
'admin_user_add_groupcenter',
|
|
array_merge($returnPathParams, ['uid' => $user->getId()])
|
|
))
|
|
->setMethod('POST')
|
|
->add(self::FORM_GROUP_CENTER_COMPOSED, ComposedGroupCenterType::class)
|
|
->add('submit', SubmitType::class, array('label' => 'Add a new groupCenter'))
|
|
->getForm();
|
|
}
|
|
|
|
private function getDeleteLinkGroupCenterByUser(User $user, Request $request)
|
|
{
|
|
foreach ($user->getGroupCenters() as $groupCenter) {
|
|
yield $groupCenter->getId() => $this->createDeleteLinkGroupCenterForm($user, $groupCenter, $request);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Displays a form to edit the user current location.
|
|
*
|
|
* @Route("/{_locale}/main/user/current-location/edit", name="chill_main_user_currentlocation_edit")
|
|
*/
|
|
public function editCurrentLocationAction(Request $request)
|
|
{
|
|
$user = $this->getUser();
|
|
$form = $this->createForm(UserCurrentLocationType::class, $user)
|
|
->add('submit', SubmitType::class, ['label' => 'Save'])
|
|
->handleRequest($request);
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
$currentLocation = $form->get('currentLocation')->getData();
|
|
|
|
$user->setCurrentLocation($currentLocation);
|
|
|
|
$this->getDoctrine()->getManager()->flush();
|
|
$this->addFlash('success', $this->get('translator')->trans('Current location successfully updated'));
|
|
|
|
return $this->redirect(
|
|
$request->query->has('returnPath') ? $request->query->get('returnPath') :
|
|
$this->generateUrl('chill_main_homepage')
|
|
);
|
|
}
|
|
|
|
return $this->render('@ChillMain/User/edit_current_location.html.twig', [
|
|
'entity' => $user,
|
|
'edit_form' => $form->createView()
|
|
]);
|
|
}
|
|
}
|