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