/** * Updates an existing Role model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { if (!$this->is_access('role/update')) { Yii::$app->session->setFlash('error', $this->errorInfo); return $this->redirect($this->redirectUrl); } $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->addData()) { Yii::$app->cache->delete('role_list_' . $id); // 删除缓存 Yii::$app->session->setFlash('success', '编辑成功'); return $this->redirect(['index']); } else { Yii::$app->view->params['meta_title'] = '编辑角色'; // 权限节点 $RoleAction = new RoleAction(); $treeRoleList = $RoleAction->treeList(); return $this->render('update', ['model' => $model, 'treeRoleList' => $treeRoleList]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = RoleAction::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'action_name', $this->action_name])->andFilterWhere(['like', 'controller_name', $this->controller_name])->andFilterWhere(['like', 'module_name', $this->module_name]); return $dataProvider; }
/** * @inheritdoc */ static function accessRule($controller = '', $action = '') { $userid = Yii::$app->user->id; $user = User::find()->select('user_type_id')->where(['id' => $userid])->one(); if ($user->user_type_id == 1) { return true; } $roleAction = RoleAction::find()->where(['action_name' => strtolower($action), 'controller_name' => strtolower($controller)])->one(); if (!empty($roleAction)) { $actionPermission = RoleMaster::find()->where(['role_action_id' => $roleAction->id, 'user_type_id' => $user->user_type_id])->one(); if (!empty($actionPermission)) { return true; } else { return false; } } else { return true; } }
use common\models\RoleController; /* @var $this yii\web\View */ /* @var $model common\models\RoleAction */ /* @var $form yii\widgets\ActiveForm */ $form = ActiveForm::begin(); ?> <div class="box-body"> <div class="form-group"> <?php echo $form->field($model, 'module_name')->dropDownList(ArrayHelper::map(RoleAction::find()->all(), 'module_name', 'module_name')); ?> </div> <div class="form-group"> <?php if ($model->isNewRecord) { echo $form->field($model, 'controller_name')->dropDownList(ArrayHelper::map(RoleAction::find()->all(), 'controller_name', 'controller_name')); } else { echo $form->field($model, 'controller_name')->textInput(['maxlength' => true, 'class' => 'form-control', 'readonly' => 'readonly']); } ?> </div> <div class="form-group"> <?php echo $form->field($model, 'action_name')->textInput(['maxlength' => true, 'class' => 'form-control']); ?> </div> </div> <div class="box-footer"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => 'btn btn-primary']); ?>
public function getTreeModuleView($user_type_id = '') { $html_tree_view = ""; $m = 1; $c = 1; $a = 1; $roleModule = RoleAction::find()->select('module_name')->groupBy('module_name')->all(); if (!empty($roleModule)) { $html_tree_view .= '<div class="row">'; foreach ($roleModule as $key => $module) { $module_name = $module->module_name; $html_tree_view .= '<div class="col-md-12">'; $html_tree_view .= '<li id="module_' . $module_name . '">'; $html_tree_view .= '<input type="checkbox" id="' . $module_name . $m . '" class="module_group" name="RoleMaster[module][]" value="' . $module_name . '"/> '; $html_tree_view .= '<label for="' . $module_name . $m . '">' . ucfirst($module_name) . '</label>'; $roleController = RoleAction::find()->select('controller_name')->where(['module_name' => $module_name])->groupBy('controller_name')->all(); if (!empty($roleController)) { $html_tree_view .= '<ul>'; $html_tree_view .= '<div class="row">'; $ca = 1; foreach ($roleController as $key => $controller) { $controller_name = $controller->controller_name; if ($ca == 4) { $ca = 1; $html_tree_view .= '</div><div class="row"><div class="col-md-4">'; } else { $html_tree_view .= '<div class="col-md-4">'; } $html_tree_view .= '<li>'; $html_tree_view .= '<i class="icon-file"></i>'; $html_tree_view .= '<input id="' . $controller_name . $m . $c . '" type="checkbox" class="controller_group" ref="main_grp' . $controller_name . '" name="RoleMaster[controller][]" value="' . $controller_name . '"/> '; $html_tree_view .= '<label for="' . $controller_name . $m . $c . '">' . ucfirst($controller_name) . '</label>'; $roleAction = RoleAction::find()->select('id,action_name')->where(['controller_name' => $controller_name, 'module_name' => $module_name])->groupBy('action_name')->all(); if (!empty($roleAction)) { $html_tree_view .= '<ul>'; foreach ($roleAction as $key => $action) { $action_name = $action->action_name; $actionPermission = RoleMaster::find()->where(['role_action_id' => $action->id, 'user_type_id' => $user_type_id])->one(); $html_tree_view .= '<li>'; $html_tree_view .= '<i class="icon-file"></i>'; //if(empty($actionPermission)){echo 1; die;} $action_check = !empty($actionPermission) ? 'checked="checked"' : ''; $html_tree_view .= '<input id="' . $action_name . $m . $c . $a . '" type="checkbox" class="action_group" ref="main_grp' . $action_name . '" name="RoleMaster[action][]" value="' . $action->id . '" ' . $action_check . '/> '; $html_tree_view .= '<label for="' . $action_name . $m . $c . $a . '">' . ucfirst($action_name) . '</label>'; $html_tree_view .= '</li>'; $a++; } $html_tree_view .= '</ul>'; } $html_tree_view .= '</li>'; $html_tree_view .= '</div>'; $c++; $ca++; } $html_tree_view .= '</ul>'; } $html_tree_view .= '</li>'; $html_tree_view .= '</div>'; $m++; } } $html_tree_view .= '</div></div>'; return $html_tree_view; }
function actionGetcontrollersandactions() { $controllerlist = []; if ($handle = opendir('../backend/controllers')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && substr($file, strrpos($file, '.') - 10) == 'Controller.php') { $controllerlist[] = $file; } } closedir($handle); } asort($controllerlist); $fulllist = []; foreach ($controllerlist as $controller) { $handle = fopen('../backend/controllers/' . $controller, "r"); if ($handle) { while (($line = fgets($handle)) !== false) { if (preg_match('/function action(.*?)\\(/', $line, $display)) { if (strlen($display[1]) > 2) { $fulllist[substr($controller, 0, -14)][] = strtolower($display[1]); } } } } fclose($handle); } $model = new RoleAction(); $model->deleteAll(); if (!empty($fulllist)) { foreach ($fulllist as $key => $value) { foreach ($value as $k => $v) { $ActionModel = new RoleAction(); $ActionModel->module_name = 'TestModule'; $ActionModel->controller_name = $key; $ActionModel->action_name = $v; $ActionModel->save(); } } } }
/** * Finds the RoleAction model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return RoleAction the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = RoleAction::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getRoleActions() { return $this->hasMany(RoleAction::className(), ['module_id' => 'id']); }
/** * @return \yii\db\ActiveQuery */ public function getRoleAction() { return $this->hasOne(RoleAction::className(), ['id' => 'role_action_id']); }
/** * @return \yii\db\ActiveQuery */ public function getRoleActions() { return $this->hasMany(RoleAction::className(), ['controller_id' => 'id']); }