Psecio\Gatekeeper\Gatekeeper::authenticate PHP Метод

authenticate() публичный статический Метод

Authenticate a user given the username/password credentials
public static authenticate ( array $credentials, boolean $remember = false ) : boolean
$credentials array Credential information (must include "username" and "password")
$remember boolean Flag to activate the "remember me" functionality
Результат boolean Pass/fail of authentication
    public static function authenticate(array $credentials, $remember = false)
    {
        $username = $credentials['username'];
        $user = new UserModel(self::$datasource);
        $user->findByUsername($username);
        self::getLogger()->info('Authenticating user.', array('username' => $username));
        // If they're inactive, they can't log in
        if ($user->status === UserModel::STATUS_INACTIVE) {
            self::getLogger()->error('User is inactive and cannot login.', array('username' => $username));
            throw new Exception\UserInactiveException('User "' . $username . '" is inactive and cannot log in.');
        }
        // Handle some throttle logic, if it's turned on
        if (self::$throttleStatus === true) {
            // Set up our default throttle restriction
            $instance = new \Psecio\Gatekeeper\Restrict\Throttle(array('userId' => $user->id));
            self::$restrictions[] = $instance;
        }
        // Check any restrictions
        if (!empty(self::$restrictions)) {
            foreach (self::$restrictions as $restriction) {
                if ($restriction->evaluate() === false) {
                    self::getLogger()->error('Restriction failed.', array('restriction' => get_class($restriction)));
                    throw new Exception\RestrictionFailedException('Restriction ' . get_class($restriction) . ' failed.');
                }
            }
        }
        // Verify the password!
        $result = password_verify($credentials['password'], $user->password);
        if (self::$throttleStatus === true && $result === true) {
            self::getLogger()->info('User login verified.', array('username' => $username));
            // If throttling is enabled, set the user back to allow
            if (isset($instance)) {
                $instance->model->allow();
            }
            $user->updateLastLogin();
            if ($remember === true) {
                self::getLogger()->info('Activating remember me.', array('username' => $username));
                self::rememberMe($user);
            }
        }
        return $result;
    }

Usage Example

Пример #1
0
 public function login($username, $pass)
 {
     $credentials = array('username' => $username, 'password' => $pass);
     if (Gatekeeper::authenticate($credentials) == true) {
         $_SESSION['username'] = $username;
         $_SESSION['pass'] = $pass;
         return true;
     }
     return false;
 }
All Usage Examples Of Psecio\Gatekeeper\Gatekeeper::authenticate