Skip to content

Commit 3bae264

Browse files
authored
Merge pull request #633 from mcg-web/make-security-optional
Make security core component optional
2 parents 7756747 + f307291 commit 3bae264

27 files changed

+261
-126
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -870,19 +870,14 @@ parameters:
870870
count: 1
871871
path: tests/Executor/Promise/Adapter/ReactPromiseAdapterTest.php
872872

873-
-
874-
message: "#^Parameter \\#1 \\$container of class Overblog\\\\GraphQLBundle\\\\ExpressionLanguage\\\\ExpressionFunction\\\\DependencyInjection\\\\Service constructor expects Symfony\\\\Component\\\\DependencyInjection\\\\ContainerInterface, Symfony\\\\Component\\\\DependencyInjection\\\\ContainerInterface\\|null given\\.$#"
875-
count: 2
876-
path: tests/ExpressionLanguage/ExpressionFunction/DependencyInjection/ServiceTest.php
877-
878873
-
879874
message: "#^Call to method disableOriginalConstructor\\(\\) on an unknown class PHPUnit_Framework_MockObject_MockBuilder\\.$#"
880875
count: 1
881876
path: tests/ExpressionLanguage/ExpressionFunction/GraphQL/ArgumentsTest.php
882877

883878
-
884879
message: "#^Call to method getMock\\(\\) on an unknown class PHPUnit_Framework_MockObject_MockBuilder\\.$#"
885-
count: 5
880+
count: 4
886881
path: tests/ExpressionLanguage/ExpressionFunction/Security/GetUserTest.php
887882

888883
-

src/ExpressionLanguage/ExpressionFunction/Security/GetUser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,18 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Symfony\Component\Security\Core\Security;
9-
use Symfony\Component\Security\Core\User\UserInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
109

