Grav\Plugin\Admin\Utils::findUserByEmail PHP Method

findUserByEmail() public static method

Matches an email to a user
public static findUserByEmail ( $email ) : Grav\Common\User\User
$email
return Grav\Common\User\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);
    }

Usage Example

Beispiel #1
0
 /**
  * Authenticate user.
  *
  * @param  array $data Form data.
  * @param  array $post Additional form fields.
  *
  * @return bool
  */
 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)) {
             $user->set('language', 'en');
         }
         if ($user->exists()) {
             $user->authenticated = true;
             // Authenticate user.
             $result = $user->authenticate($data['password']);
             if (!$result) {
                 return false;
             }
         }
     }
     $action = [];
     if ($user->authorize('admin.login')) {
         $this->user = $this->session->user = $user;
         /** @var Grav $grav */
         $grav = $this->grav;
         unset($this->grav['user']);
         $this->grav['user'] = $user;
         $this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info');
         $grav->redirect($post['redirect']);
         return true;
         //never reached
     }
     return false;
 }