Skip to content

Commit 117ff44

Browse files
committed
Allow to show what permissions a user is lacking in case of access denied message
Should help with errors like 1026
1 parent ba7d139 commit 117ff44

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/Security/Voter/PermissionVoter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
use App\Services\UserSystem\VoterHelper;
2626
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
27+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
2728
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
2829

2930
/**
@@ -39,12 +40,17 @@ public function __construct(private readonly VoterHelper $helper)
3940

4041
}
4142

42-
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
43+
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token, ?Vote $vote = null): bool
4344
{
4445
$attribute = ltrim($attribute, '@');
4546
[$perm, $op] = explode('.', $attribute);
4647

47-
return $this->helper->isGranted($token, $perm, $op);
48+
$result = $this->helper->isGranted($token, $perm, $op);
49+
if ($result === false) {
50+
$this->helper->addReason($vote, $perm, $op);
51+
}
52+
53+
return $result;
4854
}
4955

5056
public function supportsAttribute(string $attribute): bool

src/Services/UserSystem/VoterHelper.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,24 @@
2828
use App\Security\ApiTokenAuthenticatedToken;
2929
use Doctrine\ORM\EntityManagerInterface;
3030
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
31+
use Symfony\Component\Security\Core\Authorization\Voter\Vote;
32+
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
33+
use Symfony\Contracts\Translation\TranslatorInterface;
3134

3235
/**
3336
* @see \App\Tests\Services\UserSystem\VoterHelperTest
3437
*/
3538
final class VoterHelper
3639
{
3740
private readonly UserRepository $userRepository;
41+
private readonly array $permissionStructure;
3842

39-
public function __construct(private readonly PermissionManager $permissionManager, private readonly EntityManagerInterface $entityManager)
43+
public function __construct(private readonly PermissionManager $permissionManager,
44+
private readonly TranslatorInterface $translator,
45+
private readonly EntityManagerInterface $entityManager)
4046
{
4147
$this->userRepository = $this->entityManager->getRepository(User::class);
48+
$this->permissionStructure = $this->permissionManager->getPermissionStructure();
4249
}
4350

4451
/**
@@ -124,4 +131,17 @@ public function isValidOperation(string $permission, string $operation): bool
124131
{
125132
return $this->permissionManager->isValidOperation($permission, $operation);
126133
}
127-
}
134+
135+
public function addReason(?Vote $voter, string $permission, $operation): void
136+
{
137+
if ($voter !== null) {
138+
$voter->addReason(sprintf("User does not have permission %s -> %s -> %s (%s.%s).",
139+
$this->translator->trans('perm.group.'.$this->permissionStructure['perms'][$permission]['group'] ?? 'default' ),
140+
$this->translator->trans($this->permissionStructure['perms'][$permission]['label'] ?? $permission),
141+
$this->translator->trans($this->permissionStructure['perms'][$permission]['operations'][$operation]['label'] ?? $operation),
142+
$permission,
143+
$operation
144+
));
145+
}
146+
}
147+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{% extends "bundles/TwigBundle/Exception/error.html.twig" %}
22

33
{% block status_comment %}
4-
Nice try! But you are not allowed to do this!
4+
Nice try! But you are not allowed to do this!<br>
5+
<code>{{ exception.message }}</code>
56
<br> <small>If you think you should have access to this ressource, contact the adminstrator.</small>
6-
{% endblock %}
7+
8+
9+
{% endblock %}

0 commit comments

Comments
 (0)