voku/simpleacl
Composer 安装命令:
composer require voku/simpleacl
包简介
Simple Access Control List (ACL) for PHP.
README 文档
README
Simple Access Control List (ACL) for PHP.
WARNING: this is only a Maintained-Fork of "https://github.com/alexshelkov/SimpleAcl/"
Install
Using composer
Add following in your composer.json:
{
"require": {
"voku/simpleacl": "3.*"
}
}
Manual
Download library and register PSR-0 compatible autoloader.
Usage
Basic usage
Theory
There is 4 kind of objects: Rules, Roles, Resources and Acl which holds list of Rules. Some Rule can grant access for some Role to some Resource.
Create rules
Lets create "View" Rule, and with with it grant access for "User" to "Page" (note: all names are case sensitive):
$view = new Rule('View'); $view->setRole(new Role('User')); $view->setResource(new Resource('Page')); $view->setAction(true); // true means that we allow access var_dump((bool)$view->isAllowed('View', 'User', 'Page')); // true
Add rules
There is not much sense in rules without Acl. So we need to add rules in it. In next example we add few rules in Acl and see whats happens.
$acl = new Acl(); $user = new Role('User'); $admin = new Role('Admin'); $siteFrontend = new Resource('SiteFrontend'); $siteBackend = new Resource('SiteBackend'); $acl->addRule($user, $siteFrontend, new Rule('View'), true); $acl->addRule($admin, $siteFrontend, 'View', true); // you can use string as rule $acl->addRule($admin, $siteBackend, 'View', true); var_dump($acl->isAllowed('User', 'SiteFrontend', 'View')); // true var_dump($acl->isAllowed('User', 'SiteBackend', 'View')); // false var_dump($acl->isAllowed('Admin', 'SiteFrontend', 'View')); // true var_dump($acl->isAllowed('Admin', 'SiteBackend', 'View')); // true
They are various way to add rules to Acl, addRule method accepts from one to four arguments, so you can also add rules like this:
<?php // before add $view rule to Acl you can set it action, role or resource $acl->addRule($view); // where is true -- is action $acl->addRule($view, true); // in that case action must be set before adding rule $acl->addRule($user, $siteBackend, $view);
Roles and resource inheritance
As you maybe notice in previous example we have some duplication of code, because both "User" and "Admin" was allowed to "View" "SiteFrontend" we added 2 rules. But it is possible to avoid this using roles inheritance.
$acl = new Acl(); $user = new Role('User'); $admin = new Role('Admin'); $siteFrontend = new Resource('SiteFrontend'); $siteBackend = new Resource('SiteBackend'); $acl->addRule($user, $siteFrontend, 'View', true); $acl->addRule($admin, $siteFrontend, 'View', true); $acl->addRule($admin, $siteBackend, 'View', true); var_dump($acl->isAllowed('User', 'SiteFrontend', 'View')); // true var_dump($acl->isAllowed('User', 'SiteBackend', 'View')); // false var_dump($acl->isAllowed('Admin', 'SiteFrontend', 'View')); // true var_dump($acl->isAllowed('Admin', 'SiteBackend', 'View')); // true
Inheritance works for resources too.
Using callbacks
You can create more complex rules using callbacks.
$acl = new Acl(); $user = new Role('User'); $siteFrontend = new Resource('SiteFrontend'); $acl->addRule($user, $siteFrontend, 'View', function (SimpleAcl\RuleResult $ruleResult) { echo $ruleResult->getNeedRoleName() . "\n"; echo $ruleResult->getNeedResourceName() . "\n"; echo $ruleResult->getRule()->getRole()->getName() . "\n"; echo $ruleResult->getRule()->getResource()->getName() . "\n"; return true; }); var_dump($acl->isAllowed('User', 'SiteFrontend', 'View')); // true // Outputs: // User // SiteFrontend // User // SiteFrontend // bool(true)
Using role and resource aggregates
It is possible to check access not for particular Role or Resource, but for objects which aggregate them. These kind of objects must implement, respectively, SimpleAcl\Role\RoleAggregateInterface and SimpleAcl\Role\ResourceAggregateInterface.
You can use SimpleAcl\Role\RoleAggregate and SimpleAcl\Role\ResourceAggregate as object which allow aggregation.
$acl = new Acl(); $user = new Role('User'); $admin = new Role('Admin'); $all = new RoleAggregate; $all->addRole($user); $all->addRole($admin); $siteFrontend = new Resource('SiteFrontend'); $siteBackend = new Resource('SiteBackend'); $acl->addRule($user, $siteFrontend, 'View', true); $acl->addRule($admin, $siteBackend, 'View', true); var_dump($acl->isAllowed($all, 'SiteFrontend', 'View')); // true var_dump($acl->isAllowed($all, 'SiteBackend', 'View')); // true
You can have access to role and resource aggregates in callbacks.
$acl->addRule($user, $siteFrontend, 'View', function (SimpleAcl\RuleResult $ruleResult) { var_dump($ruleResult->getRoleAggregate()); var_dump($ruleResult->getResourceAggregate()); }); var_dump($acl->isAllowed($all, 'SiteFrontend', 'View')); // true
For more help check out wiki pages.
voku/simpleacl 适用场景与选型建议
voku/simpleacl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.73k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2015 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「acl」 「permission」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 voku/simpleacl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 voku/simpleacl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 voku/simpleacl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
This package provides a flexible way to add Role-based Permissions to Laravel
Laravel JWT auth service package
统计信息
- 总下载量: 5.73k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-10-16