1110
final class GetUser extends ExpressionFunction
1211
{
1312
public function __construct(Security $security)
1413
{
1514
parent::__construct(
1615
'getUser',
17-
function (): string {
18-
return \sprintf('\%s::getUser($globalVariable)', Helper::class);
16+
static function (): string {
17+
return '$globalVariable->get(\'security\')->getUser()';
1918
},
20-
function () use ($security): ?UserInterface {
19+
static function () use ($security) {
2120
return $security->getUser();
2221
}
2322
);

src/ExpressionLanguage/ExpressionFunction/Security/HasAnyPermission.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Overblog\GraphQLBundle\Generator\TypeGenerator;
9-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
109

1110
final class HasAnyPermission extends ExpressionFunction
1211
{
13-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1413
{
1514
parent::__construct(
1615
'hasAnyPermission',
17-
function ($object, $permissions) {
18-
$code = \sprintf('array_reduce(%s, function ($isGranted, $permission) use (%s, $object) { return $isGranted || $globalVariable->get(\'container\')->get(\'security.authorization_checker\')->isGranted($permission, %s); }, false)', $permissions, TypeGenerator::USE_FOR_CLOSURES, $object);
19-
20-
return $code;
16+
static function ($object, $permissions): string {
17+
return \sprintf('$globalVariable->get(\'security\')->hasAnyPermission(%s, %s)', $object, $permissions);
2118
},
22-
function ($_, $object, $permissions) use ($authorizationChecker) {
23-
return \array_reduce(
24-
$permissions,
25-
function ($isGranted, $permission) use ($authorizationChecker, $object) { return $isGranted || $authorizationChecker->isGranted($permission, $object); },
26-
false
27-
);
19+
function ($_, $object, $permissions) use ($security): bool {
20+
return $security->hasAnyPermission($object, $permissions);
2821
}
2922
);
3023
}

src/ExpressionLanguage/ExpressionFunction/Security/HasAnyRole.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Overblog\GraphQLBundle\Generator\TypeGenerator;
9-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
109

1110
final class HasAnyRole extends ExpressionFunction
1211
{
13-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1413
{
1514
parent::__construct(
1615
'hasAnyRole',
17-
function ($roles): string {
18-
$code = \sprintf('array_reduce(%s, function ($isGranted, $role) use (%s) { return $isGranted || $globalVariable->get(\'container\')->get(\'security.authorization_checker\')->isGranted($role); }, false)', $roles, TypeGenerator::USE_FOR_CLOSURES);
19-
20-
return $code;
16+
static function ($roles): string {
17+
return \sprintf('$globalVariable->get(\'security\')->hasAnyRole(%s)', $roles);
2118
},
22-
function ($_, $roles) use ($authorizationChecker): bool {
23-
return \array_reduce(
24-
$roles,
25-
function ($isGranted, $role) use ($authorizationChecker) {
26-
return $isGranted || $authorizationChecker->isGranted($role);
27-
},
28-
false
29-
);
19+
static function ($_, $roles) use ($security): bool {
20+
return $security->hasAnyRole($roles);
3021
}
3122
);
3223
}

src/ExpressionLanguage/ExpressionFunction/Security/HasPermission.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
99

1010
final class HasPermission extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'hasPermission',
16-
function ($object, $permission) {
17-
return "\$globalVariable->get('container')->get('security.authorization_checker')->isGranted($permission, $object)";
16+
static function ($object, $permission): string {
17+
return \sprintf('$globalVariable->get(\'security\')->hasPermission(%s, %s)', $object, $permission);
1818
},
19-
function ($_, $object, $permission) use ($authorizationChecker) {
20-
return $authorizationChecker->isGranted($permission, $object);
19+
static function ($_, $object, $permission) use ($security): bool {
20+
return $security->hasPermission($object, $permission);
2121
}
2222
);
2323
}

src/ExpressionLanguage/ExpressionFunction/Security/HasRole.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
99

1010
final class HasRole extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'hasRole',
16-
function ($role): string {
17-
return "\$globalVariable->get('container')->get('security.authorization_checker')->isGranted($role)";
16+
static function ($role): string {
17+
return \sprintf('$globalVariable->get(\'security\')->hasRole(%s)', $role);
1818
},
19-
function ($_, $role) use ($authorizationChecker): bool {
20-
return $authorizationChecker->isGranted($role);
19+
static function ($_, $role) use ($security): bool {
20+
return $security->hasRole($role);
2121
}
2222
);
2323
}

src/ExpressionLanguage/ExpressionFunction/Security/Helper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Overblog\GraphQLBundle\Definition\GlobalVariables;
88
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
99

10+
/**
11+
* @deprecated since 0.13 will be remove in 0.14
12+
* @codeCoverageIgnore
13+
*/
1014
final class Helper
1115
{
1216
/**

src/ExpressionLanguage/ExpressionFunction/Security/IsAnonymous.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
99

1010
final class IsAnonymous extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'isAnonymous',
16-
function () {
17-
return "\$globalVariable->get('container')->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')";
16+
static function (): string {
17+
return '$globalVariable->get(\'security\')->isAnonymous()';
1818
},
19-
function () use ($authorizationChecker) {
20-
return $authorizationChecker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY');
19+
static function () use ($security): bool {
20+
return $security->isAnonymous();
2121
}
2222
);
2323
}

src/ExpressionLanguage/ExpressionFunction/Security/IsAuthenticated.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
99

1010
final class IsAuthenticated extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'isAuthenticated',
16-
function () {
17-
return "(\$globalVariable->get('container')->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED') || \$globalVariable->get('container')->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY'))";
16+
static function (): string {
17+
return '$globalVariable->get(\'security\')->isAuthenticated()';
1818
},
19-
function () use ($authorizationChecker) {
20-
return $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED') || $authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY');
19+
static function () use ($security): bool {
20+
return $security->isAuthenticated();
2121
}
2222
);
2323
}

src/ExpressionLanguage/ExpressionFunction/Security/IsFullyAuthenticated.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
namespace Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security;
66

77
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
8-
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
8+
use Overblog\GraphQLBundle\Security\Security;
99

1010
final class IsFullyAuthenticated extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'isFullyAuthenticated',
16-
function () {
17-
return "\$globalVariable->get('container')->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')";
16+
static function (): string {
17+
return '$globalVariable->get(\'security\')->isFullyAuthenticated()';
1818
},
19-
function () use ($authorizationChecker) {
20-
return $authorizationChecker->isGranted('IS_AUTHENTICATED_FULLY');
19+
static function () use ($security): bool {
20+
return $security->isFullyAuthenticated();
2121
}
2222
);
2323
}

0 commit comments

Comments
 (0)