casbin/hyperf-permission 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

casbin/hyperf-permission

最新稳定版本:v1.0.0

Composer 安装命令:

composer require casbin/hyperf-permission

包简介

An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf.

README 文档

README

An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf..

Installing

Require this package in the composer.json of your Hyperf project. This will download the package.

$ composer require casbin/hyperf-permission -vvv

To publish the config, run the vendor publish command:

$ php bin/hyperf.php vendor:publish casbin/hyperf-permission

This will create a new model config file named config/autoload/casbin-rbac-model.conf, a new permission config file named config/autoload/permission.php and new migrate file named 2020_07_22_213202_create_rules_table.php.

To migrate the migrations, run the migrate command:

$ php bin/hyperf.php migrate

This will create a new table named rules .

Usage

Quick start

Once installed you can do stuff like this:

use Hyperf\Permission\Casbin;

$casbin = new Casbin();

// adds permissions to a user
$casbin->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$casbin->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$casbin->addPolicy('writer', 'articles', 'edit');

You can check if a user has a permission like this:

// to check if a user has permission
if ($casbin->enforce('eve', 'articles', 'edit')) {
  // permit eve to edit articles
} else {
  // deny the request, show an error
}

Using Enforcer Api

It provides a very rich api to facilitate various operations on the Policy:

Gets all roles:

Enforcer::getAllRoles(); // ['writer', 'reader']

Gets all the authorization rules in the policy.:

Enforcer::getPolicy();

Gets the roles that a user has.

Enforcer::getRolesForUser('eve'); // ['writer']

Gets the users that has a role.

Enforcer::getUsersForRole('writer'); // ['eve']

Determines whether a user has a role.

Enforcer::hasRoleForUser('eve', 'writer'); // true or false

Adds a role for a user.

Enforcer::addRoleForUser('eve', 'writer');

Adds a permission for a user or role.

// to user
Enforcer::addPermissionForUser('eve', 'articles', 'read');
// to role
Enforcer::addPermissionForUser('writer', 'articles','edit');

Deletes a role for a user.

Enforcer::deleteRoleForUser('eve', 'writer');

Deletes all roles for a user.

Enforcer::deleteRolesForUser('eve');

Deletes a role.

Enforcer::deleteRole('writer');

Deletes a permission.

Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).

Deletes a permission for a user or role.

Enforcer::deletePermissionForUser('eve', 'articles', 'read');

Deletes permissions for a user or role.

// to user
Enforcer::deletePermissionsForUser('eve');
// to role
Enforcer::deletePermissionsForUser('writer');

Gets permissions for a user or role.

Enforcer::getPermissionsForUser('eve'); // return array

Determines whether a user has a permission.

Enforcer::hasPermissionForUser('eve', 'articles', 'read');  // true or false

See Casbin API for more APIs.

Contributing

You can contribute in one of three ways:

  1. File bug reports using the [issue tracker](https://github.com/php-casbin/hyperf-permission/issues).
  2. Answer questions or fix bugs on the [issue tracker](https://github.com/php-casbin/hyperf-permission/issues).
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License

Apache-2.0

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 3
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2023-08-28