ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Think-Casbin > 需要 thinkphp 5.1+ ### **安装** 在`ThinkPHP`项目里,安装`Think-Casbin`扩展: ``` <pre class="calibre10">``` composer require casbin<span class="token1">/</span>think<span class="token1">-</span>adapter ``` ``` ### **发布资源:** 这将自动创建model配置文件`config/casbin-basic-model.conf`,和Casbin的配置文件`config/casbin.php` ``` <pre class="calibre10">``` php think casbin<span class="token3">:</span>publish ``` ``` ### **数据迁移:** 由于Think-Casbin默认将Casbin的策略(Policy)存储在数据库中,所以需要初始化数据库表信息。 执行前,请**确保数据库连接信息配置正确**,如需单独修改`Casbin`的数据库连接信息或表名,可以修改`config/casbin.php`里的配置。 执行下面的命令将自动创建Casbin的策略(Policy)表`casbin_rule`。 ``` <pre class="calibre10">``` php think casbin<span class="token3">:</span>migrate ``` ``` ## **用法** #### **为用户分配权限** ``` <pre class="calibre10">``` use Casbin<span class="token3">;</span> <span class="token">// 给用户alice赋予对data1的read权限</span> Casbin<span class="token3">:</span><span class="token3">:</span><span class="token4">addPolicy</span><span class="token3">(</span><span class="token2">'alice'</span><span class="token3">,</span> <span class="token2">'data1'</span><span class="token3">,</span> <span class="token2">'read'</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ### **判断是权限策略是否存在** ``` <pre class="calibre10">``` Casbin<span class="token3">:</span><span class="token3">:</span><span class="token4">hasPolicy</span><span class="token3">(</span><span class="token2">'alice'</span><span class="token3">,</span> <span class="token2">'data1'</span><span class="token3">,</span> <span class="token2">'read'</span><span class="token3">)</span><span class="token3">;</span> <span class="token">// true</span> ``` ``` ### **移除权限** ``` <pre class="calibre10">``` Casbin<span class="token3">:</span><span class="token3">:</span><span class="token4">removePolicy</span><span class="token3">(</span><span class="token2">'alice'</span><span class="token3">,</span> <span class="token2">'data1'</span><span class="token3">,</span> <span class="token2">'read'</span><span class="token3">)</span><span class="token3">;</span> ``` ``` ### **使用决策器,验证权限** ``` <pre class="calibre10">``` use Casbin<span class="token3">;</span> $sub <span class="token1">=</span> <span class="token2">'alice'</span><span class="token3">;</span> <span class="token">// the user that wants to access a resource.</span> $obj <span class="token1">=</span> <span class="token2">'data1'</span><span class="token3">;</span> <span class="token">// the resource that is going to be accessed.</span> $act <span class="token1">=</span> <span class="token2">'read'</span><span class="token3">;</span> <span class="token">// the operation that the user performs on the resource.</span> <span class="token5">if</span> <span class="token3">(</span><span class="token6">true</span> <span class="token1">===</span> Casbin<span class="token3">:</span><span class="token3">:</span><span class="token4">enforce</span><span class="token3">(</span>$sub<span class="token3">,</span> $obj<span class="token3">,</span> $act<span class="token3">)</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// permit alice to read data1</span> echo <span class="token2">'permit alice to read data1'</span><span class="token3">;</span> <span class="token3">}</span> <span class="token5">else</span> <span class="token3">{</span> <span class="token">// deny the request, show an error</span> <span class="token3">}</span> ``` ``` ### **自定义配置** `config/casbin-basic-model.conf`为Casbin的model文件 `config/casbin.php`为Casbin的adapter、db配置信息 ### **关于:** - 实现基于Think-ORM的Adapter存储(将Policy存储在数据库中) - 实现Casbin的门面(think\\Facade)调用,使用`\Casbin::`可以静态调用`PHP-Casbin`里`Enforcer`的所有方法。 - 使用配置文件对Casbin的Model、Adapter的可配置化