1 | <?php |
||
2 | |||
3 | |||
4 | namespace Apps\Controller\Front\User; |
||
5 | |||
6 | use Apps\Model\Front\User\FormLogin; |
||
7 | use Ffcms\Core\App; |
||
8 | use Ffcms\Core\Arch\View; |
||
9 | use Ffcms\Core\Exception\ForbiddenException; |
||
10 | use Ffcms\Core\Exception\SyntaxException; |
||
11 | use Ffcms\Core\Network\Request; |
||
12 | use Ffcms\Core\Network\Response; |
||
13 | |||
14 | /** |
||
15 | * Trait ActionLogin |
||
16 | * @package Apps\Controller\Front\User |
||
17 | * @property View $view |
||
18 | * @property Response $response |
||
19 | * @property Request $request |
||
20 | * @method array getConfigs |
||
21 | */ |
||
22 | trait ActionLogin |
||
23 | { |
||
24 | /** |
||
25 | * View login form and process submit action |
||
26 | * @throws ForbiddenException |
||
27 | * @throws SyntaxException |
||
28 | */ |
||
29 | public function login(): ?string |
||
30 | { |
||
31 | // check if user is always authorized |
||
32 | if (App::$User->isAuth()) { |
||
33 | throw new ForbiddenException(__('You are always authorized on website')); |
||
34 | } |
||
35 | |||
36 | $configs = $this->getConfigs(); |
||
37 | // load login model |
||
38 | $loginForm = new FormLogin((bool)$configs['captchaOnLogin']); |
||
39 | |||
40 | // build redirect back route |
||
41 | $redirectRoute = '/'; |
||
42 | if ($this->request->query->has('r') && !preg_match('/[^A-Za-z0-9\/]/i', $this->request->query->get('r'))) { |
||
43 | $redirectRoute = $this->request->query->get('r'); |
||
44 | } |
||
45 | |||
46 | // check if data is send and valid |
||
47 | if ($loginForm->send() && $loginForm->validate()) { |
||
48 | if ($loginForm->tryAuth()) { |
||
49 | // initialize success event |
||
50 | App::$Event->run(static::EVENT_USER_LOGIN_SUCCESS, [ |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
51 | 'model' => $loginForm |
||
52 | ]); |
||
53 | $this->response->redirect($redirectRoute); // void header change & exit() |
||
54 | } |
||
55 | App::$Session->getFlashBag()->add('error', __('User is never exist or password is incorrect!')); |
||
56 | // initialize fail event |
||
57 | App::$Event->run(static::EVENT_USER_LOGIN_FAIL, [ |
||
0 ignored issues
–
show
|
|||
58 | 'model' => $loginForm |
||
59 | ]); |
||
60 | } |
||
61 | |||
62 | // render view |
||
63 | return $this->view->render('user/login', [ |
||
64 | 'model' => $loginForm, |
||
65 | 'useCaptcha' => $configs['captchaOnLogin'] === 1, |
||
66 | 'redirect' => $redirectRoute |
||
67 | ]); |
||
68 | } |
||
69 | } |
||
70 |