Skip to content

Commit

Permalink
add OwnedByInterface and support in security OwnVoter
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed May 24, 2019
1 parent 4d81503 commit d9df4c5
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Security/OwnedByInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php namespace Draw\DrawBundle\Security;

interface OwnedByInterface
{
/**
* @return null|OwnerInterface
*/
public function getOwnedBy();
}
2 changes: 1 addition & 1 deletion Security/OwnedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface OwnedInterface
/**
* Return if the object is owned by the possible owner
*
* @param OwnerInterface $owner
* @param OwnerInterface $possibleOwner
* @return boolean
*/
public function isOwnedBy(OwnerInterface $possibleOwner);
Expand Down
21 changes: 18 additions & 3 deletions Security/Voter/OwnVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Draw\DrawBundle\Security\Voter;

use Draw\DrawBundle\Security\OwnedByInterface;
use Draw\DrawBundle\Security\OwnedInterface;
use Draw\DrawBundle\Security\OwnerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
Expand All @@ -17,12 +19,14 @@ public function supportsClass($class)
{
$class = new \ReflectionClass($class);

return $class->implementsInterface('Draw\DrawBundle\Security\OwnedInterface');
return
$class->implementsInterface(OwnedInterface::class)
|| $class->implementsInterface(OwnedByInterface::class);
}

/**
* @param TokenInterface $token
* @param null|\Draw\DrawBundle\Security\OwnedInterface $object
* @param null|OwnedInterface|OwnedByInterface $object
* @param array $attributes
* @return int
*/
Expand All @@ -46,7 +50,18 @@ public function vote(TokenInterface $token, $object, array $attributes)
continue;
}

return $object->isOwnedBy($user) ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED;
if($object instanceof OwnedInterface) {
$object->isOwnedBy($user) ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED;
} elseif($object instanceof OwnedByInterface) {
$ownedBy = $object->getOwnedBy();
if(is_null($ownedBy)) {
continue;
}

return $ownedBy->getOwnerId() == $user->getOwnerId()
? VoterInterface::ACCESS_GRANTED
: VoterInterface::ACCESS_DENIED;
}
}

return VoterInterface::ACCESS_ABSTAIN;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
},
"require": {
"php": ">=5.3.3",
"php": ">=5.6",
"symfony/framework-bundle": ">=2.3"
},
"require-dev": {
Expand Down

0 comments on commit d9df4c5

Please sign in to comment.