HEX
Server: Apache
System: Linux WWW 6.1.0-40-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.153-1 (2025-09-20) x86_64
User: web11 (1011)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /var/www/dvpis2025/dvpis.kaunokolegija.lt/src/Controller/UserController.php
<?php

namespace App\Controller;

use App\Repository\UserRepository;
use App\Service\DbDataFilter;
use Doctrine\DBAL\Exception;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Form;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
 * User controller.
 */
#[Route(path: 'user')]
class UserController extends AbstractController
{
    public function __construct(
        private readonly EntityManagerInterface $em,
        private readonly UserRepository $userRepository,
        private readonly AuthorizationCheckerInterface $authorizationChecker,
        private readonly DbDataFilter $dbDataFilter,
    ) {
    }

    /**
     * Lists all user entities.
     *
     * @Security("is_granted('ROLE_STUDY_DEPARTMENT')")
     * @Method("GET")
     */
    #[Route(path: '/', name: 'user_index')]
    public function indexAction()
    {
        return $this->render('user/index.html.twig', array(
            'users' => $this->userRepository->findAll(),
        ));
    }

    /**
     * Creates a new user entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/new', name: 'user_new')]
    public function newAction(Request $request, UserPasswordHasherInterface $encoder)
    {
        $user = new User();
        $form = $this->createForm('App\Form\UserType', $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {

            $user->setPassword($encoder->hashPassword($user, $user->getPassword()));

            $this->em->persist($user);
            $this->em->flush();

            return $this->redirectToRoute('user_index', array('id' => $user->getId()));
        }

        return $this->render('user/new.html.twig', array(
            'user' => $user,
            'form' => $form->createView(),
        ));
    }

    /**
     * Finds and displays a user entity.
     *
     * @Method("GET")
     */
    #[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'user_show')]
    public function showAction(User $user)
    {
        $deleteForm = $this->createDeleteForm($user);

        return $this->render('user/show.html.twig', array(
            'user' => $user,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing user entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/{id}/edit', name: 'user_edit')]
    public function editAction(Request $request, User $user, UserPasswordHasherInterface $encoder)
    {
        $deleteForm = $this->createDeleteForm($user);
        $editForm = $this->createForm('App\Form\UserEditType', $user);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $paswTmp = $editForm->get("password")->getData();
            if (!empty($paswTmp)) {
                $user->setPassword($encoder->hashPassword($user, $paswTmp));
            }

            $this->em->flush();

            return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
        }

        return $this->render('user/edit.html.twig', array(
            'user' => $user,
            'edit_form' => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Displays a form to edit an existing user entity.
     *
     * @Method({"GET", "POST"})
     */
    #[Route(path: '/{id}/edituser', name: 'user_edit_self')]
    public function editSelfAction(Request $request, User $user, UserPasswordHasherInterface $encoder)
    {
        $editForm = $this->createForm('App\Form\UserEditSelfType', $user);
        $editForm->handleRequest($request);

        if ($editForm->isSubmitted() && $editForm->isValid()) {
            $paswTmp = $editForm->get("password")->getData();
            if (!empty($paswTmp)) {
                $user->setPassword($encoder->hashPassword($user, $paswTmp));
            }

            $this->em->flush();

            return $this->redirectToRoute('user_edit_self', array('id' => $user->getId()));
        }

        return $this->render('user/editself.html.twig', array(
            'user' => $user,
            'edit_form' => $editForm->createView(),
        ));
    }

    /**
     * Deletes a user entity.
     *
     * @Method("DELETE")
     */
    #[Route(path: '/{id}', requirements: ['id' => '\d+'], name: 'user_delete')]
    public function deleteAction(Request $request, User $user)
    {
        $form = $this->createDeleteForm($user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $this->em->remove($user);
            try {
                $this->em->flush();
            } catch (Exception $ex) {
                $this->addFlash('warning', "Ištrinti įrašo nepavyko! Jis gali turėti susijusių įrašų." . $ex->getMessage());
            }
        }

        return $this->redirectToRoute('user_index');
    }

    /**
     * Creates a form to delete a user entity.
     *
     * @param User $user The user entity
     *
     * @return Form The form
     */
    private function createDeleteForm(User $user)
    {
        return $this->createFormBuilder()
            ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
            ->setMethod('DELETE')
            ->getForm();
    }
}