Fixing generic doc providers from calendar + fix cs

This commit is contained in:
2023-06-13 11:01:40 +02:00
parent 727e9d0f74
commit 4456fb3749
11 changed files with 401 additions and 53 deletions

View File

@@ -18,13 +18,21 @@ use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\DocStoreBundle\GenericDoc\FetchQuery;
use Chill\DocStoreBundle\GenericDoc\FetchQueryInterface;
use Chill\DocStoreBundle\GenericDoc\GenericDocForAccompanyingPeriodProviderInterface;
use Chill\DocStoreBundle\GenericDoc\GenericDocForPersonProviderInterface;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\MappingException;
use Service\GenericDoc\Providers\AccompanyingPeriodCalendarGenericDocProviderTest;
use Symfony\Component\Security\Core\Security;
final class AccompanyingPeriodCalendarGenericDocProvider implements GenericDocForAccompanyingPeriodProviderInterface
/**
* @see AccompanyingPeriodCalendarGenericDocProviderTest
*/
final readonly class AccompanyingPeriodCalendarGenericDocProvider implements GenericDocForAccompanyingPeriodProviderInterface, GenericDocForPersonProviderInterface
{
public const KEY = 'accompanying_period_calendar_document';
@@ -50,14 +58,17 @@ final class AccompanyingPeriodCalendarGenericDocProvider implements GenericDocFo
$classMetadata->getSchemaName().'.'.$classMetadata->getTableName().' AS cd'
);
$query->addJoinClause(
sprintf('JOIN %s doc_store ON doc_store.%s = cd.%s',
$storedObjectMetadata->getSchemaName().'.'.$storedObjectMetadata->getTableName(),
$storedObjectMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('storedObject')
));
sprintf(
'JOIN %s doc_store ON doc_store.%s = cd.%s',
$storedObjectMetadata->getSchemaName().'.'.$storedObjectMetadata->getTableName(),
$storedObjectMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('storedObject')
)
);
$query->addJoinClause(
sprintf('JOIN %s calendar ON calendar.%s = cd.%s',
sprintf(
'JOIN %s calendar ON calendar.%s = cd.%s',
$calendarMetadata->getSchemaName().'.'.$calendarMetadata->getTableName(),
$calendarMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('calendar')
@@ -65,12 +76,91 @@ final class AccompanyingPeriodCalendarGenericDocProvider implements GenericDocFo
);
$query->addWhereClause(
sprintf('calendar.%s = ?',
$calendarMetadata->getAssociationMapping('accompanyingPeriod')['joinColumns'][0]['name']),
sprintf(
'calendar.%s = ?',
$calendarMetadata->getAssociationMapping('accompanyingPeriod')['joinColumns'][0]['name']
),
[$accompanyingPeriod->getId()],
[Types::INTEGER]
);
return $query;
}
public function isAllowedForAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod): bool
{
return $this->security->isGranted(CalendarVoter::SEE, $accompanyingPeriod);
}
public function buildFetchQueryForPerson(Person $person, ?DateTimeImmutable $startDate = null, ?DateTimeImmutable $endDate = null, ?string $content = null, ?string $origin = null): FetchQueryInterface
{
$classMetadata = $this->em->getClassMetadata(CalendarDoc::class);
$storedObjectMetadata = $this->em->getClassMetadata(StoredObject::class);
$calendarMetadata = $this->em->getClassMetadata(Calendar::class);
$query = new FetchQuery(
self::KEY,
sprintf("jsonb_build_object('id', cd.%s)", $classMetadata->getColumnName('id')),
'cd.'.$storedObjectMetadata->getColumnName('createdAt'),
$classMetadata->getSchemaName().'.'.$classMetadata->getTableName().' AS cd'
);
$query->addJoinClause(
sprintf(
'JOIN %s doc_store ON doc_store.%s = cd.%s',
$storedObjectMetadata->getSchemaName().'.'.$storedObjectMetadata->getTableName(),
$storedObjectMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('storedObject')
)
);
$query->addJoinClause(
sprintf(
'JOIN %s calendar ON calendar.%s = cd.%s',
$calendarMetadata->getSchemaName().'.'.$calendarMetadata->getTableName(),
$calendarMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('calendar')
)
);
// get the documents associated with accompanying periods in which person participates
$or = [];
$orParams = [];
$orTypes = [];
foreach ($person->getAccompanyingPeriodParticipations() as $participation) {
if (!$this->security->isGranted(CalendarVoter::SEE, $participation->getAccompanyingPeriod())) {
continue;
}
$or[] = sprintf(
'(calendar.%s = ? AND cd.%s BETWEEN ?::date AND COALESCE(?::date, \'infinity\'::date))',
$calendarMetadata->getSingleAssociationJoinColumnName('accompanyingPeriod'),
$storedObjectMetadata->getColumnName('createdAt')
);
$orParams = [...$orParams, $participation->getAccompanyingPeriod()->getId(),
DateTimeImmutable::createFromInterface($participation->getStartDate()),
null === $participation->getEndDate() ? null : DateTimeImmutable::createFromInterface($participation->getEndDate())];
$orTypes = [...$orTypes, Types::INTEGER, Types::DATE_IMMUTABLE, Types::DATE_IMMUTABLE];
}
if ([] === $or) {
$query->addWhereClause('TRUE = FALSE');
return $query;
}
return $this->addWhereClausesToQuery($query, $startDate, $endDate, $content);
}
public function isAllowedForPerson(Person $person): bool
{
// check that the person is allowed to see an accompanying period. If yes, the
// ACL on each accompanying period will be checked when the query is build
return $this->security->isGranted(AccompanyingPeriodVoter::SEE, $person);
}
private function addWhereClausesToQuery(FetchQuery $query, ?DateTimeImmutable $startDate, ?DateTimeImmutable $endDate, ?string $content): FetchQuery
{
$storedObjectMetadata = $this->em->getClassMetadata(StoredObject::class);
if (null !== $startDate) {
$query->addWhereClause(
sprintf('doc_store.%s >= ?', $storedObjectMetadata->getColumnName('createdAt')),
@@ -98,10 +188,5 @@ final class AccompanyingPeriodCalendarGenericDocProvider implements GenericDocFo
return $query;
}
public function isAllowedForAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod): bool
{
return $this->security->isGranted(CalendarVoter::SEE, $accompanyingPeriod);
}
}

