简介
ACLRBACABAC
安装
composer require -W casbin/webman-permission
使用
1. 依赖注入配置
config/container.php
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
return $builder->build();
2. 数据库配置
🚀 (1) 模型配置
以下模型可以二选一,ThinkORM(默认)
📒📒📒 使用ThinkORM(默认) 📒📒📒
thinkorm.php
📕📕📕 使用laravel数据库(可选) 📕📕📕
database.phppermission.phpadapter
casbin_rule
CREATE TABLE `casbin_rule` (
`id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT,
`ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v0` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v1` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v2` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v3` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v4` VARCHAR ( 128 ) NOT NULL DEFAULT '',
`v5` VARCHAR ( 128 ) NOT NULL DEFAULT '',
PRIMARY KEY ( `id` ) USING BTREE,
KEY `idx_ptype` ( `ptype` ) USING BTREE,
KEY `idx_v0` ( `v0` ) USING BTREE,
KEY `idx_v1` ( `v1` ) USING BTREE,
KEY `idx_v2` ( `v2` ) USING BTREE,
KEY `idx_v3` ( `v3` ) USING BTREE,
KEY `idx_v4` ( `v4` ) USING BTREE,
KEY `idx_v5` ( `v5` ) USING BTREE
) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表';
config/redis
重启webman
php start.php restart
或者
php start.php restart -d
快速开始
安装成功后,可以这样使用:
use Casbin\WebmanPermission\Permission;
// adds permissions to a user
Permission::addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
Permission::addRoleForUser('eve', 'writer');
// adds permissions to a rule
Permission::addPolicy('writer', 'articles','edit');
你可以检查一个用户是否拥有某个权限:
if (Permission::enforce("eve", "articles", "edit")) {
echo '恭喜你!通过权限认证';
} else {
echo '对不起,您没有该资源访问权限';
}
多套驱动配置
webman-permission >=1.2.0
use Casbin\WebmanPermission\Permission;
$permission = Permission::driver("other_conf")
// adds permissions to a user
$permission->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$permission->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$permission->addPolicy('writer', 'articles','edit');
if ($permission->enforce("eve", "articles", "edit")) {
echo '恭喜你!通过权限认证';
} else {
echo '对不起,您没有该资源访问权限';
}
API