piko / 用户
轻量级用户会话管理器,用于登录/注销和检索用户身份。
v2.0.1
2022-11-04 14:33 UTC
Requires
- php: >=7.1.0
- piko/core: ^2.2
Requires (Dev)
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.5
README
轻量级用户会话管理器,用于登录/注销、检查权限和在会话之间检索用户身份。
安装
建议您使用Composer安装Piko用户。
composer require piko/user
用法
基本示例
require 'vendor/autoload.php'; use Piko\User; use Piko\User\IdentityInterface; // Define first your user identity class class Identity implements IdentityInterface { private static $users = [ 1 => 'paul', 2 => 'pierre', ]; public $id; public $username; public static function findIdentity($id) { if (isset(static::$users[$id])) { $user = new static(); $user->id = $id; $user->username = static::$users[$id]; return $user; } return null; } public function getId() { return $this->id; } } $user = new User([ 'identityClass' => Identity::class, 'checkAccess' => function($id, $permission) { return $id == 1 && $permission == 'test'; } ]); // Login $user->login(Identity::findIdentity(1)); if (!$user->isGuest()) { echo $user->getIdentity()->username; // paul } if ($user->can('test')) { echo 'I can test'; } $user->logout(); if ($user->isGuest()) { var_dump($user->getIdentity()); // null echo 'Not Authenticated'; } if (!$user->can('test')) { echo 'I cannot test'; }
高级示例:查看UserTest.php