Giter Site home page Giter Site logo

php-casbin / think-casbin Goto Github PK

View Code? Open in Web Editor NEW
151.0 13.0 21.0 31 KB

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

Home Page: https://casbin.org

License: Apache License 2.0

PHP 100.00%
casbin thinkphp acl rbac permission abac security access-control roles restful

think-casbin's Introduction

Think-Casbin

Build Status Coverage Status Latest Stable Version Total Downloads License

PHP-Casbin 是一个强大的、高效的开源访问控制框架,它支持基于各种访问控制模型的权限管理。

Think-Casbin 是一个专为ThinkPHP5.1定制的Casbin的扩展包,使开发者更便捷的在thinkphp项目中使用Casbin。

针对 ThinkPHP6.0 现在推出了更加强大的扩展 ThinkPHP 6.0 Authorization.

知识储备

  • 熟练使用Composer包管理工具
  • 掌握ThinkPHP框架各个功能,例如:门面(Facade)、模型、数据库迁移工具等
  • 熟悉PHP命令行、ThinkPHP命令行的使用
  • 了解Casbin工作原理及用法

安装

  1. 创建thinkphp项目(如果没有):
composer create-project topthink/think=5.1.* tp5
  1. ThinkPHP项目里,安装Think-Casbin扩展:
composer require casbin/think-adapter
  1. 发布资源:
php think casbin:publish

这将自动创建model配置文件config/casbin-basic-model.conf,和Casbin的配置文件config/casbin.php

  1. 数据迁移:

由于Think-Casbin默认将Casbin的策略(Policy)存储在数据库中,所以需要初始化数据库表信息。

执行前,请确保数据库连接信息配置正确,如需单独修改Casbin的数据库连接信息或表名,可以修改config/casbin.php里的配置。

php think casbin:migrate

这将会自动创建Casbin的策略(Policy)表casbin_rule

用法

为用户分配权限

use Casbin;

// 给用户alice赋予对data1的read权限
Casbin::addPolicy('alice', 'data1', 'read');

判断是权限策略是否存在

Casbin::hasPolicy('alice', 'data1', 'read'); // true

移除权限

Casbin::removePolicy('alice', 'data1', 'read');

使用决策器,验证权限

use Casbin;

$sub = 'alice'; // the user that wants to access a resource.
$obj = 'data1'; // the resource that is going to be accessed.
$act = 'read'; // the operation that the user performs on the resource.

if (true === Casbin::enforce($sub, $obj, $act)) {
    // permit alice to read data1
    echo 'permit alice to read data1';
} else {
    // deny the request, show an error
}

自定义配置

config/casbin-basic-model.conf为Casbin的model文件

config/casbin.php为Casbin的adapter、db配置信息

更多API参考

关于

Think-Casbin

  • 实现基于Think-ORM的Adapter存储(将Policy存储在数据库中)
  • 实现Casbin的门面(think\Facade)调用,使用\Casbin::可以静态调用PHP-CasbinEnforcer的所有方法。
  • 使用配置文件对Casbin的Model、Adapter的可配置化

通过Casbin官网 (https://casbin.org )查看更多用法。

think-casbin's People

Contributors

leeqvip avatar tinywan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

think-casbin's Issues

多租户报错grouping policy elements do not meet role definition

`
grouping policy elements do not meet role definition
[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && r.obj == p.obj && r.act == p.act

// 设置角色权限
Casbin::addPolicy('zzyRole','1', 'data1', 'filed', 'read');
// 设置用户角色
Casbin::AddRoleForUser('zzy','zzyRole','1');

`

AddRoleForUser 方法node版是有最后一个参数的

casbin/node-casbin@e876217

composer 出错

Problem 1 - casbin/think-adapter v1.0.1 requires topthink/think-migration ^2.0 -> satisfiable by topthink/t hink-migration[2.0.x-dev, v2.0.1, v2.0.2, v2.0.3] but these conflict with your requirements or minimu m-stability. - casbin/think-adapter v1.0.0 requires topthink/think-migration ^2.0 -> satisfiable by topthink/t hink-migration[2.0.x-dev, v2.0.1, v2.0.2, v2.0.3] but these conflict with your requirements or minimu m-stability. - Installation request for casbin/think-adapter ^1.0 -> satisfiable by casbin/think-adapter[v1.0. 0, v1.0.1]. Installation failed, reverting ./composer.json to its original content.


tp6官方的topthink/think-migration已经出到^3.0

thinkphp5.1下载不了think-adapter

微信截图_20190909114712
我发现这个问题是因为tp5.1项目就是包topthink/think,而think-adapter依赖着topthink/think,相互依赖所以下载不了,如果依赖topthink/framework核心库,应该就能下载了,是不是?

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.