Commit 89114128 by Damien Moulard

prevent deleting user if has flux to its account OR its adherent account

parent 70f6b7d9
...@@ -232,12 +232,21 @@ class CRUDController extends Controller ...@@ -232,12 +232,21 @@ class CRUDController extends Controller
} }
// Prevent deleting user if flux related to its Adherent account exist // Prevent deleting user if flux related to its Adherent account exist
if ($object instanceof User && $object->getAdherent()) { if ($object instanceof User) {
$query = $this->em->getRepository(Flux::class)->getQueryByAdherent($object->getAdherent()); $query = $this->em->getRepository(Flux::class)->getQueryByUser($object);
if (null != $query && count($query->getResult()) > 0) {
$hasFluxAdherent = false;
if ($object->getAdherent()) {
$queryAdherent = $this->em->getRepository(Flux::class)->getQueryByAdherent($object->getAdherent());
if (null != $queryAdherent && count($queryAdherent->getResult()) > 0) {
$hasFluxAdherent = true;
}
}
if (null != $query && count($query->getResult()) > 0 || $hasFluxAdherent) {
$this->addFlash( $this->addFlash(
'sonata_flash_error', 'sonata_flash_error',
'Vous ne pouvez pas supprimer ce compte utilisateur car des flux en relation à son compte Adhérent existent.' 'Vous ne pouvez pas supprimer ce compte utilisateur : des flux en relation à son compte sont enregistrés.'
); );
return $this->redirectTo($object); return $this->redirectTo($object);
......
...@@ -7,6 +7,7 @@ use App\Entity\Comptoir; ...@@ -7,6 +7,7 @@ use App\Entity\Comptoir;
use App\Entity\Flux; use App\Entity\Flux;
use App\Entity\Groupe; use App\Entity\Groupe;
use App\Entity\Prestataire; use App\Entity\Prestataire;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry; use Doctrine\Persistence\ManagerRegistry;
...@@ -237,6 +238,28 @@ class FluxRepository extends ServiceEntityRepository ...@@ -237,6 +238,28 @@ class FluxRepository extends ServiceEntityRepository
; ;
} }
/**
* @param User $user
*
* @return Query Returns a query fo finding an array of Flux
*/
public function getQueryByUser(User $user)
{
$sqlQuery = "SELECT f.id FROM {$this->tableName} f WHERE f.user_id = :id";
$statement = $this->connection->prepare($sqlQuery);
$statement->bindValue(':id', $user->getId());
$statement->execute();
$results = $statement->fetchAll();
$qb = $this->createQueryBuilder('f');
return $qb
->where($qb->expr()->in('f.id', ':ids'))
->setParameter('ids', $results)
->orderBy('f.createdAt', 'DESC')
->getQuery()
;
}
public function getTotalVenteAchat() public function getTotalVenteAchat()
{ {
$qb = $this->createQueryBuilder('f'); $qb = $this->createQueryBuilder('f');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment