Skip to content

Commit

Permalink
Merge pull request #224 from symfony-cmf/bug/non-object-to-voter
Browse files Browse the repository at this point in the history
[Voters] Account for subject to not be an object
  • Loading branch information
wouterj authored Jan 29, 2017
2 parents f45a637 + c9ff106 commit 2c4018d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ cache:

env:
matrix: SYMFONY_VERSION=2.8.*
global: SYMFONY_DEPRECATIONS_HELPER=7
global: SYMFONY_DEPRECATIONS_HELPER=weak

matrix:
include:
Expand Down
10 changes: 5 additions & 5 deletions PublishWorkflow/Voter/PublishTimePeriodVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ public function supportsClass($class)
/**
* {@inheritdoc}
*
* @param PublishTimePeriodReadInterface $object
* @param PublishTimePeriodReadInterface $subject
*/
public function vote(TokenInterface $token, $object, array $attributes)
public function vote(TokenInterface $token, $subject, array $attributes)
{
if (!$this->supportsClass(get_class($object))) {
if (!is_object($subject) || !$this->supportsClass(get_class($subject))) {
return self::ACCESS_ABSTAIN;
}

$startDate = $object->getPublishStartDate();
$endDate = $object->getPublishEndDate();
$startDate = $subject->getPublishStartDate();
$endDate = $subject->getPublishEndDate();

$decision = self::ACCESS_GRANTED;
foreach ($attributes as $attribute) {
Expand Down
8 changes: 4 additions & 4 deletions PublishWorkflow/Voter/PublishableVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public function supportsClass($class)
/**
* {@inheritdoc}
*
* @param PublishableReadInterface $object
* @param PublishableReadInterface $subject
*/
public function vote(TokenInterface $token, $object, array $attributes)
public function vote(TokenInterface $token, $subject, array $attributes)
{
if (!$this->supportsClass(get_class($object))) {
if (!is_object($subject) || !$this->supportsClass(get_class($subject))) {
return self::ACCESS_ABSTAIN;
}

Expand All @@ -61,7 +61,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
$decision = self::ACCESS_ABSTAIN;
continue;
}
if (!$object->isPublishable()) {
if (!$subject->isPublishable()) {
return self::ACCESS_DENIED;
}
}
Expand Down
8 changes: 4 additions & 4 deletions Security/Authorization/Voter/PublishedVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public function supportsClass($class)
/**
* {@inheritdoc}
*
* @param object $object
* @param object $subject
*/
public function vote(TokenInterface $token, $object, array $attributes)
public function vote(TokenInterface $token, $subject, array $attributes)
{
if (!$this->supportsClass(get_class($object))) {
if (!is_object($subject) || !$this->supportsClass(get_class($subject))) {
return self::ACCESS_ABSTAIN;
}
foreach ($attributes as $attribute) {
Expand All @@ -70,7 +70,7 @@ public function vote(TokenInterface $token, $object, array $attributes)
}
}

if ($this->publishWorkflowChecker->isGranted($attributes, $object)) {
if ($this->publishWorkflowChecker->isGranted($attributes, $subject)) {
return self::ACCESS_GRANTED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,10 @@ public function testUnsupportedClass()
);
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $result);
}

public function testNonClassSubject()
{
$result = $this->voter->vote($this->token, array(1, 2, 3), array(PublishWorkflowChecker::VIEW_ATTRIBUTE));
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $result);
}
}
6 changes: 6 additions & 0 deletions Tests/Unit/PublishWorkflow/Voter/PublishableVoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,10 @@ public function testUnsupportedClass()
);
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $result);
}

public function testNonClassSubject()
{
$result = $this->voter->vote($this->token, array(1, 2, 3), array(PublishWorkflowChecker::VIEW_ATTRIBUTE));
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $result);
}
}

0 comments on commit 2c4018d

Please sign in to comment.