Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.00% covered (danger)
50.00%
8 / 16
55.56% covered (warning)
55.56%
5 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Authentication
50.00% covered (danger)
50.00%
8 / 16
55.56% covered (warning)
55.56%
5 / 9
30.00
0.00% covered (danger)
0.00%
0 / 1
 verifyAuthentication
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 currentUser
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 currentSession
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 acceptLocale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 locale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 dataLocale
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 localeObject
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 restrictedToLoggedInUsers
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isThisMe
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Apie\IntegrationTests\Apie\TypeDemo\Actions;
3
4use Apie\Core\Attributes\Context;
5use Apie\Core\Attributes\Description;
6use Apie\Core\Attributes\Route;
7use Apie\Core\Attributes\RuntimeCheck;
8use Apie\Core\BoundedContext\BoundedContext;
9use Apie\Core\Context\ApieContext;
10use Apie\Core\ContextConstants;
11use Apie\Core\Datalayers\ApieDatalayer;
12use Apie\Core\Entities\EntityInterface;
13use Apie\IanaValueObjects\LanguageAndRegion;
14use Apie\IntegrationTests\Apie\TypeDemo\Identifiers\UserIdentifier;
15use Apie\IntegrationTests\Apie\TypeDemo\Resources\User;
16use Apie\IntegrationTests\Apie\TypeDemo\Rules\RequiresAuthenticatedUser;
17use Exception;
18use Symfony\Component\HttpFoundation\Session\SessionInterface;
19
20class 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}