-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lung
committed
Sep 21, 2023
1 parent
4f06726
commit 29a4e5d
Showing
27 changed files
with
671 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace kissj\Middleware; | ||
|
||
use kissj\FlashMessages\FlashMessagesInterface; | ||
use kissj\Participant\ParticipantRepository; | ||
use kissj\Participant\Patrol\PatrolLeader; | ||
use kissj\Participant\Patrol\PatrolService; | ||
use kissj\Participant\Troop\TroopLeader; | ||
use kissj\Participant\Troop\TroopParticipantRepository; | ||
use kissj\Participant\Troop\TroopService; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\RequestHandlerInterface as ResponseHandler; | ||
use Slim\Routing\RouteContext; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
/** | ||
* participants actions are allowed only for their Patrol Leader or for their Troop Leader | ||
*/ | ||
class CheckLeaderParticipants extends BaseMiddleware | ||
{ | ||
public function __construct( | ||
private readonly PatrolService $patrolService, | ||
private readonly TroopService $troopService, | ||
private readonly TroopParticipantRepository $troopParticipantRepository, | ||
private readonly ParticipantRepository $participantRepository, | ||
private readonly FlashMessagesInterface $flashMessages, | ||
private readonly TranslatorInterface $translator, | ||
) { | ||
} | ||
|
||
public function process(Request $request, ResponseHandler $handler): Response | ||
{ | ||
$route = RouteContext::fromRequest($request)->getRoute(); | ||
if ($route === null) { | ||
throw new \RuntimeException('Cannot access route in CheckLeaderParticipants middleware'); | ||
} | ||
|
||
$participantId = (int)$route->getArgument('participantId'); | ||
$leader = $this->participantRepository->getParticipantFromUser($this->getUser($request)); | ||
|
||
if ($leader instanceof PatrolLeader) { | ||
if (!$this->patrolService->patrolParticipantBelongsPatrolLeader( | ||
$this->patrolService->getPatrolParticipant($participantId), | ||
$leader, | ||
)) { | ||
$this->flashMessages->error($this->translator->trans('flash.error.wrongPatrol')); | ||
|
||
return $this->createRedirectResponse($request, 'dashboard'); | ||
} | ||
} elseif ($leader instanceof TroopLeader) { | ||
if (!$this->troopService->troopParticipantBelongsTroopLeader( | ||
$this->troopParticipantRepository->get($participantId), | ||
$leader, | ||
)) { | ||
$this->flashMessages->error($this->translator->trans('flash.error.wrongTroop')); | ||
|
||
return $this->createRedirectResponse($request, 'dashboard'); | ||
} | ||
} else { | ||
$this->flashMessages->error($this->translator->trans('flash.error.notLeader')); | ||
|
||
return $this->createRedirectResponse($request, 'dashboard'); | ||
} | ||
|
||
return $handler->handle($request); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace kissj\Middleware; | ||
|
||
use kissj\FlashMessages\FlashMessagesInterface; | ||
use kissj\Participant\ParticipantRepository; | ||
use kissj\Participant\ParticipantRole; | ||
use kissj\User\User; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\RequestHandlerInterface as ResponseHandler; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class TroopLeadersOnlyMiddleware extends BaseMiddleware | ||
{ | ||
public function __construct( | ||
private readonly FlashMessagesInterface $flashMessages, | ||
private readonly TranslatorInterface $translator, | ||
private readonly ParticipantRepository $participantRepository, | ||
) { | ||
} | ||
|
||
public function process(Request $request, ResponseHandler $handler): Response | ||
{ | ||
$user = $request->getAttribute('user'); | ||
|
||
if ( | ||
$user instanceof User | ||
&& $this->participantRepository->getParticipantFromUser($user)->role !== ParticipantRole::TroopLeader | ||
) { | ||
$this->flashMessages->error($this->translator->trans('flash.error.tlOnly')); | ||
|
||
return $this->createRedirectResponse($request, 'landing'); | ||
} | ||
|
||
return $handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace kissj\Middleware; | ||
|
||
use kissj\FlashMessages\FlashMessagesInterface; | ||
use kissj\Participant\ParticipantRepository; | ||
use kissj\Participant\ParticipantRole; | ||
use kissj\User\User; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Server\RequestHandlerInterface as ResponseHandler; | ||
use Symfony\Contracts\Translation\TranslatorInterface; | ||
|
||
class TroopParticipantsOnlyMiddleware extends BaseMiddleware | ||
{ | ||
public function __construct( | ||
private readonly FlashMessagesInterface $flashMessages, | ||
private readonly TranslatorInterface $translator, | ||
private readonly ParticipantRepository $participantRepository, | ||
) { | ||
} | ||
|
||
public function process(Request $request, ResponseHandler $handler): Response | ||
{ | ||
$user = $request->getAttribute('user'); | ||
|
||
if ( | ||
$user instanceof User | ||
&& $this->participantRepository->getParticipantFromUser($user)->role !== ParticipantRole::TroopParticipant | ||
) { | ||
$this->flashMessages->error($this->translator->trans('flash.error.tpOnly')); | ||
|
||
return $this->createRedirectResponse($request, 'landing'); | ||
} | ||
|
||
return $handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.