vojtech-dobes/nette-multi-authenticator

允许通过统一的API定义多种认证方式(适用于Nette框架)。

v2.0.0 2014-05-27 14:10 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:00:54 UTC


README

允许通过统一的API定义多种认证方式(适用于Nette框架)

许可证

新BSD

依赖

Nette 2.0 或更高版本

安装

实际上没有 :).

使用方法

编写您的认证器,但不要让它们实现 Nette\Security\IAuthenticator。另一方面,请以依赖项的形式请求 Nette\Security\User 服务。

class CredentialsAuthenticator
{

    /** @var Nette\Security\User */
    private $user;
    
    public function __construct(Nette\Security\User $user)
    {
        $this->user = $user;
    }

}

现在编写您的登录方法。发挥创意!

public function login($username, $password)
{
    // ... your logic
    
    $this->user->login(new Identity( ... ));
}

将认证器注册为服务

services:
    - CredentialsAuthenticator

完成。

对于认证,您应该使用特定的认证器

class SignPresenter extends Nette\Application\UI\Presenter
{

    /** @var CredentialsAuthenticator @inject */
    public $credentialsAuthenticator;

    // ...
    
    public function processLoginForm($form)
    {
        $values = $form->getValues();
        $this->credentialsAuthenticator->login($values->username, $values->password);
    }

}

关键点:使用常规依赖项,并将 Nette\Security\User 包裹在它们中,而不是反过来。