craftcamp/abac-bundle
Composer 安装命令:
composer require craftcamp/abac-bundle
包简介
Symfony Bundle to wrap php-abac library into symfony applications
README 文档
README
Introduction
This Symfony bundle implements support in the Symfony framework for the PHP ABAC library.
This is meant to implement in Symfony applications a new way to handle access control.
This method is based on a policy rules engine, analyzing user and resources attributes instead of roles alone.
Roles can be used, considering them as user attributes.
The advantages of this method is to easily define rules checking user and accessed resources attributes to handle access control.
<?php class MarketController extends Controller { public function buyAction($productId) { $product = $this->get('product_manager')->getProduct($productId); // Call the "craftcamp_abac.security" to check if the user can buy the given product $access = $this->get('craftcamp_abac.security')->enforce( 'product_buying_rule', // the rule name $this->getUser(), // The current user $product // The resource we want to check for access ); if($access !== true) { return new JsonResponse([ // In case of denied access, the library will return an array of the unmatched attributes slugs 'rejected_attributes' => $access ], 403); } } }
Installation
Use composer to set the bundle as your project dependency :
composer require craftcamp/abac-bundle
Then you must load the bundle in your AppKernel file and configure it :
<?php // app/AppKernel.php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; class AppKernel extends Kernel { public function registerBundles() { $bundles = [ // ... new CraftCamp\AbacBundle\CraftCampAbacBundle(), ]; // ... return $bundles; } }
#app/config/config.yml craftcamp_abac: configuration_files: - app/config/attributes.yml - app/config/policy_rules.yml cache_options: # optional cache_folder: '%kernel.cache_dir%/abac'
Documentation
Please refer to the PHP ABAC documentation
Usage
This bundle creates a Symfony service with the php-abac main class.
To check if a rule is enforced, you must define a rule in your configuration file and then check it.
A rule can check user and resource attributes or just the user's.
This is an example of configured rule:
# policy_rules.yml # You can set the attributes and the rules definitions in the same file if you want # Or in multiple files --- attributes: main_user: class: PhpAbac\Example\User type: user fields: age: name: Age parentNationality: name: Parents nationality hasDrivingLicense: name: Driving License vehicle: class: PhpAbac\Example\Vehicle type: resource fields: origin: name: Origin owner.id: name: Owner manufactureDate: name: Release date lastTechnicalReviewDate: name: Last technical review environment: service_status: name: Service status variable_name: SERVICE_STATUS rules: vehicle-homologation: attributes: main_user.hasDrivingLicense: comparison_type: boolean comparison: boolAnd value: true vehicle.lastTechnicalReviewDate: comparison_type: datetime comparison: isMoreRecentThan value: -2Y vehicle.manufactureDate: comparison_type: datetime comparison: isMoreRecentThan value: -25Y vehicle.owner.id: comparison_type: numeric comparison: isEqual value: dynamic vehicle.origin: comparison_type: array comparison: isIn value: ["FR", "DE", "IT", "L", "GB", "P", "ES", "NL", "B"] environment.service_status: comparison_type: string comparison: isEqual value: OPEN
And then in your controller :
<?php class VehicleHomologationController extends Controller { public function homologateAction($vehicleId) { $vehicle = $this->get('vehicle_manager')->getProduct($vehicleId); // Call the "craftcamp_abac.security" to check if the user can homologate the given vehicle $access = $this->get('craftcamp_abac.security')->enforce( 'vehicle-homologation', // the rule name $this->getUser(), // The current user $vehicle // The resource we want to check for access ); if($access !== true) { return new JsonResponse([ // In case of denied access, the library will return an array of the unmatched attributes slugs 'rejected_attributes' => $access ], 403); } } }
Since 0.3.0, you can use autowiring in your controller
<?php use PhpAbac\Abac; class VehicleHomologationController extends Controller { public function homologateAction(Abac $abac, $vehicleId) { $vehicle = $this->get('vehicle_manager')->getProduct($vehicleId); $access = $abac->enforce( 'vehicle-homologation', // the rule name $this->getUser(), // The current user $vehicle // The resource we want to check for access ); if($access !== true) { return new JsonResponse([ // In case of denied access, the library will return an array of the unmatched attributes slugs 'rejected_attributes' => $access ], 403); } } }
Overiding components
The Abac service being autowired, you can replace any of its dependencies by reconfiguring their aliases.
For instance, if you want to implement your own CacheManager, you just have to implement the following configuration:
# services.yaml
services:
App\Cache\MyCacheManager:
public: true
autowire: true
PhpAbac\Manager\CacheManagerInterface: '@App\Cache\MyCacheManager'
Of course your component must implement the associated interface.
The overridable interfaces are:
- PhpAbac\Configuration\ConfigurationInterface
- PhpAbac\Manager\PolicyRuleManagerInterface
- PhpAbac\Manager\AttributeManagerInterface
- PhpAbac\Manager\ComparisonManagerInterface
- PhpAbac\Manager\CacheManagerInterface
craftcamp/abac-bundle 适用场景与选型建议
craftcamp/abac-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 404 次下载、GitHub Stars 达 15, 最近一次更新时间为 2018 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「attributes」 「access-control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 craftcamp/abac-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 craftcamp/abac-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 craftcamp/abac-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony PhpRabcBundle allow to use RBAC control access for symfony project
Provide a way to secure accesses to all routes of an symfony application.
Library used to implement Attribute-Based Access Control in a PHP application
A library that allows you to easily use the PHP-VCR library in your PHPUnit tests.
This Package provides some usefully console features like the attribute syntax for arguments and options, validation, auto ask and casting.
It's a barebone security class written on PHP
统计信息
- 总下载量: 404
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-14