moponphp/rbac

mopon泰久php项目组 权限控制

2.1.2 2017-10-23 02:14 UTC

This package is not auto-updated.

Last update: 2020-02-27 23:35:14 UTC


README

权限控制

安装数据库

./yii migrate/up -p=@vendor/moponphp/rbac/migrations

卸载数据库

php yii migrate/down -p=@vendor/moponphp/rbac/migrations

插件后台使用方法: @main-local.php $config['modules']['rbac'] = [

    'class' => 'moponphp\rbac\Module',

]; 配置访问 ?r=rbac

插件使用方法(提供对外的api方法)

moponphp\rbac\Api::isHasPermission($arrRole,$action) 
        功能: 返回角色是否有访问action的权限    同样适用于按钮的显示与否判断  $action 改成mca即可 比如: isHasPermission($arrRole,"user/reg"); 
        参数: $arrRole:角色组,  $action:\yii\base\Action
        使用实例: 
                if($this->enableRbacValidation and $this->user_role_id != 0) {      //  角色0为超级管理员不受任何权限限制
                    $role = \moponphp\rbac\Api::isHasPermission([$this->user_role_id],$action);
                    if(! $role) {   //->enable($action)
                        throw new \Exception("没有权限");
                    }
                }

moponphp\rbac\Api::getNav($arrRole)
        功能: 返回菜单树结构 
        参数: 权限组
        使用实例: 
                <?php foreach($arrNav as $k=> $nav) : ?>
                <?php if(!empty($nav['children'])){?>
                    <div title="<?=$nav['title']?>" data-options="selected:true"   style="text-align: center;">
                        <?php if(isset($nav['children'])) foreach($nav['children'] as $children) : ?>
                            <ul class="nav">
                                <li>
                                    <a href="javascript:void(0)" onclick="$.tab_add('<?=$children['title']?>','<?=Url::to(["{$children['m']}/{$children['c']}/{$children['a']}"]);?>')"><?=$children['title']?></a>
                                </li>
                            </ul>
                        <?php endforeach;?>
                    </div>
                <?php }?>
              <?php endforeach;?>

扩展的rabc操作

可以 控制器直接继承 moponphp\rbac\controllers\RoleController 改写 模板 或者改写其中的方法 

多权限系统部署方案: 生成表结构

1 配置 $config['modules']['rbac'] = [
    'class' => 'moponphp\rbac\Module',
    priKey => "xflh_rbac_",
];
2 访问  ?r=rbac/default/init  来进行表生成

生效新表:

app初始化之后 比如在beforAction中设置表前缀 moponphp\rbac\models\Model::$priKey = "xflh_rbac_";  

注意: 角色id为0 为超级管理员 拥有所有页面的访问权限 并且显示所有菜单

keli@2016.10.13 by sz.mopon.cn