Feature: Ability to Login with Email

Thanks to @gsumpster for https://github.com/getgrav/grav-plugin-admin/pull/685

* implemented email-login, close #674
* changed placeholder text
* Fix Utils namespace thing
* Drop use
* Cleanup styling
This commit is contained in:
Flavio Copes
2016-07-07 18:47:48 +02:00
committed by GitHub
parent 35013859cf
commit 64a88c916c
4 changed files with 50 additions and 4 deletions

View File

@@ -14,6 +14,7 @@ use Grav\Common\Themes;
use Grav\Common\Uri;
use Grav\Common\User\User;
use Grav\Common\Utils;
use Grav\Plugin\Admin\Utils as AdminUtils;
use RocketTheme\Toolbox\File\File;
use RocketTheme\Toolbox\File\JsonFile;
use RocketTheme\Toolbox\ResourceLocator\UniformResourceIterator;
@@ -105,6 +106,9 @@ class Admin
$this->user = $this->grav['user'];
$this->permissions = [];
$language = $this->grav['language'];
// Load utility class
require_once __DIR__ . '/utils.php';
if ($language->enabled()) {
$this->multilang = true;
@@ -175,7 +179,12 @@ class Admin
public function authenticate($data, $post)
{
if (!$this->user->authenticated && isset($data['username']) && isset($data['password'])) {
$user = User::load($data['username']);
// Perform RegEX check on submitted username to check for emails
if (filter_var($data['username'], FILTER_VALIDATE_EMAIL)) {
$user = AdminUtils::findUserbyEmail($data['username']);
} else {
$user = User::load($data['username']);
}
//default to english if language not set
if (empty($user->language)) {

36
classes/utils.php Normal file
View File

@@ -0,0 +1,36 @@
<?php
namespace Grav\Plugin\Admin;
use Grav\Common\Grav;
use Grav\Common\User\User;
/**
* Admin utils class
*
* @license MIT
*/
class Utils
{
/**
* Matches an email to a user
*
* @return User
*/
public static function findUserbyEmail($email)
{
$account_dir = Grav::instance()['locator']->findResource('account://');
$files = array_diff(scandir($account_dir), ['.', '..']);
foreach ($files as $file) {
if (strpos($file, '.yaml') !== false) {
$user = User::load(trim(substr($file, 0, -5)));
if ($user['email'] == $email) {
return $user;
}
}
}
// If a User with the provided email cannot be found, then load user with that email as the username
return User::load($email);
}
}

View File

@@ -171,7 +171,8 @@ PLUGIN_ADMIN:
LAST_BACKUP: "Last Backup"
FULL_NAME: "Full name"
USERNAME: "Username"
EMAIL: "Email"
EMAIL: "Email"
USERNAME_EMAIL: "Username or Email"
PASSWORD: "Password"
PASSWORD_CONFIRM: "Confirm Password"
TITLE: "Title"
@@ -577,4 +578,4 @@ PLUGIN_ADMIN:
FROM: "from"
TO: "to"
RELEASE_DATE: "Release Date"
SORT_BY: "Sort By"
SORT_BY: "Sort By"

View File

@@ -9,7 +9,7 @@ form:
fields:
- name: username
type: text
placeholder: PLUGIN_ADMIN.USERNAME
placeholder: PLUGIN_ADMIN.USERNAME_EMAIL
autofocus: true
validate:
required: true