Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
LoginEndpoint | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
30 | |
0.00% |
0 / 1 |
handle | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace Olz\Api\Endpoints; |
4 | |
5 | use Olz\Api\OlzTypedEndpoint; |
6 | use Olz\Exceptions\AuthBlockedException; |
7 | use Olz\Exceptions\InvalidCredentialsException; |
8 | |
9 | /** |
10 | * @extends OlzTypedEndpoint< |
11 | * array{ |
12 | * usernameOrEmail: non-empty-string, |
13 | * password: non-empty-string, |
14 | * rememberMe: bool, |
15 | * }, |
16 | * array{ |
17 | * status: 'AUTHENTICATED'|'INVALID_CREDENTIALS'|'BLOCKED', |
18 | * numRemainingAttempts: ?int<0, max>, |
19 | * } |
20 | * > |
21 | */ |
22 | class LoginEndpoint extends OlzTypedEndpoint { |
23 | protected function handle(mixed $input): mixed { |
24 | $username_or_email = trim($input['usernameOrEmail']); |
25 | $password = $input['password']; |
26 | $remember_me = $input['rememberMe']; |
27 | |
28 | try { |
29 | $user = $this->authUtils()->authenticate($username_or_email, $password); |
30 | } catch (AuthBlockedException $exc) { |
31 | return [ |
32 | 'status' => 'BLOCKED', |
33 | 'numRemainingAttempts' => 0, |
34 | ]; |
35 | } catch (InvalidCredentialsException $exc) { |
36 | return [ |
37 | 'status' => 'INVALID_CREDENTIALS', |
38 | 'numRemainingAttempts' => $exc->getNumRemainingAttempts(), |
39 | ]; |
40 | } |
41 | |
42 | $now_datetime = new \DateTime($this->dateUtils()->getIsoNow()); |
43 | $user->setLastLoginAt($now_datetime); |
44 | $this->entityManager()->flush(); |
45 | |
46 | $this->session()->resetConfigure([ |
47 | 'timeout' => $remember_me ? 2419200 : 3600, // a month / an hour |
48 | ]); |
49 | |
50 | $root = $user->getRoot() !== '' ? $user->getRoot() : './'; |
51 | $this->session()->set('auth', $user->getPermissions()); |
52 | $this->session()->set('root', $root); |
53 | $this->session()->set('user', $user->getUsername()); |
54 | $this->session()->set('user_id', "{$user->getId()}"); |
55 | $this->session()->set('auth_user', $user->getUsername()); |
56 | $this->session()->set('auth_user_id', "{$user->getId()}"); |
57 | return [ |
58 | 'status' => 'AUTHENTICATED', |
59 | 'numRemainingAttempts' => null, |
60 | ]; |
61 | } |
62 | } |