File tree Expand file tree Collapse file tree 2 files changed +18
-9
lines changed
Expand file tree Collapse file tree 2 files changed +18
-9
lines changed Original file line number Diff line number Diff line change 55
66use Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorageInterface ;
77use TheCodingMachine \GraphQLite \Security \AuthenticationServiceInterface ;
8+ use function is_object ;
89
910class 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments