user/plugins/login/login.php
/**
     * Initialize login plugin if path matches.
     */
Initialize login plugin if path matches.
Initialize login plugin if path matches .
public function initialize()
    {
        $uri = $this->grav['uri'];
        if ($this->grav['config']->get('system.session.enabled') === false) {
            throw new \RuntimeException('The Login plugin requires "system.session" to be enabled');
        }
        $autoload = __DIR__ . '/vendor/autoload.php';
        if (!is_file($autoload)) {
            throw new \Exception('Login Plugin failed to load. Composer dependencies not met.');
        }
        require_once $autoload;
        $this->grav['messages'] = function ($c) {
            $session = $c['session'];
            if (!isset($session->messages)) {
                $session->messages = new Message;
            }
            return $session->messages;
        };
        $this->grav['user'] = function ($c) {
            $session = $c['session'];
            if (!isset($session->user)) {
                $session->user = new User;
                if ($c['config']->get('plugins.login.rememberme.enabled')) {
                    $controller = new Controller($c, '');
                    $rememberMe = $controller->rememberMe();
                    $username = $rememberMe->login();
                    if ($username) {
                        $user = User::load($username);
                        if ($user->exists()) {
                            $session->remember_me = $rememberMe;
                            $session->user = $user;
                        }
                    }
                    if ($rememberMe->loginTokenWasInvalid()) {
                        $controller->setMessage($c['language']->translate('PLUGIN_LOGIN.REMEMBER_ME_STOLEN_COOKIE'));
                    }
                }
            }
            return $session->user;
        };
        $this->login = new Login($this->grav);
        $this->grav['login'] = $this->login;
        $admin_route = $this->config->get('plugins.admin.route');
        if ($uri->path() != $admin_route && substr($uri->path(), 0, strlen($admin_route) + 1) != ($admin_route . '/')) {
            $this->route = $this->config->get('plugins.login.route');
        }
        if ($this->route && $this->route == $uri->path()) {
            $this->enable([
                'onPagesInitialized' => ['addLoginPage', 0],
            ]);
        }
        if ($uri->path() == $this->config->get('plugins.login.route_forgot')) {
            $this->enable([
                'onPagesInitialized' => ['addForgotPage', 0],
            ]);
        }
        if ($uri->path() == $this->config->get('plugins.login.route_reset')) {
            $this->enable([
                'onPagesInitialized' => ['addResetPage', 0],
            ]);
        }
        if ($uri->path() == $this->config->get('plugins.login.route_register')) {
            if ($this->config->get('plugins.login.user_registration.enabled')) {
                $this->enable([
                    'onPagesInitialized' => ['addRegisterPage', 0],
                ]);
            } else {
                throw new \RuntimeException($this->grav['language']->translate('PLUGIN_LOGIN.REGISTRATION_DISABLED'), 404);
            }
        }
        if ($uri->path() == $this->config->get('plugins.login.route_activate')) {
            $this->enable([
                'onPagesInitialized' => ['handleUserActivation', 0],
            ]);
        }
    }
public function initialize ( ) { $ uri = $ this -> grav [ 'uri' ] ; if ( $ this -> grav [ 'config' ] -> get ( 'system.session.enabled' ) === false ) { throw new \ RuntimeException ( 'The Login plugin requires "system.session" to be enabled' ) ; } $ autoload = __DIR__ . '/vendor/autoload.php' ; if ( ! is_file ( $ autoload ) ) { throw new \ Exception ( 'Login Plugin failed to load. Composer dependencies not met.' ) ; } require_once $ autoload ; $ this -> grav [ 'messages' ] = function ( $ c ) { $ session = $ c [ 'session' ] ; if ( ! isset ( $ session -> messages ) ) { $ session -> messages = new Message ; } return $ session -> messages ; } ; $ this -> grav [ 'user' ] = function ( $ c ) { $ session = $ c [ 'session' ] ; if ( ! isset ( $ session -> user ) ) { $ session -> user = new User ; if ( $ c [ 'config' ] -> get ( 'plugins.login.rememberme.enabled' ) ) { $ controller = new Controller ( $ c , '' ) ; $ rememberMe = $ controller -> rememberMe ( ) ; $ username = $ rememberMe -> login ( ) ; if ( $ username ) { $ user = User :: load ( $ username ) ; if ( $ user -> exists ( ) ) { $ session -> remember_me = $ rememberMe ; $ session -> user = $ user ; } } if ( $ rememberMe -> loginTokenWasInvalid ( ) ) { $ controller -> setMessage ( $ c [ 'language' ] -> translate ( 'PLUGIN_LOGIN.REMEMBER_ME_STOLEN_COOKIE' ) ) ; } } } return $ session -> user ; } ; $ this -> login = new Login ( $ this -> grav ) ; $ this -> grav [ 'login' ] = $ this -> login ; $ admin_route = $ this -> config -> get ( 'plugins.admin.route' ) ; if ( $ uri -> path ( ) != $ admin_route && substr ( $ uri -> path ( ) , 0 , strlen ( $ admin_route ) + 1 ) != ( $ admin_route . '/' ) ) { $ this -> route = $ this -> config -> get ( 'plugins.login.route' ) ; } if ( $ this -> route && $ this -> route == $ uri -> path ( ) ) { $ this -> enable ( [ 'onPagesInitialized' => [ 'addLoginPage' , 0 ] , ] ) ; } if ( $ uri -> path ( ) == $ this -> config -> get ( 'plugins.login.route_forgot' ) ) { $ this -> enable ( [ 'onPagesInitialized' => [ 'addForgotPage' , 0 ] , ] ) ; } if ( $ uri -> path ( ) == $ this -> config -> get ( 'plugins.login.route_reset' ) ) { $ this -> enable ( [ 'onPagesInitialized' => [ 'addResetPage' , 0 ] , ] ) ; } if ( $ uri -> path ( ) == $ this -> config -> get ( 'plugins.login.route_register' ) ) { if ( $ this -> config -> get ( 'plugins.login.user_registration.enabled' ) ) { $ this -> enable ( [ 'onPagesInitialized' => [ 'addRegisterPage' , 0 ] , ] ) ; } else { throw new \ RuntimeException ( $ this -> grav [ 'language' ] -> translate ( 'PLUGIN_LOGIN.REGISTRATION_DISABLED' ) , 404 ) ; } } if ( $ uri -> path ( ) == $ this -> config -> get ( 'plugins.login.route_activate' ) ) { $ this -> enable ( [ 'onPagesInitialized' => [ 'handleUserActivation' , 0 ] , ] ) ; } }
Initialize login plugin if path matches.
Initialize login plugin if path matches .
/** @var Uri $uri */ // Check to ensure sessions are enabled. // Autoload classes // Define session message service. // Define current user service. /** @var Grav $c */ // If we can present the correct tokens from the cookie, we are logged in // Normal login process // There is a chance that an attacker has stolen // the login token, so we store the fact that // the user was logged in via RememberMe // (instead of login form) // Check if the token was invalid //Initialize Login Object //Store Login Object in Grav // Register route to login page if it has been set.