Skip to content

Commit 6bf4cbc

Browse files
authored
Merge 14fbf9b into 7756747
2 parents 7756747 + 14fbf9b commit 6bf4cbc

25 files changed

+222
-113
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,7 @@ parameters:
882882

883883
-
884884
message: "#^Call to method getMock\\(\\) on an unknown class PHPUnit_Framework_MockObject_MockBuilder\\.$#"
885-
count: 5
885+
count: 4
886886
path: tests/ExpressionLanguage/ExpressionFunction/Security/GetUserTest.php
887887

888888
-

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
}

src/ExpressionLanguage/ExpressionFunction/Security/IsGranted.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 IsGranted extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'isGranted',
16-
function ($attributes, $object = null) {
17-
return "\$globalVariable->get('container')->get('security.authorization_checker')->isGranted($attributes, $object)";
16+
static function ($attributes, $subject): string {
17+
return \sprintf('$globalVariable->get(\'security\')->isGranted(%s, %s)', $attributes, $subject);
1818
},
19-
function ($_, $attributes, $object = null) use ($authorizationChecker) {
20-
return $authorizationChecker->isGranted($attributes, $object);
19+
static function ($_, $attributes, $subject) use ($security): bool {
20+
return $security->isGranted($attributes, $subject);
2121
}
2222
);
2323
}

src/ExpressionLanguage/ExpressionFunction/Security/IsRememberMe.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 IsRememberMe extends ExpressionFunction
1111
{
12-
public function __construct(AuthorizationCheckerInterface $authorizationChecker)
12+
public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'isRememberMe',
16-
function () {
17-
return "\$globalVariable->get('container')->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')";
16+
static function (): string {
17+
return '$globalVariable->get(\'security\')->isRememberMe()';
1818
},
19-
function () use ($authorizationChecker) {
20-
return $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED');
19+
static function () use ($security): bool {
20+
return $security->isRememberMe();
2121
}
2222
);
2323
}

src/Resources/config/services.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,9 @@ services:
9898
Overblog\GraphQLBundle\Validator\Formatter:
9999
tags:
100100
- { name: kernel.event_listener, event: graphql.error_formatting, method: onErrorFormatting }
101+
102+
Overblog\GraphQLBundle\Security\Security:
103+
arguments:
104+
- '@?security.helper'
105+
tags:
106+
- { name: overblog_graphql.global_variable, alias: security, public: false }

src/Security/Security.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Security;
6+
7+
use Symfony\Component\Security\Core\Security as CoreSecurity;
8+
9+
final class Security
10+
{
11+
private $coreSecurity;
12+
13+
public function __construct(?CoreSecurity $security)
14+
{
15+
$this->coreSecurity = $security ?? new class() {
16+
public function isGranted(): void
17+
{
18+
throw new \LogicException('The "symfony/security-core" component is required.');
19+
}
20+
21+
public function getUser(): void
22+
{
23+
throw new \LogicException('The "symfony/security-core" component is required.');
24+
}
25+
};
26+
}
27+
28+
public function getUser()
29+
{
30+
return $this->coreSecurity->getUser();
31+
}
32+
33+
public function isGranted($attributes, $subject = null): bool
34+
{
35+
return $this->coreSecurity->isGranted($attributes, $subject);
36+
}
37+
38+
public function hasAnyPermission($object, array $permissions): bool
39+
{
40+
return \array_reduce(
41+
$permissions,
42+
function ($isGranted, $permission) use ($object) {
43+
return $isGranted || $this->isGranted($permission, $object);
44+
},
45+
false
46+
);
47+
}
48+
49+
public function hasAnyRole(array $roles): bool
50+
{
51+
return \array_reduce(
52+
$roles,
53+
function ($isGranted, $role) {
54+
return $isGranted || $this->isGranted($role);
55+
},
56+
false
57+
);
58+
}
59+
60+
public function hasPermission($object, $permission): bool
61+
{
62+
return $this->isGranted($permission, $object);
63+
}
64+
65+
public function hasRole($role): bool
66+
{
67+
return $this->isGranted($role);
68+
}
69+
70+
public function isAnonymous(): bool
71+
{
72+
return $this->isGranted('IS_AUTHENTICATED_ANONYMOUSLY');
73+
}
74+
75+
public function isAuthenticated(): bool
76+
{
77+
return $this->hasAnyRole(['IS_AUTHENTICATED_REMEMBERED', 'IS_AUTHENTICATED_FULLY']);
78+
}
79+
80+
public function isFullyAuthenticated(): bool
81+
{
82+
return $this->isGranted('IS_AUTHENTICATED_FULLY');
83+
}
84+
85+
public function isRememberMe(): bool
86+
{
87+
return $this->isGranted('IS_AUTHENTICATED_REMEMBERED');
88+
}
89+
}

0 commit comments

Comments
 (0)