Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
36.36% |
4 / 11 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Authentication | |
36.36% |
4 / 11 |
|
25.00% |
1 / 4 |
19.63 | |
0.00% |
0 / 1 |
| verifyAuthentication | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
3.58 | |||
| currentUser | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| currentSession | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isThisMe | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | namespace Apie\IntegrationTests\Apie\TypeDemo\Actions; |
| 3 | |
| 4 | use Apie\Core\Attributes\Context; |
| 5 | use Apie\Core\Attributes\Description; |
| 6 | use Apie\Core\Attributes\Route; |
| 7 | use Apie\Core\BoundedContext\BoundedContext; |
| 8 | use Apie\Core\Context\ApieContext; |
| 9 | use Apie\Core\ContextConstants; |
| 10 | use Apie\Core\Datalayers\ApieDatalayer; |
| 11 | use Apie\Core\Entities\EntityInterface; |
| 12 | use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\UserIdentifier; |
| 13 | use Apie\IntegrationTests\Apie\TypeDemo\Resources\User; |
| 14 | use Exception; |
| 15 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 16 | |
| 17 | class Authentication |
| 18 | { |
| 19 | #[Description('Login as user with username and password, returns null if authentication fails')] |
| 20 | public function verifyAuthentication( |
| 21 | #[Context()] ApieDatalayer $apieDatalayer, |
| 22 | #[Context()] BoundedContext $boundedContext, |
| 23 | string $username, |
| 24 | string $password |
| 25 | ): ?User { |
| 26 | try { |
| 27 | /** @var UserIdentifier @userId */ |
| 28 | $userId = UserIdentifier::fromNative($username); |
| 29 | $user = $apieDatalayer->find($userId, $boundedContext->getId()); |
| 30 | } catch (Exception) { |
| 31 | return null; |
| 32 | } |
| 33 | return $user->verify($password) ? $user : null; |
| 34 | } |
| 35 | |
| 36 | #[Description('Display the current logged in user, returns null if not logged in')] |
| 37 | #[Route('/me')] |
| 38 | #[Route('/profile', target: Route::CMS)] |
| 39 | public function currentUser(#[Context('authenticated')] ?EntityInterface $currentUser = null): ?EntityInterface |
| 40 | { |
| 41 | return $currentUser; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @return array<string|int, mixed> |
| 46 | */ |
| 47 | public function currentSession(#[Context] SessionInterface $sessionInterface): array |
| 48 | { |
| 49 | return $sessionInterface->all(); |
| 50 | } |
| 51 | |
| 52 | public function isThisMe(#[Context] ApieContext $apieContext, UserIdentifier $userId): bool |
| 53 | { |
| 54 | $authenticated = $apieContext->getContext(ContextConstants::AUTHENTICATED_USER, false); |
| 55 | if (!$authenticated) { |
| 56 | return false; |
| 57 | } |
| 58 | return $authenticated->getId()->toNative() === $userId->toNative(); |
| 59 | } |
| 60 | } |