laasti/warden

一个不依赖于任何PHP框架的认证和授权包。

v0.1.3 2016-04-21 22:12 UTC

This package is auto-updated.

Last update: 2024-09-20 22:58:32 UTC


README

这是一个不依赖于任何PHP框架的认证和授权包。它不会也永远不会提供创建用户的方式。默认情况下,它只读取提供数据库中的用户表。你可以自由地使用最适合你的方法(PDO、ORM...)。

这是一个相当简单的库。目前你不会在这里找到像Sentinel那样的高级安全特性。密码使用PHP 5.5的最新password_函数进行散列。

不过请注意,你应该确保你的会话能够很好地保护免受已知漏洞的侵害。你还应该添加一个激活和重置机制。这个包可能会在未来提供这些功能。对抗暴力破解攻击的节流机制也可以提高安全性。

安装

composer require laasti/warden

原生哈希器使用了PHP 5.5的password_*函数。

对于PHP 5.4,你需要另一个库

composer require ircmaxell/password-compat

使用方法

角色应该使用大写字母,权限使用小写字母。

使用PHP的原生会话,只需确保使用SessionHandlerInterface和session_set_save_handler()注册一个会话处理器,或者你可以实现自己的SessionInterface。

默认情况下使用PHP 5.5的本地密码函数,对于向后兼容,你需要ircmaxell/password-compat或者你可以提供自己的HasherInterface。

提供了一个基本的PDO存储库来从数据库中检索用户,但你可以创建自己的RepositoryInterface。

$pdo = new PDO($dsn, $user, $password);
//By default the repository looks for a table "users" with columns: id, email, password, roles, permissions
//Roles and permissions are comma-delimited.
$repo = new Laasti\Warden\Repositories\PdoUserRepository($pdo);
$warden = new Laasti\Warden\Warden();

//API
$warden->admit($identifier, $password); //Logs in user matching credentials
$warden->isAdmitted(); //User is logged in
$warden->couldBeAdmitted($identifier, $password); //Checks if user could be logged in
$warden->admitUser($user); //Logs in provided user, useful to bypass authentication
$warden->currentUser(); //Logged in user, instance of GuestUser if none
$warden->dismiss(); //Logs out current user
$warden->grantAccess($roleOrPermission); //Check for role or permission in current user
$warden->grantAccessByPermission($permission); // Grant access if user matches permission
$warden->grantAccessByPermissions($permissions); // Grant access if user matches all permissions
$warden->grantAccessByRole($role); // Grant access if user matches role
$warden->grantAccessByRoles($roles); // Grant access if user matches all roles
$warden->getHasher()->hash($password); //Get a hash for a password

//Using Roles Dictionary
//Roles can inherit permissions by default, to assign permissions to roles
//you need to define a roles dictionary using an array
$dictionary = [
    'ROLE' => ['permission', 'permission2']
];
$warden->setRolesDictionary($dictionary);

贡献

  1. 将它分支出来!
  2. 创建你的功能分支: git checkout -b my-new-feature
  3. 提交你的更改: git commit -am '添加一些功能'
  4. 将分支推送到远程: git push origin my-new-feature
  5. 提交一个拉取请求 :D

历史记录

查看CHANGELOG.md获取更多信息。

致谢

作者:Sonia Marquette (@nebulousGirl)

许可

在MIT许可证下发布。查看LICENSE.txt文件。