apaoww/yii2-cerbos
Composer 安装命令:
composer require apaoww/yii2-cerbos
包简介
Yii2 extension for Cerbos access control with HTTP API integration
README 文档
README
A Yii2 extension that provides seamless integration with Cerbos for fine-grained access control and authorization.
Features
- CerbosAccessControl: Action filter for automatic route-based access control
- CerbosHttpAuth: HTTP client component for Cerbos API integration
- System Prefix Support: Add system-wide prefixes to resource names
- RBAC Fallback: Optional fallback to Yii2's built-in RBAC system
- Flexible Resource Mapping: Custom resource and action mapping
- Batch Permission Checking: Check multiple permissions in a single request
Installation
Via Composer (Recommended)
composer require apaoww/yii2-cerbos
Local Development
Add to your project's composer.json:
{
"repositories": [
{
"type": "path",
"url": "vendor/apaoww/yii2-cerbos"
}
],
"require": {
"apaoww/yii2-cerbos": "*"
}
}
Configuration
1. Configure the Cerbos HTTP Component
Add the Cerbos component to your application config:
// config/main.php or config/main-local.php return [ 'components' => [ 'cerbos' => [ 'class' => 'apaoww\cerbos\CerbosHttpAuth', 'host' => 'localhost:3592', // Your Cerbos server 'httpHost' => null, // Optional separate HTTP host 'systemPrefix' => 'imap', // Optional system prefix for resources 'projectCode' => 'imap', 'db' => 'db', // Use custom database component 'authAssignmentTable' => 'ad_sso_admin.auth_assignment', ], ], ];
2. Configure Access Control
You can use CerbosAccessControl in two ways: globally for the entire application (recommended) or per-controller.
Option A: Global Application-Wide Access Control (Recommended)
Configure access control for your entire application by adding it to your main config file:
// frontend/config/main-local.php (or backend/config/main-local.php) return [ 'components' => [ // ... other components ], // Global access control filter 'as access' => [ 'class' => 'apaoww\cerbos\CerbosAccessControl', 'systemPrefix' => 'imap', // System prefix for all resources 'projectCode' => 'imap', 'db' => 'db', // Use custom database component 'authAssignmentTable' => 'ad_sso_admin.auth_assignment', 'except' => ['debug/*', 'gii/*'], // Exclude debug and development tools 'allowActions' => [ // Public actions that don't require authentication 'site/index', 'site/login', 'site/logout', 'site/error', 'site/captcha', 'cas/*', // CAS authentication routes ], // Map specific routes to custom Cerbos resources and actions 'resourceMap' => [ 'site/index' => [ 'resource' => 'dashboard', 'action' => 'view', ], 'user/*' => [ 'resource' => 'user', 'action' => function() { // Dynamic action mapping based on controller action $actionId = Yii::$app->controller->action->id; $actionMap = [ 'index' => 'list', 'view' => 'read', 'create' => 'create', 'update' => 'update', 'delete' => 'delete', ]; return $actionMap[$actionId] ?? $actionId; }, ], 'report/*' => [ 'resource' => 'report', 'action' => function() { return Yii::$app->controller->action->id; }, 'resourceId' => function() { return Yii::$app->request->get('id'); }, 'attributes' => [ 'department' => function() { return Yii::$app->user->identity->department ?? 'unknown'; }, ], ], ], // Enable fallback to existing RBAC system during migration 'useMdmFallback' => true, // Custom extractors for resource identification 'resourceIdExtractor' => function($action) { return Yii::$app->request->get('id') ?: Yii::$app->request->post('id'); }, 'resourceAttributesExtractor' => function($action) { $attributes = []; $user = Yii::$app->user->identity; if ($user) { if (isset($user->department)) { $attributes['department'] = $user->department; } if (isset($user->role)) { $attributes['role'] = $user->role; } } return $attributes; }, ], ];
Option B: Per-Controller Access Control
Add the access control filter to individual controller behaviors:
use apaoww\cerbos\CerbosAccessControl; class ExampleController extends Controller { public function behaviors() { return [ 'access' => [ 'class' => CerbosAccessControl::class, 'systemPrefix' => 'imap', // Optional: override system prefix 'useMdmFallback' => true, // Enable RBAC fallback 'allowActions' => ['login', 'signup'], // Always allowed actions 'resourceMap' => [ // Custom resource mappings 'example/special' => [ 'resource' => 'special_resource', 'action' => 'custom_action', ], ], ], ]; } }
When to Use Which Approach:
- Global Configuration: Use when you want consistent access control across your entire application. This is the recommended approach for most applications.
- Per-Controller Configuration: Use when you need different access control rules for specific controllers or when gradually migrating to Cerbos.
Usage Examples
Basic Usage
The extension automatically maps controller/action routes to Cerbos resources:
example/index→ Resource:imap_example, Action:indexexample/view→ Resource:imap_example, Action:readexample/create→ Resource:imap_example, Action:createexample/update→ Resource:imap_example, Action:updateexample/delete→ Resource:imap_example, Action:delete
Custom Resource Mapping
'resourceMap' => [ 'user/*' => [ 'resource' => 'user_management', 'action' => function() { return Yii::$app->controller->action->id === 'index' ? 'list' : 'manage'; }, ], 'report/generate' => [ 'resource' => 'reports', 'action' => 'generate', 'resourceId' => function() { return Yii::$app->request->get('type', 'default'); }, 'attributes' => [ 'department' => Yii::$app->user->identity->department ?? 'unknown', ], ], ],
Resource ID and Attributes Extraction
'resourceIdExtractor' => function($action) { return Yii::$app->request->get('id') ?: 'default'; }, 'resourceAttributesExtractor' => function($action) { return [ 'owner_id' => Yii::$app->user->id, 'created_at' => date('Y-m-d'), ]; },
Direct Cerbos API Usage
// Single permission check $allowed = Yii::$app->cerbos->checkPermission( 'read', 'imap_document', '123', ['department' => 'sales'] ); // Batch permission check $permissions = Yii::$app->cerbos->batchCheckPermissions( ['read', 'write', 'delete'], 'imap_document', '123' ); // Returns: ['read' => true, 'write' => false, 'delete' => false]
Configuration Options
CerbosAccessControl Options
| Option | Type | Default | Description |
|---|---|---|---|
allowActions |
array | [] |
Actions that are always allowed without checking permissions |
resourceMap |
array | [] |
Custom mapping of routes to Cerbos resources and actions |
useMdmFallback |
bool | true |
Enable fallback to Yii2 RBAC when Cerbos policy doesn't exist |
systemPrefix |
string | null |
Prefix to prepend to all resource names |
resourceIdExtractor |
callable | null |
Custom function to extract resource ID from request |
resourceAttributesExtractor |
callable | null |
Custom function to extract resource attributes |
CerbosHttpAuth Options
| Option | Type | Default | Description |
|---|---|---|---|
host |
string | 'localhost:3592' |
Cerbos server host and port |
httpHost |
string | null |
Alternative HTTP host (if different from main host) |
systemPrefix |
string | null |
System prefix for resource names |
Cerbos Policy Example
Here's an example Cerbos policy file (imap_example.yaml) that works with this extension:
--- apiVersion: api.cerbos.dev/v1 resourcePolicy: version: "default" resource: "imap_example" rules: - actions: ['*'] effect: EFFECT_ALLOW roles: - admin - actions: ['index', 'read'] effect: EFFECT_ALLOW roles: - user - manager - actions: ['create', 'update', 'delete'] effect: EFFECT_ALLOW roles: - manager condition: match: expr: request.resource.attr.department == principal.attr.department
Requirements
- PHP 8.0 or higher
- Yii2 framework 2.0.14 or higher
- Cerbos server running and accessible
- GuzzleHTTP 7.0 or higher
License
This extension is released under the MIT License. See LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please create an issue in the GitHub repository.
apaoww/yii2-cerbos 适用场景与选型建议
apaoww/yii2-cerbos 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 29 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「security」 「extension」 「rbac」 「yii2」 「access-control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 apaoww/yii2-cerbos 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 apaoww/yii2-cerbos 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 apaoww/yii2-cerbos 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
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.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Provide a way to secure accesses to all routes of an symfony application.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 29
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-12