[task] Feature: use the resolution of center with person's center

history
This commit is contained in:
Julien Fastré 2022-09-27 16:09:09 +02:00
parent 451f7f4230
commit f39b0ee002
2 changed files with 231 additions and 12 deletions

View File

@ -13,6 +13,7 @@ namespace Chill\TaskBundle\Repository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\TaskBundle\Entity\SingleTask;
@ -31,14 +32,14 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
{
private AuthorizationHelperInterface $authorizationHelper;
private CenterResolverDispatcherInterface $centerResolverDispatcher;
private CenterResolverManagerInterface $centerResolverDispatcher;
private EntityManagerInterface $em;
private Security $security;
public function __construct(
CenterResolverDispatcherInterface $centerResolverDispatcher,
CenterResolverManagerInterface $centerResolverDispatcher,
EntityManagerInterface $em,
Security $security,
AuthorizationHelperInterface $authorizationHelper
@ -304,14 +305,18 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
QueryBuilder $qb,
$entity
): QueryBuilder {
$scopes = $this->authorizationHelper->getReachableScopes(
$this->security->getUser(),
TaskVoter::SHOW,
$this->centerResolverDispatcher->resolveCenter($entity)
);
foreach ($this->centerResolverDispatcher->resolveCenters($entity) as $center) {
$scopes = $this->authorizationHelper->getReachableScopes(
$this->security->getUser(),
TaskVoter::SHOW,
$center
);
return $qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
->setParameter('scopes', $scopes);
$qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
->setParameter('scopes', $scopes);
}
return $qb;
}
private function addACLGlobal(
@ -329,7 +334,10 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
$qb->leftJoin('t.person', 'person')
->leftJoin('t.course', 'course')
->leftJoin('course.participations', 'participation')
->leftJoin('participation.person', 'person_p');
->leftJoin('participation.person', 'person_p')
->leftJoin('person.centerCurrent', 'center_current_person')
->leftJoin('person_p.centerCurrent', 'center_current_participation')
;
$qb->distinct(true);
$k = 0;
@ -344,8 +352,8 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
$and = $qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->eq('person.center', ':center_' . $k),
$qb->expr()->eq('person_p.center', ':center_' . $k)
$qb->expr()->eq('center_current_person.center', ':center_' . $k),
$qb->expr()->eq('center_current_participation.center', ':center_' . $k)
),
$qb->expr()->in('t.circle', ':scopes_' . $k)
);

View File

