mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
Merge branch 'issue719_filter_activities_version_2' into 'master'
Filter the list of activities See merge request Chill-Projet/chill-bundles!563
This commit is contained in:
@@ -18,67 +18,193 @@ use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
|
||||
use Chill\MainBundle\Entity\Location;
|
||||
use Chill\MainBundle\Entity\LocationType;
|
||||
use Chill\MainBundle\Entity\Scope;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcherInterface;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Entity\UserJob;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelperForCurrentUserInterface;
|
||||
use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\AbstractQuery;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\NonUniqueResultException;
|
||||
use Doctrine\ORM\NoResultException;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Doctrine\ORM\Query\ResultSetMappingBuilder;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
use function count;
|
||||
use function in_array;
|
||||
|
||||
final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInterface
|
||||
final readonly class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInterface
|
||||
{
|
||||
private AuthorizationHelper $authorizationHelper;
|
||||
|
||||
private CenterResolverDispatcherInterface $centerResolverDispatcher;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private ActivityRepository $repository;
|
||||
|
||||
private Security $security;
|
||||
|
||||
private TokenStorageInterface $tokenStorage;
|
||||
|
||||
public function __construct(
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
CenterResolverDispatcherInterface $centerResolverDispatcher,
|
||||
TokenStorageInterface $tokenStorage,
|
||||
ActivityRepository $repository,
|
||||
EntityManagerInterface $em,
|
||||
Security $security
|
||||
private AuthorizationHelperForCurrentUserInterface $authorizationHelper,
|
||||
private CenterResolverManagerInterface $centerResolverManager,
|
||||
private ActivityRepository $repository,
|
||||
private EntityManagerInterface $em,
|
||||
private Security $security,
|
||||
private RequestStack $requestStack,
|
||||
) {
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->centerResolverDispatcher = $centerResolverDispatcher;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->repository = $repository;
|
||||
$this->em = $em;
|
||||
$this->security = $security;
|
||||
}
|
||||
|
||||
public function findByAccompanyingPeriod(AccompanyingPeriod $period, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = []): array
|
||||
/**
|
||||
* @throws NonUniqueResultException
|
||||
* @throws NoResultException
|
||||
*/
|
||||
public function countByAccompanyingPeriod(AccompanyingPeriod $period, string $role, array $filters = []): int
|
||||
{
|
||||
$user = $this->security->getUser();
|
||||
$center = $this->centerResolverDispatcher->resolveCenter($period);
|
||||
$qb = $this->buildBaseQuery($filters);
|
||||
|
||||
if (0 === count($orderBy)) {
|
||||
$orderBy = ['date' => 'DESC'];
|
||||
$qb
|
||||
->select('COUNT(a)')
|
||||
->andWhere('a.accompanyingPeriod = :period')->setParameter('period', $period);
|
||||
|
||||
return $qb->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function countByPerson(Person $person, string $role, array $filters = []): int
|
||||
{
|
||||
$qb = $this->buildBaseQuery($filters);
|
||||
|
||||
$qb = $this->filterBaseQueryByPerson($qb, $person, $role);
|
||||
|
||||
$qb->select('COUNT(a)');
|
||||
|
||||
return $qb->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
|
||||
public function findByAccompanyingPeriod(AccompanyingPeriod $period, string $role, ?int $start = 0, ?int $limit = 1000, array $orderBy = ['date' => 'DESC'], array $filters = []): array
|
||||
{
|
||||
$qb = $this->buildBaseQuery($filters);
|
||||
|
||||
$qb->andWhere('a.accompanyingPeriod = :period')->setParameter('period', $period);
|
||||
|
||||
foreach ($orderBy as $field => $order) {
|
||||
$qb->addOrderBy('a.' . $field, $order);
|
||||
}
|
||||
|
||||
$scopes = $this->authorizationHelper
|
||||
->getReachableCircles($user, $role, $center);
|
||||
if (null !== $start) {
|
||||
$qb->setFirstResult($start);
|
||||
}
|
||||
if (null !== $limit) {
|
||||
$qb->setMaxResults($limit);
|
||||
}
|
||||
|
||||
return $this->em->getRepository(Activity::class)
|
||||
->findByAccompanyingPeriod($period, $scopes, true, $limit, $start, $orderBy);
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function buildBaseQuery(array $filters): QueryBuilder
|
||||
{
|
||||
$qb = $this->repository
|
||||
->createQueryBuilder('a')
|
||||
;
|
||||
|
||||
if (($filters['my_activities'] ?? false) and ($user = $this->security->getUser()) instanceof User) {
|
||||
$qb->andWhere(
|
||||
$qb->expr()->orX(
|
||||
'a.createdBy = :user',
|
||||
'a.user = :user',
|
||||
':user MEMBER OF a.users'
|
||||
)
|
||||
)->setParameter('user', $user);
|
||||
}
|
||||
|
||||
if ([] !== ($types = $filters['types'] ?? [])) {
|
||||
$qb->andWhere('a.activityType IN (:types)')->setParameter('types', $types);
|
||||
}
|
||||
|
||||
if ([] !== ($jobs = $filters['jobs'] ?? [])) {
|
||||
$qb
|
||||
->leftJoin('a.createdBy', 'creator')
|
||||
->leftJoin('a.user', 'activity_u')
|
||||
->andWhere(
|
||||
$qb->expr()->orX(
|
||||
'creator.userJob IN (:jobs)',
|
||||
'activity_u.userJob IN (:jobs)',
|
||||
'EXISTS (SELECT 1 FROM ' . User::class . ' activity_user WHERE activity_user MEMBER OF a.users AND activity_user.userJob IN (:jobs))'
|
||||
)
|
||||
)
|
||||
->setParameter('jobs', $jobs);
|
||||
}
|
||||
|
||||
if (null !== ($after = $filters['after'] ?? null)) {
|
||||
$qb->andWhere('a.date >= :after')->setParameter('after', $after);
|
||||
}
|
||||
|
||||
if (null !== ($before = $filters['before'] ?? null)) {
|
||||
$qb->andWhere('a.date <= :before')->setParameter('before', $before);
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AccompanyingPeriod|Person $associated
|
||||
* @return array<ActivityType>
|
||||
*/
|
||||
public function findActivityTypeByAssociated(AccompanyingPeriod|Person $associated): array
|
||||
{
|
||||
$in = $this->em->createQueryBuilder();
|
||||
$in
|
||||
->select('1')
|
||||
->from(Activity::class, 'a');
|
||||
|
||||
if ($associated instanceof Person) {
|
||||
$in = $this->filterBaseQueryByPerson($in, $associated, ActivityVoter::SEE);
|
||||
} else {
|
||||
$in->andWhere('a.accompanyingPeriod = :period')->setParameter('period', $associated);
|
||||
}
|
||||
|
||||
// join between the embedded exist query and the main query
|
||||
$in->andWhere('a.activityType = t');
|
||||
|
||||
$qb = $this->em->createQueryBuilder()->setParameters($in->getParameters());
|
||||
$qb
|
||||
->select('t')
|
||||
->from(ActivityType::class, 't')
|
||||
->where(
|
||||
$qb->expr()->exists($in->getDQL())
|
||||
);
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
public function findUserJobByAssociated(Person|AccompanyingPeriod $associated): array
|
||||
{
|
||||
$in = $this->em->createQueryBuilder();
|
||||
$in->select('IDENTITY(u.userJob)')
|
||||
->from(User::class, 'u')
|
||||
->join(
|
||||
Activity::class,
|
||||
'a',
|
||||
Join::WITH,
|
||||
'a.createdBy = u OR a.user = u OR u MEMBER OF a.users'
|
||||
);
|
||||
|
||||
if ($associated instanceof Person) {
|
||||
$in = $this->filterBaseQueryByPerson($in, $associated, ActivityVoter::SEE);
|
||||
} else {
|
||||
$in->andWhere('a.accompanyingPeriod = :associated');
|
||||
$in->setParameter('associated', $associated);
|
||||
}
|
||||
|
||||
$qb = $this->em->createQueryBuilder()->setParameters($in->getParameters());
|
||||
|
||||
$qb->select('ub', 'JSON_EXTRACT(ub.label, :lang) AS HIDDEN lang')
|
||||
->from(UserJob::class, 'ub')
|
||||
->where($qb->expr()->in('ub.id', $in->getDQL()))
|
||||
->setParameter('lang', $this->requestStack->getCurrentRequest()->getLocale())
|
||||
->orderBy('lang')
|
||||
;
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
|
||||
public function findByAccompanyingPeriodSimplified(AccompanyingPeriod $period, ?int $limit = 1000): array
|
||||
{
|
||||
$rsm = new ResultSetMappingBuilder($this->em);
|
||||
@@ -159,25 +285,73 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte
|
||||
return $nq->getResult(AbstractQuery::HYDRATE_ARRAY);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $orderBy
|
||||
*
|
||||
* @return Activity[]|array
|
||||
*/
|
||||
public function findByPerson(Person $person, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = []): array
|
||||
public function findByPerson(Person $person, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = [], array $filters = []): array
|
||||
{
|
||||
$user = $this->security->getUser();
|
||||
$center = $this->centerResolverDispatcher->resolveCenter($person);
|
||||
$qb = $this->buildBaseQuery($filters);
|
||||
|
||||
if (0 === count($orderBy)) {
|
||||
$orderBy = ['date' => 'DESC'];
|
||||
$qb = $this->filterBaseQueryByPerson($qb, $person, $role);
|
||||
|
||||
foreach ($orderBy as $field => $direction) {
|
||||
$qb->addOrderBy('a.' . $field, $direction);
|
||||
}
|
||||
|
||||
$reachableScopes = $this->authorizationHelper
|
||||
->getReachableCircles($user, $role, $center);
|
||||
if (null !== $start) {
|
||||
$qb->setFirstResult($start);
|
||||
}
|
||||
if (null !== $limit) {
|
||||
$qb->setMaxResults($limit);
|
||||
}
|
||||
|
||||
return $this->em->getRepository(Activity::class)
|
||||
->findByPersonImplied($person, $reachableScopes, $orderBy, $limit, $start);
|
||||
return $qb->getQuery()->getResult();
|
||||
}
|
||||
|
||||
private function filterBaseQueryByPerson(QueryBuilder $qb, Person $person, string $role): QueryBuilder
|
||||
{
|
||||
$orX = $qb->expr()->orX();
|
||||
$counter = 0;
|
||||
foreach ($this->centerResolverManager->resolveCenters($person) as $center) {
|
||||
$scopes = $this->authorizationHelper->getReachableScopes($role, $center);
|
||||
|
||||
if ([] === $scopes) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$orX->add(sprintf('a.person = :person AND a.scope IN (:scopes_%d)', $counter));
|
||||
$qb->setParameter(sprintf('scopes_%d', $counter), $scopes);
|
||||
$qb->setParameter('person', $person);
|
||||
$counter++;
|
||||
}
|
||||
|
||||
foreach ($person->getAccompanyingPeriodParticipations() as $participation) {
|
||||
if (!$this->security->isGranted(ActivityVoter::SEE, $participation->getAccompanyingPeriod())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$and = $qb->expr()->andX(
|
||||
sprintf('a.accompanyingPeriod = :period_%d', $counter),
|
||||
sprintf('a.date >= :participation_start_%d', $counter)
|
||||
);
|
||||
|
||||
$qb
|
||||
->setParameter(sprintf('period_%d', $counter), $participation->getAccompanyingPeriod())
|
||||
->setParameter(sprintf('participation_start_%d', $counter), $participation->getStartDate());
|
||||
|
||||
if (null !== $participation->getEndDate()) {
|
||||
$and->add(sprintf('a.date < :participation_end_%d', $counter));
|
||||
$qb
|
||||
->setParameter(sprintf('participation_end_%d', $counter), $participation->getEndDate());
|
||||
}
|
||||
$orX->add($and);
|
||||
$counter++;
|
||||
}
|
||||
|
||||
if (0 === $orX->count()) {
|
||||
$qb->andWhere('FALSE = TRUE');
|
||||
} else {
|
||||
$qb->andWhere($orX);
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function queryTimelineIndexer(string $context, array $args = []): array
|
||||
@@ -226,7 +400,6 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte
|
||||
|
||||
// acls:
|
||||
$reachableCenters = $this->authorizationHelper->getReachableCenters(
|
||||
$this->tokenStorage->getToken()->getUser(),
|
||||
ActivityVoter::SEE
|
||||
);
|
||||
|
||||
@@ -251,7 +424,7 @@ final class ActivityACLAwareRepository implements ActivityACLAwareRepositoryInte
|
||||
continue;
|
||||
}
|
||||
// we get all the reachable scopes for this center
|
||||
$reachableScopes = $this->authorizationHelper->getReachableScopes($this->tokenStorage->getToken()->getUser(), ActivityVoter::SEE, $center);
|
||||
$reachableScopes = $this->authorizationHelper->getReachableScopes(ActivityVoter::SEE, $center);
|
||||
// we get the ids for those scopes
|
||||
$reachablesScopesId = array_map(
|
||||
static fn (Scope $scope) => $scope->getId(),
|
||||
|
@@ -11,15 +11,32 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\ActivityBundle\Repository;
|
||||
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Chill\ActivityBundle\Entity\ActivityType;
|
||||
use Chill\MainBundle\Entity\UserJob;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
interface ActivityACLAwareRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* @return Activity[]|array
|
||||
* Return all the activities associated to an accompanying period and that the user is allowed to apply the given role.
|
||||
*
|
||||
*
|
||||
* @param array{my_activities?: bool, types?: array<ActivityType>, jobs?: array<UserJob>, after?: \DateTimeImmutable|null, before?: \DateTimeImmutable|null} $filters
|
||||
* @return array<Activity>
|
||||
*/
|
||||
public function findByAccompanyingPeriod(AccompanyingPeriod $period, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = []): array;
|
||||
public function findByAccompanyingPeriod(AccompanyingPeriod $period, string $role, ?int $start = 0, ?int $limit = 1000, array $orderBy = ['date' => 'DESC'], array $filters = []): array;
|
||||
|
||||
/**
|
||||
* @param array{my_activities?: bool, types?: array<ActivityType>, jobs?: array<UserJob>, after?: \DateTimeImmutable|null, before?: \DateTimeImmutable|null} $filters
|
||||
*/
|
||||
public function countByAccompanyingPeriod(AccompanyingPeriod $period, string $role, array $filters = []): int;
|
||||
|
||||
/**
|
||||
* @param array{my_activities?: bool, types?: array<ActivityType>, jobs?: array<UserJob>, after?: \DateTimeImmutable|null, before?: \DateTimeImmutable|null} $filters
|
||||
*/
|
||||
public function countByPerson(Person $person, string $role, array $filters = []): int;
|
||||
|
||||
/**
|
||||
* Return a list of activities, simplified as array (not object).
|
||||
@@ -31,7 +48,28 @@ interface ActivityACLAwareRepositoryInterface
|
||||
public function findByAccompanyingPeriodSimplified(AccompanyingPeriod $period, ?int $limit = 1000): array;
|
||||
|
||||
/**
|
||||
* @return Activity[]|array
|
||||
* @param array{my_activities?: bool, types?: array<ActivityType>, jobs?: array<UserJob>, after?: \DateTimeImmutable|null, before?: \DateTimeImmutable|null} $filters
|
||||
* @return array<Activity>
|
||||
*/
|
||||
public function findByPerson(Person $person, string $role, ?int $start = 0, ?int $limit = 1000, ?array $orderBy = []): array;
|
||||
public function findByPerson(Person $person, string $role, ?int $start = 0, ?int $limit = 1000, array $orderBy = ['date' => 'DESC'], array $filters = []): array;
|
||||
|
||||
|
||||
/**
|
||||
* Return a list of the type for the activities associated to person or accompanying period
|
||||
*
|
||||
* @return array<ActivityType>
|
||||
*/
|
||||
public function findActivityTypeByAssociated(AccompanyingPeriod|Person $associated): array;
|
||||
|
||||
/**
|
||||
* Return a list of the user job for the activities associated to person or accompanying period
|
||||
*
|
||||
* Associated mean the job:
|
||||
* - of the creator;
|
||||
* - of the user (activity.user)
|
||||
* - of all the users
|
||||
*
|
||||
* @return array<UserJob>
|
||||
*/
|
||||
public function findUserJobByAssociated(AccompanyingPeriod|Person $associated): array;
|
||||
}
|
||||
|
@@ -11,9 +11,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\ActivityBundle\Repository;
|
||||
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Chill\ActivityBundle\Entity\ActivityType;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
|
||||
final class ActivityTypeRepository implements ActivityTypeRepositoryInterface
|
||||
{
|
||||
|
@@ -12,12 +12,14 @@ declare(strict_types=1);
|
||||
namespace Chill\ActivityBundle\Repository;
|
||||
|
||||
use Chill\ActivityBundle\Entity\ActivityType;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Doctrine\Persistence\ObjectRepository;
|
||||
|
||||
interface ActivityTypeRepositoryInterface extends ObjectRepository
|
||||
{
|
||||
/**
|
||||
* @return array|ActivityType[]
|
||||
* @return array<ActivityType>
|
||||
*/
|
||||
public function findAllActive(): array;
|
||||
}
|
||||
|
Reference in New Issue
Block a user