定制 casbin/easyswoole-permission 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

casbin/easyswoole-permission

最新稳定版本:v1.6.0

Composer 安装命令:

composer require casbin/easyswoole-permission

包简介

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

README 文档

README

easyswoole-permission is an authorization library for the easyswoole framework.

Build Status Coverage Status Latest Stable Version Total Downloads License

Chinese Version

It's based on Casbin, an authorization library that supports access control models like ACL, RBAC, ABAC.

All you need to learn to use Casbin first.

Installation

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

$ composer install

Or in the root directory of your easyswoole application, you can use the following composer command to install this package directly .

$ composer require casbin/easyswoole-permission

Usage

Database settings

add mysql configuration to dev.php:

/*################ MYSQL CONFIG ##################*/

'MYSQL'  => [
    'host'          => '127.0.0.1',
    'port'          => 3306,
    'user'          => 'root',
    'password'      => 'root',
    'database'      => 'easyswoole',
    'timeout'       => 5,
    'charset'       => 'utf8mb4',
]

add mysql configuration to EasySwooleEvent.php:

use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;

public static function initialize()
{
  ...
  $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
  DbManager::getInstance()->addConnection(new Connection($config));
}

Create corresponding data table

Before using it, you need to create a table named casbin_rules for Casbin to store the policy.

Take mysql as an example:

CREATE TABLE  if not exists  `casbin_rules` (
  `id` BigInt(20) unsigned NOT NULL AUTO_INCREMENT,
  `ptype` varchar(255) DEFAULT NULL,
  `v0` varchar(255) DEFAULT NULL,
  `v1` varchar(255) DEFAULT NULL,
  `v2` varchar(255) DEFAULT NULL,
  `v3` varchar(255) DEFAULT NULL,
  `v4` varchar(255) DEFAULT NULL,
  `v5` varchar(255) DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;

Quick start

Then you can start like this:

use EasySwoole\Permission\Casbin;
use EasySwoole\Permission\Config;

$config = new Config();
$casbin = new Casbin($config);

// 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:

First create an instance of the enforcer class, and the following operations are based on this instance:

$config = new Config();
$casbin = new Casbin($config);
$enforcer = $casbin->enforcer();

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.

Thinks

Casbin in Easyswoole. You can find the full documentation of Casbin on the website.

License

This project is licensed under the Apache 2.0 license.

统计信息

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

GitHub 信息

  • Stars: 9
  • Watchers: 3
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2021-03-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固