Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| AuthController | |
100.00% |
21 / 21 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| login | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| showLogin | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| postLogin | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
2 | |||
| loginUser | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| logout | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Engelsystem\Controllers; |
| 6 | |
| 7 | use Carbon\Carbon; |
| 8 | use Engelsystem\Config\Config; |
| 9 | use Engelsystem\Helpers\Authenticator; |
| 10 | use Engelsystem\Http\Redirector; |
| 11 | use Engelsystem\Http\Request; |
| 12 | use Engelsystem\Http\Response; |
| 13 | use Engelsystem\Models\User\User; |
| 14 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 15 | |
| 16 | class AuthController extends BaseController |
| 17 | { |
| 18 | use HasUserNotifications; |
| 19 | |
| 20 | /** @var array<string, string> */ |
| 21 | protected array $permissions = [ |
| 22 | 'login' => 'login', |
| 23 | 'postLogin' => 'login', |
| 24 | ]; |
| 25 | |
| 26 | public function __construct( |
| 27 | protected Response $response, |
| 28 | protected SessionInterface $session, |
| 29 | protected Redirector $redirect, |
| 30 | protected Config $config, |
| 31 | protected Authenticator $auth |
| 32 | ) { |
| 33 | } |
| 34 | |
| 35 | public function login(): Response |
| 36 | { |
| 37 | return $this->showLogin(); |
| 38 | } |
| 39 | |
| 40 | protected function showLogin(): Response |
| 41 | { |
| 42 | return $this->response->withView('pages/login'); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Posted login form |
| 47 | */ |
| 48 | public function postLogin(Request $request): Response |
| 49 | { |
| 50 | $data = $this->validate($request, [ |
| 51 | 'login' => 'required', |
| 52 | 'password' => 'required', |
| 53 | ]); |
| 54 | |
| 55 | $user = $this->auth->authenticate($data['login'], $data['password']); |
| 56 | |
| 57 | if (!$user instanceof User) { |
| 58 | $this->addNotification('auth.not-found', NotificationType::ERROR); |
| 59 | |
| 60 | return $this->showLogin(); |
| 61 | } |
| 62 | |
| 63 | return $this->loginUser($user); |
| 64 | } |
| 65 | |
| 66 | public function loginUser(User $user): Response |
| 67 | { |
| 68 | $previousPage = $this->session->get('previous_page'); |
| 69 | |
| 70 | $this->session->invalidate(); |
| 71 | $this->session->set('user_id', $user->id); |
| 72 | $this->session->set('locale', $user->settings->language); |
| 73 | |
| 74 | $user->last_login_at = new Carbon(); |
| 75 | $user->save(['touch' => false]); |
| 76 | |
| 77 | return $this->redirect->to($previousPage ?: $this->config->get('home_site')); |
| 78 | } |
| 79 | |
| 80 | public function logout(): Response |
| 81 | { |
| 82 | $this->session->invalidate(); |
| 83 | |
| 84 | return $this->redirect->to('/'); |
| 85 | } |
| 86 | } |