Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
8 / 16 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Authentication | |
50.00% |
8 / 16 |
|
55.56% |
5 / 9 |
30.00 | |
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 | |||
| acceptLocale | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| locale | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| dataLocale | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| localeObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| restrictedToLoggedInUsers | |
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\Attributes\RuntimeCheck; |
| 8 | use Apie\Core\BoundedContext\BoundedContext; |
| 9 | use Apie\Core\Context\ApieContext; |
| 10 | use Apie\Core\ContextConstants; |
| 11 | use Apie\Core\Datalayers\ApieDatalayer; |
| 12 | use Apie\Core\Entities\EntityInterface; |
| 13 | use Apie\IanaValueObjects\LanguageAndRegion; |
| 14 | use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\UserIdentifier; |
| 15 | use Apie\IntegrationTests\Apie\TypeDemo\Resources\User; |
| 16 | use Apie\IntegrationTests\Apie\TypeDemo\Rules\RequiresAuthenticatedUser; |
| 17 | use Exception; |
| 18 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 19 | |
| 20 | class Authentication |
| 21 | { |
| 22 | #[Description('Login as user with username and password, returns null if authentication fails')] |
| 23 | public function verifyAuthentication( |
| 24 | #[Context()] ApieDatalayer $apieDatalayer, |
| 25 | #[Context()] BoundedContext $boundedContext, |
| 26 | string $username, |
| 27 | string $password |
| 28 | ): ?User { |
| 29 | try { |
| 30 | /** @var UserIdentifier @userId */ |
| 31 | $userId = UserIdentifier::fromNative($username); |
| 32 | $user = $apieDatalayer->find($userId, $boundedContext->getId()); |
| 33 | } catch (Exception) { |
| 34 | return null; |
| 35 | } |
| 36 | return $user->verify($password) ? $user : null; |
| 37 | } |
| 38 | |
| 39 | #[Description('Display the current logged in user, returns null if not logged in')] |
| 40 | #[Route('/me')] |
| 41 | #[Route('/profile', target: Route::CMS)] |
| 42 | public function currentUser(#[Context('authenticated')] ?EntityInterface $currentUser = null): ?EntityInterface |
| 43 | { |
| 44 | return $currentUser; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @return array<string|int, mixed> |
| 49 | */ |
| 50 | public function currentSession(#[Context] SessionInterface $sessionInterface): array |
| 51 | { |
| 52 | return $sessionInterface->all(); |
| 53 | } |
| 54 | |
| 55 | public function acceptLocale(#[Context(ContextConstants::ACCEPT_LOCALE)] ?string $locale = null): ?string |
| 56 | { |
| 57 | return $locale; |
| 58 | } |
| 59 | |
| 60 | public function locale(#[Context(ContextConstants::LOCALE)] ?string $locale = null): ?string |
| 61 | { |
| 62 | return $locale; |
| 63 | } |
| 64 | |
| 65 | public function dataLocale(#[Context(ContextConstants::DATA_LOCALE)] ?string $locale = null): ?string |
| 66 | { |
| 67 | return $locale; |
| 68 | } |
| 69 | |
| 70 | public function localeObject(#[Context] ?LanguageAndRegion $locale = null): ?LanguageAndRegion |
| 71 | { |
| 72 | return $locale; |
| 73 | } |
| 74 | |
| 75 | #[Description('Only accessible to logged in users, used to verify authorization errors return the correct HTTP status code')] |
| 76 | #[RuntimeCheck(new RequiresAuthenticatedUser())] |
| 77 | public function restrictedToLoggedInUsers(): string |
| 78 | { |
| 79 | return 'secret'; |
| 80 | } |
| 81 | |
| 82 | public function isThisMe(#[Context] ApieContext $apieContext, UserIdentifier $userId): bool |
| 83 | { |
| 84 | $authenticated = $apieContext->getContext(ContextConstants::AUTHENTICATED_USER, false); |
| 85 | if (!$authenticated) { |
| 86 | return false; |
| 87 | } |
| 88 | return $authenticated->getId()->toNative() === $userId->toNative(); |
| 89 | } |
| 90 | } |