*/ trait AuthRoleTrait { /** * @throws InvalidConfigException */ public function init() { parent::init(); if ($this->modelClass === null) { throw new InvalidConfigException('"modelClass" 属性必须设置.'); } if ($this->appId === null) { throw new InvalidConfigException('"appId" 属性必须设置.'); } if ($this->sourceAuthChild === null) { throw new InvalidConfigException('"sourceAuthChild" 属性必须设置.'); } if ($this->viewPrefix === null) { throw new InvalidConfigException('"viewPrefix" 属性必须设置.'); } } /** * 首页 * * @return mixed * @throws \yii\web\UnauthorizedHttpException */ public function actionIndex() { $merchantId = Yii::$app->services->merchant->getNotNullId(); $defaultRole = $this->getDefaultRole(); $dataProvider = new ActiveDataProvider([ 'query' => AuthRole::find() ->where(['app_id' => $this->appId]) ->andWhere(['>=', 'status', StatusEnum::DISABLED]) ->andFilterWhere(['merchant_id' => $merchantId]) ->andFilterWhere(Yii::$app->services->rbacAuthRole->roleCondition($this->sourceAuthChild)) ->orderBy('sort asc, created_at asc') ->asArray(), 'pagination' => false ]); $roles = $this->sourceAuthChild ? Yii::$app->services->rbacAuthRole->getRoles() : []; $models = $dataProvider->getModels(); !empty($defaultRole) && $models[] = $defaultRole; !empty($defaultRole) && $roles[] = $defaultRole; $ids = ArrayHelper::getColumn($models, 'id'); foreach ($models as &$model) { if (!in_array($model['pid'], $ids)) { $model['pid'] = 0; } } $dataProvider->setModels($models); return $this->render($this->viewPrefix . $this->action->id, [ 'dataProvider' => $dataProvider, 'merchant_id' => $merchantId, 'roleIds' => ArrayHelper::getColumn($roles, 'id'), ]); } /** * @return array|mixed * @throws \yii\db\Exception * @throws \yii\web\UnauthorizedHttpException */ public function actionEdit() { $id = Yii::$app->request->get('id', null); $merchantId = Yii::$app->services->merchant->getNotNullId(); /** @var AuthRole $model */ $model = $this->findModel($id); $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id $model->app_id = $this->appId; $defaultRole = $this->getDefaultRole(); // 获取所有权限还是当前已有的权限 if ($this->sourceAuthChild == true && !Yii::$app->services->rbacAuth->isSuperAdmin()) { $roles = Yii::$app->services->rbacAuthRole->findByLoginUser(Yii::$app->id); !empty($defaultRole) && $roles[$defaultRole['id']] = $defaultRole; if (!in_array($model->pid, array_keys($roles))) { throw new UnprocessableEntityHttpException('无权限操作当前角色'); } $allAuth = Yii::$app->services->rbacAuthItemChild->findItemByRoleId($model->pid); } else { $allAuth = Yii::$app->services->rbacAuthItem->findAll($this->appId); } if (Yii::$app->request->isAjax) { $data = Yii::$app->request->post(); $model->attributes = $data; $model->merchant_id = $merchantId ?? 0; if (!$model->save()) { return ResultHelper::json(422, $this->getError($model)); } // 创建角色关联的权限信息 Yii::$app->services->rbacAuthItemChild->accredit($model->id, $data['userTreeIds'] ?? [], WhetherEnum::DISABLED, $this->appId); Yii::$app->services->rbacAuthItemChild->accredit($model->id, $data['plugTreeIds'] ?? [], WhetherEnum::ENABLED, $this->appId); return ResultHelper::json(200, '提交成功'); } list($defaultFormAuth, $defaultCheckIds, $addonsFormAuth, $addonsCheckIds) = Yii::$app->services->rbacAuthRole->getJsTreeData($id, $allAuth); $dropDownList = Yii::$app->services->rbacAuthRole->getDropDownForEdit($this->appId, $this->sourceAuthChild, '', !empty($defaultRole) ? [$defaultRole] : []); return $this->render($this->viewPrefix . $this->action->id, [ 'model' => $model, 'defaultFormAuth' => $defaultFormAuth, 'defaultCheckIds' => $defaultCheckIds, 'addonsFormAuth' => $addonsFormAuth, 'addonsCheckIds' => $addonsCheckIds, 'dropDownList' => $dropDownList, 'merchant_id' => $merchantId ]); } /** * 删除 * * @param $id * @return mixed * @throws \Throwable * @throws \yii\db\StaleObjectException */ public function actionDelete($id) { if ($this->findModel($id)->delete()) { return $this->message("删除成功", $this->redirect(['index'])); } return $this->message("删除失败", $this->redirect(['index']), 'error'); } /** * ajax更新排序/状态 * * @param $id * @return array */ public function actionAjaxUpdate($id) { if (!($model = $this->modelClass::findOne($id))) { return ResultHelper::json(404, '找不到数据'); } $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']); if (!$model->save()) { return ResultHelper::json(422, $this->getError($model)); } return ResultHelper::json(200, '修改成功'); } /** * 获取默认角色 * * @return array */ public function getDefaultRole() { return []; } /** * 返回模型 * * @param $id * @return \yii\db\ActiveRecord */ protected function findModel($id) { /* @var $model \yii\db\ActiveRecord */ if (empty($id) || empty(($model = $this->modelClass::findOne($id)))) { $model = new $this->modelClass; return $model->loadDefaultValues(); } return $model; } }