Grav\Plugin\Admin\Admin::authenticate PHP Méthode

authenticate() public méthode

Authenticate user.
public authenticate ( array $data, array $post ) : boolean
$data array Form data.
$post array Additional form fields.
Résultat boolean
    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;
    }

Usage Example

 /**
  * Handle login.
  *
  * @return bool True if the action was performed.
  */
 protected function taskLogin()
 {
     $this->data['username'] = strip_tags(strtolower($this->data['username']));
     if ($this->admin->authenticate($this->data, $this->post)) {
         // should never reach here, redirects first
     } else {
         $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.LOGIN_FAILED'), 'error');
     }
     return true;
 }