Merge branch 'signature-app-master' into 'master'

Signature app master

Closes #307

See merge request Chill-Projet/chill-bundles!743
This commit is contained in:
2024-11-12 20:30:00 +00:00
426 changed files with 22236 additions and 2253 deletions

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSendView;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectRepository;
/**
* @template-implements ObjectRepository<EntityWorkflowSendView>
*/
class EntityWorkflowSendViewRepository implements ObjectRepository
{
private readonly ObjectRepository $repository;
public function __construct(ManagerRegistry $registry)
{
$this->repository = $registry->getRepository($this->getClassName());
}
public function find($id): ?EntityWorkflowSendView
{
return $this->repository->find($id);
}
public function findAll()
{
return $this->repository->findAll();
}
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?EntityWorkflowSendView
{
return $this->repository->findOneBy($criteria);
}
public function getClassName()
{
return EntityWorkflowSendView::class;
}
}

View File

@@ -0,0 +1,133 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Search\SearchApiQuery;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Symfony\Contracts\Translation\LocaleAwareInterface;
final class UserGroupRepository implements UserGroupRepositoryInterface, LocaleAwareInterface
{
private readonly EntityRepository $repository;
private string $locale;
public function __construct(EntityManagerInterface $em)
{
$this->repository = $em->getRepository(UserGroup::class);
}
public function setLocale(string $locale): void
{
$this->locale = $locale;
}
public function getLocale(): string
{
return $this->locale;
}
public function find($id): ?UserGroup
{
return $this->repository->find($id);
}
public function findAll(): array
{
return $this->repository->findAll();
}
public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
{
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
public function findOneBy(array $criteria): ?UserGroup
{
return $this->repository->findOneBy($criteria);
}
public function getClassName(): string
{
return UserGroup::class;
}
public function provideSearchApiQuery(string $pattern, string $lang, string $selectKey = 'user-group'): SearchApiQuery
{
$query = new SearchApiQuery();
$query
->setSelectKey($selectKey)
->setSelectJsonbMetadata("jsonb_build_object('id', ug.id)")
->setSelectPertinence('3 + SIMILARITY(LOWER(UNACCENT(?)), ug.label->>?) + CASE WHEN (EXISTS(SELECT 1 FROM unnest(string_to_array(label->>?, \' \')) AS t WHERE LOWER(t) LIKE \'%\' || LOWER(UNACCENT(?)) || \'%\')) THEN 100 ELSE 0 END', [$pattern, $lang, $lang, $pattern])
->setFromClause('chill_main_user_group AS ug')
->setWhereClauses('
ug.active AND (
SIMILARITY(LOWER(UNACCENT(?)), ug.label->>?) > 0.15
OR ug.label->>? LIKE \'%\' || LOWER(UNACCENT(?)) || \'%\')
', [$pattern, $lang, $pattern, $lang]);
return $query;
}
public function findByUser(User $user, bool $onlyActive = true, ?int $limit = null, ?int $offset = null): array
{
$qb = $this->buildQueryByUser($user, $onlyActive);
if (null !== $limit) {
$qb->setMaxResults($limit);
}
if (null !== $offset) {
$qb->setFirstResult($offset);
}
// ordering thing
$qb->addSelect('JSON_EXTRACT(ug.label, :lang) AS HIDDEN label_ordering')
->addOrderBy('label_ordering', 'ASC')
->setParameter('lang', $this->getLocale());
return $qb->getQuery()->getResult();
}
public function countByUser(User $user, bool $onlyActive = true): int
{
$qb = $this->buildQueryByUser($user, $onlyActive);
$qb->select('count(ug)');
return $qb->getQuery()->getSingleScalarResult();
}
private function buildQueryByUser(User $user, bool $onlyActive): \Doctrine\ORM\QueryBuilder
{
$qb = $this->repository->createQueryBuilder('ug');
$qb->where(
$qb->expr()->orX(
$qb->expr()->isMemberOf(':user', 'ug.users'),
$qb->expr()->isMemberOf(':user', 'ug.adminUsers')
)
);
$qb->setParameter('user', $user);
if ($onlyActive) {
$qb->andWhere(
$qb->expr()->eq('ug.active', ':active')
);
$qb->setParameter('active', true);
}
return $qb;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Search\SearchApiQuery;
use Doctrine\Persistence\ObjectRepository;
/**
* @template-extends ObjectRepository<UserGroup>
*/
interface UserGroupRepositoryInterface extends ObjectRepository
{
/**
* Provide a SearchApiQuery for searching amongst user groups.
*/
public function provideSearchApiQuery(string $pattern, string $lang, string $selectKey = 'user-group'): SearchApiQuery;
public function findByUser(User $user, bool $onlyActive = true, ?int $limit = null, ?int $offset = null): array;
public function countByUser(User $user, bool $onlyActive = true): int;
}

View File

@@ -12,7 +12,9 @@ declare(strict_types=1);
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
@@ -99,6 +101,24 @@ class EntityWorkflowRepository implements ObjectRepository
return $this->repository->findAll();
}
/**
* @return list<EntityWorkflow>
*/
public function findByRelatedEntity($entityClass, $relatedEntityId): array
{
$qb = $this->repository->createQueryBuilder('w');
$query = $qb->where(
$qb->expr()->andX(
$qb->expr()->eq('w.relatedEntityClass', ':entity_class'),
$qb->expr()->eq('w.relatedEntityId', ':entity_id'),
)
)->setParameter('entity_class', $entityClass)
->setParameter('entity_id', $relatedEntityId);
return $query->getQuery()->getResult();
}
/**
* @param mixed|null $limit
* @param mixed|null $offset
@@ -180,6 +200,36 @@ class EntityWorkflowRepository implements ObjectRepository
return $this->repository->findOneBy($criteria);
}
/**
* Finds workflows that are not finalized and are older than the specified date.
*
* @param \DateTimeImmutable $olderThanDate the date to compare against
*
* @return iterable<EntityWorkflow>
*/
public function findWorkflowsWithoutFinalStepAndOlderThan(\DateTimeImmutable $olderThanDate): iterable
{
$qb = $this->repository->createQueryBuilder('sw');
$qb->select('sw')
// only the workflow which are not finalized
->where(sprintf('NOT EXISTS (SELECT 1 FROM %s ews WHERE ews.isFinal = TRUE AND ews.entityWorkflow = sw.id)', EntityWorkflowStep::class))
->andWhere(
$qb->expr()->orX(
// only the workflow where all the transitions are older than transitionAt
sprintf(':olderThanDate > ALL (SELECT ews2.transitionAt FROM %s ews2 WHERE ews2.transitionAt IS NOT NULL AND ews2.entityWorkflow = sw)', EntityWorkflowStep::class),
// or the workflow which have only the initial step, with no transition
sprintf('1 = (SELECT COUNT(ews3.id) FROM %s ews3 WHERE ews3.currentStep = :initial AND ews3.transitionAt IS NULL AND ews3.entityWorkflow = sw)', EntityWorkflowStep::class),
)
)
->andWhere('sw.createdAt < :olderThanDate')
->setParameter('olderThanDate', $olderThanDate)
->setParameter('initial', 'initial')
;
return $qb->getQuery()->toIterable();
}
public function getClassName(): string
{
return EntityWorkflow::class;
@@ -212,7 +262,11 @@ class EntityWorkflowRepository implements ObjectRepository
$qb->where(
$qb->expr()->andX(
$qb->expr()->isMemberOf(':user', 'step.destUser'),
$qb->expr()->orX(
$qb->expr()->isMemberOf(':user', 'step.destUser'),
$qb->expr()->isMemberOf(':user', 'step.destUserByAccessKey'),
$qb->expr()->exists(sprintf('SELECT 1 FROM %s ug WHERE ug MEMBER OF step.destUserGroups AND :user MEMBER OF ug.users', UserGroup::class))
),
$qb->expr()->isNull('step.transitionAfter'),
$qb->expr()->eq('step.isFinal', "'FALSE'")
)

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature;
use Chill\PersonBundle\Entity\Person;
interface EntityWorkflowSignatureACLAwareRepositoryInterface
{
/**
* @return array<EntityWorkflowStepSignature>
*/
public function findByPersonAndPendingState(Person $person): array;
}

View File

@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepHold;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\NoResultException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @template-extends ServiceEntityRepository<EntityWorkflowStepHold>
*/
class EntityWorkflowStepHoldRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, EntityWorkflowStepHold::class);
}
/**
* Find an EntityWorkflowStepHold by its ID.
*/
public function findById(int $id): ?EntityWorkflowStepHold
{
return $this->find($id);
}
/**
* Find all EntityWorkflowStepHold records.
*
* @return EntityWorkflowStepHold[]
*/
public function findAllHolds(): array
{
return $this->findAll();
}
/**
* Find EntityWorkflowStepHold by a specific step.
*
* @return EntityWorkflowStepHold[]
*/
public function findByStep(EntityWorkflowStep $step): array
{
return $this->findBy(['step' => $step]);
}
/**
* Find a single EntityWorkflowStepHold by step and user.
*
* @throws NonUniqueResultException
*/
public function findOneByStepAndUser(EntityWorkflowStep $step, User $user): ?EntityWorkflowStepHold
{
try {
return $this->createQueryBuilder('e')
->andWhere('e.step = :step')
->andWhere('e.byUser = :user')
->setParameter('step', $step)
->setParameter('user', $user)
->getQuery()
->getSingleResult();
} catch (NoResultException) {
return null;
}
}
}

View File

@@ -12,15 +12,16 @@ declare(strict_types=1);
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStep;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectRepository;
class EntityWorkflowStepRepository implements ObjectRepository
readonly class EntityWorkflowStepRepository implements ObjectRepository
{
private readonly EntityRepository $repository;
private EntityRepository $repository;
public function __construct(EntityManagerInterface $entityManager)
{
@@ -65,7 +66,11 @@ class EntityWorkflowStepRepository implements ObjectRepository
$qb->where(
$qb->expr()->andX(
$qb->expr()->isMemberOf(':user', 'e.destUser'),
$qb->expr()->orX(
$qb->expr()->isMemberOf(':user', 'e.destUser'),
$qb->expr()->isMemberOf(':user', 'e.destUserByAccessKey'),
$qb->expr()->exists(sprintf('SELECT 1 FROM %s ug WHERE ug MEMBER OF e.destUserGroups AND :user MEMBER OF ug.users', UserGroup::class))
),
$qb->expr()->isNull('e.transitionAt'),
$qb->expr()->eq('e.isFinal', ':bool'),
)

View File

@@ -0,0 +1,54 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository\Workflow;
use Chill\DocStoreBundle\Security\Authorization\StoredObjectRoleEnum;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowSignatureStateEnum;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Security\Core\Security;
class EntityWorkflowStepSignatureACLAwareRepository implements EntityWorkflowSignatureACLAwareRepositoryInterface
{
public function __construct(
private readonly EntityWorkflowStepSignatureRepository $repository,
private readonly EntityWorkflowManager $entityWorkflowManager,
private readonly Security $security,
) {}
public function findByPersonAndPendingState(Person $person): array
{
$signatures = $this->repository->findByPerson($person);
$filteredSignatures = [];
foreach ($signatures as $signature) {
if (EntityWorkflowSignatureStateEnum::SIGNED === $signature->getState() || EntityWorkflowSignatureStateEnum::CANCELED === $signature->getState() || EntityWorkflowSignatureStateEnum::REJECTED === $signature->getState()) {
continue;
}
$workflow = $signature->getStep()->getEntityWorkflow();
$storedObject = $this->entityWorkflowManager->getAssociatedStoredObject($workflow);
if (null === $storedObject) {
continue;
}
if ($this->security->isGranted(StoredObjectRoleEnum::SEE->value, $storedObject)) {
$filteredSignatures[] = $signature;
}
}
return $filteredSignatures;
}
}

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
/*
* 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.
*/
namespace Chill\MainBundle\Repository\Workflow;
use Chill\MainBundle\Entity\Workflow\EntityWorkflowStepSignature;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Chill\PersonBundle\Entity\Person;
use Doctrine\Persistence\ManagerRegistry;
/**
* @template-extends ServiceEntityRepository<EntityWorkflowStepSignature>
*/
class EntityWorkflowStepSignatureRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, EntityWorkflowStepSignature::class);
}
public function findByPerson(Person $person): array
{
$qb = $this->createQueryBuilder('ewss');
$qb->select('ewss');
$qb->where($qb->expr()->eq('ewss.personSigner', ':person'));
$qb->setParameter('person', $person);
return $qb->getQuery()->getResult();
}
}