This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from symfony-cmf/issue-45/default-voter
Added security configuration
- Loading branch information
Showing
14 changed files
with
280 additions
and
9 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="cmf_resource_rest.security.resource_path_voter" class="Symfony\Cmf\Bundle\ResourceRestBundle\Security\ResourcePathVoter" public="false"> | ||
<argument type="service" id="security.access.decision_manager" /> | ||
<argument>%cmf_resource_rest.security.access_map%</argument> | ||
|
||
<tag name="security.voter"/> | ||
</service> | ||
</services> | ||
</container> |
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,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2017 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Security; | ||
|
||
use Symfony\Cmf\Bundle\ResourceRestBundle\Controller\ResourceController; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
|
||
/** | ||
* @author Wouter de Jong <[email protected]> | ||
*/ | ||
class ResourcePathVoter extends Voter | ||
{ | ||
private $accessDecisionManager; | ||
private $accessMap; | ||
|
||
public function __construct(AccessDecisionManagerInterface $accessDecisionManager, array $accessMap) | ||
{ | ||
$this->accessDecisionManager = $accessDecisionManager; | ||
$this->accessMap = $accessMap; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function supports($attribute, $subject) | ||
{ | ||
return in_array($attribute, [ResourceController::ROLE_RESOURCE_READ, ResourceController::ROLE_RESOURCE_WRITE]) | ||
&& is_array($subject) && isset($subject['repository_name']) && isset($subject['path']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) | ||
{ | ||
foreach ($this->accessMap as $rule) { | ||
if (!$this->ruleMatches($rule, $attribute, $subject)) { | ||
continue; | ||
} | ||
|
||
if ($this->accessDecisionManager->decide($token, $rule['require'])) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function ruleMatches($rule, $attribute, $subject) | ||
{ | ||
if (!in_array($attribute, $rule['attributes'])) { | ||
return false; | ||
} | ||
|
||
if (null !== $rule['repository'] && $rule['repository'] !== $subject['repository_name']) { | ||
return false; | ||
} | ||
|
||
if (!preg_match('{'.$rule['pattern'].'}', $subject['path'])) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -1,9 +1,24 @@ | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:cmf_resource_rest="http://cmf.symfony.com/schema/dic/cmf_resource_rest" | ||
|
||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd | ||
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"> | ||
<cmf_resource_rest:config> | ||
<cmf_resource_rest:payload_alias name="article" type="Namespace\Article" repository="doctrine_phpcr_odm" /> | ||
</cmf_resource_rest:config> | ||
|
||
<config xmlns="http://cmf.symfony.com/schema/dic/cmf_resource_rest"> | ||
|
||
<payload-alias name="article" type="Namespace\Article" repository="doctrine_phpcr_odm" /> | ||
|
||
<security> | ||
<rule pattern="^/cms/public" require="IS_AUTHENTICATED_ANONYMOUSLY"> | ||
<attribute>CMF_RESOURCE_READ</attribute> | ||
</rule> | ||
|
||
<rule pattern="^/cms/members-only" attribute="CMF_RESOURCE_READ"> | ||
<require>ROLE_USER</require> | ||
</rule> | ||
|
||
<rule pattern="^/" attribute="CMF_RESOURCE_WRITE" require="ROLE_ADMIN" /> | ||
</security> | ||
</config> | ||
|
||
</container> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2017 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\ResourceRestBundle\Tests\Security; | ||
|
||
use Symfony\Cmf\Bundle\ResourceRestBundle\Security\ResourcePathVoter; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter as V; | ||
|
||
class ResourcePathVoterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
private $accessDecisionManager; | ||
|
||
protected function setUp() | ||
{ | ||
$this->accessDecisionManager = $this->prophesize(AccessDecisionManagerInterface::class); | ||
} | ||
|
||
/** | ||
* @dataProvider provideVoteData | ||
*/ | ||
public function testVote($rules, $subject, array $attributes, $result) | ||
{ | ||
$token = $this->prophesize(TokenInterface::class)->reveal(); | ||
|
||
$this->accessDecisionManager->decide($token, ['ROLE_USER'])->willReturn(true); | ||
$this->accessDecisionManager->decide($token, ['ROLE_ADMIN'])->willReturn(false); | ||
|
||
$voter = new ResourcePathVoter($this->accessDecisionManager->reveal(), $rules); | ||
|
||
$this->assertSame($result, $voter->vote($token, $subject, $attributes)); | ||
} | ||
|
||
public function provideVoteData() | ||
{ | ||
$ruleSet1 = [ | ||
$this->buildRule('^/', ['ROLE_USER'], ['CMF_RESOURCE_READ']), | ||
$this->buildRule('^/cms/private', ['ROLE_ADMIN'], ['CMF_RESOURCE_WRITE']), | ||
]; | ||
|
||
return [ | ||
// Basic behaviour | ||
[[$this->buildRule('^/')], $this->buildSubject('/cms/articles/foo'), ['CMF_RESOURCE_READ'], V::ACCESS_GRANTED], | ||
[[$this->buildRule('^/')], $this->buildSubject('/cms/articles/foo'), ['CMF_RESOURCE_WRITE'], V::ACCESS_GRANTED], | ||
[[$this->buildRule('^/', ['ROLE_ADMIN'])], $this->buildSubject('/cms/articles/foo'), ['CMF_RESOURCE_READ'], V::ACCESS_DENIED], | ||
|
||
// Multiple rules | ||
[$ruleSet1, $this->buildSubject('/cms/private/admin'), ['CMF_RESOURCE_READ'], V::ACCESS_GRANTED], | ||
[$ruleSet1, $this->buildSubject('/cms/private/admin'), ['CMF_RESOURCE_WRITE'], V::ACCESS_DENIED], | ||
[$ruleSet1, $this->buildSubject('/cms/public'), ['CMF_RESOURCE_READ', 'CMF_RESOURCE_WRITE'], V::ACCESS_GRANTED], | ||
|
||
// Unsupported attributes or subjects | ||
[[], $this->buildSubject('/cms/articles'), ['CMF_RESOURCE_READ'], V::ACCESS_DENIED], | ||
[[$this->buildRule('^/')], $this->buildSubject('/cms/articles'), ['ROLE_USER'], V::ACCESS_ABSTAIN], | ||
[[$this->buildRule('^/')], new \stdClass(), ['CMF_RESOURCE_READ'], V::ACCESS_ABSTAIN], | ||
|
||
// Repository name matching | ||
[[$this->buildRule('^/')], $this->buildSubject('/cms/articles', 'other_repo'), ['CMF_RESOURCE_READ'], V::ACCESS_DENIED], | ||
[[$this->buildRule('^/', ['ROLE_USER'], ['CMF_RESOURCE_READ'], 'other_repo')], $this->buildSubject('/cms/articles'), ['CMF_RESOURCE_READ'], V::ACCESS_DENIED], | ||
]; | ||
} | ||
|
||
private function buildRule($pattern, $require = ['ROLE_USER'], $attributes = ['CMF_RESOURCE_READ', 'CMF_RESOURCE_WRITE'], $repository = 'default') | ||
{ | ||
return ['pattern' => $pattern, 'attributes' => $attributes, 'require' => $require, 'repository' => $repository]; | ||
} | ||
|
||
private function buildSubject($path, $repository = 'default') | ||
{ | ||
return ['path' => $path, 'repository_name' => $repository]; | ||
} | ||
} |