From 4311b80eec3005070636594545385fa24675c673 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Thu, 31 Aug 2017 12:46:51 -0600 Subject: [PATCH] minor improvements --- admin.php | 16 ++++++++++------ classes/admin.php | 13 +++++++++++++ classes/admincontroller.php | 2 ++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/admin.php b/admin.php index 5f0dd4cc..3b34d797 100644 --- a/admin.php +++ b/admin.php @@ -1,7 +1,6 @@ admin_route = rtrim($this->grav['pages']->base(), '/') . $this->base; $this->uri = $this->grav['uri']; - // check for existence of a user account - $account_dir = $file_path = $this->grav['locator']->findResource('account://'); - $user_check = glob($account_dir . '/*.yaml'); + $users_exist = Admin::doAnyUsersExist(); // If no users found, go to register - if ($user_check == false || count((array)$user_check) == 0) { + if (!$users_exist) { if (!$this->isAdminPath()) { $this->grav->redirect($this->admin_route); } @@ -212,6 +210,11 @@ class AdminPlugin extends Plugin switch ($action) { case 'register_admin_user': + + if (Admin::doAnyUsersExist()) { + throw new \RuntimeException('A user account already exists, please create an admin account manually.'); + } + if (!$this->config->get('plugins.login.enabled')) { throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.PLUGIN_LOGIN_DISABLED')); } @@ -272,7 +275,8 @@ class AdminPlugin extends Plugin $this->grav['session']->user = $user; unset($this->grav['user']); $this->grav['user'] = $user; - $user->authenticated = $user->authorize('site.login'); + $user->authenticated = true; + $user->authorized = $user->authorize('site.login'); $messages = $this->grav['messages']; $messages->add($this->grav['language']->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info'); diff --git a/classes/admin.php b/classes/admin.php index d35568e3..5e0fc00c 100644 --- a/classes/admin.php +++ b/classes/admin.php @@ -1776,4 +1776,17 @@ class Admin } return true; } + + public static function doAnyUsersExist() + { + // check for existence of a user account + $account_dir = $file_path = Grav::instance()['locator']->findResource('account://'); + $user_check = glob($account_dir . '/*.yaml'); + + if ($user_check != false && count((array)$user_check) > 0) { + return true; + } + + return false; + } } diff --git a/classes/admincontroller.php b/classes/admincontroller.php index 7c37be22..bbeff992 100644 --- a/classes/admincontroller.php +++ b/classes/admincontroller.php @@ -711,6 +711,8 @@ class AdminController extends AdminBaseController return true; } + $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info'); + $user->authenticated = true; $this->grav->redirect($this->post['redirect']); }