• Stars
    star
    260
  • Rank 157,189 (Top 4 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

ThinkPHP 6.0 Authorization

Think-authz 是一个专为 ThinkPHP6.0 打造的授权(角色和权限控制)工具

Build Status Coverage Status Latest Stable Version Total Downloads License

它基于 PHP-Casbin, 一个强大的、高效的开源访问控制框架,支持基于ACL, RBAC, ABAC等访问控制模型。

在这之前,你需要了解 Casbin 的相关知识。

安装

该扩展需要 PHP 7.1+ 和 ThinkPHP 6.0+,针对 TP 5.1 请使用 Think-Casbin .

使用composer安装:

composer require casbin/think-authz

注册服务,在应用的全局公共文件service.php中加入:

return [
    // ...

    tauthz\TauthzService::class,
];

发布配置文件和数据库迁移文件:

php think tauthz:publish

这将自动生成 config/tauthz-rbac-model.confconfig/tauthz.php 文件。

执行迁移工具(确保数据库配置信息正确):

php think migrate:run

这将创建名为 rules 的表。

用法

快速开始

安装成功后,可以这样使用:

use tauthz\facade\Enforcer;

// adds permissions to a user
Enforcer::addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
Enforcer::addRoleForUser('eve', 'writer');
// adds permissions to a rule
Enforcer::addPolicy('writer', 'articles','edit');

你可以检查一个用户是否拥有某个权限:

// to check if a user has permission
if (Enforcer::enforce("eve", "articles", "edit")) {
    // permit eve to edit articles
} else {
    // deny the request, show an error
}

使用 Enforcer Api

它提供了非常丰富的 API,以促进对 Policy 的各种操作:

获取所有角色:

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

获取所有的角色的授权规则:

Enforcer::getPolicy();

获取某个用户的所有角色:

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

获取某个角色的所有用户:

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

决定用户是否拥有某个角色:

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

给用户添加角色:

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

赋予权限给某个用户或角色:

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

删除用户的角色:

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

删除某个用户的所有角色:

Enforcer::deleteRolesForUser('eve');

删除单个角色:

Enforcer::deleteRole('writer');

删除某个权限:

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

删除某个用户或角色的权限:

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

删除某个用户或角色的所有权限:

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

获取用户或角色的所有权限:

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

决定某个用户是否拥有某个权限

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

更多 API 参考 Casbin API

使用中间件

该扩展包带有一个 \tauthz\middleware\Basic::class 中间件:

Route::get('news/:id','News/Show')
	->middleware(\tauthz\middleware\Basic::class, ['news', 'read']);

感谢

Casbin,你可以查看全部文档在其 官网 上。

License

This project is licensed under the Apache 2.0 license.

More Repositories

1

php-casbin

An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
PHP
1,204
star
2

laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
PHP
279
star
3

think-casbin

专为ThinkPHP定制的Casbin的扩展包,Casbin是一个功能强大,高效的开源访问控制库。
PHP
154
star
4

codeigniter-permission

Associate users with roles and permissions, use Casbin in CodeIgniter4 Web Framework.
PHP
45
star
5

yii-permission

Use casbin in Yii PHP Framework, Casbin is a powerful and efficient open-source access control library.
PHP
45
star
6

laravel-casbin

This repository has moved to https://github.com/php-casbin/laravel-authz
PHP
43
star
7

webman-permission

🔒 An authorization library that supports access control models like ACL, RBAC, ABAC for webman plugin
PHP
39
star
8

database-adapter

Database adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
28
star
9

hyperf-permission

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

casbin-tutorials

Here are some tutorials of PHP-Casbin.
10
star
11

dbal-adapter

Doctrine DBAL Adapter for Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
10
star
12

easyswoole-permission

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

casbin-with-slim

Casbin skeleton application with Slim Framework 4.
PHP
9
star
14

yii-casbin

Use casbin in Yii PHP Framework, Casbin is a powerful and efficient open-source access control library.
PHP
7
star
15

casbin-client

PHP's client for Casbin-Server. Casbin-Server is the Access Control as a Service (ACaaS) solution based on Casbin.
PHP
6
star
16

symfony-permission

Use Casbin in Symfony Framework, Casbin is a powerful and efficient open-source access control library.
PHP
5
star
17

medoo-adapter

Medoo Adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
4
star
18

workerman-redis-watcher

Workerman Redis watcher for PHP-Casbin
PHP
4
star
19

laminas-db-adapter

Laminas-db Adapter for Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
4
star
20

cake-permission

Use Casbin in CakePHP Framework, Casbin is a powerful and efficient open-source access control library.
PHP
4
star
21

phalcon-permission

An authorization library that supports access control models like ACL, RBAC, ABAC in Phalcon.
PHP
3
star
22

redis-watcher

Redis watcher for PHP-Casbin
2
star
23

psr3-bridge

This library provides a PSR-3 compliant bridge for Casbin Logger.
PHP
2
star
24

zend-db-adapter

Zend Db Adapter for Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
2
star
25

swoole-redis-watcher

Redis watcher for PHP-Casbin in Swoole.
PHP
1
star