mirror of
				https://github.com/getgrav/grav-plugin-admin.git
				synced 2025-11-03 20:05:53 +01:00 
			
		
		
		
	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:
		@@ -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;
 | 
			
		||||
@@ -106,6 +107,9 @@ class Admin
 | 
			
		||||
        $this->permissions = [];
 | 
			
		||||
        $language = $this->grav['language'];
 | 
			
		||||
        
 | 
			
		||||
        // Load utility class
 | 
			
		||||
        require_once __DIR__ . '/utils.php';
 | 
			
		||||
 | 
			
		||||
        if ($language->enabled()) {
 | 
			
		||||
            $this->multilang = true;
 | 
			
		||||
            $this->languages_enabled = $this->grav['config']->get('system.languages.supported', []);
 | 
			
		||||
@@ -175,7 +179,12 @@ class Admin
 | 
			
		||||
    public function authenticate($data, $post)
 | 
			
		||||
    {
 | 
			
		||||
        if (!$this->user->authenticated && isset($data['username']) && isset($data['password'])) {
 | 
			
		||||
            // 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
									
								
							
							
						
						
									
										36
									
								
								classes/utils.php
									
									
									
									
									
										Normal 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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -172,6 +172,7 @@ PLUGIN_ADMIN:
 | 
			
		||||
  FULL_NAME: "Full name"
 | 
			
		||||
  USERNAME: "Username"
 | 
			
		||||
  EMAIL: "Email"
 | 
			
		||||
  USERNAME_EMAIL: "Username or Email"
 | 
			
		||||
  PASSWORD: "Password"
 | 
			
		||||
  PASSWORD_CONFIRM: "Confirm Password"
 | 
			
		||||
  TITLE: "Title"
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,7 @@ form:
 | 
			
		||||
    fields:
 | 
			
		||||
        - name: username
 | 
			
		||||
          type: text
 | 
			
		||||
          placeholder: PLUGIN_ADMIN.USERNAME
 | 
			
		||||
          placeholder: PLUGIN_ADMIN.USERNAME_EMAIL
 | 
			
		||||
          autofocus: true
 | 
			
		||||
          validate:
 | 
			
		||||
            required: true
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user