Skip to content

Adapting code for the new @Security annotation #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions DependencyInjection/GraphqliteCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ public function process(ContainerBuilder $container)
foreach ($controllersNamespaces as $controllersNamespace) {
$schemaFactory->addMethodCall('addControllerNamespace', [ $controllersNamespace ]);
foreach ($this->getClassList($controllersNamespace) as $className => $refClass) {
$this->makePublicInjectedServices($refClass, $reader, $container);
$this->makePublicInjectedServices($refClass, $reader, $container, true);
}
}

foreach ($typesNamespaces as $typeNamespace) {
$schemaFactory->addMethodCall('addTypeNamespace', [ $typeNamespace ]);
foreach ($this->getClassList($typeNamespace) as $className => $refClass) {
$this->makePublicInjectedServices($refClass, $reader, $container);
$this->makePublicInjectedServices($refClass, $reader, $container, false);
}
}

Expand Down Expand Up @@ -273,13 +273,16 @@ private function mapAdderToTag(string $tag, string $methodName, ContainerBuilder
}
}

private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container): void
private function makePublicInjectedServices(ReflectionClass $refClass, AnnotationReader $reader, ContainerBuilder $container, bool $isController): void
{
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container) {
$services = $this->getCodeCache()->get($refClass, function() use ($refClass, $reader, $container, $isController) {
$services = [];
foreach ($refClass->getMethods() as $method) {
$field = $reader->getRequestAnnotation($method, AbstractRequest::class);
if ($field !== null) {
if ($isController) {
$services[$refClass->getName()] = $refClass->getName();
}
$services += $this->getListOfInjectedServices($method, $container);
}
}
Expand Down
20 changes: 15 additions & 5 deletions Security/AuthenticationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use TheCodingMachine\GraphQLite\Security\AuthenticationServiceInterface;
use function is_object;

class AuthenticationService implements AuthenticationServiceInterface
{
Expand All @@ -24,21 +25,30 @@ public function __construct(?TokenStorageInterface $tokenStorage)
* @return bool
*/
public function isLogged(): bool
{
return $this->getUser() !== null;
}

/**
* Returns an object representing the current logged user.
* Can return null if the user is not logged.
*/
public function getUser(): ?object
{
if ($this->tokenStorage === null) {
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
}

$token = $this->tokenStorage->getToken();
if (null === $token) {
return false;
return null;
}

if (!\is_object($token->getUser())) {
$user = $token->getUser();
if (!\is_object($user)) {
// e.g. anonymous authentication
return false;
return null;
}

return true;
return $user;
}
}
7 changes: 3 additions & 4 deletions Security/AuthorizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ public function __construct(?AuthorizationCheckerInterface $authorizationChecker
/**
* Returns true if the "current" user has access to the right "$right"
*
* @param string $right
* @return bool
* @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.
*/
public function isAllowed(string $right): bool
public function isAllowed(string $right, $subject = null): bool
{
if ($this->authorizationChecker === null || $this->tokenStorage === null) {
throw new \LogicException('The SecurityBundle is not registered in your application. Try running "composer require symfony/security-bundle".');
Expand All @@ -42,6 +41,6 @@ public function isAllowed(string $right): bool
return false;
}

return $this->authorizationChecker->isGranted($right);
return $this->authorizationChecker->isGranted($right, $subject);
}
}