@ -0,0 +1,211 @@
<?php
namespace Chill\TaskBundle\Tests\Repository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\CenterRepositoryInterface;
use Chill\MainBundle\Repository\ScopeRepository;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
use Chill\PersonBundle\DataFixtures\Helper\PersonRandomHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\TaskBundle\Repository\SingleTaskAclAwareRepository;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Doctrine\ORM\EntityManagerInterface;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
class SingleTaskACLAwareRepositoryTest extends KernelTestCase
{
private EntityManagerInterface $em;
private UserRepository $userRepository;
private AuthorizationHelperInterface $authorizationHelper;
private CenterRepositoryInterface $centerRepository;
private ScopeRepository $scopeRepository;
private PersonRepository $personRepository;
use PersonRandomHelper;
use ProphecyTrait;
protected function setUp(): void
{
self::bootKernel();
$this->em = self::$container->get(EntityManagerInterface::class);
$this->userRepository = self::$container->get(UserRepository::class);
$this->centerRepository = self::$container->get(CenterRepositoryInterface::class);
$this->scopeRepository = self::$container->get(ScopeRepository::class);
$this->personRepository = self::$container->get(PersonRepository::class);
}
public function testCountByPerson(): void
{
$centerA = $this->centerRepository->findOneBy(['name' => 'Center A']);
$user = new User();
$scopes = $this->scopeRepository->findAll();
$person = $this->getRandomPerson($this->em);
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$centerResolverDispatcher = $this->prophesize(CenterResolverManagerInterface::class);
$centerResolverDispatcher->resolveCenters(Argument::type(Person::class), Argument::any())
->willReturn([$centerA]);
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
$authorizationHelper->getReachableScopes(Argument::exact($user), Argument::exact(TaskVoter::SHOW), Argument::exact($centerA))
->willReturn($scopes);
$repository = new SingleTaskAclAwareRepository(
$centerResolverDispatcher->reveal(),
$this->em,
$security->reveal(),
$authorizationHelper->reveal()
);
$nb = $repository->countByPerson($person, null, []);
$this->assertGreaterThanOrEqual(0, $nb);
}
public function testFindByPerson(): void
{
$centerA = $this->centerRepository->findOneBy(['name' => 'Center A']);
$user = new User();
$scopes = $this->scopeRepository->findAll();
$person = $this->getRandomPerson($this->em);
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$centerResolverDispatcher = $this->prophesize(CenterResolverManagerInterface::class);
$centerResolverDispatcher->resolveCenters(Argument::type(Person::class), Argument::any())
->willReturn([$centerA]);
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
$authorizationHelper->getReachableScopes(Argument::exact($user), Argument::exact(TaskVoter::SHOW), Argument::exact($centerA))
->willReturn($scopes);
$repository = new SingleTaskAclAwareRepository(
$centerResolverDispatcher->reveal(),
$this->em,
$security->reveal(),
$authorizationHelper->reveal()
);
$tasks = $repository->findByPerson($person, null, []);
$this->assertGreaterThanOrEqual(0, count($tasks));
}
public function testFindByAllViewable(): void
{
$centerA = $this->centerRepository->findOneBy(['name' => 'Center A']);
$user = new User();
$scopes = $this->scopeRepository->findAll();
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$centerResolverDispatcher = $this->prophesize(CenterResolverManagerInterface::class);
$centerResolverDispatcher->resolveCenters(Argument::type(Person::class), Argument::any())
->willReturn([$centerA]);
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
$authorizationHelper->getReachableCenters(Argument::exact($user), Argument::exact(TaskVoter::SHOW))
->willReturn([$centerA]);
$authorizationHelper->getReachableScopes(Argument::exact($user), Argument::exact(TaskVoter::SHOW), Argument::exact($centerA))
->willReturn($scopes);
$repository = new SingleTaskAclAwareRepository(
$centerResolverDispatcher->reveal(),
$this->em,
$security->reveal(),
$authorizationHelper->reveal()
);
$tasks = $repository->findByAllViewable(null, []);
$this->assertGreaterThanOrEqual(0, count($tasks));
}
public function testCountByAllViewable(): void
{
$centerA = $this->centerRepository->findOneBy(['name' => 'Center A']);
$user = new User();
$scopes = $this->scopeRepository->findAll();
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$centerResolverDispatcher = $this->prophesize(CenterResolverManagerInterface::class);
$centerResolverDispatcher->resolveCenters(Argument::type(Person::class), Argument::any())
->willReturn([$centerA]);
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
$authorizationHelper->getReachableCenters(Argument::exact($user), Argument::exact(TaskVoter::SHOW))
->willReturn([$centerA]);
$authorizationHelper->getReachableScopes(Argument::exact($user), Argument::exact(TaskVoter::SHOW), Argument::exact($centerA))
->willReturn($scopes);
$repository = new SingleTaskAclAwareRepository(
$centerResolverDispatcher->reveal(),
$this->em,
$security->reveal(),
$authorizationHelper->reveal()
);
$nb = $repository->countByAllViewable(null, []);
$this->assertGreaterThanOrEqual(0, $nb);
}
public function testFindByCourse(): void
{
$centerA = $this->centerRepository->findOneBy(['name' => 'Center A']);
$user = new User();
$scopes = $this->scopeRepository->findAll();
/** @var Person $person */
$person = $this->em->createQuery(
'SELECT p FROM '.Person::class.' p JOIN p.centerCurrent cc
WHERE SIZE(p.accompanyingPeriodParticipations) > 0
AND cc.center = :center'
)
->setParameter('center', $centerA)
->setMaxResults(1)
->getSingleResult()
;
$period = $person->getAccompanyingPeriodParticipations()->first()->getAccompanyingPeriod();
$security = $this->prophesize(Security::class);
$security->getUser()->willReturn($user);
$centerResolverDispatcher = $this->prophesize(CenterResolverManagerInterface::class);
$centerResolverDispatcher->resolveCenters(Argument::type(AccompanyingPeriod::class), Argument::any())
->willReturn([$centerA]);
$authorizationHelper = $this->prophesize(AuthorizationHelperInterface::class);
$authorizationHelper->getReachableScopes(Argument::exact($user), Argument::exact(TaskVoter::SHOW), Argument::any())
->willReturn($scopes);
$repository = new SingleTaskAclAwareRepository(
$centerResolverDispatcher->reveal(),
$this->em,
$security->reveal(),
$authorizationHelper->reveal()
);
$tasks = $repository->findByCourse($period);
$this->assertGreaterThanOrEqual(0, count($tasks));
}
}