PHP code example of windhoney / yii2-rest-rbac

1. Go to this page and download the library: Download windhoney/yii2-rest-rbac 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/ */

    

windhoney / yii2-rest-rbac example snippets



composer 

   'modules' => [
        'rbac' => [
            'class' => 'wind\rest\Modules'
        ],
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            //'class' => 'wind\oauth2\Module',相对filsh\yii2\oauth2server做了一点优化,增加了可修改oauth2表的db name
            'tokenParamName' => 'access_token',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => 'common\models\User',//可自定义
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'client_credentials' => [
                    'class' => 'OAuth2\GrantType\ClientCredentials',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true
                ],
                'authorization_code' => [
                    'class' => 'OAuth2\GrantType\AuthorizationCode'
                ],
            ],
            //选填,oauth2组件版本问题可能导致错误时可添加
            'components' => [
                    'request' => function () {
                        return \filsh\yii2\oauth2server\Request::createFromGlobals();
                    },
                    'response' => [
                        'class' => \filsh\yii2\oauth2server\Response::class,
                    ],
            ],
        ]
    ],
    'components' => [
        'authManager' => [
            'class' => 'wind\rest\components\DbManager', //配置文件
            'defaultRoles' => ['普通员工'] //选填,默认角色(默认角色下->公共权限(登陆,oauth2,首页等公共页面))
            'groupTable' => 'auth_groups',//选填,分组表(已默认,可根据自己表名修改)
            'groupChildTable' => 'auth_groups_child',//选填,分组子表(已默认,可根据自己表名修改)
        ],
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is 

    'as access' => [
        'class' => 'wind\rest\components\AccessControl',
        'allowActions' => [
            'site/*',//允许访问的节点,可自行添加
            'rbac/menu/user-menu',//可将路由配置到“普通员工”(默认角色)下
            'oauth2/*',//可将路由配置到“普通员工”(默认角色)下
        ]
    ],

yii migrate --migrationPath=@vendor/windhoney/yii2-rest-rbac/migrations

yii migrate --migrationPath=@yii/rbac/migrations/

yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations

$dir = __DIR__ . "/route";
$main = wind\rest\helper\RbacHelper::addRoute($dir, $main);
return $main;