/** * add or update module * @param array $data * @return bool */ public static function saveModule($data) { $module = $data['moduleId'] ? static::findOne($data['moduleId']) : ''; if (!$module) { $module = new Module(); } $module->moduleName = $data['moduleName']; $module->module = $data['module']; $module->controller = $data['controller']; $module->method = $data['method']; $module->description = $data['description']; $module->url = $data['url']; $module->app = $data['app']; $module->save(); static::savePrivilege($module); return $module; }
/** * @return \yii\db\ActiveQuery */ public function getModules() { return $this->hasMany(Module::className(), ['created_by' => 'id']); }
/** * 普通用户组权限分配 */ public function actionPrivilege() { $view = \Yii::$app->view->params['moduleName'] = '用户分组'; $search = \Yii::$app->request->_get('search', ''); $where = 1; if ($search) { $where .= ' AND moduleName LIKE \'%' . $search . '%\''; } $app = \Yii::$app->request->_get('app', 'app-frontend'); if ($app) { $where .= ' AND app=\'' . $app . '\''; } $module = Module::find()->where($where); $groupId = \Yii::$app->request->_get('groupId', ''); $pageSize = \Yii::$app->params['pageSize']; $total = $module->count(); $pagination = new Pagination(['defaultPageSize' => $pageSize, 'totalCount' => $total]); $data = $module->asArray()->offset($pagination->offset)->limit($pagination->limit)->orderBy('controller DESC,createTime DESC')->all(); $privilegedata = GroupPrivilege::find()->where('groupId=' . $groupId)->asArray()->All(); foreach ($data as &$modules) { foreach ($privilegedata as $privilege) { if ($modules['moduleId'] == $privilege['moduleId']) { $modules['isAble'] = 1; } } } $data = EasyHelpers::kGroup($data, 'controller'); $token = Token::getToken(); $userId = \Yii::$app->user->id; $saveprivilegeIdentity = EasyHelpers::dataEncrypt('saveprivilege,' . $userId); return $this->render('privilege', ['data' => $data, 'groupId' => $groupId, 'search' => $search, 'app' => $app, 'token' => $token, 'pagination' => $pagination, 'saveprivilegeIdentity' => $saveprivilegeIdentity]); }
/** * 模块管理(删除) */ public function actionDeletemodule() { $request = \Yii::$app->request; $identity = Identity::checkIdentity('deletemodule', '/app-backend/backend/adminmanage/deletemodule'); if (is_array($identity)) { return $identity; } $moduleId = $request->_get('moduleId', 0); if (!$moduleId) { return ['code' => 1, 'msg' => '数据非法', 'data' => []]; } $res = Module::deleteAll('moduleId=' . $moduleId); if ($res !== false) { return ['code' => 0, 'msg' => '操作成功', 'data' => []]; } else { return ['code' => 2, 'msg' => '操作失败,请重试', 'data' => []]; } }
/** * Finds the Module model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Module the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Module::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }