From aec81ecf7834be4c4cfd7697fe5e6b34208c61a6 Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 16 Apr 2019 00:43:49 +0300 Subject: [PATCH] Flextype Slim Integration - next round of integration --- site/plugins/admin/classes/UsersManager.php | 91 +++++++++++---------- 1 file changed, 49 insertions(+), 42 deletions(-) diff --git a/site/plugins/admin/classes/UsersManager.php b/site/plugins/admin/classes/UsersManager.php index fdc41f27..e4293e91 100644 --- a/site/plugins/admin/classes/UsersManager.php +++ b/site/plugins/admin/classes/UsersManager.php @@ -7,51 +7,43 @@ use Flextype\Component\Session\Session; use Flextype\Component\Registry\Registry; use Flextype\Component\Text\Text; use function Flextype\Component\I18n\__; - use Slim\Http\Request; use Slim\Http\Response; +use Psr\Container\ContainerInterface; -$app->get('/admin/login', function (Request $request, Response $response, array $args) { - return $this->view->render($response, - 'plugins/admin/views/templates/users/login.html'); -})->setName('login'); +$app->get('/admin/login', UsersController::class . ':login')->setName('admin.login'); +$app->get('/admin/profile', UsersController::class . ':profile')->setName('admin.profile'); +$app->post('/admin/login', UsersController::class . ':processLoginForm'); +$app->get('/admin/logout', UsersController::class . ':processLogoutForm')->setName('admin.logout'); +$app->get('/admin/registration', UsersController::class . ':registration')->setName('admin.registration'); +$app->post('/admin/registration', UsersController::class . ':processRegistrationForm'); -$app->get('/admin/profile', function (Request $request, Response $response, array $args) { - return $this->view->render($response, - 'plugins/admin/views/templates/users/profile.html', [ - 'username' => Session::get('username'), - 'rolename' => Session::get('role'), - 'sidebar_menu_item' => 'profile' - ]); -})->setName('profile'); +class UsersController { -$app->get('/admin/logout', function (Request $request, Response $response, array $args) { - Session::destroy(); - return $response->withRedirect('/admin'); -}); + protected $container; -$app->get('/admin/registration', function (Request $request, Response $response, array $args) { - return $this->view->render($response, - 'plugins/admin/views/templates/users/registration.html'); -})->setName('registration'); - -$app->post('/admin/registration', function (Request $request, Response $response, array $args) { - if (UsersManager::processRegistrationForm($request->getParsedBody())) { - return $response->withRedirect('admin'); + // constructor receives container instance + public function __construct(ContainerInterface $container) { + $this->container = $container; } -}); -$app->post('/admin/login', function (Request $request, Response $response, array $args) { - if (UsersManager::processLoginForm($request->getParsedBody())) { - return $response->withRedirect('admin/entries'); - } else { - Notification::set('error', __('admin_message_wrong_username_password')); + public function login($request, $response, $args) + { + return $this->container->get('view')->render($response, + 'plugins/admin/views/templates/users/login.html'); } -}); -class UsersManager -{ - public static function processLoginForm(array $data) : bool + public function profile($request, $response, $args) + { + return $this->container->get('view')->render($response, + 'plugins/admin/views/templates/users/profile.html', [ + 'username' => Session::get('username'), + 'rolename' => Session::get('role'), + 'sidebar_menu_item' => 'profile' + ]); + } + + public function processLoginForm($request, $response, $args) { if (Filesystem::has($_user_file = PATH['site'] . '/accounts/' . $data['username'] . '.yaml')) { @@ -61,17 +53,29 @@ class UsersManager Session::set('username', $user_file['username']); Session::set('role', $user_file['role']); - return true; + return $response->withRedirect('admin/entries'); } else { - return false; + //Notification::set('error', __('admin_message_wrong_username_password')); } } else { - return false; + //Notification::set('error', __('admin_message_wrong_username_password')); } } - public static function processRegistrationForm(array $data) : bool + public function processLogoutForm($request, $response, $args) + { + Session::destroy(); + return $response->withRedirect('/admin'); + } + + public function registration($request, $response, $args) + { + return $this->view->render($response, + 'plugins/admin/views/templates/users/registration.html'); + } + + public function processRegistrationForm($request, $response, $args) { if (!Filesystem::has($_user_file = PATH['site'] . '/accounts/' . Text::safeString($data['username']) . '.yaml')) { if (Filesystem::write( @@ -81,15 +85,18 @@ class UsersManager 'email' => $data['email'], 'role' => 'admin', 'state' => 'enabled']))) { - return true; + return $response->withRedirect('admin/entries'); } else { - return false; + //return false; } } else { - return false; + //return false; } } +} +class Users +{ public static function isUsersExists() : bool { // Get Users Profiles