diff --git a/backend/controllers/AdminUserController.php b/backend/controllers/AdminUserController.php index 0008638c..e48e0aa6 100644 --- a/backend/controllers/AdminUserController.php +++ b/backend/controllers/AdminUserController.php @@ -84,11 +84,13 @@ public function actionUpdate($id) { $model = User::findOne($id); $model->setScenario('update'); - $model->roles = $model->permissions = array_keys( yii::$app->getAuthManager()->getAssignments($id) ); - if( in_array($id, yii::$app->getBehavior('access')->superAdminUserIds) ){ - $model->permissions = array_keys( yii::$app->getAuthManager()->getPermissions() ); - $model->roles = array_keys( yii::$app->getAuthManager()->getRoles() ); - } + $model->roles = $model->permissions = call_user_func(function() use($id){ + $permissions = yii::$app->getAuthManager()->getAssignments($id); + foreach ($permissions as $k => &$v){ + $v = $k; + } + return $permissions; + }); if (Yii::$app->getRequest()->getIsPost()) { if ($model->load(Yii::$app->request->post()) && $model->save() && $model->assignPermission() ) { Yii::$app->getSession()->setFlash('success', yii::t('app', 'Success')); diff --git a/backend/controllers/RbacController.php b/backend/controllers/RbacController.php index 5a42696f..d3d426be 100644 --- a/backend/controllers/RbacController.php +++ b/backend/controllers/RbacController.php @@ -151,7 +151,7 @@ public function actionRoleCreate() public function actionRoleUpdate($name) { $model = new Rbac(['scenario'=>'role']); - $model->fillModel($name); + $model->fillModel($name);//var_dump($model->roles);exit; if( yii::$app->getRequest()->getIsPost() ) { if ($model->load(yii::$app->getRequest()->post()) && $model->validate() && $model->updateRole($name)) { yii::$app->getSession()->setFlash('success', yii::t('app', 'Success')); diff --git a/backend/models/User.php b/backend/models/User.php index 8b07aba4..991fd775 100644 --- a/backend/models/User.php +++ b/backend/models/User.php @@ -298,6 +298,9 @@ public function beforeSave($insert) public function assignPermission() { $authManager = yii::$app->getAuthManager(); + if(!$this->getIsNewRecord() && in_array($this->id, yii::$app->getBehavior('access')->superAdminUserIds)){ + $this->permissions = $this->roles = []; + } $assignments = $authManager->getAssignments($this->id); $roles = $permissions = []; foreach ($assignments as $key => $assignment){ @@ -335,15 +338,13 @@ public function assignPermission() } //权限permission - if( $this->permissions === null ){ - $this->permissions = []; - }else if( !is_array( $this->permissions ) ) { - $this->permissions = explode(',', $this->permissions); - } + $this->permissions = array_flip($this->permissions); + if (isset($this->permissions[0])) unset($this->permissions[0]); + $this->permissions = array_flip($this->permissions); $needAdds = array_diff($this->permissions, $permissions); $needRemoves = array_diff($permissions, $this->permissions); - if( !empty($needAdds) && !empty($needAdds[0]) ) { + if( !empty($needAdds) ) { $str .= ' 增加了权限: '; foreach ($needAdds as $permission) { $permissionItem = $authManager->getPermission($permission); diff --git a/backend/models/form/Rbac.php b/backend/models/form/Rbac.php index 40df3007..20bc5a10 100644 --- a/backend/models/form/Rbac.php +++ b/backend/models/form/Rbac.php @@ -58,7 +58,7 @@ public function rules() ], [['name', 'description'], 'required', 'on' => 'role'], - [['roles', 'permissions'], 'default', 'value'=>[], 'on' => 'role'], + [['roles', 'permissions'], 'default', 'on' => 'role'], ]; } @@ -156,9 +156,6 @@ public function deletePermission() public function createRole() { - if( !is_array($this->permissions) ) $this->permissions = explode(',', $this->permissions); - if( !is_array($this->roles) ) $this->permissions = explode(',', $this->roles); - $authManager = yii::$app->getAuthManager(); if ($authManager->getRole($this->name) !== null) { $this->addError('name', yii::t('app', 'Role exists')); @@ -170,16 +167,14 @@ public function createRole() 'sort' => $this->sort, ]); if( $authManager->add($role) ){ + $this->permissions = array_flip($this->permissions); + if (isset($this->permissions[0])) unset($this->permissions[0]); + $this->permissions = array_flip($this->permissions); foreach ($this->permissions as $permission){ $permission = $authManager->getPermission($permission); $authManager->addChild($role, $permission); } - foreach ($this->roles as $r){ - $r = $authManager->getRole($r); - $authManager->addChild($role, $r); - } - Event::trigger(CustomLog::className(), CustomLog::EVENT_AFTER_CREATE, new CustomLog([ 'sender' => $this, ])); @@ -193,9 +188,6 @@ public function updateRole($name) $oldModel = clone $this; $oldModel->fillModel($name); - if( !is_array($this->permissions) ) $this->permissions = explode(',', $this->permissions); - if( !is_array($this->roles) ) $this->permissions = explode(',', $this->roles); - $authManager = yii::$app->getAuthManager(); $role = $authManager->getRole($name); if( $role->name != $this->name ){//修改角色名称 @@ -211,12 +203,11 @@ public function updateRole($name) ]); $oldPermissions = array_keys( $authManager->getPermissionsByRole($name) ); - $oldChildRoles = array_keys( $authManager->getChildRoles($name) ); - $oldChildRoles = array_flip($oldChildRoles); - unset($oldChildRoles[$name]); - $oldChildRoles = array_flip($oldChildRoles); if( $authManager->update($name, $role) ){ + $this->permissions = array_flip($this->permissions); + if (isset($this->permissions[0])) unset($this->permissions[0]); + $this->permissions = array_flip($this->permissions); $needAdds = array_diff($this->permissions, $oldPermissions); foreach ($needAdds as $permission){ @@ -230,19 +221,6 @@ public function updateRole($name) $authManager->removeChild($role, $permission); } - $needAdds = array_diff($this->roles, $oldChildRoles); - - foreach ($needAdds as $r){ - $r = $authManager->getRole($r); - $authManager->addChild($role, $r); - } - - $needRemoves = array_diff($oldChildRoles, $this->roles); - foreach ($needRemoves as $r){ - $r = $authManager->getRole($r); - $authManager->removeChild($role, $r); - } - Event::trigger(CustomLog::className(), CustomLog::EVENT_CUSTOM, new CustomLog([ 'sender' => $this, 'old' => $oldModel, diff --git a/backend/views/admin-user/_form.php b/backend/views/admin-user/_form.php index e27a5868..9d8458e7 100644 --- a/backend/views/admin-user/_form.php +++ b/backend/views/admin-user/_form.php @@ -11,11 +11,11 @@ * @var $model backend\models\User */ +use backend\models\form\Rbac; use backend\widgets\ActiveForm; use backend\models\User; use common\widgets\JsBlock; -use backend\models\form\Rbac; -use yii\helpers\ArrayHelper; +use yii\helpers\Html; $this->title = "Admin"; ?> @@ -54,7 +54,7 @@ $itemsOptions = []; if(in_array( $model->getId(), yii::$app->getBehavior('access')->superAdminUserIds)){ $itemsOptions = ['disabled'=>'true']; - }//var_dump($itemsOptions);exit; + } ?> field($model, 'roles', [ 'labelOptions' => [ @@ -62,20 +62,21 @@ ] ])->checkboxList($temp, ['itemOptions'=>$itemsOptions]) ?>
-
- +
+ 'permission-all','class'=>'chooseAll'])?>
getId(), yii::$app->getBehavior('access')->superAdminUserIds)){ - $itemsOptions = ['disabled'=>'true']; - } $rbac = new Rbac(); foreach ($rbac->getPermissionsByGroup('form') as $key => $value){ - echo "

{$key}

"; + echo "
" . Html::checkbox("", false, ['id'=>"permission-all-{$key}", 'class'=>'chooseAll']) . "
"; echo "
"; foreach ($value as $k => $val){ - echo $form->field($model, 'permissions', ['labelOptions'=>['class'=>'col-sm-1']])->label($k)->checkboxList(ArrayHelper::map($val, 'name', 'description'), ['itemOptions'=>$itemsOptions]); + echo "
" . Html::checkbox("", false, ['id'=>"permission-all-{$k}", 'class'=>'chooseAll']) . "
"; + echo "
"; + foreach ($val as $v) { + echo $form->field($model, "permissions[{$v['name']}]", ['options'=>['style'=>'display:inline'], 'labelOptions'=>['class'=>'col-sm-12 control-label']])->checkbox(['value'=>$v['name']])->label($v['description']); + } + echo "
"; } echo "
"; } @@ -90,16 +91,56 @@
- \ No newline at end of file diff --git a/backend/views/rbac/_role-form.php b/backend/views/rbac/_role-form.php index bdbe9e2c..62422787 100644 --- a/backend/views/rbac/_role-form.php +++ b/backend/views/rbac/_role-form.php @@ -13,11 +13,14 @@ use backend\widgets\ActiveForm; use common\widgets\JsBlock; -use yii\helpers\ArrayHelper; +use yii\helpers\Html; $this->title = "Roles"; ?> +
@@ -31,14 +34,19 @@ field($model, 'sort')->textInput() ?>
- + 'permission-all','class'=>'chooseAll'])?>
getPermissionsByGroup('form') as $key => $value){ - echo "

{$key}

"; + echo "
" . Html::checkbox("", false, ['id'=>"permission-all-{$key}", 'class'=>'chooseAll']) . "
"; echo "
"; foreach ($value as $k => $val){ - echo $form->field($model, 'permissions', ['labelOptions'=>['class'=>'col-sm-1']])->label($k)->checkboxList(ArrayHelper::map($val, 'name', 'description')); + echo "
" . Html::checkbox("", false, ['id'=>"permission-all-{$k}", 'class'=>'chooseAll']) . "
"; + echo "
"; + foreach ($val as $v) { + echo $form->field($model, "permissions[{$v['name']}]", ['options'=>['style'=>'display:inline'], 'labelOptions'=>['class'=>'col-sm-12 control-label']])->checkbox(['value'=>$v['name']])->label($v['description']); + } + echo "
"; } echo "
"; } @@ -47,36 +55,54 @@
- getAuthManager()->getRoles(); - $curChainRoles = []; - if( $model->name != '' ) { - $curChainRoles = array_keys(yii::$app->getAuthManager()->getChildRoles($model->name)); - } - $temp = []; - foreach ($roles as $role){ - if( in_array($role->name, $curChainRoles) ) continue; - $temp[$role->name] = $role->name; - } - ?> - field($model, 'roles')->label(yii::t('app', 'Roles'))->checkboxList($temp) ?> -
defaultButtons() ?>
-