windhoney 2017-09-30 12:59:51 3310次浏览 5条回复 5 3 0

根据yii2-admin(https://github.com/mdmsoft/yii2-admin)修改,yii2-admin只是web方式,这个相当于yii2-admin的rest版本。适合用于前后端分离项目,rest方式提供接口,实现对接口的权限控制。 项目地址:https://github.com/windhoney/yii2-rest-rbac

yii2-rest-rbac

Yii2权限系统,rest版,根据yii2-admin(https://github.com/mdmsoft/yii2-admin)修改

  • 安装:
    composer require windhoney/yii2-rest-rbac
    
使用
  • 配置oauth2和rbac
     'modules' => [
          'rbac' => [
              'class' => 'wind\rest\modules'
          ],
          'oauth2' => [
              'class' => 'filsh\yii2\oauth2server\Module',
              'tokenParamName' => 'access_token',
              'tokenAccessLifetime' => 3600 * 24,
              'storageMap' => [
                  'user_credentials' => 'backend\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'
                  ],
              ]
          ]
      ],
    
  • 配置权限
      'as access' => [
          'class' => 'wind\rest\components\AccessControl',
          'allowActions' => [
              'site/*',//允许访问的节点,可自行添加
              'rbac/menu/user-menu',
              'oauth2/*',
          ]
      ],
    
  • 创建所需要的表 //用户表user和菜单表menu

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

    //rbac相关权限表

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

    //oauth2相关表

    yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations
    
  • 添加路由配置

将yii2-rest-rbac/example/rbac_route.php文件内容配置到项目的urlManager的rules规则下 也可在main.php文件中 添加

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

直接将此文件放到config/route/rbac_route.php

  • 接口文档参考

文档

觉得很赞
亿速云
  • 回复于 2017-11-04 22:57 举报

    auth_item表中没有‘parent_name’字段,但是你的代码中

     public function getRoutes()
        {
            $manager = Yii::$app->getAuthManager();
            $routes = $this->getAppRoutes();
    //        print_r($routes);die;
            $exists = [];
            foreach ($manager->getPermissions() as $name) {
                $name = ArrayHelper::toArray($name);
                if ($name['name'][0] !== '/') {
                    continue;
                }
                $route['name'] = $name['name'];
                $route['description'] = $name['description'];
                $route['parent_name'] = $name['parent_name'];
                $exists[] = $route;
                unset($routes[$name['name']]);
            }
            $exists = ArrayHelper::index($exists, null, 'parent_name');
            
            return [
                'avaliable' => array_keys($routes),
                'assigned' => $exists
            ];
        }
    

    这里面怎么取了‘parent_name’字段了呢,我这里就报错了

  • 回复于 2017-11-06 11:14 举报

    文档中漏掉了
    新增一个字段
    auth_item 表添加一个字段 parent_name varchar(30) COLLATE utf8_unicode_ci DEFAULT '' COMMENT '父级名称',

  • 回复于 2018-05-09 17:12 举报

    配置文件中
    {'storageMap' => ['user_credentials' => 'backend\models\User',]}
    我把改成common\models\User(我这有这个文件)继承了UserCredentialsInterface并实现了checkUserCredentials和getUserDetails方法:

    class User extends ActiveRecord implements IdentityInterface,UserCredentialsInterface
    {
        const STATUS_DELETED = 0;
        const STATUS_ACTIVE = 10;
        ...
        public function checkUserCredentials($username, $password)
        {
            $user = static::findByUsername($username);
            if(!$user){
                return false;
            }
            return $this->validatePassword($password);
        }
    
        public function getUserDetails($username)
        {
            $user = static::findByUsername($username);
            return ['user_id' => $user->getId()];
        }
       }
    

    但还是提示错误:

    {
        "name": "Exception",
        "message": "Argument 1 passed to OAuth2\\Server::verifyResourceRequest() must be an instance of OAuth2\\RequestInterface, instance of yii\\web\\Request given, called in D:\\phpStudy\\WWW\\advanced\\vendor\\filsh\\yii2-oauth2-server\\Server.php on line 31",
        "code": 0,
        "type": "TypeError",
        "file": "D:\\phpStudy\\WWW\\advanced\\vendor\\bshaffer\\oauth2-server-php\\src\\OAuth2\\Server.php",
        "line": 423,
        "stack-trace": [
            "#0 D:\\phpStudy\\WWW\\advanced\\vendor\\filsh\\yii2-oauth2-server\\Server.php(31): OAuth2\\Server->verifyResourceRequest(Object(yii\\web\\Request), NULL, NULL)",
            "#1 D:\\phpStudy\\WWW\\advanced\\vendor\\filsh\\yii2-oauth2-server\\filters\\auth\\CompositeAuth.php(15): filsh\\yii2\\oauth2server\\Server->verifyResourceRequest()",
            "#2 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\base\\ActionFilter.php(77): filsh\\yii2\\oauth2server\\filters\\auth\\CompositeAuth->beforeAction(Object(yii\\base\\InlineAction))",
            "#3 [internal function]: yii\\base\\ActionFilter->beforeFilter(Object(yii\\base\\ActionEvent))",
            "#4 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\base\\Component.php(627): call_user_func(Array, Object(yii\\base\\ActionEvent))",
            "#5 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\base\\Controller.php(274): yii\\base\\Component->trigger('beforeAction', Object(yii\\base\\ActionEvent))",
            "#6 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\web\\Controller.php(164): yii\\base\\Controller->beforeAction(Object(yii\\base\\InlineAction))",
            "#7 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\base\\Controller.php(155): yii\\web\\Controller->beforeAction(Object(yii\\base\\InlineAction))",
            "#8 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction('user-menu', Array)",
            "#9 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction('rbac/menu/user-...', Array)",
            "#10 D:\\phpStudy\\WWW\\advanced\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))",
            "#11 D:\\phpStudy\\WWW\\advanced\\api\\web\\index.php(18): yii\\base\\Application->run()",
            "#12 {main}"
        ]
    }
    

    这个怎么解决啊?

    1 条回复
    回复于 2019-04-11 18:02 回复

    yii2 -2.0.13版本以后getRequest() 有些变化
    oauth2 模块下可以增加一行设置“components”

    'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'tokenParamName' => 'access_token',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => 'app\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'
                ],
            ],
            'components' => [
                'request' => function () {
                    return \filsh\yii2\oauth2server\Request::createFromGlobals();
                },
                'response' => [
                    'class' => \filsh\yii2\oauth2server\Response::class,
                ],
            ],
        ]
    
  • 回复于 2018-05-22 21:31 举报

    我也遇到了这个问题,您解决了吗?

  • 回复于 2019-03-20 17:38 举报

    1553074530364.jpg
    遇到同样的错误,有人解决了吗

    1 条回复
    回复于 2019-04-11 18:02 回复

    yii2 -2.0.13版本以后getRequest() 有些变化
    oauth2 模块下可以增加一行设置“components”

    'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',
            'tokenParamName' => 'access_token',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => 'app\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'
                ],
            ],
            'components' => [
                'request' => function () {
                    return \filsh\yii2\oauth2server\Request::createFromGlobals();
                },
                'response' => [
                    'class' => \filsh\yii2\oauth2server\Response::class,
                ],
            ],
        ]
    
您需要登录后才可以回复。登录 | 立即注册