-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAuthorizationChecker.php
More file actions
40 lines (34 loc) · 1.55 KB
/
AuthorizationChecker.php
File metadata and controls
40 lines (34 loc) · 1.55 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
<?php
namespace EasyCorp\Bundle\EasyAdminBundle\Security;
use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
/**
* A slightly modified authorization checker optimized for performance and which
* doesn't trigger exceptions when security is not enabled.
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class AuthorizationChecker implements AuthorizationCheckerInterface
{
private AuthorizationCheckerInterface $authorizationChecker;
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
{
$this->authorizationChecker = $authorizationChecker;
}
public function isGranted($permission, $subject = null, ?AccessDecision $accessDecision = null): bool
{
// this check is needed for performance reasons because most of the times permissions
// won't be set, so this function must return as early as possible in those cases
if (null === $permission || '' === $permission) {
return true;
}
try {
return $this->authorizationChecker->isGranted($permission, $subject);
} catch (AuthenticationCredentialsNotFoundException) {
// this exception happens when there's no security configured in the application
// that's a valid scenario for EasyAdmin, where security is not required (although very common)
return true;
}
}
}