- //Config array
- protected $config;
-
- //Translator instance
- protected $translator;
-
- public function __construct(ContainerInterface $container, TranslatorInterface $translator, RouterInterface $router) {
- //Retrieve config
- $this->config = $container->getParameter($this->getAlias());
-
- //Set the translator
- $this->translator = $translator;
-
- //Get current action
- //XXX: we don't use this as it would be too slow, maybe ???
- #$action = str_replace(self::getAlias().'_', '', $container->get('request_stack')->getCurrentRequest()->get('_route'));
-
- //Inject every requested route in view and mail context
- foreach($this->config as $tag => $current) {
- //Look for entry with route subkey
- if (!empty($current['route'])) {
- //Generate url for both view and mail
- foreach(['view', 'mail'] as $view) {
- //Check that context key is usable
- if (isset($current[$view]['context']) && is_array($current[$view]['context'])) {
- //Process every routes
- foreach($current['route'] as $route => $key) {
- //Skip recover_mail route as it requires some parameters
- if ($route == 'recover_mail') {
- continue;
- }
- //Check that key is empty
- if (!isset($current[$view]['context'][$key])) {
- //Generate the route
- $this->config[$tag][$view]['context'][$key] = $router->generate(
- $this->config['route'][$route]['name'],
- $this->config['route'][$route]['context'],
- //Generate absolute url for mails
- $view=='mail'?UrlGeneratorInterface::ABSOLUTE_URL:UrlGeneratorInterface::ABSOLUTE_PATH
- );
- }
- }
- }
+ /**
+ * User index
+ *
+ * @param Request $request The request
+ * @return Response The response
+ */
+ public function index(Request $request): Response {
+ //Without admin
+ if (!$this->checker->isGranted($this->config['default']['admin'])) {
+ //Throw 403
+ throw $this->createAccessDeniedException($this->translator->trans('Unable to list users'));
+ }
+
+ //Get count
+ $this->context['count'] = $this->doctrine->getRepository($this->config['class']['user'])->findCountAsInt();
+
+ //With not enough users
+ if ($this->context['count'] - $this->page * $this->limit < 0) {
+ //Throw 404
+ throw $this->createNotFoundException($this->translator->trans('Unable to find users'));
+ }
+
+ //Get users
+ $this->context['users'] = $this->doctrine->getRepository($this->config['class']['user'])->findAllAsArray($this->page, $this->limit);
+
+ //Render view
+ return $this->render(
+ //Template
+ $this->config['index']['view']['name'],
+ //Context
+ $this->context+$this->config['index']['view']['context']
+ );
+ }
+
+ /**
+ * Confirm account from mail link
+ *
+ * @param Request $request The request
+ * @param string $hash The hashed password
+ * @param string $mail The shorted mail address
+ * @return Response The response
+ */
+ public function confirm(Request $request, string $hash, string $mail): Response {
+ //With invalid hash
+ if ($hash != $this->slugger->hash($mail)) {
+ //Throw bad request
+ throw new BadRequestHttpException($this->translator->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash]));
+ }
+
+ //Get mail
+ $mail = $this->slugger->unshort($smail = $mail);
+
+ //Without valid mail
+ if (filter_var($mail, FILTER_VALIDATE_EMAIL) === false) {
+ //Throw bad request
+ //XXX: prevent slugger reverse engineering by not displaying decoded mail
+ throw new BadRequestHttpException($this->translator->trans('Invalid %field% field: %value%', ['%field%' => 'mail', '%value%' => $smail]));
+ }
+
+ //Without existing registrant
+ if (!($user = $this->doctrine->getRepository($this->config['class']['user'])->findOneByMail($mail))) {
+ //Add error message mail already exists
+ //XXX: prevent slugger reverse engineering by not displaying decoded mail
+ $this->addFlash('error', $this->translator->trans('Account %mail% do not exists', ['%mail%' => $smail]));
+
+ //Redirect to register view
+ return $this->redirectToRoute($this->config['route']['register']['name'], $this->config['route']['register']['context']);
+ }
+
+ //Set active
+ $user->setActive(true);
+
+ //Persist user
+ $this->manager->persist($user);
+
+ //Send to database
+ $this->manager->flush();
+
+ //Add error message mail already exists
+ $this->addFlash('notice', $this->translator->trans('Your account has been activated'));
+
+ //Redirect to user view
+ return $this->redirectToRoute($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']);
+ }
+
+ /**
+ * Edit account by shorted mail
+ *
+ * @param Request $request The request
+ * @param string $hash The hashed password
+ * @param string $mail The shorted mail address
+ * @return Response The response
+ */
+ public function edit(Request $request, string $hash, string $mail): Response {
+ //With invalid hash
+ if ($hash != $this->slugger->hash($mail)) {
+ //Throw bad request
+ throw new BadRequestHttpException($this->translator->trans('Invalid %field% field: %value%', ['%field%' => 'hash', '%value%' => $hash]));
+ }
+
+ //Get mail
+ $mail = $this->slugger->unshort($smail = $mail);
+
+ //With existing subscriber
+ if (empty($user = $this->doctrine->getRepository($this->config['class']['user'])->findOneByMail($mail))) {
+ //Throw not found
+ //XXX: prevent slugger reverse engineering by not displaying decoded mail
+ throw $this->createNotFoundException($this->translator->trans('Unable to find account %mail%', ['%mail%' => $smail]));
+ }
+
+ //Prevent access when not admin, user is not guest and not currently logged user
+ if (!$this->checker->isGranted($this->config['default']['admin']) && $user != $this->security->getUser() || !$this->checker->isGranted('IS_AUTHENTICATED_FULLY')) {
+ //Throw access denied
+ //XXX: prevent slugger reverse engineering by not displaying decoded mail
+ throw $this->createAccessDeniedException($this->translator->trans('Unable to access user: %mail%', ['%mail%' => $smail]));
+ }
+
+ //Create the EditType form and give the proper parameters
+ $edit = $this->createForm($this->config['edit']['view']['edit'], $user, [
+ //Set action to edit route name and context
+ 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']),
+ //Set civility class
+ 'civility_class' => $this->config['class']['civility'],
+ //Set civility default
+ 'civility_default' => $this->doctrine->getRepository($this->config['class']['civility'])->findOneByTitle($this->config['default']['civility']),
+ //Set method
+ 'method' => 'POST'
+ ]+($this->checker->isGranted($this->config['default']['admin'])?$this->config['edit']['admin']:$this->config['edit']['field']));
+
+ //With admin role
+ if ($this->checker->isGranted($this->config['default']['admin'])) {
+ //Create the EditType form and give the proper parameters
+ $reset = $this->createForm($this->config['edit']['view']['reset'], $user, [
+ //Set action to edit route name and context
+ 'action' => $this->generateUrl($this->config['route']['edit']['name'], ['mail' => $smail, 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']),
+ //Set method
+ 'method' => 'POST'
+ ]);
+
+ //With post method
+ if ($request->isMethod('POST')) {
+ //Refill the fields in case the form is not valid.
+ $reset->handleRequest($request);
+
+ //With reset submitted and valid
+ if ($reset->isSubmitted() && $reset->isValid()) {
+ //Set data
+ $data = $reset->getData();
+
+ //Set password
+ $data->setPassword($this->hasher->hashPassword($data, $data->getPassword()));
+
+ //Queue snippet save
+ $this->manager->persist($data);
+
+ //Flush to get the ids
+ $this->manager->flush();
+
+ //Add notice
+ $this->addFlash('notice', $this->translator->trans('Account %mail% password updated', ['%mail%' => $mail = $data->getMail()]));
+
+ //Redirect to cleanup the form
+ return $this->redirectToRoute($this->config['route']['edit']['name'], ['mail' => $smail = $this->slugger->short($mail), 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']);
+ }
+ }
+
+ //Add reset view
+ $this->config['edit']['view']['context']['reset'] = $reset->createView();
+ }
+
+ //With post method
+ if ($request->isMethod('POST')) {
+ //Refill the fields in case the form is not valid.
+ $edit->handleRequest($request);
+
+ //With edit submitted and valid
+ if ($edit->isSubmitted() && $edit->isValid()) {
+ //Set data
+ $data = $edit->getData();
+
+ //Queue snippet save
+ $this->manager->persist($data);
+
+ //Try saving in database
+ try {
+ //Flush to get the ids
+ $this->manager->flush();
+
+ //Add notice
+ $this->addFlash('notice', $this->translator->trans('Account %mail% updated', ['%mail%' => $mail = $data->getMail()]));
+
+ //Redirect to cleanup the form
+ return $this->redirectToRoute($this->config['route']['edit']['name'], ['mail' => $smail = $this->slugger->short($mail), 'hash' => $this->slugger->hash($smail)]+$this->config['route']['edit']['context']);
+ //Catch double slug or mail
+ } catch (UniqueConstraintViolationException $e) {
+ //Add error message mail already exists
+ $this->addFlash('error', $this->translator->trans('Account %mail% already exists', ['%mail%' => $data->getMail()]));