-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileController.php
More file actions
75 lines (61 loc) · 2.33 KB
/
Copy pathProfileController.php
File metadata and controls
75 lines (61 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\User;
use App\Security\UserManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[IsGranted('IS_AUTHENTICATED_FULLY')]
final class ProfileController extends AbstractController
{
public function __construct(private readonly UserManager $userManager)
{
}
#[Route(path: '/profile', name: 'app_profile_show', methods: ['GET'])]
public function show(): Response
{
return $this->render('profile/show.html.twig', [
'user' => $this->currentUser(),
]);
}
#[Route(path: '/profile/edit', name: 'app_profile_edit', methods: ['GET', 'POST'])]
public function edit(Request $request): Response
{
$user = $this->currentUser();
if ('POST' !== $request->getMethod()) {
return $this->render('profile/edit.html.twig', [
'user' => $user,
'submitted_name' => $user->getName(),
'error' => null,
]);
}
if (!$this->isCsrfTokenValid('profile-edit', (string) $request->request->get('_token'))) {
return $this->render('profile/edit.html.twig', [
'user' => $user,
'submitted_name' => $user->getName(),
'error' => 'profile.edit.error.invalid_token',
], new Response('', Response::HTTP_FORBIDDEN));
}
$submitted = (string) $request->request->get('name', '');
try {
$this->userManager->updateName($user, $submitted);
} catch (\InvalidArgumentException) {
return $this->render('profile/edit.html.twig', [
'user' => $user,
'submitted_name' => $submitted,
'error' => 'profile.edit.error.empty_name',
], new Response('', Response::HTTP_UNPROCESSABLE_ENTITY));
}
$this->addFlash('success', 'profile.edit.flash.success');
return $this->redirectToRoute('app_profile_show');
}
private function currentUser(): User
{
$user = $this->getUser();
\assert($user instanceof User);
return $user;
}
}