Giter VIP home page Giter VIP logo

think-authz's Introduction

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.

think-authz's People

Contributors

basakest avatar leeqvip avatar qeq66 avatar tinywan avatar zhanghangt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.