Skip to content

Commit

Permalink
Feature: Ability to Login with Email
Browse files Browse the repository at this point in the history
Thanks to @gsumpster for #685

* implemented email-login, close #674
* changed placeholder text
* Fix Utils namespace thing
* Drop use
* Cleanup styling
  • Loading branch information
flaviocopes authored Jul 7, 2016
1 parent 3501385 commit 64a88c9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
11 changes: 10 additions & 1 deletion classes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand Down Expand Up @@ -105,6 +106,9 @@ public function __construct(Grav $grav, $base, $location, $route)
$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;
Expand Down Expand Up @@ -175,7 +179,12 @@ public function messages($type = null)
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)) {
Expand Down
36 changes: 36 additions & 0 deletions classes/utils.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 3 additions & 2 deletions languages/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -577,4 +578,4 @@ PLUGIN_ADMIN:
FROM: "from"
TO: "to"
RELEASE_DATE: "Release Date"
SORT_BY: "Sort By"
SORT_BY: "Sort By"
2 changes: 1 addition & 1 deletion pages/admin/login.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ form:
fields:
- name: username
type: text
placeholder: PLUGIN_ADMIN.USERNAME
placeholder: PLUGIN_ADMIN.USERNAME_EMAIL
autofocus: true
validate:
required: true
Expand Down

0 comments on commit 64a88c9

Please sign in to comment.