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
|
|
|
|
9
|
|
|
final class Security |
10
|
|
|
{ |
11
|
|
|
private $coreSecurity; |
12
|
|
|
|
13
|
169 |
|
public function __construct(?CoreSecurity $security) |
14
|
|
|
{ |
15
|
|
|
$this->coreSecurity = $security ?? new class() { |
16
|
|
|
public function isGranted(): void |
17
|
|
|
{ |
18
|
1 |
|
throw new \LogicException('The "symfony/security-core" component is required.'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getUser(): void |
22
|
|
|
{ |
23
|
1 |
|
throw new \LogicException('The "symfony/security-core" component is required.'); |
24
|
|
|
} |
25
|
|
|
}; |
26
|
169 |
|
} |
27
|
|
|
|
28
|
9 |
|
public function getUser() |
29
|
|
|
{ |
30
|
9 |
|
return $this->coreSecurity->getUser(); |
31
|
|
|
} |
32
|
|
|
|
33
|
27 |
|
public function isGranted($attributes, $subject = null): bool |
34
|
|
|
{ |
35
|
27 |
|
return $this->coreSecurity->isGranted($attributes, $subject); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
2 |
|
public function hasAnyPermission($object, array $permissions): bool |
39
|
|
|
{ |
40
|
2 |
|
return \array_reduce( |
41
|
2 |
|
$permissions, |
42
|
|
|
function ($isGranted, $permission) use ($object) { |
43
|
2 |
|
return $isGranted || $this->isGranted($permission, $object); |
44
|
2 |
|
}, |
45
|
2 |
|
false |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
public function hasAnyRole(array $roles): bool |
50
|
|
|
{ |
51
|
4 |
|
return \array_reduce( |
52
|
4 |
|
$roles, |
53
|
|
|
function ($isGranted, $role) { |
54
|
4 |
|
return $isGranted || $this->isGranted($role); |
55
|
4 |
|
}, |
56
|
4 |
|
false |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function hasPermission($object, $permission): bool |
61
|
|
|
{ |
62
|
2 |
|
return $this->isGranted($permission, $object); |
63
|
|
|
} |
64
|
|
|
|
65
|
8 |
|
public function hasRole($role): bool |
66
|
|
|
{ |
67
|
8 |
|
return $this->isGranted($role); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
public function isAnonymous(): bool |
71
|
|
|
{ |
72
|
2 |
|
return $this->isGranted('IS_AUTHENTICATED_ANONYMOUSLY'); |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
public function isAuthenticated(): bool |
76
|
|
|
{ |
77
|
2 |
|
return $this->hasAnyRole(['IS_AUTHENTICATED_REMEMBERED', 'IS_AUTHENTICATED_FULLY']); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
public function isFullyAuthenticated(): bool |
81
|
|
|
{ |
82
|
6 |
|
return $this->isGranted('IS_AUTHENTICATED_FULLY'); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public function isRememberMe(): bool |
86
|
|
|
{ |
87
|
2 |
|
return $this->isGranted('IS_AUTHENTICATED_REMEMBERED'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.