Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Latest
## Changes
* [#138](https://github.com/cleverage/ui-process-bundle/issues/138) Installation documentation: describe the Symfony Flex recipe, which registers the bundle, imports the bundle controllers routes (`config/routes/clever_age_ui_process.yaml`, the EasyAdmin routes being imported by the EasyAdmin recipe) and creates `config/packages/clever_age_ui_process.yaml`, and the manual setup without Flex.

## Fixes
* [#200](https://github.com/cleverage/ui-process-bundle/issues/200) `UserCrudController::generateToken()`: reserved to `ROLE_ADMIN` (`403`), as the users, and `404` for an unknown user: the action relied on EasyAdmin not loading the entity for a user without the entity permission (a `500`).

v3.3
------

Expand Down
8 changes: 7 additions & 1 deletion src/Controller/Admin/UserCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ public function configureActions(Actions $actions): Actions
->addCssClass(''))->add(Crud::PAGE_EDIT, Action::new('generateToken')->linkToCrudAction('generateToken'));
}

/**
* Same permission as the users (setEntityPermission()), which EasyAdmin only applies to its built-in actions.
*/
#[AdminRoute(path: '{id}/generate-token', name: 'generateToken')]
#[IsGranted('ROLE_ADMIN')]
public function generateToken(): Response
{
$adminContext = $this->getContext();
/** @var User $user */
$user = $adminContext?->getEntity()->getInstance();
if (!$user instanceof User) {
throw $this->createNotFoundException('User not found.');
}
$token = md5(uniqid(date('YmdHis')));
$user->setToken((new Pbkdf2PasswordHasher())->hash($token));
/** @var ManagerRegistry $doctrine */
Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/UserCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,30 @@ public function testGenerateToken(): void
// Only the hash of the token is stored
self::assertSame((new Pbkdf2PasswordHasher())->hash($token), $this->getEntityManager()->find(User::class, $admin->getId())?->getToken());
}

/**
* The users are reserved to the admins: the custom action too (it was only protected because EasyAdmin does not
* load the entity, a 500).
*/
public function testGenerateTokenIsReservedToAdmins(): void
{
$user = $this->login();
$other = $this->createUser('other@example.com');

$this->client->request('GET', '/process/user/'.$other->getId().'/generate-token');
self::assertResponseStatusCodeSame(403);
$this->client->request('GET', '/process/user/'.$user->getId().'/generate-token');
self::assertResponseStatusCodeSame(403);

$this->getEntityManager()->clear();
self::assertNull($this->getEntityManager()->find(User::class, $other->getId())?->getToken());
}

public function testGenerateTokenOfAnUnknownUser(): void
{
$this->login(['ROLE_ADMIN']);

$this->client->request('GET', '/process/user/999999/generate-token');
self::assertResponseStatusCodeSame(404);
}
}
Loading