mirror of
https://github.com/flextype/flextype.git
synced 2025-08-16 01:54:28 +02:00
Admin Panel - Slim Integration - next round of integration
This commit is contained in:
@@ -17,6 +17,11 @@ use function Flextype\Component\I18n\__;
|
||||
use Gajus\Dindent\Indenter;
|
||||
use Intervention\Image\ImageManagerStatic as Image;
|
||||
|
||||
$app->get('/admin/entries', function (Request $request, Response $response, array $args) {
|
||||
return 'asd';
|
||||
})->setName('entries');
|
||||
|
||||
|
||||
class EntriesManager
|
||||
{
|
||||
|
||||
|
@@ -8,11 +8,11 @@ class NavigationManager
|
||||
{
|
||||
public static function addItem(string $area, string $item, string $title, string $link, array $attributes = []) : void
|
||||
{
|
||||
Registry::set("admin_navigation.{$area}.{$item}.area", $area);
|
||||
Registry::set("admin_navigation.{$area}.{$item}.item", $item);
|
||||
Registry::set("admin_navigation.{$area}.{$item}.title", $title);
|
||||
Registry::set("admin_navigation.{$area}.{$item}.link", $link);
|
||||
Registry::set("admin_navigation.{$area}.{$item}.attributes", $attributes);
|
||||
$flextype->registry->set("admin_navigation.{$area}.{$item}.area", $area);
|
||||
$flextype->registry->set("admin_navigation.{$area}.{$item}.item", $item);
|
||||
$flextype->registry->set("admin_navigation.{$area}.{$item}.title", $title);
|
||||
$flextype->registry->set("admin_navigation.{$area}.{$item}.link", $link);
|
||||
$flextype->registry->set("admin_navigation.{$area}.{$item}.attributes", $attributes);
|
||||
}
|
||||
|
||||
public static function getItems(string $area)
|
||||
|
@@ -9,37 +9,25 @@ use Flextype\Component\Filesystem\Filesystem;
|
||||
use Flextype\Component\Registry\Registry;
|
||||
use Flextype\Component\Token\Token;
|
||||
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
class PluginsManager
|
||||
{
|
||||
|
||||
/**
|
||||
* _pluginsChangeStatusAjax
|
||||
*/
|
||||
public static function _pluginsChangeStatusAjax()
|
||||
{
|
||||
if (Http::post('plugin_change_status')) {
|
||||
if (Token::check((Http::post('token')))) {
|
||||
$plugin_settings = YamlParser::decode(Filesystem::read(PATH['plugins'] . '/' . Http::post('plugin') . '/' . 'settings.yaml'));
|
||||
Arr::set($plugin_settings, 'enabled', (Http::post('status') == 'true' ? true : false));
|
||||
Filesystem::write(PATH['plugins'] . '/' . Http::post('plugin') . '/' . 'settings.yaml', YamlParser::encode($plugin_settings));
|
||||
Cache::clear();
|
||||
} else {
|
||||
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
$app->get('/admin/plugins', function (Request $request, Response $response, array $args) {
|
||||
return $this->view->render($response,
|
||||
'plugins/admin/views/templates/extends/plugins/index.html', [
|
||||
'registry' => $this->get('registry')->dump(),
|
||||
'plugins_list' => $this->get('registry')->get('plugins')
|
||||
]);
|
||||
})->setName('plugins');
|
||||
|
||||
public static function getPluginsManager()
|
||||
{
|
||||
Registry::set('sidebar_menu_item', 'plugins');
|
||||
$app->post('/admin/plugins/change_status', function (Request $request, Response $response, array $args) {
|
||||
|
||||
Event::addListener('onBeforeRequestShutdown', function() {
|
||||
PluginsManager::_pluginsChangeStatusAjax();
|
||||
});
|
||||
$data = $request->getParsedBody();
|
||||
|
||||
Themes::view('admin/views/templates/extends/plugins/list')
|
||||
->assign('plugins_list', Registry::get('plugins'))
|
||||
->display();
|
||||
}
|
||||
}
|
||||
$plugin_settings = YamlParser::decode(Filesystem::read(PATH['plugins'] . '/' . $data['plugin'] . '/' . 'settings.yaml'));
|
||||
Arr::set($plugin_settings, 'enabled', ($data['status'] == 'true' ? true : false));
|
||||
Filesystem::write(PATH['plugins'] . '/' . $data['plugin'] . '/' . 'settings.yaml', YamlParser::encode($plugin_settings));
|
||||
$this->get('cache')->clear();
|
||||
|
||||
})->setName('plugins-change-status');
|
||||
|
@@ -2,67 +2,100 @@
|
||||
|
||||
namespace Flextype;
|
||||
|
||||
use Flextype\Component\Http\Http;
|
||||
use Flextype\Component\Filesystem\Filesystem;
|
||||
use Flextype\Component\Session\Session;
|
||||
use Flextype\Component\Token\Token;
|
||||
use Flextype\Component\Registry\Registry;
|
||||
use Flextype\Component\Text\Text;
|
||||
use Flextype\Component\Notification\Notification;
|
||||
use function Flextype\Component\I18n\__;
|
||||
|
||||
use Slim\Http\Request;
|
||||
use Slim\Http\Response;
|
||||
|
||||
$app->get('/admin/login', function (Request $request, Response $response, array $args) {
|
||||
return $this->view->render($response,
|
||||
'plugins/admin/views/templates/users/login.html', [
|
||||
'registry' => $this->get('registry')->dump()
|
||||
]);
|
||||
})->setName('login');
|
||||
|
||||
$app->get('/admin/profile', function (Request $request, Response $response, array $args) {
|
||||
return $this->view->render($response,
|
||||
'plugins/admin/views/templates/users/profile.html', [
|
||||
'registry' => $this->get('registry')->dump(),
|
||||
'username' => Session::get('username'),
|
||||
'rolename' => Session::get('role'),
|
||||
'sidebar_menu_item' => 'profile'
|
||||
]);
|
||||
})->setName('profile');
|
||||
|
||||
$app->get('/admin/logout', function (Request $request, Response $response, array $args) {
|
||||
Session::destroy();
|
||||
return $response->withRedirect('/admin');
|
||||
});
|
||||
|
||||
$app->get('/admin/registration', function (Request $request, Response $response, array $args) {
|
||||
return $this->view->render($response,
|
||||
'plugins/admin/views/templates/users/registration.html', [
|
||||
'registry' => $this->get('registry')->dump()
|
||||
]);
|
||||
})->setName('registration');
|
||||
|
||||
$app->post('/admin/registration', function (Request $request, Response $response, array $args) {
|
||||
if (UsersManager::processRegistrationForm($request->getParsedBody())) {
|
||||
return $response->withRedirect('admin');
|
||||
}
|
||||
});
|
||||
|
||||
$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'));
|
||||
}
|
||||
});
|
||||
|
||||
class UsersManager
|
||||
{
|
||||
|
||||
public static function getProfileManager()
|
||||
public static function processLoginForm(array $data) : bool
|
||||
{
|
||||
Registry::set('sidebar_menu_item', 'profile');
|
||||
if (Filesystem::has($_user_file = PATH['site'] . '/accounts/' . $data['username'] . '.yaml')) {
|
||||
|
||||
Themes::view('admin/views/templates/users/profile')
|
||||
->display();
|
||||
}
|
||||
$user_file = YamlParser::decode(Filesystem::read($_user_file));
|
||||
|
||||
public static function logout()
|
||||
{
|
||||
if (Token::check((Http::get('token')))) {
|
||||
Session::destroy();
|
||||
Http::redirect(Http::getBaseUrl() . '/admin');
|
||||
} else {
|
||||
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
|
||||
}
|
||||
}
|
||||
if (password_verify(trim($data['password']), $user_file['hashed_password'])) {
|
||||
Session::set('username', $user_file['username']);
|
||||
Session::set('role', $user_file['role']);
|
||||
|
||||
public static function getRegistrationPage()
|
||||
{
|
||||
Registry::set('sidebar_menu_item', '');
|
||||
return true;
|
||||
|
||||
$registration = Http::post('registration');
|
||||
|
||||
if (isset($registration)) {
|
||||
if (Token::check((Http::post('token')))) {
|
||||
if (Filesystem::has($_user_file = PATH['site'] . '/accounts/' . Text::safeString(Http::post('username')) . '.yaml')) {
|
||||
} else {
|
||||
Filesystem::write(
|
||||
PATH['site'] . '/accounts/' . Http::post('username') . '.yaml',
|
||||
YamlParser::encode(['username' => Text::safeString(Http::post('username')),
|
||||
'hashed_password' => password_hash(trim(Http::post('password')), PASSWORD_BCRYPT),
|
||||
'email' => Http::post('email'),
|
||||
'role' => 'admin',
|
||||
'state' => 'enabled'])
|
||||
);
|
||||
|
||||
Http::redirect(Http::getBaseUrl() . '/admin/entries');
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
Themes::view('admin/views/templates/auth/registration')
|
||||
->display();
|
||||
}
|
||||
|
||||
public static function isUsersExists()
|
||||
public static function processRegistrationForm(array $data) : bool
|
||||
{
|
||||
if (!Filesystem::has($_user_file = PATH['site'] . '/accounts/' . Text::safeString($data['username']) . '.yaml')) {
|
||||
if (Filesystem::write(
|
||||
PATH['site'] . '/accounts/' . $data['username'] . '.yaml',
|
||||
YamlParser::encode(['username' => Text::safeString($data['username']),
|
||||
'hashed_password' => password_hash($data['password'], PASSWORD_BCRYPT),
|
||||
'email' => $data['email'],
|
||||
'role' => 'admin',
|
||||
'state' => 'enabled']))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function isUsersExists() : bool
|
||||
{
|
||||
// Get Users Profiles
|
||||
$users = Filesystem::listContents(PATH['site'] . '/accounts/');
|
||||
@@ -75,33 +108,4 @@ class UsersManager
|
||||
{
|
||||
return (Session::exists('role') && Session::get('role') == 'admin') ? true : false;
|
||||
}
|
||||
|
||||
public static function getAuthPage()
|
||||
{
|
||||
Registry::set('sidebar_menu_item', '');
|
||||
|
||||
$login = Http::post('login');
|
||||
|
||||
if (isset($login)) {
|
||||
if (Token::check((Http::post('token')))) {
|
||||
if (Filesystem::has($_user_file = PATH['site'] . '/accounts/' . Http::post('username') . '.yaml')) {
|
||||
$user_file = YamlParser::decode(Filesystem::read($_user_file));
|
||||
if (password_verify(trim(Http::post('password')), $user_file['hashed_password'])) {
|
||||
Session::set('username', $user_file['username']);
|
||||
Session::set('role', $user_file['role']);
|
||||
Http::redirect(Http::getBaseUrl() . '/admin/entries');
|
||||
} else {
|
||||
Notification::set('error', __('admin_message_wrong_username_password'));
|
||||
}
|
||||
} else {
|
||||
Notification::set('error', __('admin_message_wrong_username_password'));
|
||||
}
|
||||
} else {
|
||||
throw new \RuntimeException("Request was denied because it contained an invalid security token. Please refresh the page and try again.");
|
||||
}
|
||||
}
|
||||
|
||||
Themes::view('admin/views/templates/auth/login')
|
||||
->display();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user