mrjulio/rapture-auth

Rapture 身份验证组件

v1.0.1 2017-11-18 19:09 UTC

This package is not auto-updated.

Last update: 2024-09-21 16:13:38 UTC


README

PhpVersion License

身份验证组件使用接口来创建认证逻辑

  • UserInterface - 用户模型的主要方法,如 getIdgetNameisAuthenticatedisAnonymous
  • SourceInterface - 用户获取方法
  • StorageInterface - 用户存储(通常是一个会话适配器)

要求

  • PHP v5.4

安装

composer require mrjulio/rapture-auth

快速入门

class User implements UserInterface, SourceInterface
{
    /*
     * SourceInterface
     */

	/**
     * @param array $data Authentication data
	 *
	 * @return $this
     */
    public function fetchUser($data)
    {
        $data += ['email' => null, 'password' => null];

        $user = UserQuery::create()->filterByEmail($data['email'])->findOne();

        if ($user && password_verify($data['password'], $user->getPassword())) {
            return $user;
        }
    
        return new self;
    }
    
    /*
     *  UserInterface
     */
    
    public function getId()
    {
    	return $this->id;
    }
    
    /**
     * getName
     *
     * @return string
     */
    public function getName()
    {
        return $this->getFirstname();
    }

    /**
     * Check if user is anonymous
     *
     * @return bool
     */
    public function isAnonymous()
    {
        return (int)$this->getId() == 0;
    }

    /**
     * Check if user is authenticated
     *
     * @return bool
     */
    public function isAuthenticated()
    {
        return !$this->isAnonymous();
    }
}

关于

作者

Iulian N. rapture@iuliann.ro

测试

cd ./test && phpunit

许可

Rapture PHP 身份验证遵循 MIT 许可协议 - 详细信息请参阅 LICENSE 文件。