Skip to content

Commit 4419a7c

Browse files
authored
Update security voter (#278)
* make published voter compatible with new voter abstract * change the security voter to the published voter because you should only be allowed access when the workflow checker determines you should be allowed access and not only when the document isPublishable returns true. because then documents that are only publised after a period would slip through
1 parent 4676e05 commit 4419a7c

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

src/Resources/config/publish-workflow.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535

3636
<!-- integration with core security service -->
3737

38-
<service id="cmf_core.security.published_voter" class="Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\Voter\PublishableVoter" public="false">
38+
<service id="cmf_core.security.published_voter" class="Symfony\Cmf\Bundle\CoreBundle\Security\Authorization\Voter\PublishedVoter" public="false">
39+
<argument type="service" id="cmf_core.publish_workflow.checker"/>
3940
<tag name="security.voter"/>
4041
</service>
4142

src/Security/Authorization/Voter/PublishedVoter.php

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,20 @@
1111

1212
namespace Symfony\Cmf\Bundle\CoreBundle\Security\Authorization\Voter;
1313

14+
use function is_subclass_of;
15+
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishableReadInterface;
16+
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishTimePeriodReadInterface;
1417
use Symfony\Cmf\Bundle\CoreBundle\PublishWorkflow\PublishWorkflowChecker;
1518
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
16-
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
19+
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
1720

1821
/**
1922
* This is a security voter registered with the Symfony security system that
2023
* brings the publish workflow into standard Symfony security.
2124
*
2225
* @author David Buchmann <[email protected]>
2326
*/
24-
class PublishedVoter implements VoterInterface
27+
class PublishedVoter extends Voter
2528
{
2629
/**
2730
* @var PublishWorkflowChecker
@@ -36,41 +39,27 @@ public function __construct(PublishWorkflowChecker $publishWorkflowChecker)
3639
/**
3740
* {@inheritdoc}
3841
*/
39-
public function supportsAttribute($attribute)
42+
public function supportsAttribute($attribute): bool
4043
{
4144
return PublishWorkflowChecker::VIEW_ATTRIBUTE === $attribute
4245
|| PublishWorkflowChecker::VIEW_ANONYMOUS_ATTRIBUTE === $attribute
4346
;
4447
}
4548

46-
/**
47-
* {@inheritdoc}
48-
*/
49-
public function supportsClass($class)
49+
public function supportsType(string $subjectType): bool
5050
{
51-
return $this->publishWorkflowChecker->supportsClass($class);
51+
return is_subclass_of($subjectType, PublishableReadInterface::class)
52+
|| is_subclass_of($subjectType, PublishTimePeriodReadInterface::class);
5253
}
5354

54-
/**
55-
* {@inheritdoc}
56-
*
57-
* @param object $subject
58-
*/
59-
public function vote(TokenInterface $token, $subject, array $attributes)
55+
protected function supports($attribute, $subject)
6056
{
61-
if (!\is_object($subject) || !$this->supportsClass(\get_class($subject))) {
62-
return self::ACCESS_ABSTAIN;
63-
}
64-
foreach ($attributes as $attribute) {
65-
if (!$this->supportsAttribute($attribute)) {
66-
return self::ACCESS_ABSTAIN;
67-
}
68-
}
69-
70-
if ($this->publishWorkflowChecker->isGranted($attributes, $subject)) {
71-
return self::ACCESS_GRANTED;
72-
}
57+
return \is_object($subject) && $this->supportsType(\get_class($subject))
58+
&& $this->supportsAttribute($attribute);
59+
}
7360

74-
return self::ACCESS_DENIED;
61+
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
62+
{
63+
return $this->publishWorkflowChecker->isGranted($attribute, $subject);
7564
}
7665
}

0 commit comments

Comments
 (0)