Files
xk-api-yii/admin/controllers/AuthController.php

453 lines
15 KiB
PHP
Raw Normal View History

2024-09-19 11:32:39 +08:00
<?php
namespace admin\controllers;
use admin\components\BaseAdminController;
use admin\models\Admin;
use common\enums\UserRoleEnum;
use common\helpers\ArrayHelper;
2024-12-19 19:20:27 +08:00
use common\models\oldDb\AuthRole;
use common\models\oldDb\AuthRule;
use common\models\oldDb\Role;
2024-09-19 11:32:39 +08:00
use yii\db\Exception;
use yii\web\ForbiddenHttpException;
class AuthController extends BaseAdminController
{
/**
* @var \common\core\rbac\DbManager
*/
public $authManager;
public $roles;
public $login_role;
/**
* @var bool 这里很多自定义的表单,就没有添加验证
*/
public $enableCsrfValidation = false;
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
$this->authManager = \Yii::$app->authManager;
}
public function beforeAction($action)
{
parent::beforeAction($action); // TODO: Change the autogenerated stub
//验证权限
if ($this->actionCheckAuth()) {
return true;
}
throw new ForbiddenHttpException(\Yii::t('yii', 'You are not allowed to perform this action.'));
}
/**
* @doc-name 权限列表
* @doc-param int role_id 角色id
* @doc-return mixed @List{AuthRule{id,pid,component-string-组件地址,title-string-权限名称,path-string-权限对应路由,redirect-string-跳转路由,icon-string-菜单图标,api_url-int-接口地址,status-int-权限状态1启用 0禁用} 规则列表
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionRuleList()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get,[
['role_id','required']
]);
if ($get['role_id']==UserRoleEnum::SUPER_ADMIN ){
$data=['217','218','219'];
$AuthRule = AuthRule::find()->where(['hidden' => 0])->andWhere(['not in','id',$data])->asArray()->all();
}elseif($get['role_id']==UserRoleEnum::STORE_ADMIN){
$data=['219'];
$AuthRule = AuthRule::find()->where(['hidden' => 0])->andWhere(['not in','id',$data])->asArray()->all();
}else{
$AuthRule = AuthRule::find()->where(['hidden' => 0])->asArray()->all();
}
return ArrayHelper::list_to_tree($AuthRule, 'id', 'pid', 'children');
}
/**
* @doc-name 添加权限
* @doc-param int pid 父级id / optional
* @doc-param string title 权限名称
* @doc-param string path 权限对应路由
* @doc-param string component 组件地址 / optional
* @doc-param string redirect 跳转路由 / optional
* @doc-param string icon 菜单图标 / optional
* @doc-param string api_url 接口地址 / optional
* @doc-param int status 权限状态 1启用 0 禁用 / optional
*/
public function actionAddRule()
{
if (\Yii::$app->request->isPost) {
$data = \Yii::$app->request->post();
$this->requestValidate($data, [
[['pid', 'title', 'path', 'status'], 'required'],
]);
$path = 'Admin/' . $data['path'];
$path = explode('/', $path);
foreach ($path as $value) {
$v = ucfirst($value);
$arr[] = $v;
}
$new_path = implode('/', $arr);
$AuthRule = AuthRule::find()->where(['path' => $new_path])->one();
if ($AuthRule) throw new Exception('权限已存在');
$data['path'] = $new_path;
$model = new AuthRule();
$model->setAttributes($data);
if (!$model->saveOrFail()) {
throw new Exception('添加失败' . array_values($model->getFirstErrors())[0]);
} else {
return ['添加成功'];
}
}
throw new Exception('请求方式错误');
}
/**
* @doc-name 编辑权限
* @doc-param int id id
* @doc-param int pid 父级id / optional
* @doc-param string title 权限名称
* @doc-param string path 权限对应路由
* @doc-param string component 组件地址 / optional
* @doc-param string redirect 跳转路由 / optional
* @doc-param string icon 菜单图标 / optional
* @doc-param string api_url 接口地址 / optional
* @doc-param int status 权限状态 1启用 0 禁用 / optional
*/
public function actionEditRule()
{
if (\Yii::$app->request->isPost) {
$data = \Yii::$app->request->post();
$this->requestValidate($data, [
['id', 'required'],
]);
$AuthRule = AuthRule::find()->where(['id' => $data['id']])->one();
if (!$AuthRule) throw new Exception('需要编辑的规则不存在');
$AuthRule->pid = $data['pid'] ?? $AuthRule->pid;
$AuthRule->title = $data['title'] ?? $AuthRule->title;
$AuthRule->path = $data['path'] ?? $AuthRule->path;
$AuthRule->component = $data['component'] ?? $AuthRule->component;
$AuthRule->redirect = $data['redirect'] ?? $AuthRule->redirect;
$AuthRule->icon = $data['icon'] ?? $AuthRule->icon;
$AuthRule->api_url = $data['api_url'] ?? $AuthRule->api_url;
$AuthRule->status = $data['status'] ?? $AuthRule->status;
if (!$AuthRule->saveOrFail()) {
throw new Exception('编辑失败');
}
return ['编辑成功'];
}
throw new Exception('请求方式错误');
}
/**
* @doc-name 删除权限
* @doc-param int id id
*/
public function actionDelRule()
{
$data = \Yii::$app->request->get();
$this->requestValidate($data, [
['id', 'required'],
]);
$id = AuthRule::find()->where(['id' => $data['id']])->one();
if (!$id) throw new Exception('所删除的权限不存在');
$t = \Yii::$app->db->beginTransaction();
try {
//删除权限
$id->delete();
//删除角色的权限
AuthRole::deleteAll(['rule_id' => $id]);
$t->commit();
return ['删除成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @doc-name 角色列表
*/
public function actionRoleList()
{
$data = \Yii::$app->request->get();
$query = Role::find()->where(['>','id',9]);
$this->field = [
Role::class => [
'id', 'name', 'role_code', 'description',
'authRole' => function ($a) {
return AuthRole::find()->where(['role_id' => $a->id])->all();
}
]
];
return $this->create($query, $data);
}
/**
* @doc-name 添加角色
* @doc-param string name 名字
* @doc-param string role_code
* @doc-param string description 描述
*/
public function actionAddRole()
{
$data = \Yii::$app->request->post();
$this->requestValidate($data, [
['name', 'required'],
]);
$role = Role::find()->where(['name' => $data['name']])->one();
if ($role) throw new Exception('角色已经存在');
$t = \Yii::$app->db->beginTransaction();
try {
$model = new Role();
$model->name = $data['name'];
$model->role_code = $data['role_code'] ?? '';
$model->description = $data['description'] ?? '';
$model->save();
$path = 'Admin/Menu/List';
$AuthRule = AuthRule::find()->where(['path' => $path])->one();
if (!$AuthRule) throw new Exception('菜单列表不存在');
//添加菜单列表权限
$AuthRole = AuthRole::find()->where(['role_id' => $model->id, 'rule_id' => $AuthRule->id])->one();
if (!$AuthRole) {
$AuthRole = new AuthRole();
$AuthRole->role_id = $model->id;
$AuthRole->rule_id = $AuthRule->id;
$AuthRole->saveOrFail();
}
$t->commit();
return ['添加成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @doc-name 编辑角色
* @doc-param int id id
* @doc-param string name 名字
* @doc-param string role_code
* @doc-param string description 描述
*/
public function actionEditRole()
{
if (\Yii::$app->request->isPost) {
$data = \Yii::$app->request->post();
$this->requestValidate($data, [
['id', 'required'],
]);
$edit_role = Role::find()->where(['id' => $data['id']])->one();
if (!$edit_role) throw new Exception('角色不存在');
if (!empty($data['name'])) {
$role = Role::find()->where(['name' => $data['name']])->andWhere(['!=', 'id', $data['id']])->one();
if ($role) {
throw new Exception('角色已存在');
}
}
$edit_role->name = $data['name'] ?? $edit_role->name;
$edit_role->role_code = $data['role_code'] ?? $edit_role->role_code;
$edit_role->description = $data['description'] ?? $edit_role->description;
if (!$edit_role->save()) {
throw new Exception('编辑失败');
}
return ['编辑成功'];
}
throw new Exception('请求方式错误');
}
/**
* @doc-name 删除角色
* @doc-param int id id
*/
public function actionDelRole()
{
$data = \Yii::$app->request->get();
$this->requestValidate($data, [
['id', 'required'],
]);
$role = Role::find()->where(['id' => $data['id']])->one();
if (!$role) throw new Exception('所删除的角色不存在');
$t = \Yii::$app->db->beginTransaction();
try {
//删除权限
$role->delete();
//删除角色的权限
AuthRole::deleteAll(['role_id' => $role->id]);
$t->commit();
return ['删除成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @@doc-name 给角色添加权限
* @doc-param int role_id role_id
* @doc-param int rule_id 权限id 1,2,3
* @doc-param int status 状态1启用 0禁用
*/
public function actionRoleAddRule()
{
$data = \Yii::$app->request->post();
$this->requestValidate($data, [
[['role_id', 'rule_id', 'status'], 'required'],
]);
$rule_id = explode(',', $data['rule_id']);
$t = \Yii::$app->db->beginTransaction();
try {
foreach ($rule_id as $value) {
$AuthRole = AuthRole::find()->where(['rule_id' => $value, 'role_id' => $data['role_id']])->one();
if (!$AuthRole) {
$AuthRole = new AuthRole();
$AuthRole->role_id = $data['role_id'];
$AuthRole->rule_id = $value;
$AuthRole->status = $data['status'];
$AuthRole->saveOrFail();
}
}
$t->commit();
return ['添加成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @@doc-name 给角色修改权限
* @doc-param int role_id role_id
* @doc-param int rule_id 权限id 12345
* @doc-param int status 状态1启用 0禁用
*/
public function actionRoleEditRule()
{
$data = \Yii::$app->request->post();
$this->requestValidate($data, [
[['role_id', 'rule_id', 'status'], 'required'],
]);
$rule_id = explode(',', $data['rule_id']);
$t = \Yii::$app->db->beginTransaction();
try {
foreach ($rule_id as $value) {
$AuthRole = AuthRole::find()->where(['rule_id' => $value, 'role_id' => $data['role_id']])->one();
if (!$AuthRole) {
$AuthRole = new AuthRole();
$AuthRole->role_id = $data['role_id'];
$AuthRole->rule_id = $value;
$AuthRole->status = $data['status'];
$AuthRole->saveOrFail();
}
}
$all_rule_id = AuthRole::find()->select(['rule_id'])->where(['role_id' => $data['role_id']])->column();
$arr = array_diff($all_rule_id, $rule_id);
// if ($data['role_id']!=UserRoleEnum::SUPER_ADMIN){
foreach ($arr as $val) {
$AuthRole = AuthRole::find()->where(['role_id' => $data['role_id'], 'rule_id' => $val])->one();
if ($AuthRole) {
$AuthRole->delete();
}
}
// }
$t->commit();
return ['修改成功'];
} catch (\Exception $e) {
$t->rollBack();
throw new Exception($e->getMessage());
}
}
/**
* @@doc-name 给角色删除权限
* @doc-param int id id
*/
public function actionRoleDelRule()
{
$data = \Yii::$app->request->get();
$this->requestValidate($data, [
['id', 'required'],
]);
$AuthRole = AuthRole::find()->where(['id' => $data['id']])->one();
if (!$AuthRole) throw new Exception('该权限不存在');
$AuthRole->delete();
return ['删除成功'];
}
/**
* @doc-name 查看角色权限
* @doc-param int role_id 角色ID
* @doc-return mixed @List{id,role_id,rule_id,rule-string-权限} 查看角色权限列表
*/
public function actionQueryRoleRule()
{
$data = \Yii::$app->request->get();
$this->requestValidate($data, [
['role_id', 'required'],
]);
$query = AuthRole::find()->where(['role_id' => $data['role_id']]);
$this->field = [
AuthRole::class => [
'id', 'role_id', 'rule_id', 'rule' => 'authRule.title'
]
];
return $this->create($query, $data);
}
/**
* @doc-name 账号修改角色
* @doc-param string mobile 手机号
* @doc-param int role 角色
*/
public function actionAccountEditRole()
{
$data = \Yii::$app->request->get();
$this->requestValidate($data, [
[['mobile', 'role'], 'required'],
]);
$Admin = Admin::find()->where(['mobile' => $data['mobile']])->one();
if (!$Admin) {
throw new Exception('账号不存在');
}
$role = Role::find()->where(['id' => $data['role']])->one();
if (!$role) throw new Exception('角色不存在');
$Admin->role = $data['role'];
$Admin->saveOrFail();
return [];
}
}