Skip to content

Commit f307291

Browse files
author
Jeremiah VALERIE
committed
Fix isGranted expression function
1 parent fc4381c commit f307291

File tree

10 files changed

+50
-19
lines changed

10 files changed

+50
-19
lines changed

src/ExpressionLanguage/ExpressionFunction/Security/IsGranted.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public function __construct(Security $security)
1313
{
1414
parent::__construct(
1515
'isGranted',
16-
static function ($attributes, $subject): string {
17-
return \sprintf('$globalVariable->get(\'security\')->isGranted(%s, %s)', $attributes, $subject);
16+
static function ($attributes, $object = 'null'): string {
17+
return \sprintf('$globalVariable->get(\'security\')->isGranted(%s, %s)', $attributes, $object);
1818
},
19-
static function ($_, $attributes, $subject) use ($security): bool {
20-
return $security->isGranted($attributes, $subject);
19+
static function ($_, $attributes, $object = null) use ($security): bool {
20+
return $security->isGranted($attributes, $object);
2121
}
2222
);
2323
}

tests/ExpressionLanguage/ExpressionFunction/Security/HasAnyPermissionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function getFunctions()
1616
{
1717
$this->expectedObject = new \stdClass();
1818

19-
$security = parent::getSecurityIsGrantedWithExpectation(
19+
$security = $this->getSecurityIsGrantedWithExpectation(
2020
[
2121
$this->matchesRegularExpression('/^(OWNER|WRITER)$/'),
2222
$this->identicalTo($this->expectedObject),

tests/ExpressionLanguage/ExpressionFunction/Security/HasAnyRoleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class HasAnyRoleTest extends TestCase
1111
{
1212
protected function getFunctions()
1313
{
14-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
14+
$Security = $this->getSecurityIsGrantedWithExpectation(
1515
'ROLE_ADMIN',
1616
$this->any()
1717
);
1818

19-
return [new HasAnyRole($authorizationChecker)];
19+
return [new HasAnyRole($Security)];
2020
}
2121

2222
public function testEvaluator(): void

tests/ExpressionLanguage/ExpressionFunction/Security/HasPermissionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ protected function getFunctions()
1616
{
1717
$this->expectedObject = new \stdClass();
1818

19-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
19+
$Security = $this->getSecurityIsGrantedWithExpectation(
2020
[
2121
'OWNER',
2222
$this->identicalTo($this->expectedObject),
2323
],
2424
$this->any()
2525
);
2626

27-
return [new HasPermission($authorizationChecker)];
27+
return [new HasPermission($Security)];
2828
}
2929

3030
public function testEvaluator(): void

tests/ExpressionLanguage/ExpressionFunction/Security/HasRoleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class HasRoleTest extends TestCase
1111
{
1212
protected function getFunctions()
1313
{
14-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
14+
$Security = $this->getSecurityIsGrantedWithExpectation(
1515
'ROLE_USER',
1616
$this->any()
1717
);
1818

19-
return [new HasRole($authorizationChecker)];
19+
return [new HasRole($Security)];
2020
}
2121

2222
public function testEvaluator(): void

tests/ExpressionLanguage/ExpressionFunction/Security/IsAnonymousTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class IsAnonymousTest extends TestCase
1111
{
1212
protected function getFunctions()
1313
{
14-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
14+
$Security = $this->getSecurityIsGrantedWithExpectation(
1515
'IS_AUTHENTICATED_ANONYMOUSLY',
1616
$this->any()
1717
);
1818

19-
return [new IsAnonymous($authorizationChecker)];
19+
return [new IsAnonymous($Security)];
2020
}
2121

2222
public function testEvaluator(): void

tests/ExpressionLanguage/ExpressionFunction/Security/IsAuthenticatedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class IsAuthenticatedTest extends TestCase
1111
{
1212
protected function getFunctions()
1313
{
14-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
14+
$Security = $this->getSecurityIsGrantedWithExpectation(
1515
$this->matchesRegularExpression('/^IS_AUTHENTICATED_(REMEMBERED|FULLY)$/'),
1616
$this->any()
1717
);
1818

19-
return [new IsAuthenticated($authorizationChecker)];
19+
return [new IsAuthenticated($Security)];
2020
}
2121

2222
public function testEvaluator(): void

tests/ExpressionLanguage/ExpressionFunction/Security/IsFullyAuthenticatedTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class IsFullyAuthenticatedTest extends TestCase
1111
{
1212
protected function getFunctions()
1313
{
14-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
14+
$Security = $this->getSecurityIsGrantedWithExpectation(
1515
'IS_AUTHENTICATED_FULLY',
1616
$this->any()
1717
);
1818

19-
return [new IsFullyAuthenticated($authorizationChecker)];
19+
return [new IsFullyAuthenticated($Security)];
2020
}
2121

2222
public function testEvaluator(): void
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\ExpressionLanguage\ExpressionFunction\Security;
6+
7+
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction\Security\IsGranted;
8+
use Overblog\GraphQLBundle\Tests\ExpressionLanguage\TestCase;
9+
10+
class IsGrantedTest extends TestCase
11+
{
12+
protected function getFunctions()
13+
{
14+
$security = $this->getSecurityIsGrantedWithExpectation(
15+
$this->matchesRegularExpression('/^ROLE_(USER|ADMIN)$/'),
16+
$this->any()
17+
);
18+
19+
return [new IsGranted($security)];
20+
}
21+
22+
public function testEvaluator(): void
23+
{
24+
$this->assertTrue($this->expressionLanguage->evaluate('isGranted("ROLE_USER")'));
25+
}
26+
27+
public function testIsGranted(): void
28+
{
29+
$this->assertExpressionCompile('isGranted("ROLE_ADMIN")', 'ROLE_ADMIN');
30+
}
31+
}

tests/ExpressionLanguage/ExpressionFunction/Security/IsRememberMeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ class IsRememberMeTest extends TestCase
1111
{
1212
protected function getFunctions()
1313
{
14-
$authorizationChecker = parent::getSecurityIsGrantedWithExpectation(
14+
$Security = $this->getSecurityIsGrantedWithExpectation(
1515
'IS_AUTHENTICATED_REMEMBERED',
1616
$this->any()
1717
);
1818

19-
return [new IsRememberMe($authorizationChecker)];
19+
return [new IsRememberMe($Security)];
2020
}
2121

2222
public function testEvaluator(): void

0 commit comments

Comments
 (0)