-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #26660 [SecurityBundle] allow using custom function inside al…
…low_if expressions (dmaicher) This PR was merged into the 4.1-dev branch. Discussion ---------- [SecurityBundle] allow using custom function inside allow_if expressions | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | no | Fixed tickets | #23208 | License | MIT | Doc PR | symfony/symfony-docs#9552 This is a follow-up for #26263 As discussed there I propose to allow using custom functions as a new feature only and thus targeting `master` here. If we agree to move forward with this there are some todos: - [x] fix tests - [x] add cache warmer for allow_if expressions - [x] update documentation to mention this new feature - [x] update UPGRADE files ping @nicolas-grekas @stof Commits ------- 41552cd [SecurityBundle] allow using custom function inside allow_if expressions
- Loading branch information
Showing
8 changed files
with
176 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?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\Bundle\SecurityBundle\CacheWarmer; | ||
|
||
use Symfony\Component\ExpressionLanguage\Expression; | ||
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; | ||
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage; | ||
|
||
class ExpressionCacheWarmer implements CacheWarmerInterface | ||
{ | ||
private $expressions; | ||
private $expressionLanguage; | ||
|
||
/** | ||
* @param iterable|Expression[] $expressions | ||
*/ | ||
public function __construct(iterable $expressions, ExpressionLanguage $expressionLanguage) | ||
{ | ||
$this->expressions = $expressions; | ||
$this->expressionLanguage = $expressionLanguage; | ||
} | ||
|
||
public function isOptional() | ||
{ | ||
return true; | ||
} | ||
|
||
public function warmUp($cacheDir) | ||
{ | ||
foreach ($this->expressions as $expression) { | ||
$this->expressionLanguage->parse($expression, array('token', 'user', 'object', 'subject', 'roles', 'request', 'trust_resolver')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Symfony/Bundle/SecurityBundle/Tests/CacheWarmer/ExpressionCacheWarmerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?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\Bundle\SecurityBundle\Tests\CacheWarmer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\SecurityBundle\CacheWarmer\ExpressionCacheWarmer; | ||
use Symfony\Component\ExpressionLanguage\Expression; | ||
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage; | ||
|
||
class ExpressionCacheWarmerTest extends TestCase | ||
{ | ||
public function testWarmUp() | ||
{ | ||
$expressions = array(new Expression('A'), new Expression('B')); | ||
|
||
$expressionLang = $this->createMock(ExpressionLanguage::class); | ||
$expressionLang->expects($this->exactly(2)) | ||
->method('parse') | ||
->withConsecutive( | ||
array($expressions[0], array('token', 'user', 'object', 'subject', 'roles', 'request', 'trust_resolver')), | ||
array($expressions[1], array('token', 'user', 'object', 'subject', 'roles', 'request', 'trust_resolver')) | ||
); | ||
|
||
(new ExpressionCacheWarmer($expressions, $expressionLang))->warmUp(''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters