mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-04 20:36:03 +01:00
Merge branch 'feature/login-separation' of bitbucket.org:rockettheme/grav-plugin-admin into feature/redesign
Conflicts: admin.php admin.yaml pages/admin/login.md pages/admin/themes.md
This commit is contained in:
134
admin.php
134
admin.php
@@ -47,7 +47,7 @@ class AdminPlugin extends Plugin
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
return [
|
||||
'onPluginsInitialized' => ['onPluginsInitialized', 1000],
|
||||
'onPluginsInitialized' => [['login', 100000], ['onPluginsInitialized', 1000]],
|
||||
'onShutdown' => ['onShutdown', 1000]
|
||||
];
|
||||
}
|
||||
@@ -57,21 +57,45 @@ class AdminPlugin extends Plugin
|
||||
*
|
||||
* Disables system cache.
|
||||
*/
|
||||
public function onPluginsInitialized()
|
||||
public function login()
|
||||
{
|
||||
|
||||
// Check for Pro version and disable this plugin if found
|
||||
// if (file_exists(PLUGINS_DIR . 'admin_pro/admin_pro.php')) {
|
||||
// $this->enabled = false;
|
||||
// return;
|
||||
// }
|
||||
|
||||
// echo "<h1>Admin Free</h1>";
|
||||
//
|
||||
require_once PLUGINS_DIR . 'admin/classes/popularity.php';
|
||||
$this->popularity = new Popularity();
|
||||
$route = $this->config->get('plugins.admin.route');
|
||||
if (!$route) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->initializeAdmin();
|
||||
$this->base = '/' . trim($route, '/');
|
||||
$this->uri = $this->grav['uri'];
|
||||
|
||||
// Only activate admin if we're inside the admin path.
|
||||
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->initializeAdmin();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -88,17 +112,6 @@ class AdminPlugin extends Plugin
|
||||
$this->grav['admin']->routes = $pages->routes();
|
||||
|
||||
$pages->dispatch('/', true)->route($home);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main administration controller.
|
||||
*/
|
||||
public function onPageInitialized()
|
||||
{
|
||||
// Set page if user hasn't been authorised.
|
||||
if (!$this->admin->authorise()) {
|
||||
$this->template = $this->admin->user ? 'denied' : 'login';
|
||||
}
|
||||
|
||||
// Make local copy of POST.
|
||||
$post = !empty($_POST) ? $_POST : array();
|
||||
@@ -116,16 +129,16 @@ class AdminPlugin extends Plugin
|
||||
exit();
|
||||
}
|
||||
|
||||
/** @var Grav $grav */
|
||||
$grav = $this->grav;
|
||||
$self = $this;
|
||||
|
||||
// Finally create admin page.
|
||||
$page = new Page;
|
||||
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$this->template}.md"));
|
||||
$page->slug(basename($this->template));
|
||||
// Replace page service with admin.
|
||||
$this->grav['page'] = function ($c) use ($self) {
|
||||
$page = new Page;
|
||||
$page->init(new \SplFileInfo(__DIR__ . "/pages/admin/{$self->template}.md"));
|
||||
$page->slug(basename($self->template));
|
||||
|
||||
unset($grav['page']);
|
||||
$grav['page'] = $page;
|
||||
return $page;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -181,52 +194,41 @@ class AdminPlugin extends Plugin
|
||||
|
||||
protected function initializeAdmin()
|
||||
{
|
||||
$this->route = $this->config->get('plugins.admin.route');
|
||||
$this->enable([
|
||||
'onPagesInitialized' => ['onPagesInitialized', 1000],
|
||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
|
||||
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000]
|
||||
]);
|
||||
|
||||
if (!$this->route) {
|
||||
return;
|
||||
}
|
||||
require_once PLUGINS_DIR . 'admin/classes/popularity.php';
|
||||
$this->popularity = new Popularity();
|
||||
|
||||
$this->uri = $this->grav['uri'];
|
||||
$base = '/' . trim($this->route, '/');
|
||||
// Disable system caching.
|
||||
$this->config->set('system.cache.enabled', false);
|
||||
|
||||
// Only activate admin if we're inside the admin path.
|
||||
if (substr($this->uri->route(), 0, strlen($base)) == $base) {
|
||||
$this->active = true;
|
||||
$this->enable([
|
||||
'onPagesInitialized' => ['onPagesInitialized', 1000],
|
||||
'onPageInitialized' => ['onPageInitialized', 1000],
|
||||
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 1000],
|
||||
'onTwigSiteVariables' => ['onTwigSiteVariables', 1000]
|
||||
]);
|
||||
// Change login behavior.
|
||||
$this->config->set('plugins.login', $this->config->get('plugins.admin.login'));
|
||||
|
||||
// Disable system caching.
|
||||
$this->config->set('system.cache.enabled', false);
|
||||
// Decide admin template and route.
|
||||
$path = trim(substr($this->uri->route(), strlen($this->base)), '/');
|
||||
$this->template = 'dashboard';
|
||||
|
||||
// Decide admin template and route.
|
||||
$path = trim(substr($this->uri->route(), strlen($base)), '/');
|
||||
$this->template = 'dashboard';
|
||||
if ($path) {
|
||||
$array = explode('/', $path, 2);
|
||||
$this->template = array_shift($array);
|
||||
$this->route = array_shift($array);
|
||||
|
||||
if ($path) {
|
||||
$array = explode('/', $path, 2);
|
||||
$this->template = array_shift($array);
|
||||
$this->route = array_shift($array);
|
||||
|
||||
// Set path for new page.
|
||||
if ($this->uri->param('new')) {
|
||||
$this->route .= '/new';
|
||||
}
|
||||
// Set path for new page.
|
||||
if ($this->uri->param('new')) {
|
||||
$this->route .= '/new';
|
||||
}
|
||||
|
||||
// Initialize admin class.
|
||||
require_once PLUGINS_DIR . 'admin/classes/admin.php';
|
||||
$this->admin = new Admin($this->grav, $base, $this->template, $this->route);
|
||||
|
||||
|
||||
|
||||
// And store the class into DI container.
|
||||
$this->grav['admin'] = $this->admin;
|
||||
|
||||
}
|
||||
|
||||
// Initialize admin class.
|
||||
require_once __DIR__ . '/classes/admin.php';
|
||||
$this->admin = new Admin($this->grav, $this->base, $this->template, $this->route);
|
||||
|
||||
// And store the class into DI container.
|
||||
$this->grav['admin'] = $this->admin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
enabled: true
|
||||
route: '/admin'
|
||||
theme: grav
|
||||
login:
|
||||
enabled: true
|
||||
route: null
|
||||
timeout: 1800
|
||||
popularity:
|
||||
enabled: true
|
||||
ignore: ['/test*','/modular']
|
||||
@@ -7,4 +12,3 @@ popularity:
|
||||
daily: 7
|
||||
monthly: 12
|
||||
visitors: 20
|
||||
theme: grav
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
namespace Grav\Plugin;
|
||||
|
||||
use Grav\Common\User\User;
|
||||
use Grav\Common\User\Authentication;
|
||||
use Grav\Common\Filesystem\File;
|
||||
use Grav\Common\Grav;
|
||||
use Grav\Common\Plugins;
|
||||
@@ -40,11 +39,6 @@ class Admin
|
||||
*/
|
||||
protected $blueprints;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $message;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@@ -61,7 +55,7 @@ class Admin
|
||||
public $route;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @var User
|
||||
*/
|
||||
public $user;
|
||||
|
||||
@@ -81,17 +75,9 @@ class Admin
|
||||
$this->location = $location;
|
||||
$this->route = $route;
|
||||
|
||||
/** @var Uri uri */
|
||||
$this->uri = $this->grav['uri'];
|
||||
|
||||
// TODO: add session timeout into configuration
|
||||
$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;
|
||||
}
|
||||
$this->session = $this->grav['session'];
|
||||
$this->user = $this->grav['user'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,12 +98,8 @@ class Admin
|
||||
*/
|
||||
public function setMessage($msg, $type = 'info')
|
||||
{
|
||||
if (!isset($this->session->messages)) {
|
||||
$this->session->messages = new Session\Message;
|
||||
}
|
||||
|
||||
/** @var Session\Message $messages */
|
||||
$messages = $this->session->messages;
|
||||
$messages = $this->grav['messages'];
|
||||
$messages->add($msg, $type);
|
||||
}
|
||||
|
||||
@@ -125,14 +107,13 @@ class Admin
|
||||
* Fetch and delete messages from the session queue.
|
||||
*
|
||||
* @param string $type
|
||||
* @return array
|
||||
*/
|
||||
public function messages($type = null)
|
||||
{
|
||||
if (!isset($this->session->messages)) {
|
||||
$this->session->messages = new Session\Message;
|
||||
}
|
||||
|
||||
return $this->session->messages->fetch($type);
|
||||
/** @var Session\Message $messages */
|
||||
$messages = $this->grav['messages'];
|
||||
return $messages->fetch($type);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -143,10 +124,11 @@ class Admin
|
||||
*/
|
||||
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);
|
||||
if ($file->exists()) {
|
||||
$user = new User($file->content());
|
||||
$user->authenticated = true;
|
||||
|
||||
// Authenticate user.
|
||||
$result = $user->authenticate($form['password']);
|
||||
@@ -172,7 +154,7 @@ class Admin
|
||||
*/
|
||||
public function authorise($action = 'admin.login')
|
||||
{
|
||||
return isset($this->user) && $this->user->authorise($action);
|
||||
return $this->user->authorise($action);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -409,9 +391,7 @@ class Admin
|
||||
$page->filePath($parent->path().'/'.$slug.'/'.$page->name());
|
||||
$page->header();
|
||||
|
||||
// Attach page to parent and add routing information.
|
||||
// FIXME:
|
||||
$parent->{$slug} = $page;
|
||||
// Add routing information.
|
||||
$pages->addPage($page, $path);
|
||||
|
||||
// Determine page type.
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Configuration
|
||||
|
||||
access:
|
||||
admin.configuration: true
|
||||
admin.super: true
|
||||
---
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
---
|
||||
title: Dashboard
|
||||
|
||||
access:
|
||||
admin.login: true
|
||||
admin.super: true
|
||||
---
|
||||
|
||||
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
|
||||
|
||||
4
pages/admin/denied.md
Normal file
4
pages/admin/denied.md
Normal file
@@ -0,0 +1,4 @@
|
||||
---
|
||||
title: Access Denied
|
||||
---
|
||||
|
||||
7
pages/admin/installer.md
Normal file
7
pages/admin/installer.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
title: Users
|
||||
|
||||
access:
|
||||
admin.users: true
|
||||
admin.super: true
|
||||
---
|
||||
@@ -2,12 +2,13 @@
|
||||
title: Dashboard Login
|
||||
|
||||
form:
|
||||
- name: username
|
||||
type: text
|
||||
placeholder: Username
|
||||
fields:
|
||||
- name: username
|
||||
type: text
|
||||
placeholder: Username
|
||||
|
||||
- name: password
|
||||
type: password
|
||||
placeholder: Password
|
||||
- name: password
|
||||
type: password
|
||||
placeholder: Password
|
||||
---
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Error Log
|
||||
|
||||
access:
|
||||
admin.logs: true
|
||||
admin.super: true
|
||||
---
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Pages
|
||||
|
||||
access:
|
||||
admin.pages: true
|
||||
admin.super: true
|
||||
---
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Plugins
|
||||
|
||||
access:
|
||||
admin.plugins: true
|
||||
admin.super: true
|
||||
---
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
---
|
||||
title: Site Settings
|
||||
|
||||
access:
|
||||
admin.settings: true
|
||||
admin.super: true
|
||||
---
|
||||
|
||||
7
pages/admin/statistics.md
Normal file
7
pages/admin/statistics.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
title: Installer
|
||||
|
||||
access:
|
||||
admin.install: true
|
||||
admin.super: true
|
||||
---
|
||||
7
pages/admin/users.md
Normal file
7
pages/admin/users.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
title: Plugins
|
||||
|
||||
access:
|
||||
admin.plugins: true
|
||||
admin.super: true
|
||||
---
|
||||
@@ -9,7 +9,7 @@
|
||||
{% include 'partials/messages.html.twig' %}
|
||||
|
||||
<form method="post">
|
||||
{% for field in page.header.form %}
|
||||
{% for field in page.header.form.fields %}
|
||||
{% if field.type %}
|
||||
<div>
|
||||
{% include ["forms/fields/#{field.type}/#{field.type}.html.twig", 'forms/fields/text/text.html.twig'] %}
|
||||
|
||||
@@ -89,7 +89,7 @@
|
||||
|
||||
{% block footer_section %}
|
||||
<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>
|
||||
{% endblock %}
|
||||
</div>
|
||||
|
||||
@@ -42,19 +42,7 @@
|
||||
</table>
|
||||
</form>
|
||||
{% else %}
|
||||
{% 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 } %}
|
||||
{% include 'partials/theme.html.twig' %}
|
||||
{% endif %}
|
||||
|
||||
</div>
|
||||
|
||||
14
themes/grav/templates/installer.html.twig
Normal file
14
themes/grav/templates/installer.html.twig
Normal file
@@ -0,0 +1,14 @@
|
||||
{% extends 'partials/base.html.twig' %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="admin-block">
|
||||
<h1>
|
||||
Users
|
||||
</h1>
|
||||
|
||||
{% include 'partials/messages.html.twig' %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
13
themes/grav/templates/partials/theme.html.twig
Normal file
13
themes/grav/templates/partials/theme.html.twig
Normal 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 } %}
|
||||
14
themes/grav/templates/statistics.html.twig
Normal file
14
themes/grav/templates/statistics.html.twig
Normal file
@@ -0,0 +1,14 @@
|
||||
{% extends 'partials/base.html.twig' %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="admin-block">
|
||||
<h1>
|
||||
Users
|
||||
</h1>
|
||||
|
||||
{% include 'partials/messages.html.twig' %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
50
themes/grav/templates/users.html.twig
Normal file
50
themes/grav/templates/users.html.twig
Normal file
@@ -0,0 +1,50 @@
|
||||
{% extends 'partials/base.html.twig' %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="admin-block">
|
||||
{% if not admin.route %}
|
||||
<h1>
|
||||
Plugins
|
||||
</h1>
|
||||
|
||||
{% include 'partials/messages.html.twig' %}
|
||||
|
||||
<table>
|
||||
{% for plugin in admin.plugins %}
|
||||
{% set blueprints = plugin.blueprints() %}
|
||||
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{ base_url_relative }}/plugins/{{ blueprints.name|url_encode }}">{{ blueprints.get('name') }}</a>
|
||||
</td>
|
||||
<td>
|
||||
<form action="{{ base_url_relative }}/plugins/{{ blueprints.name }}" method="post">
|
||||
<input type="hidden" name="enabled" value="{{ plugin.get('enabled') ? 0 : 1 }}" />
|
||||
<input type="hidden" name="_redirect" value="plugins" />
|
||||
<button class="button" name="task" value="enable"{{ blueprints.name == 'admin' ? ' disabled="disabled"' }}>
|
||||
{{ plugin.get('enabled') ? 'Enabled' : 'Disabled' }}
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
{% else %}
|
||||
{% set plugin = admin.plugins[admin.route] %}
|
||||
{% set blueprints = plugin.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: plugin } %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user