Separate login and session handling into its own plugin

This commit is contained in:
Matias Griese
2014-09-09 06:10:31 +03:00
parent 7a22ecd4d3
commit fa2975ca03
17 changed files with 112 additions and 81 deletions

View File

@@ -40,7 +40,7 @@ class AdminPlugin extends Plugin
*/ */
public static function getSubscribedEvents() { public static function getSubscribedEvents() {
return [ return [
'onPluginsInitialized' => ['onPluginsInitialized', 1000] 'onPluginsInitialized' => [['login', 100000], ['onPluginsInitialized', 1000]]
]; ];
} }
@@ -49,22 +49,39 @@ class AdminPlugin extends Plugin
* *
* Disables system cache. * Disables system cache.
*/ */
public function onPluginsInitialized() public function login()
{ {
$route = $this->config->get('plugins.admin.route'); $route = $this->config->get('plugins.admin.route');
if (!$route) { if (!$route) {
return; return;
} }
$this->base = '/' . trim($route, '/');
$this->uri = $this->grav['uri']; $this->uri = $this->grav['uri'];
$base = '/' . trim($route, '/');
// Only activate admin if we're inside the admin path. // Only activate admin if we're inside the admin path.
if (substr($this->uri->route(), 0, strlen($base)) == $base) { if (substr($this->uri->route(), 0, strlen($this->base)) == $this->base) {
// Disable system caching.
$this->config->set('system.cache.enabled', false);
// Change login behavior.
$this->config->set('plugins.login', $this->config->get('plugins.admin.login'));
$this->active = true;
}
}
/**
* Initialize administration plugin if admin path matches.
*
* Disables system cache.
*/
public function onPluginsInitialized()
{
// Only activate admin if we're inside the admin path.
if ($this->active) {
$this->enable([ $this->enable([
'onPagesInitialized' => ['onPagesInitialized', 1000], 'onPagesInitialized' => ['onPagesInitialized', 1000],
'onPageInitialized' => ['onPageInitialized', 1000],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000], 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000] 'onTwigSiteVariables' => ['onTwigSiteVariables', 1000]
]); ]);
@@ -72,8 +89,11 @@ class AdminPlugin extends Plugin
// Disable system caching. // Disable system caching.
$this->config->set('system.cache.enabled', false); $this->config->set('system.cache.enabled', false);
// Change login behavior.
$this->config->set('plugins.login', $this->config->get('plugins.admin.login'));
// Decide admin template and route. // Decide admin template and route.
$path = trim(substr($this->uri->route(), strlen($base)), '/'); $path = trim(substr($this->uri->route(), strlen($this->base)), '/');
$this->template = 'dashboard'; $this->template = 'dashboard';
if ($path) { if ($path) {
@@ -89,7 +109,7 @@ class AdminPlugin extends Plugin
// Initialize admin class. // Initialize admin class.
require_once __DIR__ . '/classes/admin.php'; require_once __DIR__ . '/classes/admin.php';
$this->admin = new Admin($this->grav, $base, $this->template, $this->route); $this->admin = new Admin($this->grav, $this->base, $this->template, $this->route);
// And store the class into DI container. // And store the class into DI container.
$this->grav['admin'] = $this->admin; $this->grav['admin'] = $this->admin;
@@ -107,18 +127,12 @@ class AdminPlugin extends Plugin
/** @var Pages $pages */ /** @var Pages $pages */
$pages = $this->grav['pages']; $pages = $this->grav['pages'];
$pages->dispatch('/', true)->route($home); $pages->dispatch('/', true)->route($home);
}
/**
* Main administration controller.
*/
public function onPageInitialized()
{
// Set page if user hasn't been authorised. // Set page if user hasn't been authorised.
if (!$this->admin->authorise()) { /* if (!$this->admin->authorise()) {
$this->template = $this->admin->user ? 'denied' : 'login'; $this->template = $this->admin->user ? 'denied' : 'login';
} }
*/
// Make local copy of POST. // Make local copy of POST.
$post = !empty($_POST) ? $_POST : array(); $post = !empty($_POST) ? $_POST : array();
@@ -135,16 +149,16 @@ class AdminPlugin extends Plugin
exit(); exit();
} }
/** @var Grav $grav */ $self = $this;
$grav = $this->grav;
// Finally create admin page. // Replace page service with admin.
$page = new Page; $this->grav['page'] = function ($c) use ($self) {
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$this->template}.md")); $page = new Page;
$page->slug(basename($this->template)); $page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md"));
$page->slug(basename($self->template));
unset($grav['page']); return $page;
$grav['page'] = $page; };
} }
/** /**

View File

@@ -1,2 +1,6 @@
enabled: true enabled: true
route: '/admin' route: '/admin'
login:
enabled: true
route: null
timeout: 1800

View File

@@ -2,7 +2,6 @@
namespace Grav\Plugin; namespace Grav\Plugin;
use Grav\Common\User\User; use Grav\Common\User\User;
use Grav\Common\User\Authentication;
use Grav\Common\Filesystem\File; use Grav\Common\Filesystem\File;
use Grav\Common\Grav; use Grav\Common\Grav;
use Grav\Common\Plugins; use Grav\Common\Plugins;
@@ -40,11 +39,6 @@ class Admin
*/ */
protected $blueprints; protected $blueprints;
/**
* @var string
*/
public $message;
/** /**
* @var string * @var string
*/ */
@@ -61,7 +55,7 @@ class Admin
public $route; public $route;
/** /**
* @var array * @var User
*/ */
public $user; public $user;
@@ -80,17 +74,9 @@ class Admin
$this->location = $location; $this->location = $location;
$this->route = $route; $this->route = $route;
/** @var Uri uri */
$this->uri = $this->grav['uri']; $this->uri = $this->grav['uri'];
$this->session = $this->grav['session'];
// TODO: add session timeout into configuration $this->user = $this->grav['user'];
$this->session = new Session\Session(1800, $this->uri->rootUrl(false) . $base);
$this->session->start();
// Get current user from the session.
if (isset($this->session->user)) {
$this->user = $this->session->user;
}
} }
/** /**
@@ -111,12 +97,8 @@ class Admin
*/ */
public function setMessage($msg, $type = 'info') public function setMessage($msg, $type = 'info')
{ {
if (!isset($this->session->messages)) {
$this->session->messages = new Session\Message;
}
/** @var Session\Message $messages */ /** @var Session\Message $messages */
$messages = $this->session->messages; $messages = $this->grav['messages'];
$messages->add($msg, $type); $messages->add($msg, $type);
} }
@@ -124,14 +106,13 @@ class Admin
* Fetch and delete messages from the session queue. * Fetch and delete messages from the session queue.
* *
* @param string $type * @param string $type
* @return array
*/ */
public function messages($type = null) public function messages($type = null)
{ {
if (!isset($this->session->messages)) { /** @var Session\Message $messages */
$this->session->messages = new Session\Message; $messages = $this->grav['messages'];
} return $messages->fetch($type);
return $this->session->messages->fetch($type);
} }
/** /**
@@ -142,11 +123,11 @@ class Admin
*/ */
public function authenticate($form) public function authenticate($form)
{ {
if (!$this->session->user && isset($form['username']) && isset($form['password'])) { if (!$this->user->authenticated && isset($form['username']) && isset($form['password'])) {
$file = File\Yaml::instance(ACCOUNTS_DIR . $form['username'] . YAML_EXT); $file = File\Yaml::instance(ACCOUNTS_DIR . $form['username'] . YAML_EXT);
if ($file->exists()) { if ($file->exists()) {
$user = new User($file->content()); $user = new User($file->content());
print_r($user); $user->authenticated = true;
// Authenticate user. // Authenticate user.
$result = $user->authenticate($form['password']); $result = $user->authenticate($form['password']);
@@ -172,7 +153,7 @@ class Admin
*/ */
public function authorise($action = 'admin.login') public function authorise($action = 'admin.login')
{ {
return isset($this->user) && $this->user->authorise($action); return $this->user->authorise($action);
} }
/** /**
@@ -352,9 +333,7 @@ class Admin
$page->filePath($parent->path().'/'.$slug.'/'.$page->name()); $page->filePath($parent->path().'/'.$slug.'/'.$page->name());
$page->header(); $page->header();
// Attach page to parent and add routing information. // Add routing information.
// FIXME:
$parent->{$slug} = $page;
$pages->addPage($page, $path); $pages->addPage($page, $path);
// Determine page type. // Determine page type.

View File

@@ -1,3 +1,7 @@
--- ---
title: Configuration title: Configuration
access:
admin.configuration: true
admin.super: true
--- ---

View File

@@ -1,4 +1,8 @@
--- ---
title: Dashboard title: Dashboard
access:
admin.login: true
admin.super: true
--- ---

4
pages/admin/denied.md Normal file
View File

@@ -0,0 +1,4 @@
---
title: Access Denied
---

View File

@@ -2,12 +2,13 @@
title: Dashboard Login title: Dashboard Login
form: form:
- name: username fields:
type: text - name: username
label: Username type: text
label: Username
- name: password - name: password
type: password type: password
label: Password label: Password
--- ---

View File

@@ -1,3 +1,7 @@
--- ---
title: Error Log title: Error Log
access:
admin.logs: true
admin.super: true
--- ---

View File

@@ -1,3 +1,7 @@
--- ---
title: Pages title: Pages
access:
admin.pages: true
admin.super: true
--- ---

View File

@@ -1,3 +1,7 @@
--- ---
title: Plugins title: Plugins
access:
admin.plugins: true
admin.super: true
--- ---

View File

@@ -1,3 +1,7 @@
--- ---
title: Site Settings title: Site Settings
access:
admin.settings: true
admin.super: true
--- ---

7
pages/admin/themes.md Normal file
View File

@@ -0,0 +1,7 @@
---
title: Grav Themes
access:
admin.themes: true
admin.super: true
---

View File

@@ -1,3 +0,0 @@
---
title: Grav Themes
---

View File

@@ -9,7 +9,7 @@
{% include 'partials/messages.html.twig' %} {% include 'partials/messages.html.twig' %}
<form method="post"> <form method="post">
{% for field in page.header.form %} {% for field in page.header.form.fields %}
{% if field.type %} {% if field.type %}
<div> <div>
{% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %} {% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}

View File

@@ -89,7 +89,7 @@
{% block footer_section %} {% block footer_section %}
<footer id="footer"> <footer id="footer">
<a href="http://getgrav.org">Grav</a> was made with <i class="fa fa-heart"></i> by <a href="http:/www.rockettheme.com">RocketTheme</a>. <a href="http://getgrav.org">Grav</a> was made with <i class="fa fa-heart"></i> by <a href="http://www.rockettheme.com">RocketTheme</a>.
</footer> </footer>
{% endblock %} {% endblock %}
</div> </div>

View File

@@ -0,0 +1,13 @@
{% set theme = admin.themes[admin.route] %}
{% set blueprints = theme.blueprints() %}
<h1>
{{ blueprints.get('name')|e }}
<small>{{ blueprints.get('version') ? 'v' ~ blueprints.get('version')|e }}</small>
</h1>
{% include 'partials/messages.html.twig' %}
<p>{{ blueprints.get('description') }}</p>
{% include 'partials/blueprints.html.twig' with { data: theme } %}

View File

@@ -42,19 +42,7 @@
</table> </table>
</form> </form>
{% else %} {% else %}
{% set theme = admin.themes[admin.route] %} {% include 'partials/theme.html.twig' %}
{% set blueprints = theme.blueprints() %}
<h1>
{{ blueprints.get('name')|e }}
<small>{{ blueprints.get('version') ? 'v' ~ blueprints.get('version')|e }}</small>
</h1>
{% include 'partials/messages.html.twig' %}
<p>{{ blueprints.get('description') }}</p>
{% include 'partials/blueprints.html.twig' with { data: theme } %}
{% endif %} {% endif %}
</div> </div>