-
Notifications
You must be signed in to change notification settings - Fork 0
feat(profile): self-service profile show + edit (#13) #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
martinydeAI
wants to merge
3
commits into
feature/issue-45-user-entity-extension
Choose a base branch
from
feature/issue-13-profile-editing
base: feature/issue-45-user-entity-extension
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| {% extends 'base.html.twig' %} | ||
|
|
||
| {% block title %}{{ 'profile.edit.title'|trans({'%brand%': brand_name}) }}{% endblock %} | ||
|
|
||
| {% block body %} | ||
| <section class="max-w-md"> | ||
| <twig:Eyebrow as="p" class="mb-4">{{ 'profile.edit.eyebrow'|trans }}</twig:Eyebrow> | ||
| <h1 class="mb-6 font-display text-[clamp(1.75rem,3vw,2.25rem)] font-medium leading-tight tracking-tight text-ink"> | ||
|
martinyde marked this conversation as resolved.
|
||
| {{ 'profile.edit.heading'|trans }} | ||
| </h1> | ||
|
|
||
| {% if error %} | ||
| <div class="mb-4 rounded-lg border border-line bg-surface-2 px-4 py-3 text-sm text-text" role="alert"> | ||
|
martinyde marked this conversation as resolved.
|
||
| {{ error|trans }} | ||
| </div> | ||
| {% endif %} | ||
|
|
||
| <form action="{{ path('app_profile_edit') }}" method="post" class="grid gap-4"> | ||
| <twig:Form:Label for="inputName" text="{{ 'profile.edit.name_label'|trans }}"> | ||
| <twig:Form:TextInput | ||
| id="inputName" | ||
| name="name" | ||
| type="text" | ||
| value="{{ submitted_name }}" | ||
| autocomplete="name" | ||
| required | ||
| autofocus | ||
| /> | ||
| </twig:Form:Label> | ||
|
|
||
| <input type="hidden" name="_token" value="{{ csrf_token('profile-edit') }}"> | ||
|
|
||
| <div class="flex items-center gap-4"> | ||
| <twig:Form:Button type="submit"> | ||
| {{ 'profile.edit.submit'|trans }} | ||
| </twig:Form:Button> | ||
| <a class="text-sm text-text underline hover:text-primary" href="{{ path('app_profile_show') }}"> | ||
| {{ 'profile.edit.cancel'|trans }} | ||
| </a> | ||
| </div> | ||
| </form> | ||
| </section> | ||
| {% endblock %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| {% extends 'base.html.twig' %} | ||
|
|
||
| {% block title %}{{ 'profile.show.title'|trans({'%brand%': brand_name}) }}{% endblock %} | ||
|
|
||
| {% block body %} | ||
| <section class="max-w-md"> | ||
| <twig:Eyebrow as="p" class="mb-4">{{ 'profile.show.eyebrow'|trans }}</twig:Eyebrow> | ||
| <h1 class="mb-6 font-display text-[clamp(1.75rem,3vw,2.25rem)] font-medium leading-tight tracking-tight text-ink"> | ||
|
martinyde marked this conversation as resolved.
|
||
| {{ 'profile.show.heading'|trans }} | ||
| </h1> | ||
|
|
||
| {% for flash in app.flashes('success') %} | ||
| <div class="mb-4 rounded-lg border border-line bg-surface-2 px-4 py-3 text-sm text-text" role="status"> | ||
|
martinyde marked this conversation as resolved.
|
||
| {{ flash|trans }} | ||
| </div> | ||
| {% endfor %} | ||
|
|
||
| <dl class="grid gap-4 text-sm"> | ||
|
martinyde marked this conversation as resolved.
|
||
| <div> | ||
| <dt class="font-medium text-ink">{{ 'profile.show.name_label'|trans }}</dt> | ||
| <dd class="text-text">{{ user.name }}</dd> | ||
| </div> | ||
| <div> | ||
| <dt class="font-medium text-ink">{{ 'profile.show.email_label'|trans }}</dt> | ||
| <dd class="text-text">{{ user.email }}</dd> | ||
| </div> | ||
| </dl> | ||
|
|
||
| <p class="mt-6"> | ||
| <a class="text-primary underline hover:text-primary-hover" href="{{ path('app_profile_edit') }}"> | ||
| {{ 'profile.show.edit_link'|trans }} | ||
| </a> | ||
| </p> | ||
| </section> | ||
| {% endblock %} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace App\Tests\Integration\Controller; | ||
|
|
||
| use App\Repository\UserRepository; | ||
| use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
| use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | ||
|
|
||
| /** | ||
| * End-to-end profile view + edit flow for an authenticated user. | ||
| * | ||
| * Relies on the baseline `UserFixtures` (alice + bob with display | ||
| * names "Alice" / "Bob", password `password`, `status = Approved`). | ||
| */ | ||
| final class ProfileControllerTest extends WebTestCase | ||
|
martinyde marked this conversation as resolved.
|
||
| { | ||
| private KernelBrowser $client; | ||
|
|
||
| protected function setUp(): void | ||
| { | ||
| $this->client = self::createClient(); | ||
| } | ||
|
|
||
| // Tests that an anonymous visitor is redirected to /login when hitting /profile. | ||
| public function testProfilePageRedirectsAnonymousToLogin(): void | ||
| { | ||
| $this->client->request('GET', '/profile'); | ||
|
|
||
| self::assertResponseRedirects(); | ||
| self::assertStringContainsString('/login', (string) $this->client->getResponse()->headers->get('Location')); | ||
| } | ||
|
|
||
| // Tests that an anonymous visitor is redirected to /login when hitting /profile/edit. | ||
| public function testEditPageRedirectsAnonymousToLogin(): void | ||
| { | ||
| $this->client->request('GET', '/profile/edit'); | ||
|
|
||
| self::assertResponseRedirects(); | ||
| self::assertStringContainsString('/login', (string) $this->client->getResponse()->headers->get('Location')); | ||
| } | ||
|
|
||
| // Tests that the profile page renders the signed-in user's name and email. | ||
| public function testShowRendersTheCurrentUsersNameAndEmail(): void | ||
| { | ||
| $this->loginAsAlice(); | ||
|
|
||
| $crawler = $this->client->request('GET', '/profile'); | ||
|
|
||
| self::assertResponseIsSuccessful(); | ||
| $body = $crawler->filter('body')->text(); | ||
| self::assertStringContainsString('Alice', $body); | ||
| self::assertStringContainsString('alice@example.test', $body); | ||
| } | ||
|
|
||
| // Ensures the edit form is pre-filled with the user's current name on GET. | ||
| public function testEditRendersFormPrefilledWithTheCurrentName(): void | ||
| { | ||
| $this->loginAsAlice(); | ||
|
|
||
| $crawler = $this->client->request('GET', '/profile/edit'); | ||
|
|
||
| self::assertResponseIsSuccessful(); | ||
| self::assertSame('Alice', $crawler->filter('input[name="name"]')->attr('value')); | ||
| } | ||
|
|
||
| // Verifies a successful edit persists the new name and surfaces the success flash. | ||
| public function testEditPersistsTheNewNameAndShowsTheFlash(): void | ||
| { | ||
| $this->loginAsAlice(); | ||
|
|
||
| $crawler = $this->client->request('GET', '/profile/edit'); | ||
| $form = $crawler->filter('form')->form(); | ||
| $form['name'] = 'Alice Andersen'; | ||
| $this->client->submit($form); | ||
|
|
||
| self::assertResponseRedirects('/profile'); | ||
| $crawler = $this->client->followRedirect(); | ||
| self::assertResponseIsSuccessful(); | ||
|
|
||
| $body = $crawler->filter('body')->text(); | ||
| self::assertStringContainsString('Alice Andersen', $body); | ||
| self::assertStringContainsString('gemt', $body); | ||
|
|
||
| $reloaded = self::getContainer() | ||
| ->get(UserRepository::class) | ||
| ->findOneBy(['email' => 'alice@example.test']); | ||
| self::assertNotNull($reloaded); | ||
| self::assertSame('Alice Andersen', $reloaded->getName()); | ||
| } | ||
|
|
||
| // Ensures a whitespace-only name is rejected with 422 and the persisted name is unchanged. | ||
| public function testEditRejectsEmptyNameAndKeepsTheCurrentValue(): void | ||
| { | ||
| $this->loginAsAlice(); | ||
|
|
||
| $crawler = $this->client->request('GET', '/profile/edit'); | ||
| $form = $crawler->filter('form')->form(); | ||
| // Bypass HTML5 `required` by clearing the input value programmatically. | ||
| $form->setValues(['name' => ' ']); | ||
| $crawler = $this->client->submit($form); | ||
|
|
||
| self::assertResponseStatusCodeSame(422); | ||
| $body = $crawler->filter('body')->text(); | ||
| self::assertStringContainsString('Navnet må ikke være tomt', $body); | ||
|
|
||
| $reloaded = self::getContainer() | ||
| ->get(UserRepository::class) | ||
| ->findOneBy(['email' => 'alice@example.test']); | ||
| self::assertNotNull($reloaded); | ||
| self::assertSame('Alice', $reloaded->getName(), 'Empty submit must not have mutated the persisted name.'); | ||
| } | ||
|
|
||
| // Ensures an invalid CSRF token yields 403 and the persisted name is unchanged. | ||
| public function testEditRejectsInvalidCsrfTokenAndDoesNotUpdate(): void | ||
| { | ||
| $this->loginAsAlice(); | ||
|
|
||
| $this->client->request('POST', '/profile/edit', [ | ||
| 'name' => 'Hacker', | ||
| '_token' => 'nope', | ||
| ]); | ||
|
|
||
| self::assertResponseStatusCodeSame(403); | ||
|
|
||
| $reloaded = self::getContainer() | ||
| ->get(UserRepository::class) | ||
| ->findOneBy(['email' => 'alice@example.test']); | ||
| self::assertNotNull($reloaded); | ||
| self::assertSame('Alice', $reloaded->getName(), 'CSRF rejection must not have mutated the persisted name.'); | ||
| } | ||
|
|
||
| private function loginAsAlice(): void | ||
| { | ||
| $crawler = $this->client->request('GET', '/login'); | ||
| $form = $crawler->filter('form')->form(); | ||
| $form['_username'] = 'alice@example.test'; | ||
| $form['_password'] = 'password'; | ||
| $this->client->submit($form); | ||
| $this->client->followRedirect(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this live as public mehotd in UserManager? @tuj ?