1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Overblog\GraphQLBundle\Security; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Security\Core\Security as CoreSecurity; |
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
9
|
|
|
|
10
|
|
|
final class Security |
11
|
|
|
{ |
12
|
|
|
private $coreSecurity; |
13
|
|
|
|
14
|
|
|
public function __construct(?CoreSecurity $security) |
15
|
|
|
{ |
16
|
|
|
$this->coreSecurity = $security ?? new class() { |
17
|
|
|
public function isGranted(): void |
18
|
|
|
{ |
19
|
|
|
throw new \LogicException('The "symfony/security-core" component is required.'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function getUser(): void |
23
|
|
|
{ |
24
|
|
|
throw new \LogicException('The "symfony/security-core" component is required.'); |
25
|
|
|
} |
26
|
|
|
}; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getUser(): ?UserInterface |
30
|
|
|
{ |
31
|
|
|
return $this->coreSecurity->getUser(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function isGranted($attributes, $subject = null): bool |
35
|
|
|
{ |
36
|
|
|
return $this->coreSecurity->isGranted($attributes, $subject); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function hasAnyPermission($object, array $permissions): bool |
40
|
|
|
{ |
41
|
|
|
return \array_reduce( |
42
|
|
|
$permissions, |
43
|
|
|
function ($isGranted, $permission) use ($object) { |
44
|
|
|
return $isGranted || $this->isGranted($permission, $object); |
45
|
|
|
}, |
46
|
|
|
false |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function hasAnyRole(array $roles): bool |
51
|
|
|
{ |
52
|
|
|
return \array_reduce( |
53
|
|
|
$roles, |
54
|
|
|
function ($isGranted, $role) { |
55
|
|
|
return $isGranted || $this->isGranted($role); |
56
|
|
|
}, |
57
|
|
|
false |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function hasPermission($object, $permission): bool |
62
|
|
|
{ |
63
|
|
|
return $this->isGranted($permission, $object); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function hasRole($role): bool |
67
|
|
|
{ |
68
|
|
|
return $this->isGranted($role); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function isAnonymous(): bool |
72
|
|
|
{ |
73
|
|
|
return $this->isGranted('IS_AUTHENTICATED_ANONYMOUSLY'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function isAuthenticated(): bool |
77
|
|
|
{ |
78
|
|
|
return $this->hasAnyRole(['IS_AUTHENTICATED_REMEMBERED', 'IS_AUTHENTICATED_FULLY']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function isFullyAuthenticated(): bool |
82
|
|
|
{ |
83
|
|
|
return $this->isGranted('IS_AUTHENTICATED_FULLY'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function isRememberMe(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->isGranted('IS_AUTHENTICATED_REMEMBERED'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.