Skip to content

Commit f342629

Browse files
committed
Adapting code for the new @Security annotation
1 parent d57857f commit f342629

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

Security/AuthenticationService.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
77
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
8+
use function is_object;
89

910
class AuthenticationService implements AuthenticationServiceInterface
1011
{
@@ -24,21 +25,30 @@ public function __construct(?TokenStorageInterface $tokenStorage)
2425
* @return bool
2526
*/
2627
public function isLogged(): bool
28+
{
29+
return $this->getUser() !== null;
30+
}
31+
32+
/**
33+
* Returns an object representing the current logged user.
34+
* Can return null if the user is not logged.
35+
*/
36+
public function getUser(): ?object
2737
{
2838
if ($this->tokenStorage === null) {
2939
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
3040
}
3141

3242
$token = $this->tokenStorage->getToken();
3343
if (null === $token) {
34-
return false;
44+
return null;
3545
}
3646

37-
if (!\is_object($token->getUser())) {
47+
$user = $token->getUser();
48+
if (!\is_object($user)) {
3849
// e.g. anonymous authentication
39-
return false;
50+
return null;
4051
}
41-
42-
return true;
52+
return $user;
4353
}
4454
}

Security/AuthorizationService.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public function __construct(?AuthorizationCheckerInterface $authorizationChecker
2828
/**
2929
* Returns true if the "current" user has access to the right "$right"
3030
*
31-
* @param string $right
32-
* @return bool
31+
* @param mixed $subject The scope this right applies on. $subject is typically an object or a FQCN. Set $subject to "null" if the right is global.
3332
*/
34-
public function isAllowed(string $right): bool
33+
public function isAllowed(string $right, $subject = null): bool
3534
{
3635
if ($this->authorizationChecker === null || $this->tokenStorage === null) {
3736
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
@@ -42,6 +41,6 @@ public function isAllowed(string $right): bool
4241
return false;
4342
}
4443

45-
return $this->authorizationChecker->isGranted($right);
44+
return $this->authorizationChecker->isGranted($right, $subject);
4645
}
4746
}

0 commit comments

Comments
 (0)