| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- namespace common\traits;
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\data\ActiveDataProvider;
- use yii\web\UnprocessableEntityHttpException;
- use common\enums\StatusEnum;
- use common\enums\WhetherEnum;
- use common\helpers\ResultHelper;
- use common\models\rbac\AuthRole;
- use common\helpers\ArrayHelper;
- /**
- * Trait AuthRoleTrait
- * @package common\traits
- * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass
- * @property string $appId 应用id
- * @property bool $sourceAuthChild 权限来源(false:所有权限,true:当前角色)
- * @property string $viewPrefix 加载视图
- * @author jianyan74 <751393839@qq.com>
- */
- 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;
- }
- }
|