mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\TaskBundle\Repository;
|
||||
@@ -10,17 +17,24 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\TaskBundle\Entity\SingleTask;
|
||||
use Chill\TaskBundle\Security\Authorization\TaskVoter;
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use LogicException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use function substr;
|
||||
|
||||
final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepositoryInterface
|
||||
{
|
||||
private AuthorizationHelperInterface $authorizationHelper;
|
||||
private EntityManagerInterface $em;
|
||||
private Security $security;
|
||||
|
||||
private CenterResolverDispatcherInterface $centerResolverDispatcher;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private Security $security;
|
||||
|
||||
public function __construct(
|
||||
CenterResolverDispatcherInterface $centerResolverDispatcher,
|
||||
EntityManagerInterface $em,
|
||||
@@ -33,41 +47,150 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
}
|
||||
|
||||
public function findByCurrentUsersTasks(
|
||||
public function buildBaseQuery(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryMyTasks($pattern, $flags);
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$qb
|
||||
->from(SingleTask::class, 't');
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
if (!empty($pattern)) {
|
||||
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(t.title))', 'LOWER(UNACCENT(:pattern))'))
|
||||
->setParameter('pattern', '%' . $pattern . '%');
|
||||
}
|
||||
|
||||
if (count($flags) > 0) {
|
||||
$orXDate = $qb->expr()->orX();
|
||||
$orXState = $qb->expr()->orX();
|
||||
$now = new DateTime();
|
||||
|
||||
foreach ($flags as $key => $flag) {
|
||||
switch ($flag) {
|
||||
case 'no-alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isNull('t.endDate'),
|
||||
$qb->expr()->gte('t.endDate - COALESCE(t.warningInterval, :intervalBlank)', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('intervalBlank', new DateInterval('P0D'))
|
||||
->setParameter('now', $now);
|
||||
|
||||
break;
|
||||
|
||||
case 'warning':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->not($qb->expr()->isNull('t.warningInterval')),
|
||||
$qb->expr()->lte('t.endDate - t.warningInterval', ':now'),
|
||||
$qb->expr()->gt('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
|
||||
break;
|
||||
|
||||
case 'alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->lte('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
|
||||
break;
|
||||
|
||||
case 'state_new':
|
||||
$orXState
|
||||
->add(
|
||||
'JSONB_ARRAY_LENGTH(t.currentStates) = 0'
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case substr($flag, 0, 6) === 'state_':
|
||||
$state = substr($flag, 6);
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_EXISTS_IN_ARRAY(t.currentStates, :state_{$key}) = 'TRUE'"
|
||||
);
|
||||
$qb->setParameter("state_{$key}", $state);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new LogicException("this flag is not supported: {$flag}");
|
||||
}
|
||||
}
|
||||
|
||||
if ($orXDate->count() > 0) {
|
||||
$qb->andWhere($orXDate);
|
||||
}
|
||||
|
||||
if ($orXState->count() > 0) {
|
||||
$qb->andWhere($orXState);
|
||||
}
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function countByCurrentUsersTasks(
|
||||
public function buildQueryByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.course', ':course'))
|
||||
->setParameter('course', $course);
|
||||
}
|
||||
|
||||
public function buildQueryByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.person', ':person'))
|
||||
->setParameter('person', $person);
|
||||
}
|
||||
|
||||
public function buildQueryMyTasks(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.assignee', ':user'))
|
||||
->setParameter('user', $this->security->getUser());
|
||||
}
|
||||
|
||||
public function countByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): int {
|
||||
return $this->buildQueryMyTasks($pattern, $flags)
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $this
|
||||
->addACLGlobal($qb)
|
||||
->select('COUNT(t)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByCourse($course, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $course);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function countByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
@@ -81,18 +204,13 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findByPerson(
|
||||
Person $person,
|
||||
public function countByCurrentUsersTasks(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByPerson($person, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $person);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
?array $flags = []
|
||||
): int {
|
||||
return $this->buildQueryMyTasks($pattern, $flags)
|
||||
->select('COUNT(t)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function countByPerson(
|
||||
@@ -108,18 +226,6 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function countByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): int {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $this
|
||||
->addACLGlobal($qb)
|
||||
->select('COUNT(t)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
@@ -133,42 +239,44 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function buildQueryByCourse(
|
||||
public function findByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
) : QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByCourse($course, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $course);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.course', ':course'))
|
||||
->setParameter('course', $course)
|
||||
;
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function buildQueryByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder
|
||||
{
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.person', ':person'))
|
||||
->setParameter('person', $person);
|
||||
}
|
||||
|
||||
public function buildQueryMyTasks(
|
||||
public function findByCurrentUsersTasks(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryMyTasks($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.assignee', ':user'))
|
||||
->setParameter('user', $this->security->getUser())
|
||||
;
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function findByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByPerson($person, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $person);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function getResult(
|
||||
@@ -181,11 +289,10 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
|
||||
$qb
|
||||
->setFirstResult($start)
|
||||
->setMaxResults($limit)
|
||||
;
|
||||
->setMaxResults($limit);
|
||||
|
||||
foreach ($orderBy as $field => $direction) {
|
||||
$qb->addOrderBy('t.'.$field, $direction);
|
||||
$qb->addOrderBy('t.' . $field, $direction);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
@@ -195,11 +302,14 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
QueryBuilder $qb,
|
||||
$entity
|
||||
): QueryBuilder {
|
||||
$scopes = $this->authorizationHelper->getReachableScopes($this->security->getUser(),
|
||||
TaskVoter::SHOW, $this->centerResolverDispatcher->resolveCenter($entity));
|
||||
$scopes = $this->authorizationHelper->getReachableScopes(
|
||||
$this->security->getUser(),
|
||||
TaskVoter::SHOW,
|
||||
$this->centerResolverDispatcher->resolveCenter($entity)
|
||||
);
|
||||
|
||||
return $qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
|
||||
->setParameter('scopes', $scopes);
|
||||
return $qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
|
||||
->setParameter('scopes', $scopes);
|
||||
}
|
||||
|
||||
private function addACLGlobal(
|
||||
@@ -217,121 +327,35 @@ 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');
|
||||
$qb->distinct(true);
|
||||
|
||||
$k = 0;
|
||||
$orX = $qb->expr()->orX();
|
||||
|
||||
foreach ($allowedCenters as $center) {
|
||||
$allowedScopes = $this->authorizationHelper->getReachableScopes($this->security->getUser(),
|
||||
TaskVoter::SHOW, $center);
|
||||
$allowedScopes = $this->authorizationHelper->getReachableScopes(
|
||||
$this->security->getUser(),
|
||||
TaskVoter::SHOW,
|
||||
$center
|
||||
);
|
||||
|
||||
$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('person.center', ':center_' . $k),
|
||||
$qb->expr()->eq('person_p.center', ':center_' . $k)
|
||||
),
|
||||
$qb->expr()->in('t.circle', ':scopes_'.$k)
|
||||
$qb->expr()->in('t.circle', ':scopes_' . $k)
|
||||
);
|
||||
$qb
|
||||
->setParameter('center_'.$k, $center)
|
||||
->setParameter('scopes_'.$k, $allowedScopes);
|
||||
->setParameter('center_' . $k, $center)
|
||||
->setParameter('scopes_' . $k, $allowedScopes);
|
||||
$orX->add($and);
|
||||
|
||||
$k++;
|
||||
++$k;
|
||||
}
|
||||
$qb->andWhere($orX);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function buildBaseQuery (
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$qb
|
||||
->from(SingleTask::class, 't')
|
||||
;
|
||||
|
||||
if (!empty($pattern)) {
|
||||
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(t.title))', 'LOWER(UNACCENT(:pattern))'))
|
||||
->setParameter('pattern', '%'.$pattern.'%')
|
||||
;
|
||||
}
|
||||
|
||||
if (count($flags) > 0) {
|
||||
$orXDate = $qb->expr()->orX();
|
||||
$orXState = $qb->expr()->orX();
|
||||
$now = new \DateTime();
|
||||
|
||||
foreach ($flags as $key => $flag) {
|
||||
switch ($flag) {
|
||||
case 'no-alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->isNull('t.endDate'),
|
||||
$qb->expr()->gte('t.endDate - COALESCE(t.warningInterval, :intervalBlank)', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('intervalBlank', new \DateInterval('P0D'))
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'warning':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->not($qb->expr()->isNull('t.warningInterval')),
|
||||
$qb->expr()->lte('t.endDate - t.warningInterval', ':now'),
|
||||
$qb->expr()->gt('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->lte('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'state_new':
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_ARRAY_LENGTH(t.currentStates) = 0"
|
||||
);
|
||||
break;
|
||||
case \substr($flag, 0, 6) === 'state_':
|
||||
$state = \substr($flag, 6);
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_EXISTS_IN_ARRAY(t.currentStates, :state_$key) = 'TRUE'"
|
||||
);
|
||||
$qb->setParameter("state_$key", $state);
|
||||
break;
|
||||
default:
|
||||
throw new \LogicException("this flag is not supported: $flag");
|
||||
}
|
||||
}
|
||||
|
||||
if ($orXDate->count() > 0) {
|
||||
$qb->andWhere($orXDate);
|
||||
}
|
||||
if ($orXState->count() > 0) {
|
||||
$qb->andWhere($orXState);
|
||||
}
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user