View File

@@ -24,9 +24,15 @@ use DateTimeImmutable;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\MappingException;
use Service\GenericDoc\Providers\PersonCalendarGenericDocProviderTest;
use Symfony\Component\Security\Core\Security;
final class PersonCalendarGenericDocProvider implements GenericDocForPersonProviderInterface
/**
* Provide calendar documents for calendar associated to persons
*
* @see PersonCalendarGenericDocProviderTest
*/
final readonly class PersonCalendarGenericDocProvider implements GenericDocForPersonProviderInterface
{
public const KEY = 'person_calendar_document';
@@ -36,7 +42,6 @@ final class PersonCalendarGenericDocProvider implements GenericDocForPersonProvi
) {
}
private function addWhereClausesToQuery(FetchQuery $query, ?\DateTimeImmutable $startDate = null, ?\DateTimeImmutable $endDate = null, ?string $content = null): FetchQuery
{
$storedObjectMetadata = $this->em->getClassMetadata(StoredObject::class);
@@ -84,50 +89,30 @@ final class PersonCalendarGenericDocProvider implements GenericDocForPersonProvi
$classMetadata->getSchemaName().'.'.$classMetadata->getTableName().' AS cd'
);
$query->addJoinClause(
sprintf('JOIN %s doc_store ON doc_store.%s = cd.%s',
sprintf(
'JOIN %s doc_store ON doc_store.%s = cd.%s',
$storedObjectMetadata->getSchemaName().'.'.$storedObjectMetadata->getTableName(),
$storedObjectMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('storedObject')
));
)
);
$query->addJoinClause(
sprintf('JOIN %s calendar ON calendar.%s = cd.%s',
sprintf(
'JOIN %s calendar ON calendar.%s = cd.%s',
$calendarMetadata->getSchemaName().'.'.$calendarMetadata->getTableName(),
$calendarMetadata->getColumnName('id'),
$classMetadata->getSingleAssociationJoinColumnName('calendar')
)
);
// get the documents associated with accompanying periods in which person participates
$or = [];
$orParams = [];
$orTypes = [];
foreach ($person->getAccompanyingPeriodParticipations() as $participation) {
if (!$this->security->isGranted(CalendarVoter::SEE, $participation->getAccompanyingPeriod())) {
continue;
}
$or[] = sprintf(
'(calendar.%s = ? AND cd.%s BETWEEN ?::date AND COALESCE(?::date, \'infinity\'::date))',
$calendarMetadata->getSingleAssociationJoinColumnName('accompanyingPeriod'),
$storedObjectMetadata->getColumnName('createdAt')
);
$orParams = [...$orParams, $participation->getAccompanyingPeriod()->getId(),
DateTimeImmutable::createFromInterface($participation->getStartDate()),
null === $participation->getEndDate() ? null : DateTimeImmutable::createFromInterface($participation->getEndDate())];
$orTypes = [...$orTypes, Types::INTEGER, Types::DATE_IMMUTABLE, Types::DATE_IMMUTABLE];
}
if ([] === $or) {
$query->addWhereClause('TRUE = FALSE');
return $query;
}
// $query->addWhereClause(sprintf('(%s)', implode(' OR ', $or)), $orParams, $orTypes);
$query->addWhereClause(
sprintf('calendar.%s = ?', $calendarMetadata->getSingleAssociationJoinColumnName('person')),
[$person->getId()],
[Types::INTEGER]
);
return $this->addWhereClausesToQuery($query, $startDate, $endDate, $content);
}
/**