PHP code example of wuguofeng / webman-bouncer

1. Go to this page and download the library: Download wuguofeng/webman-bouncer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wuguofeng / webman-bouncer example snippets


return [
    'enable'        => true,
    'user_model'    => 'app\model\User',     // 你的 Eloquent 用户模型
    'user_resolver' => fn () => session()->get('user'),  // 每请求当前用户,匿名返回 null
    'cache'         => 'request',            // 'request' | 'none' | webman/cache store 名
    'scope_resolver'=> null,                 // 多租户:返回租户 id,或 null 关闭
    'tables'        => [],
];

use Webman\Bouncer\Bouncer;

Bouncer::refresh($user);   // 只清该用户的缓存(推荐,改动哪个用户就清哪个)
Bouncer::refresh();        // 清全部缓存(用户量大时较慢,慎用)


namespace app\model;

use Silber\Bouncer\Database\Concerns\Authorizable;
use Silber\Bouncer\Database\HasRolesAndAbilities;
use support\Model;

class User extends Model
{
    use HasRolesAndAbilities;
    use Authorizable;
}

use Webman\Bouncer\Bouncer;

// 授权(幂等)
Bouncer::allow('admin')->to('edit-post');
Bouncer::assign('admin')->to($user);
Bouncer::allow($user)->toOwn(Post::class);

// 校验(门面)
Bouncer::can('edit-post');          // bool,针对当前请求用户
Bouncer::cannot('delete-post');
Bouncer::authorize('edit-post');    // 无权限抛 AuthorizationException

// 校验(模型 trait,走同一每请求实例)
$user->can('edit-post');
$user->isAn('admin');

return [
    'default' => 'pgsql',
    'connections' => [
        'pgsql' => [
            'driver' => 'pgsql',
            'host' => '127.0.0.1',
            'port' => 5432,
            'database' => 'rbac',
            'username' => 'bouncer',
            'password' => 'secret',
            'charset' => 'utf8',
            'prefix' => '',
        ],
    ],
];
bash
composer e 还需: composer ores,例如:
#   'file'  => ['driver' => 'file',  'path' => runtime_path('cache')],
#   'redis' => ['driver' => 'redis', 'connection' => 'default'],
bash
php webman bouncer:install