UserController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 52
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 6 1
A changePassword() 0 35 4
1
<?php
2
3
namespace App\Controller;
4
5
use App\Form\UserChangePasswordType;
6
use App\Repository\UserRepository;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
12
use Symfony\Component\Routing\Annotation\Route;
13
14
/**
15
 * @Route("/user", name="user_")
16
 */
17
class UserController extends AbstractController
18
{
19
    /**
20
     * @Route("/", name="index", methods={"GET"})
21
     */
22
    public function index(): Response
23
    {
24
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
25
        $user = $this->getUser();
26
        return $this->render('user/index.html.twig', [
27
            'user' => $user,
28
        ]);
29
    }
30
31
    /**
32
     * @Route("/changepassword", name="changepassword", methods={"GET", "POST"})
33
     */
34
    public function changePassword(
35
        Request $request,
36
        UserPasswordHasherInterface $encoder,
37
        EntityManagerInterface $entityManager
38
    ): Response {
39
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
40
        /** @var \App\Entity\User | null */
41
        $user = $this->getUser();
42
        if ($user === null) {
43
            throw new \Exception("Coudn't get user even though I seem to be authenticated");
44
        }
45
46
        $userInfo = ['plainPassword' => null];
47
48
        $form = $this->createForm(UserChangePasswordType::class, $userInfo);
49
        $form->handleRequest($request);
50
51
        if ($form->isSubmitted() && $form->isValid()) {
52
            $info = $form->getData();
53
            $plainPassword = $info['plainPassword'];
54
            // TODO: Password strength validation?
55
            $password = $encoder->hashPassword($user, $plainPassword);
56
            $user->setPassword($password);
57
            $entityManager->flush();
58
59
            $this->addFlash(
60
                'notice',
61
                'Password changed!'
62
            );
63
            return $this->redirectToRoute('user_index');
64
        }
65
66
        return $this->render('user/changepassword.html.twig', [
67
            'user' => $user,
68
            'form' => $form->createView()
69
        ]);
70
    }
71
}
72