Fix phpstan issues

This commit is contained in:
2023-12-12 22:34:26 +01:00
parent af663cf27c
commit da997badd9
26 changed files with 275 additions and 261 deletions

View File

@@ -12,32 +12,41 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\ActivityBundle\Entity\Activity;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\AccompanyingCourseType;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class AccompanyingCourseController.
*/
class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
final class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
{
public function __construct(protected SerializerInterface $serializer, protected EventDispatcherInterface $dispatcher, protected ValidatorInterface $validator, private readonly AccompanyingPeriodWorkRepository $workRepository, private readonly Registry $registry, private readonly TranslatorInterface $translator) {}
public function __construct(
private readonly ValidatorInterface $validator,
private readonly AccompanyingPeriodWorkRepository $workRepository,
private readonly Registry $registry,
private readonly TranslatorInterface $translator,
private readonly Security $security,
private readonly PersonRepository $personRepository,
) {}
/**
* @Route("/{_locale}/parcours/{accompanying_period_id}/close", name="chill_person_accompanying_course_close")
@@ -223,26 +232,29 @@ class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Contr
*/
public function newAction(Request $request): Response
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException();
}
$period = new AccompanyingPeriod();
$em = $this->getDoctrine()->getManager();
if ($request->query->has('person_id')) {
$personIds = $request->query->get('person_id');
$personIds = $request->query->all('person_id');
if (false === \is_array($personIds)) {
throw new BadRequestHttpException('person_id parameter should be an array');
}
foreach ($personIds as $personId) {
$person = $this->personRepository->find($personId);
foreach ($personIds as $personId) {
$person = $em->getRepository(Person::class)->find($personId);
if (null !== $person) {
$period->addPerson($person);
if (null !== $person) {
if (!$this->isGranted(PersonVoter::SEE, $person)) {
throw new AccessDeniedHttpException(sprintf('person with id %d cannot be seen', $person->getId()));
}
$period->addPerson($person);
}
}
$userLocation = $this->getUser()->getCurrentLocation();
$userLocation = $user->getCurrentLocation();
$period->setAdministrativeLocation($userLocation);
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::CREATE, $period);
@@ -260,6 +272,12 @@ class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Contr
*/
public function newHouseholdParcoursAction(Request $request): Response
{
$user = $this->getUser();
if (!$user instanceof User || !$this->security->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException();
}
$period = new AccompanyingPeriod();
$em = $this->getDoctrine()->getManager();
@@ -276,8 +294,7 @@ class AccompanyingCourseController extends \Symfony\Bundle\FrameworkBundle\Contr
}
}
$userLocation = $this->getUser()->getCurrentLocation();
$period->setAdministrativeLocation($userLocation);
$period->setAdministrativeLocation($user->getCurrentLocation());
$this->denyAccessUnlessGranted(AccompanyingPeriodVoter::CREATE, $period);

View File

@@ -12,28 +12,40 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Serializer\Model\Collection;
use Chill\MainBundle\Serializer\Model\Counter;
use Chill\PersonBundle\Repository\AccompanyingPeriod\AccompanyingPeriodWorkRepository;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
class AccompanyingCourseWorkApiController extends ApiController
{
public function __construct(private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository) {}
public function __construct(
private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository,
private readonly Security $security,
) {}
/**
* @Route("/api/1.0/person/accompanying-period/work/my-near-end")
*/
public function myWorksNearEndDate(Request $request): JsonResponse
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException();
}
$since = (new \DateTimeImmutable('now'))
->sub(new \DateInterval('P'.$request->query->getInt('since', 15).'D'));
$until = (new \DateTimeImmutable('now'))
->add(new \DateInterval('P'.$request->query->getInt('since', 15).'D'));
$total = $this->accompanyingPeriodWorkRepository
->countNearEndDateByUser($this->getUser(), $since, $until);
->countNearEndDateByUser($user, $since, $until);
if ($request->query->getBoolean('countOnly', false)) {
return $this->json(
@@ -46,7 +58,7 @@ class AccompanyingCourseWorkApiController extends ApiController
$paginator = $this->getPaginatorFactory()->create($total);
$works = $this->accompanyingPeriodWorkRepository
->findNearEndDateByUser($this->getUser(), $since, $until, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
->findNearEndDateByUser($user, $since, $until, $paginator->getItemsPerPage(), $paginator->getCurrentPageFirstItemNumber());
$collection = new Collection($works, $paginator);

View File

@@ -14,23 +14,37 @@ namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\HouseholdMemberType;
use Chill\PersonBundle\Household\MembersEditor;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Repository\Household\HouseholdRepository;
use Chill\PersonBundle\Repository\Household\PositionRepository;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Serializer\Exception;
use Symfony\Contracts\Translation\TranslatorInterface;
class HouseholdMemberController extends ApiController
{
public function __construct(private readonly UrlGeneratorInterface $generator, private readonly TranslatorInterface $translator, private readonly AccompanyingPeriodRepository $periodRepository) {}
public function __construct(
private readonly UrlGeneratorInterface $generator,
private readonly TranslatorInterface $translator,
private readonly AccompanyingPeriodRepository $periodRepository,
private readonly PersonRepository $personRepository,
private readonly HouseholdRepository $householdRepository,
private readonly Security $security,
private readonly PositionRepository $positionRepository,
) {}
/**
* @Route(
@@ -83,43 +97,50 @@ class HouseholdMemberController extends ApiController
*/
public function editor(Request $request)
{
$em = $this->getDoctrine()->getManager();
$ids = $request->query->all('persons');
if ($request->query->has('persons')) {
$ids = $request->query->get('persons', []);
if ([] !== $ids) {
$persons = [];
if (0 === \count($ids)) {
throw new BadRequestHttpException('parameters persons in query is not an array or empty');
}
foreach ($ids as $id) {
if (!is_numeric($id)) {
throw new BadRequestHttpException(sprintf('persons with id %s is not numeric', $id));
}
$persons = $em->getRepository(Person::class)
->findById($ids);
$person = $this->personRepository->find((int) $id);
if (null === $person) {
throw new NotFoundHttpException(sprintf('person with id %d not found', $id));
}
foreach ($persons as $person) {
$this->denyAccessUnlessGranted(
PersonVoter::SEE,
$person,
"You are not allowed to see person with id {$person->getId()}"
);
$persons[] = $person;
}
}
if ($request->query->has('household')) {
$householdId = $request->query->get('household', false);
$household = $em->getRepository(Household::class)
$household = $this->householdRepository
->find($householdId);
$allowHouseholdCreate = false;
$allowHouseholdSearch = false;
$allowLeaveWithoutHousehold = false;
if (null === $household) {
throw $this->createNotFoundException('household not found');
throw new NotFoundHttpException('household not found');
}
if (!$this->security->isGranted(HouseholdVoter::EDIT, $household)) {
throw new AccessDeniedHttpException('not allowed to edit this household');
}
// TODO ACL on household
}
$positions = $this->getDoctrine()->getManager()
->getRepository(Position::class)
$positions = $this->positionRepository
->findAll();
$data = [
@@ -140,10 +161,8 @@ class HouseholdMemberController extends ApiController
);
if (null === $period) {
throw $this->createNotFoundException('period not found');
throw new NotFoundHttpException('accompanying period not found');
}
// TODO add acl on accompanying Course
}
return $this->render('@ChillPerson/Household/members_editor.html.twig', [

View File

@@ -14,6 +14,7 @@ namespace Chill\PersonBundle\Controller;
use Chill\ActivityBundle\Entity\Activity;
use Chill\DocStoreBundle\Entity\PersonDocument;
use Chill\EventBundle\Entity\Participation;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Actions\Remove\PersonMove;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\PersonNotDuplicate;
@@ -24,15 +25,21 @@ use Chill\PersonBundle\Repository\PersonNotDuplicateRepository;
use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Search\SimilarPersonMatcher;
use Chill\TaskBundle\Entity\SingleTask;
use http\Exception\InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Security;
use function count;
class PersonDuplicateController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
{
public function __construct(private readonly SimilarPersonMatcher $similarPersonMatcher, private readonly TranslatorInterface $translator, private readonly PersonRepository $personRepository, private readonly PersonMove $personMove, private readonly EventDispatcherInterface $eventDispatcher) {}
public function __construct(
private readonly SimilarPersonMatcher $similarPersonMatcher,
private readonly PersonRepository $personRepository,
private readonly PersonMove $personMove,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly Security $security,
) {}
/**
* @\Symfony\Component\Routing\Annotation\Route(path="/{_locale}/person/{person1_id}/duplicate/{person2_id}/confirm", name="chill_person_duplicate_confirm")
@@ -40,7 +47,7 @@ class PersonDuplicateController extends \Symfony\Bundle\FrameworkBundle\Controll
public function confirmAction(mixed $person1_id, mixed $person2_id, Request $request)
{
if ($person1_id === $person2_id) {
throw new InvalidArgumentException('Can not merge same person');
throw new \InvalidArgumentException('Can not merge same person');
}
$person1 = $this->_getPerson($person1_id);
@@ -152,6 +159,12 @@ class PersonDuplicateController extends \Symfony\Bundle\FrameworkBundle\Controll
*/
public function notDuplicateAction(mixed $person1_id, mixed $person2_id)
{
$user = $this->security->getUser();
if (!$user instanceof User) {
throw new AccessDeniedHttpException();
}
[$person1, $person2] = $this->_getPersonsByPriority($person1_id, $person2_id);
$this->denyAccessUnlessGranted(
@@ -167,7 +180,7 @@ class PersonDuplicateController extends \Symfony\Bundle\FrameworkBundle\Controll
$personNotDuplicate = new PersonNotDuplicate();
$personNotDuplicate->setPerson1($person1);
$personNotDuplicate->setPerson2($person2);
$personNotDuplicate->setUser($this->getUser());
$personNotDuplicate->setUser($user);
$this->getDoctrine()->getManager()->persist($personNotDuplicate);
$this->getDoctrine()->getManager()->flush();
@@ -259,7 +272,7 @@ class PersonDuplicateController extends \Symfony\Bundle\FrameworkBundle\Controll
private function _getPersonsByPriority($person1_id, $person2_id)
{
if ($person1_id === $person2_id) {
throw new InvalidArgumentException('Can not merge same person');
throw new \InvalidArgumentException('Can not merge same person');
}
if ($person1_id > $person2_id) {