Skip to content

Commit db5b5d2

Browse files
authored
Merge pull request #33 from moufmouf/userinterface_usage
Adapting code for the new @Security annotation
2 parents 82343a6 + 935e647 commit db5b5d2

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

DependencyInjection/GraphqliteCompilerPass.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ public function process(ContainerBuilder $container)
200200
foreach ($controllersNamespaces as $controllersNamespace) {
201201
$schemaFactory->addMethodCall('addControllerNamespace', [ $controllersNamespace ]);
202202
foreach ($this->getClassList($controllersNamespace) as $className => $refClass) {
203-
$this->makePublicInjectedServices($refClass, $reader, $container);
203+
$this->makePublicInjectedServices($refClass, $reader, $container, true);
204204
}
205205
}
206206

207207
foreach ($typesNamespaces as $typeNamespace) {
208208
$schemaFactory->addMethodCall('addTypeNamespace', [ $typeNamespace ]);
209209
foreach ($this->getClassList($typeNamespace) as $className => $refClass) {
210-
$this->makePublicInjectedServices($refClass, $reader, $container);
210+
$this->makePublicInjectedServices($refClass, $reader, $container, false);
211211
}
212212
}
213213

@@ -273,13 +273,16 @@ private function mapAdderToTag(string $tag, string $methodName, ContainerBuilder
273273
}
274274
}
275275

276-
private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container): void
276+
private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container, bool $isController): void
277277
{
278-
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container) {
278+
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container, $isController) {
279279
$services = [];
280280
foreach ($refClass->getMethods() as $method) {
281281
$field = $reader->getRequestAnnotation($method, AbstractRequest::class);
282282
if ($field !== null) {
283+
if ($isController) {
284+
$services[$refClass->getName()] = $refClass->getName();
285+
}
283286
$services += $this->getListOfInjectedServices($method, $container);
284287
}
285288
}

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)