From 6f01931d0c1318030ed9c4678c4523714871a4a1 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 6 May 2020 01:01:13 +0100 Subject: [PATCH 1/4] Auth and forms --- src/Config/routes.php | 4 +- src/Controllers/AuthController.php | 38 ++++++++++---- .../Listeners/AuthenticationListener.php | 9 ++++ src/Form/LoginForm.php | 51 +++++++++++++++++++ 4 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 src/Form/LoginForm.php diff --git a/src/Config/routes.php b/src/Config/routes.php index 955dc65..530fea5 100644 --- a/src/Config/routes.php +++ b/src/Config/routes.php @@ -11,8 +11,8 @@ ]); $routes->addGet('/admin', [ - 'controller' => 1, - 'action' => 2, + 'controller' => 'auth', + 'action' => 'index', ]); $routes->addGet('/admin/auth', [ diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 1aa5b81..9be299f 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -22,6 +22,8 @@ public function loginAction(): void { $this->tag->setTitle('Phlexus CMS'); $this->view->setMainView('layouts/base'); + + $this->view->form = new \Phlexus\Modules\BaseAdmin\Form\LoginForm(); } /** @@ -30,21 +32,37 @@ public function loginAction(): void * @return ResponseInterface */ public function doLoginAction(): ResponseInterface - { + { $this->view->disable(); - if (!$this->request->isPost()) { - return $this->response->redirect('admin/auth'); - } + if ($this->request->isPost()) { + $form = new \Phlexus\Modules\BaseAdmin\Form\LoginForm(); - $email = $this->request->getPost('email'); - $password = $this->request->getPost('password'); + $data = $this->request->getPost(); - $login = $this->auth->login([ - 'email' => $email, - 'password' => $password, - ]); + try { + if (!$form->isValid($data)) { + foreach ($form->getMessages() as $message) { + $this->flash->error($message->getMessage()); + } + + return $this->response->redirect('admin/auth'); + } + + $email = $data['email']; + $password = $data['password']; + + $login = $this->auth->login([ + 'email' => $email, + 'password' => $password, + ]); + } catch (AuthException $e) { + $this->flash->error($e->getMessage()); + } + } + if ($login === false) { + $this->flash->error('Invalid auth data!'); return $this->response->redirect('admin/auth'); } diff --git a/src/Events/Listeners/AuthenticationListener.php b/src/Events/Listeners/AuthenticationListener.php index 2a65ad7..d66940e 100644 --- a/src/Events/Listeners/AuthenticationListener.php +++ b/src/Events/Listeners/AuthenticationListener.php @@ -8,6 +8,7 @@ use Phalcon\Mvc\DispatcherInterface; use Phlexus\Libraries\Auth\AuthException; use Phlexus\Modules\BaseAdmin\Module as AdminModule; +use Phlexus\Libraries\Auth\Manager as AuthManager; final class AuthenticationListener extends Injectable { @@ -29,6 +30,14 @@ public function beforeDispatchLoop(Event $event, DispatcherInterface $dispatcher new AuthException('User is not authorized.') ); } + + // TODO: Verify if user can login or passwords exceeded + $this->getDI()->getShared('eventsManager')->attach( + 'auth:beforeLogin', + function (Event $event, AuthManager $manager, $data) { + return true; + } + ); return !$event->isStopped(); } diff --git a/src/Form/LoginForm.php b/src/Form/LoginForm.php new file mode 100644 index 0000000..2db1bf4 --- /dev/null +++ b/src/Form/LoginForm.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace Phlexus\Modules\BaseAdmin\Form; + +use Phlexus\Form\FormBase; +use Phalcon\Forms\Element\Email; +use Phalcon\Forms\Element\Password; +use Phalcon\Validation\Validator\PresenceOf; + +class LoginForm extends FormBase +{ + + /** + * Initialize form + */ + public function initialize() + { + parent::initialize(); + + $email = new Email('email', [ + 'required' => true, + 'class' => 'form-control', + 'placeholder' => 'Email' + ]); + + $email->addValidator(new PresenceOf(['message' => 'Email is required'])); + + $this->add($email); + + $password = new Password('password', [ + 'required' => true, + 'class' => 'form-control', + 'placeholder' => 'Password' + ]); + + $password->addValidator(new PresenceOf(['message' => 'Password is required'])); + + $this->add($password); + } +} From 89f93f3455c8f10c44bb4cdd8658742b8b771390 Mon Sep 17 00:00:00 2001 From: falcon758 Date: Wed, 6 May 2020 22:43:14 +0100 Subject: [PATCH 2/4] Update src/Controllers/AuthController.php Co-authored-by: Anton Vasiliev --- src/Controllers/AuthController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 9be299f..2ca530e 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -32,7 +32,7 @@ public function loginAction(): void * @return ResponseInterface */ public function doLoginAction(): ResponseInterface - { + { $this->view->disable(); if ($this->request->isPost()) { From 8f86fa6bea8a593c17484676cbe71e900c6f50ba Mon Sep 17 00:00:00 2001 From: falcon758 Date: Wed, 6 May 2020 22:43:31 +0100 Subject: [PATCH 3/4] Update src/Form/LoginForm.php Co-authored-by: Anton Vasiliev --- src/Form/LoginForm.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Form/LoginForm.php b/src/Form/LoginForm.php index 2db1bf4..5c558c1 100644 --- a/src/Form/LoginForm.php +++ b/src/Form/LoginForm.php @@ -20,7 +20,6 @@ class LoginForm extends FormBase { - /** * Initialize form */ From 9c9dc5feeedbd1db8bd7764d677d662dcc08ac43 Mon Sep 17 00:00:00 2001 From: Luis Campos Date: Sun, 7 Feb 2021 19:56:21 +0000 Subject: [PATCH 4/4] Reset Work --- src/Config/routes.php | 4 +-- src/Controllers/AuthController.php | 36 +++++-------------- .../Listeners/AuthenticationListener.php | 15 ++++---- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/Config/routes.php b/src/Config/routes.php index 530fea5..955dc65 100644 --- a/src/Config/routes.php +++ b/src/Config/routes.php @@ -11,8 +11,8 @@ ]); $routes->addGet('/admin', [ - 'controller' => 'auth', - 'action' => 'index', + 'controller' => 1, + 'action' => 2, ]); $routes->addGet('/admin/auth', [ diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 2ca530e..1aa5b81 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -22,8 +22,6 @@ public function loginAction(): void { $this->tag->setTitle('Phlexus CMS'); $this->view->setMainView('layouts/base'); - - $this->view->form = new \Phlexus\Modules\BaseAdmin\Form\LoginForm(); } /** @@ -35,34 +33,18 @@ public function doLoginAction(): ResponseInterface { $this->view->disable(); - if ($this->request->isPost()) { - $form = new \Phlexus\Modules\BaseAdmin\Form\LoginForm(); - - $data = $this->request->getPost(); - - try { - if (!$form->isValid($data)) { - foreach ($form->getMessages() as $message) { - $this->flash->error($message->getMessage()); - } + if (!$this->request->isPost()) { + return $this->response->redirect('admin/auth'); + } - return $this->response->redirect('admin/auth'); - } + $email = $this->request->getPost('email'); + $password = $this->request->getPost('password'); - $email = $data['email']; - $password = $data['password']; - - $login = $this->auth->login([ - 'email' => $email, - 'password' => $password, - ]); - } catch (AuthException $e) { - $this->flash->error($e->getMessage()); - } - } - + $login = $this->auth->login([ + 'email' => $email, + 'password' => $password, + ]); if ($login === false) { - $this->flash->error('Invalid auth data!'); return $this->response->redirect('admin/auth'); } diff --git a/src/Events/Listeners/AuthenticationListener.php b/src/Events/Listeners/AuthenticationListener.php index d66940e..2a66c6a 100644 --- a/src/Events/Listeners/AuthenticationListener.php +++ b/src/Events/Listeners/AuthenticationListener.php @@ -8,7 +8,6 @@ use Phalcon\Mvc\DispatcherInterface; use Phlexus\Libraries\Auth\AuthException; use Phlexus\Modules\BaseAdmin\Module as AdminModule; -use Phlexus\Libraries\Auth\Manager as AuthManager; final class AuthenticationListener extends Injectable { @@ -30,14 +29,14 @@ public function beforeDispatchLoop(Event $event, DispatcherInterface $dispatcher new AuthException('User is not authorized.') ); } - + // TODO: Verify if user can login or passwords exceeded - $this->getDI()->getShared('eventsManager')->attach( - 'auth:beforeLogin', - function (Event $event, AuthManager $manager, $data) { - return true; - } - ); + //$this->getDI()->getShared('eventsManager')->attach( + // 'auth:beforeLogin', + // function (Event $event, AuthManager $manager, $data) { + // return true; + // } + // ); return !$event->isStopped(); }