dez-php/phalcon-auth

Phalcon 2 框架的认证组件

v0.2.3 2016-06-08 21:09 UTC

This package is not auto-updated.

Last update: 2024-09-22 07:22:51 UTC


README

Phalcon 2.x 框架的认证组件

#使用方法

在依赖注入容器中注册 Auth

include_once '../vendor/autoload.php';

$container  = new \Phalcon\Di\FactoryDefault();

$container->set('auth', function(){
    
    // Pass session adapter into Auth
    $auth   = new \PhalconDez\Auth\Auth(
        new \PhalconDez\Auth\Adapter\Session()
    );
    
    // Pass empty instance of Credentials Model implement PhalconModel
    $auth->setCredentialsModel(
        new \PhalconDez\Auth\Model\Credentials()
    );
    
    // Pass empty instance of Session Model implement PhalconModel
    $auth->setSessionModel(
        new \PhalconDez\Auth\Model\Session()
    );
    
    // Run initialize
    $auth->initialize();
    
    return $auth;
});

然后从容器中获取 auth

/** @var \PhalconDez\Auth\Auth $auth */
$auth   = $container->get('auth');

授权

try{
    $auth->authenticate('test@gmail.com', 'qwerty');
}catch (\Exception $e){
    echo "You have some errors: {$e->getMessage()}";
}

创建新的凭证

try{
    $auth->authenticate($email, $password);
}catch (\Exception $e){
    $auth->create($email, $password);
    $container->get('response')->redirect('auth-page');
}

验证密码

if($auth->isUser() && $auth->getAdapter()->verifyPassword('qwerty') === true){
    echo 'Password is corrected';
}

检查用户授权

if($auth->isGuest() === true){
    echo 'You are non-authorized user';
}

if($auth->isUser() === true){
    echo 'You are authorized user';
}

获取一些授权用户数据

if($auth->isUser() === true){
    $userModel  = $auth->getUser();
    echo "Hello, {$userModel->getEmail()}. You was registered at {$userModel->getCreatedAt()}";
}

#问题和拉取请求

####我很高兴收到您的批评、任务、错误和拉取请求。