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,12 +12,15 @@ declare(strict_types=1);
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Form\AbsenceType;
use Chill\MainBundle\Security\ChillSecurity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class AbsenceController extends AbstractController
{
public function __construct(private readonly ChillSecurity $security) {}
/**
* @Route(
* "/{_locale}/absence",
@@ -27,7 +30,7 @@ class AbsenceController extends AbstractController
*/
public function setAbsence(Request $request)
{
$user = $this->getUser();
$user = $this->security->getUser();
$form = $this->createForm(AbsenceType::class, $user);
$form->handleRequest($request);
@@ -54,7 +57,7 @@ class AbsenceController extends AbstractController
*/
public function unsetAbsence(Request $request)
{
$user = $this->getUser();
$user = $this->security->getUser();
$user->setAbsenceStart(null);
$em = $this->getDoctrine()->getManager();

View File

@@ -13,7 +13,6 @@ namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Entity\Notification;
use Chill\MainBundle\Entity\NotificationComment;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\NotificationCommentType;
use Chill\MainBundle\Form\NotificationType;
use Chill\MainBundle\Notification\Exception\NotificationHandlerNotFound;
@@ -22,6 +21,7 @@ use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\NotificationRepository;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Security\Authorization\NotificationVoter;
use Chill\MainBundle\Security\ChillSecurity;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -32,7 +32,6 @@ 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\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use function in_array;
@@ -41,7 +40,7 @@ use function in_array;
*/
class NotificationController extends AbstractController
{
public function __construct(private readonly EntityManagerInterface $em, private readonly LoggerInterface $chillLogger, private readonly LoggerInterface $logger, private readonly Security $security, private readonly NotificationRepository $notificationRepository, private readonly NotificationHandlerManager $notificationHandlerManager, private readonly PaginatorFactory $paginatorFactory, private readonly TranslatorInterface $translator, private readonly UserRepository $userRepository) {}
public function __construct(private readonly EntityManagerInterface $em, private readonly LoggerInterface $chillLogger, private readonly LoggerInterface $logger, private readonly ChillSecurity $security, private readonly NotificationRepository $notificationRepository, private readonly NotificationHandlerManager $notificationHandlerManager, private readonly PaginatorFactory $paginatorFactory, private readonly TranslatorInterface $translator, private readonly UserRepository $userRepository) {}
/**
* @Route("/create", name="chill_main_notification_create")
@@ -50,10 +49,6 @@ class NotificationController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
if (!$this->security->getUser() instanceof User) {
throw new AccessDeniedHttpException('You must be authenticated and a user to create a notification');
}
if (!$request->query->has('entityClass')) {
throw new BadRequestHttpException('Missing entityClass parameter');
}
@@ -68,13 +63,13 @@ class NotificationController extends AbstractController
->setRelatedEntityId($request->query->getInt('entityId'))
->setSender($this->security->getUser());
if ($request->query->has('tos')) {
foreach ($request->query->get('tos') as $toId) {
if (null === $to = $this->userRepository->find($toId)) {
throw new NotFoundHttpException("user with id {$toId} is not found");
}
$notification->addAddressee($to);
$tos = $request->query->all('tos');
foreach ($tos as $toId) {
if (null === $to = $this->userRepository->find($toId)) {
throw new NotFoundHttpException("user with id {$toId} is not found");
}
$notification->addAddressee($to);
}
try {
@@ -144,10 +139,6 @@ class NotificationController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
if (!$this->security->getUser() instanceof User) {
throw new AccessDeniedHttpException('You must be authenticated and a user to create a notification');
}
foreach (['accessKey'/* , 'email' */] as $param) {
if (!$request->query->has($param)) {
throw new BadRequestHttpException("Missing {$param} parameter");
@@ -308,8 +299,8 @@ class NotificationController extends AbstractController
]);
// we mark the notification as read after having computed the response
if ($this->getUser() instanceof User && !$notification->isReadBy($this->getUser())) {
$notification->markAsReadBy($this->getUser());
if (!$notification->isReadBy($this->security->getUser())) {
$notification->markAsReadBy($this->security->getUser());
$this->em->flush();
}

View File

@@ -13,6 +13,7 @@ namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Form\UserPasswordType;
use Chill\MainBundle\Security\ChillSecurity;
use Chill\MainBundle\Security\PasswordRecover\PasswordRecoverEvent;
use Chill\MainBundle\Security\PasswordRecover\PasswordRecoverVoter;
use Chill\MainBundle\Security\PasswordRecover\RecoverPasswordHelper;
@@ -24,6 +25,7 @@ use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Validator\Constraints\Callback;
@@ -33,56 +35,12 @@ use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class PasswordController.
*/
class PasswordController extends AbstractController
final class PasswordController extends AbstractController
{
/**
* @var LoggerInterface
*/
protected $chillLogger;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var UserPasswordEncoderInterface
*/
protected $passwordEncoder;
/**
* @var RecoverPasswordHelper
*/
protected $recoverPasswordHelper;
/**
* @var TokenManager
*/
protected $tokenManager;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* PasswordController constructor.
*/
public function __construct(
LoggerInterface $chillLogger,
UserPasswordEncoderInterface $passwordEncoder,
RecoverPasswordHelper $recoverPasswordHelper,
TokenManager $tokenManager,
TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher
) {
$this->chillLogger = $chillLogger;
$this->passwordEncoder = $passwordEncoder;
$this->translator = $translator;
$this->tokenManager = $tokenManager;
$this->recoverPasswordHelper = $recoverPasswordHelper;
$this->eventDispatcher = $eventDispatcher;
}
public function __construct(private readonly LoggerInterface $chillLogger, private readonly UserPasswordEncoderInterface $passwordEncoder, private readonly RecoverPasswordHelper $recoverPasswordHelper, private readonly TokenManager $tokenManager, private readonly TranslatorInterface $translator, private readonly EventDispatcherInterface $eventDispatcher, private readonly ChillSecurity $security) {}
/**
* @return Response
@@ -250,8 +208,11 @@ class PasswordController extends AbstractController
*/
public function UserPasswordAction(Request $request)
{
if (!$this->security->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException();
}
// get authentified user
$user = $this->getUser();
$user = $this->security->getUser();
// create a form for password_encoder
$form = $this->passwordForm($user);
@@ -269,7 +230,7 @@ class PasswordController extends AbstractController
'update password for an user',
[
'method' => $request->getMethod(),
'user' => $user->getUsername(),
'user' => $user->getUserIdentifier(),
]
);

View File

@@ -199,7 +199,7 @@ class SearchController extends AbstractController
{
// TODO this is an incomplete implementation
$query = $request->query->get('q', '');
$types = $request->query->get('type', []);
$types = $request->query->all('type');
if (0 === \count($types)) {
throw new BadRequestHttpException('The request must contains at one type');

View File

@@ -13,13 +13,17 @@ namespace Chill\MainBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Security\ChillSecurity;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
class UserApiController extends ApiController
{
public function __construct(private readonly ChillSecurity $security) {}
/**
* @Route(
* "/api/1.0/main/user-current-location.{_format}",
@@ -31,8 +35,12 @@ class UserApiController extends ApiController
*/
public function currentLocation(mixed $_format): JsonResponse
{
if (!$this->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException();
}
return $this->json(
$this->getUser()->getCurrentLocation(),
$this->security->getUser()->getCurrentLocation(),
JsonResponse::HTTP_OK,
[],
['groups' => ['read']]

View File

@@ -20,6 +20,7 @@ use Chill\MainBundle\Form\UserPasswordType;
use Chill\MainBundle\Form\UserType;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Security\ChillSecurity;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
@@ -38,7 +39,15 @@ class UserController extends CRUDController
{
final public const FORM_GROUP_CENTER_COMPOSED = 'composed_groupcenter';
public function __construct(private readonly LoggerInterface $logger, private readonly ValidatorInterface $validator, private readonly UserPasswordEncoderInterface $passwordEncoder, private readonly UserRepository $userRepository, protected ParameterBagInterface $parameterBag, private readonly TranslatorInterface $translator) {}
public function __construct(
private readonly LoggerInterface $logger,
private readonly ValidatorInterface $validator,
private readonly UserPasswordEncoderInterface $passwordEncoder,
private readonly UserRepository $userRepository,
protected ParameterBagInterface $parameterBag,
private readonly TranslatorInterface $translator,
private readonly ChillSecurity $security
) {}
/**
* @Route("/{_locale}/admin/main/user/{uid}/add_link_groupcenter",
@@ -197,7 +206,7 @@ class UserController extends CRUDController
*/
public function editCurrentLocationAction(Request $request)
{
$user = $this->getUser();
$user = $this->security->getUser();
$form = $this->createForm(UserCurrentLocationType::class, $user)
->add('submit', SubmitType::class, ['label' => 'Save'])
->handleRequest($request);

View File

@@ -12,18 +12,21 @@ declare(strict_types=1);
namespace Chill\MainBundle\Controller;
use Chill\MainBundle\Form\UserPhonenumberType;
use Chill\MainBundle\Security\ChillSecurity;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Routing\Annotation\Route;
class UserProfileController extends AbstractController
final class UserProfileController extends AbstractController
{
public function __construct(
private readonly TranslatorInterface $translator,
private readonly ChillSecurity $security,
) {}
/**
@@ -33,7 +36,11 @@ class UserProfileController extends AbstractController
*/
public function __invoke(Request $request)
{
$user = $this->getUser();
if (!$this->security->isGranted('ROLE_USER')) {
throw new AccessDeniedHttpException();
}
$user = $this->security->getUser();
$editForm = $this->createPhonenumberEditForm($user);
$editForm->handleRequest($request);

View File

@@ -20,6 +20,7 @@ use Chill\MainBundle\Form\WorkflowStepType;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Repository\Workflow\EntityWorkflowRepository;
use Chill\MainBundle\Security\Authorization\EntityWorkflowVoter;
use Chill\MainBundle\Security\ChillSecurity;
use Chill\MainBundle\Workflow\EntityWorkflowManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -27,10 +28,9 @@ use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
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\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Workflow\Registry;
use Symfony\Component\Workflow\TransitionBlocker;
@@ -38,7 +38,7 @@ use Symfony\Contracts\Translation\TranslatorInterface;
class WorkflowController extends AbstractController
{
public function __construct(private readonly EntityWorkflowManager $entityWorkflowManager, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly ValidatorInterface $validator, private readonly PaginatorFactory $paginatorFactory, private readonly Registry $registry, private readonly EntityManagerInterface $entityManager, private readonly TranslatorInterface $translator, private readonly Security $security) {}
public function __construct(private readonly EntityWorkflowManager $entityWorkflowManager, private readonly EntityWorkflowRepository $entityWorkflowRepository, private readonly ValidatorInterface $validator, private readonly PaginatorFactory $paginatorFactory, private readonly Registry $registry, private readonly EntityManagerInterface $entityManager, private readonly TranslatorInterface $translator, private readonly ChillSecurity $security) {}
/**
* @Route("/{_locale}/main/workflow/create", name="chill_main_workflow_create")
@@ -62,7 +62,7 @@ class WorkflowController extends AbstractController
->setRelatedEntityClass($request->query->get('entityClass'))
->setRelatedEntityId($request->query->getInt('entityId'))
->setWorkflowName($request->query->get('workflow'))
->addSubscriberToFinal($this->getUser());
->addSubscriberToFinal($this->security->getUser());
$errors = $this->validator->validate($entityWorkflow, null, ['creation']);
@@ -123,17 +123,17 @@ class WorkflowController extends AbstractController
}
if (!$this->getUser() instanceof User) {
throw new AccessDeniedException('Not a valid user');
throw new AccessDeniedHttpException('Not a valid user');
}
if ($entityWorkflowStep->getAccessKey() !== $accessKey) {
throw new AccessDeniedException('Access key is invalid');
throw new AccessDeniedHttpException('Access key is invalid');
}
if (!$entityWorkflowStep->isWaitingForTransition()) {
$this->addFlash('error', $this->translator->trans('workflow.Steps is not waiting for transition. Maybe someone apply the transition before you ?'));
} else {
$entityWorkflowStep->addDestUserByAccessKey($this->getUser());
$entityWorkflowStep->addDestUserByAccessKey($this->security->getUser());
$this->entityManager->flush();
$this->addFlash('success', $this->translator->trans('workflow.You get access to this step'));
}
@@ -150,11 +150,11 @@ class WorkflowController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$total = $this->entityWorkflowRepository->countByPreviousTransitionned($this->getUser());
$total = $this->entityWorkflowRepository->countByPreviousTransitionned($this->security->getUser());
$paginator = $this->paginatorFactory->create($total);
$workflows = $this->entityWorkflowRepository->findByPreviousTransitionned(
$this->getUser(),
$this->security->getUser(),
['createdAt' => 'DESC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
@@ -180,11 +180,11 @@ class WorkflowController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$total = $this->entityWorkflowRepository->countByPreviousDestWithoutReaction($this->getUser());
$total = $this->entityWorkflowRepository->countByPreviousDestWithoutReaction($this->security->getUser());
$paginator = $this->paginatorFactory->create($total);
$workflows = $this->entityWorkflowRepository->findByPreviousDestWithoutReaction(
$this->getUser(),
$this->security->getUser(),
['createdAt' => 'DESC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
@@ -208,11 +208,11 @@ class WorkflowController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$total = $this->entityWorkflowRepository->countByDest($this->getUser());
$total = $this->entityWorkflowRepository->countByDest($this->security->getUser());
$paginator = $this->paginatorFactory->create($total);
$workflows = $this->entityWorkflowRepository->findByCc(
$this->getUser(),
$this->security->getUser(),
['createdAt' => 'DESC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
@@ -235,11 +235,11 @@ class WorkflowController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$total = $this->entityWorkflowRepository->countByDest($this->getUser());
$total = $this->entityWorkflowRepository->countByDest($this->security->getUser());
$paginator = $this->paginatorFactory->create($total);
$workflows = $this->entityWorkflowRepository->findByDest(
$this->getUser(),
$this->security->getUser(),
['createdAt' => 'DESC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()
@@ -262,11 +262,11 @@ class WorkflowController extends AbstractController
{
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
$total = $this->entityWorkflowRepository->countBySubscriber($this->getUser());
$total = $this->entityWorkflowRepository->countBySubscriber($this->security->getUser());
$paginator = $this->paginatorFactory->create($total);
$workflows = $this->entityWorkflowRepository->findBySubscriber(
$this->getUser(),
$this->security->getUser(),
['createdAt' => 'DESC'],
$paginator->getItemsPerPage(),
$paginator->getCurrentPageFirstItemNumber()

View File

@@ -364,6 +364,11 @@ class User implements UserInterface, \Stringable, PasswordAuthenticatedUserInter
return $this->username;
}
public function getUserIdentifier(): string
{
return $this->username;
}
/**
* @return string
*/