mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-12 13:24:25 +00:00
[task] Feature: use the resolution of center with person's center
history
This commit is contained in:
parent
451f7f4230
commit
f39b0ee002
@ -13,6 +13,7 @@ namespace Chill\TaskBundle\Repository;
|
|||||||
|
|
||||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
|
||||||
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
|
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
|
||||||
|
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
|
||||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Chill\TaskBundle\Entity\SingleTask;
|
use Chill\TaskBundle\Entity\SingleTask;
|
||||||
@ -31,14 +32,14 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
|||||||
{
|
{
|
||||||
private AuthorizationHelperInterface $authorizationHelper;
|
private AuthorizationHelperInterface $authorizationHelper;
|
||||||
|
|
||||||
private CenterResolverDispatcherInterface $centerResolverDispatcher;
|
private CenterResolverManagerInterface $centerResolverDispatcher;
|
||||||
|
|
||||||
private EntityManagerInterface $em;
|
private EntityManagerInterface $em;
|
||||||
|
|
||||||
private Security $security;
|
private Security $security;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
CenterResolverDispatcherInterface $centerResolverDispatcher,
|
CenterResolverManagerInterface $centerResolverDispatcher,
|
||||||
EntityManagerInterface $em,
|
EntityManagerInterface $em,
|
||||||
Security $security,
|
Security $security,
|
||||||
AuthorizationHelperInterface $authorizationHelper
|
AuthorizationHelperInterface $authorizationHelper
|
||||||
@ -304,14 +305,18 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
|||||||
QueryBuilder $qb,
|
QueryBuilder $qb,
|
||||||
$entity
|
$entity
|
||||||
): QueryBuilder {
|
): QueryBuilder {
|
||||||
$scopes = $this->authorizationHelper->getReachableScopes(
|
foreach ($this->centerResolverDispatcher->resolveCenters($entity) as $center) {
|
||||||
$this->security->getUser(),
|
$scopes = $this->authorizationHelper->getReachableScopes(
|
||||||
TaskVoter::SHOW,
|
$this->security->getUser(),
|
||||||
$this->centerResolverDispatcher->resolveCenter($entity)
|
TaskVoter::SHOW,
|
||||||
);
|
$center
|
||||||
|
);
|
||||||
|
|
||||||
return $qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
|
$qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
|
||||||
->setParameter('scopes', $scopes);
|
->setParameter('scopes', $scopes);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $qb;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function addACLGlobal(
|
private function addACLGlobal(
|
||||||
@ -329,7 +334,10 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
|||||||
$qb->leftJoin('t.person', 'person')
|
$qb->leftJoin('t.person', 'person')
|
||||||
->leftJoin('t.course', 'course')
|
->leftJoin('t.course', 'course')
|
||||||
->leftJoin('course.participations', 'participation')
|
->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);
|
$qb->distinct(true);
|
||||||
|
|
||||||
$k = 0;
|
$k = 0;
|
||||||
@ -344,8 +352,8 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
|||||||
|
|
||||||
$and = $qb->expr()->andX(
|
$and = $qb->expr()->andX(
|
||||||
$qb->expr()->orX(
|
$qb->expr()->orX(
|
||||||
$qb->expr()->eq('person.center', ':center_' . $k),
|
$qb->expr()->eq('center_current_person.center', ':center_' . $k),
|
||||||
$qb->expr()->eq('person_p.center', ':center_' . $k)
|
$qb->expr()->eq('center_current_participation.center', ':center_' . $k)
|
||||||
),
|
),
|
||||||
$qb->expr()->in('t.circle', ':scopes_' . $k)
|
$qb->expr()->in('t.circle', ':scopes_' . $k)
|
||||||
);
|
);
|
||||||
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user