Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[TwigBridge] Align isGrantedForUser on isGranted with falsy $field #59407

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 1 commit into from
Jan 10, 2025
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
10 changes: 9 additions & 1 deletion 10 src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function isGranted(mixed $role, mixed $object = null, ?string $field = nu
}

if (null !== $field) {
if (!class_exists(FieldVote::class)) {
throw new \LogicException('Passing a $field to the "is_granted()" function requires symfony/acl. Try running "composer require symfony/acl-bundle" if you need field-level access control.');
}

$object = new FieldVote($object, $field);
smnandre marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -57,7 +61,11 @@ public function isGrantedForUser(UserInterface $user, mixed $attribute, mixed $s
throw new \LogicException(\sprintf('An instance of "%s" must be provided to use "%s()".', UserAuthorizationCheckerInterface::class, __METHOD__));
}

if ($field) {
if (null !== $field) {
if (!class_exists(FieldVote::class)) {
throw new \LogicException('Passing a $field to the "is_granted_for_user()" function requires symfony/acl. Try running "composer require symfony/acl-bundle" if you need field-level access control.');
}

$subject = new FieldVote($subject, $field);
}

Expand Down
108 changes: 108 additions & 0 deletions 108 src/Symfony/Bridge/Twig/Tests/Extension/SecurityExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Twig\Tests\Extension;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClassExistsMock;
use Symfony\Bridge\Twig\Extension\SecurityExtension;
use Symfony\Component\Security\Acl\Voter\FieldVote;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Authorization\UserAuthorizationCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class SecurityExtensionTest extends TestCase
{
/**
* @dataProvider provideObjectFieldAclCases
*/
public function testIsGrantedCreatesFieldVoteObjectWhenFieldNotNull($object, $field, $expectedSubject)
{
$securityChecker = $this->createMock(AuthorizationCheckerInterface::class);
$securityChecker
->expects($this->once())
->method('isGranted')
->with('ROLE', $expectedSubject)
->willReturn(true);

$securityExtension = new SecurityExtension($securityChecker);
$this->assertTrue($securityExtension->isGranted('ROLE', $object, $field));
}

public function testIsGrantedThrowsWhenFieldNotNullAndFieldVoteClassDoesNotExist()
{
if (!class_exists(UserAuthorizationCheckerInterface::class)) {
$this->markTestSkipped('This test requires symfony/security-core 7.3 or superior.');
}

$securityChecker = $this->createMock(AuthorizationCheckerInterface::class);

ClassExistsMock::register(SecurityExtension::class);
ClassExistsMock::withMockedClasses([FieldVote::class => false]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('Passing a $field to the "is_granted()" function requires symfony/acl.');

$securityExtension = new SecurityExtension($securityChecker);
$securityExtension->isGranted('ROLE', 'object', 'bar');
}

/**
* @dataProvider provideObjectFieldAclCases
*/
public function testIsGrantedForUserCreatesFieldVoteObjectWhenFieldNotNull($object, $field, $expectedSubject)
{
if (!class_exists(UserAuthorizationCheckerInterface::class)) {
$this->markTestSkipped('This test requires symfony/security-core 7.3 or superior.');
}

$user = $this->createMock(UserInterface::class);
$userSecurityChecker = $this->createMock(UserAuthorizationCheckerInterface::class);
$userSecurityChecker
->expects($this->once())
->method('isGrantedForUser')
->with($user, 'ROLE', $expectedSubject)
->willReturn(true);

$securityExtension = new SecurityExtension(null, null, $userSecurityChecker);
$this->assertTrue($securityExtension->isGrantedForUser($user, 'ROLE', $object, $field));
}

public function testIsGrantedForUserThrowsWhenFieldNotNullAndFieldVoteClassDoesNotExist()
{
if (!class_exists(UserAuthorizationCheckerInterface::class)) {
$this->markTestSkipped('This test requires symfony/security-core 7.3 or superior.');
}

$securityChecker = $this->createMock(UserAuthorizationCheckerInterface::class);

ClassExistsMock::register(SecurityExtension::class);
ClassExistsMock::withMockedClasses([FieldVote::class => false]);

$this->expectException(\LogicException::class);
$this->expectExceptionMessageMatches('Passing a $field to the "is_granted_for_user()" function requires symfony/acl.');

$securityExtension = new SecurityExtension(null, null, $securityChecker);
$securityExtension->isGrantedForUser($this->createMock(UserInterface::class), 'object', 'bar');
}

public static function provideObjectFieldAclCases()
{
return [
[null, null, null],
['object', null, 'object'],
['object', false, new FieldVote('object', false)],
['object', 0, new FieldVote('object', 0)],
['object', '', new FieldVote('object', '')],
['object', 'field', new FieldVote('object', 'field')],
];
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.