parent
a8c2f6840f
commit
8ae7ab69b7
@ -0,0 +1,52 @@ |
||||
<?php |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Chamilo\CoreBundle\DataProvider\Extension; |
||||
|
||||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
||||
//use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; |
||||
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
||||
use Chamilo\CoreBundle\Entity\Message; |
||||
use Chamilo\CoreBundle\Entity\MessageTag; |
||||
use Doctrine\ORM\QueryBuilder; |
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
||||
use Symfony\Component\Security\Core\Security; |
||||
|
||||
final class MessageTagExtension implements QueryCollectionExtensionInterface //, QueryItemExtensionInterface |
||||
{ |
||||
private Security $security; |
||||
|
||||
public function __construct(Security $security) |
||||
{ |
||||
$this->security = $security; |
||||
} |
||||
|
||||
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void |
||||
{ |
||||
$this->addWhere($queryBuilder, $resourceClass); |
||||
} |
||||
|
||||
/*public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void |
||||
{ |
||||
//error_log('applyToItem'); |
||||
//$this->addWhere($queryBuilder, $resourceClass); |
||||
}*/ |
||||
|
||||
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void |
||||
{ |
||||
if (MessageTag::class !== $resourceClass) { |
||||
return; |
||||
} |
||||
|
||||
$user = $this->security->getUser(); |
||||
$alias = $queryBuilder->getRootAliases()[0]; |
||||
|
||||
$queryBuilder->andWhere(" $alias.user = :current "); |
||||
$queryBuilder->setParameters([ |
||||
'current' => $user, |
||||
]); |
||||
} |
||||
} |
@ -0,0 +1,84 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Security\Authorization\Voter; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Message; |
||||
use Chamilo\CoreBundle\Entity\MessageTag; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Doctrine\ORM\EntityManagerInterface; |
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
||||
use Symfony\Component\Security\Core\Security; |
||||
use Symfony\Component\Security\Core\User\UserInterface; |
||||
|
||||
class MessageTagVoter extends Voter |
||||
{ |
||||
public const CREATE = 'CREATE'; |
||||
public const VIEW = 'VIEW'; |
||||
public const EDIT = 'EDIT'; |
||||
public const DELETE = 'DELETE'; |
||||
|
||||
private EntityManagerInterface $entityManager; |
||||
private Security $security; |
||||
|
||||
public function __construct( |
||||
EntityManagerInterface $entityManager, |
||||
Security $security |
||||
) { |
||||
$this->entityManager = $entityManager; |
||||
$this->security = $security; |
||||
} |
||||
|
||||
protected function supports(string $attribute, $subject): bool |
||||
{ |
||||
$options = [ |
||||
self::CREATE, |
||||
self::VIEW, |
||||
self::EDIT, |
||||
self::DELETE, |
||||
]; |
||||
|
||||
// if the attribute isn't one we support, return false |
||||
if (!\in_array($attribute, $options, true)) { |
||||
return false; |
||||
} |
||||
|
||||
// only vote on Post objects inside this voter |
||||
return $subject instanceof MessageTag; |
||||
} |
||||
|
||||
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool |
||||
{ |
||||
/** @var User $user */ |
||||
$user = $token->getUser(); |
||||
|
||||
if (!$user instanceof UserInterface) { |
||||
return false; |
||||
} |
||||
|
||||
if ($this->security->isGranted('ROLE_ADMIN')) { |
||||
return true; |
||||
} |
||||
|
||||
/** @var MessageTag $message */ |
||||
$message = $subject; |
||||
|
||||
switch ($attribute) { |
||||
case self::CREATE: |
||||
case self::VIEW: |
||||
case self::EDIT: |
||||
case self::DELETE: |
||||
if ($message->getUser() === $user) { |
||||
return true; |
||||
} |
||||
|
||||
break; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
} |
Loading…
Reference in new issue