Integrate Casbin Permission Management into Webman (PHP) – A Step‑by‑Step Guide
This guide explains how to add the Casbin access‑control library to a Webman PHP project, covering installation, dependency‑injection setup, database schema creation, Redis configuration, quick‑start code examples, and custom logging with PSR‑3 support.
Overview
Casbin is a powerful, high‑performance open‑source access‑control framework that supports multiple models and many programming languages such as PHP, Go, Java, and Python.
Supported Languages
Installation
composer require -W casbin/webman-permissionPlugin address: https://www.workerman.net/plugin/6
Usage
Dependency Injection Configuration
Edit config/container.php so that it returns a DI container builder:
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
return $builder->build();Database Configuration
The default policy storage uses ThinkORM. If you prefer Laravel's illuminate/database, follow the official documentation at https://www.workerman.net/doc/webman/db/tutorial.html.
Create the casbin_rule table:
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='策略规则表';Redis Configuration ( config/redis )
return [
'default' => [
'host' => 'dnmp-redis',
'password' => '123456',
'port' => 6379,
'database' => 0,
],
'pool_size' => 3,
];Quick Start
After installation, you can use the library as follows:
use Casbin\WebmanPermission\Permission;
// Add permissions to a user
Permission::addPermissionForUser('eve', 'articles', 'read');
// Add a role for a user
Permission::addRoleForUser('eve', 'writer');
// Add a policy to a role
Permission::addPolicy('writer', 'articles', 'edit');Check whether a user has a specific permission:
use Casbin\WebmanPermission\Permission;
if (Permission::enforce('eve', 'articles', 'edit')) {
echo '恭喜你!通过权限认证';
} else {
echo '对不起,您没有该资源访问权限';
}Start Webman and request a protected route to see the result:
Custom Logging
Version v2.0.0 adds support for the PSR‑3 \Psr\Log\LoggerInterface standard, allowing you to plug in a custom logger.
Log configuration file:
app/config/plugin/casbin/webman-permission/permission.php /** 日志配置 */
'log' => [
'enabled' => true, // enable logging
'logger' => 'Casbin', // can be a PSR‑3 logger class or string identifier
'path' => runtime_path() . '/logs/casbin.log', // log file path
],When enabled, Casbin writes detailed information such as model loading, policy evaluation, and request results to /app/runtime/logs/casbin.log. Example log entries:
[2024-11-02T00:49:34.311871+08:00] Casbin.INFO: Model: {info} {"info":"[r r sub, obj, act]
[p p sub, obj, act]
[e e some(where (p_eft == allow))]
[m m g(r_sub, p_sub) && r_obj == p_obj && r_act == p_act]
[g g _, _]
"} []
[2024-11-02T00:49:34.440760+08:00] Casbin.INFO: Policy: {policy} {"policy":"p : [[eve articles read] [writer articles edit]
g : [[eve writer]]"} []
[2024-11-02T00:49:34.640251+08:00] Casbin.INFO: Request: {request}Hit Policy: {hitPolicy} {"request":"eve, articles, edit --> true
","hitPolicy":"writer, articles, edit
"} []Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
