Commit 128dfd46 authored by 孙磊's avatar 孙磊

权限角色节点和添加权限

Signed-off-by: 孙磊's avatarsunlei <sunlei@romens.cn>
parent 5cc29bd1
...@@ -290,8 +290,6 @@ return [ ...@@ -290,8 +290,6 @@ return [
'GET,OPTIONS authority-role-detail' => 'authority-role-detail', 'GET,OPTIONS authority-role-detail' => 'authority-role-detail',
'POST,OPTIONS add-authority-role' => 'add-authority-role', 'POST,OPTIONS add-authority-role' => 'add-authority-role',
'PUT,OPTIONS revise-authority-role' => 'revise-authority-role', 'PUT,OPTIONS revise-authority-role' => 'revise-authority-role',
'GET,OPTIONS role-authorities' => 'role-authorities',
'GET,OPTIONS authority-nodes' => 'authority-nodes',
'POST,OPTIONS assign-authority-role' => 'assign-authority-role', 'POST,OPTIONS assign-authority-role' => 'assign-authority-role',
], ],
], ],
...@@ -307,6 +305,7 @@ return [ ...@@ -307,6 +305,7 @@ return [
'PUT,OPTIONS node-update' => 'node-update', 'PUT,OPTIONS node-update' => 'node-update',
'POST,OPTIONS node-add' => 'node-add', 'POST,OPTIONS node-add' => 'node-add',
'DELETE,OPTIONS node-del' => 'node-del', 'DELETE,OPTIONS node-del' => 'node-del',
'GET,OPTIONS role-authorities' => 'role-authorities',
] ]
], ],
//权限-管理员列表 //权限-管理员列表
......
...@@ -8,6 +8,9 @@ use Yii; ...@@ -8,6 +8,9 @@ use Yii;
use yii\web\BadRequestHttpException; use yii\web\BadRequestHttpException;
use yii\web\HttpException; use yii\web\HttpException;
use yii\web\ServerErrorHttpException; use yii\web\ServerErrorHttpException;
use app\models\v1\rbacuser\ShopRbacRoleAccess;
use app\models\v1\rbacuser\ShopRbacNode;
use app\models\v1\rbacuser\ShopRbacRole;
class AuthorityNodeController extends BaseController class AuthorityNodeController extends BaseController
{ {
...@@ -322,4 +325,57 @@ class AuthorityNodeController extends BaseController ...@@ -322,4 +325,57 @@ class AuthorityNodeController extends BaseController
throw new HttpException(200, '更新成功!'); throw new HttpException(200, '更新成功!');
} }
/**
* @OA\Get(
* path="/backend/web/v1/authority/authority-nodes/role-authorities",
* tags={"权限管理"},
* description="角色的权限信息",
* summary="角色的权限信息(孙磊)",
* @OA\Parameter(name="ID",in="query",required=true,description="角色主键ID",@OA\Schema(type="string")),
* @OA\Parameter(name="NAME",in="query",required=true,description="角色名称",@OA\Schema(type="string")),
* @OA\Response(response="200",description="
* ROLE_ID:角色ID,
* ROLE_NAME:角色名称,
* NODE_ID:角色对应的权限ID,
* "),
* security={{"Authorization": {}}}
* )
*/
public function actionRoleAuthorities()
{
$model = new ShopRbacRole();
$roleAcessModel = new ShopRbacRoleAccess();
$nodeModel = new ShopRbacNode();
//用户输入数据赋值到模型属性
$params = Yii::$app->request->queryParams;
$id = $model->ID = $params['ID'];
$name = $model->NAME = $params['NAME'];
if ($model->validate()) {
//获取角色的权限
$info = $roleAcessModel->getRoleAuthorities($id);
if (!$info) {
throw new BadRequestHttpException('没有找到角色的权限信息');
}
//获取节点ID
$role_nodes = array();
foreach ($info as $k => $v) {
$role_nodes[] = $v['NODE_ID'];
}
//获取角色拥有的权限节点信息
if ($info[0]['NODE_ID']) {
$role_authorities = $nodeModel->getRoleAuthrities($role_nodes);
//调整节点关系
$role_authorities = $this->getTree($role_authorities);
}
if (!$role_authorities) {
throw new BadRequestHttpException('没有找到角色的权限节点信息');
}
$role_authorities[0]['ROLE_NAME'] = $name;
return $role_authorities;
} else {
//验证失败:$errors 是一个包含错误信息的数组
Yii::$app->response->statusCode = 422;
return $model->errors;
}
}
} }
...@@ -36,20 +36,11 @@ class AuthorityRoleController extends BaseController ...@@ -36,20 +36,11 @@ class AuthorityRoleController extends BaseController
public function actionAuthorityRoleList() public function actionAuthorityRoleList()
{ {
$model = new $this->modelClass; $model = new $this->modelClass;
$params = Yii::$app->request->queryParams; $info = $model->getAuthorityRoles();
$model->attributes = $params; if (!$info->getModels()) {
if ($model->validate()) { throw new BadRequestHttpException('未找到符合的角色信息');
//所有输入数据都有效 all inputs are valid
$info = $model->getAuthorityRoles($params);
if (!$info->getModels()) {
throw new BadRequestHttpException('未找到符合的角色信息');
}
return $info;
} else {
//验证失败:$errors 是一个包含错误信息的数组
Yii::$app->response->statusCode = 422;
return $model->errors;
} }
return $info;
} }
/** /**
* @OA\Get( * @OA\Get(
...@@ -176,70 +167,6 @@ class AuthorityRoleController extends BaseController ...@@ -176,70 +167,6 @@ class AuthorityRoleController extends BaseController
Yii::$app->getResponse()->setStatusCode(202); Yii::$app->getResponse()->setStatusCode(202);
return $model; return $model;
} }
/**
* @OA\Get(
* path="/backend/web/v1/authority/authority-roles/role-authorities",
* tags={"权限管理"},
* description="角色的权限信息",
* summary="角色的权限信息(孙磊)",
* @OA\Parameter(name="ID",in="query",required=true,description="角色主键ID",@OA\Schema(type="string")),
* @OA\Parameter(name="NAME",in="query",required=true,description="角色名称",@OA\Schema(type="string")),
* @OA\Response(response="200",description="
* ROLE_ID:角色ID,
* ROLE_NAME:角色名称,
* NODE_ID:角色对应的权限ID,
* "),
* security={{"Authorization": {}}}
* )
*/
public function actionRoleAuthorities()
{
$model = new $this->modelClass();
$roleAcessModel = new ShopRbacRoleAccess();
//用户输入数据赋值到模型属性
$params = Yii::$app->request->queryParams;
$id = $model->ID = $params['ID'];
$name = $model->ID = $params['NAME'];
if ($model->validate()) {
//获取角色的权限
$info = $roleAcessModel->getRoleAuthorities($id);
if (!$info) {
throw new BadRequestHttpException('没有找到角色的权限信息');
}
$info[0]['ROLE_NAME'] = $name;
return $info;
} else {
//验证失败:$errors 是一个包含错误信息的数组
Yii::$app->response->statusCode = 422;
return $model->errors;
}
}
/**
* @OA\Get(
* path="/backend/web/v1/authority/authority-roles/authority-nodes",
* tags={"权限管理"},
* description="权限全部节点",
* summary="权限全部节点(孙磊)",
* @OA\Response(response="200",description="权限全部节点获取成功"),
* security={{"Authorization": {}}}
* )
*/
public function actionAuthorityNodes()
{
$model = new $this->modelClass();
$authNodesModel = new ShopRbacNode();
//用户输入数据赋值到模型属性
$model->attributes = Yii::$app->request->queryParams;
if ($model->validate()) {
//获取全部的权限节点
$info = $authNodesModel->getAuthorityNodes();
return $info;
} else {
//验证失败:$errors 是一个包含错误信息的数组
Yii::$app->response->statusCode = 422;
return $model->errors;
}
}
/** /**
* @OA\POST( * @OA\POST(
* path="/backend/web/v1/authority/authority-roles/assign-authority-role", * path="/backend/web/v1/authority/authority-roles/assign-authority-role",
...@@ -276,17 +203,26 @@ class AuthorityRoleController extends BaseController ...@@ -276,17 +203,26 @@ class AuthorityRoleController extends BaseController
throw new BadRequestHttpException('删除原有角色的权限失败,请稍后重试'); throw new BadRequestHttpException('删除原有角色的权限失败,请稍后重试');
} }
//然后添加角色的权限 //然后添加角色的权限
$other_param = array( if (!$params['NODE_ID']) {
'ORG_GUID' => Yii::$app->user->identity->ORG_GUID throw new BadRequestHttpException('未给角色添加权限');
);
$model->setAttributes($other_param);
$model->setAttributes($params);
//保存
if ($model->save()) {
Yii::$app->getResponse()->setStatusCode(203);
} elseif (!$model->hasErrors()) {
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
} }
return $model; $nodes_arr = explode(',', $params['NODE_ID']);
//拼装信息
$role_access = array();
foreach ($nodes_arr as $k => $v) {
$role_access[$k]['ROLE_ID'] = $params['ROLE_ID'];
$role_access[$k]['NODE_ID'] = $v;
$role_access[$k]['ORG_GUID'] = Yii::$app->user->identity->ORG_GUID;
}
//保存信息
foreach ($role_access as $attributes) {
$_model = clone $model;
$_model->setAttributes($attributes);
if (!$_model->save()) {
throw new BadRequestHttpException('保存角色权限节点ID失败');
}
}
Yii::$app->getResponse()->setStatusCode(203);
return $_model;
} }
} }
...@@ -70,12 +70,16 @@ class ShopRbacNode extends BaseModel ...@@ -70,12 +70,16 @@ class ShopRbacNode extends BaseModel
'LEVEL' => '类型', 'LEVEL' => '类型',
]; ];
} }
//获取全部的权限节点 /**
public function getAuthorityNodes() * 获取角色拥有的权限节点信息
*/
public function getRoleAuthrities($auth_arr)
{ {
return $query = static::find() return $query = static::find()
->select('ID,NAME,TITLE,STATUS,PID') ->select('ID,NAME,TITLE,STATUS,PID')
->where(['ifnull(STATUS,0)' => 1]) ->where(['ifnull(STATUS,0)' => 1])
->all(); ->andFilterWhere(['ID' => $auth_arr])
->asArray()
->all();
} }
} }
...@@ -68,12 +68,11 @@ class ShopRbacRole extends BaseModel ...@@ -68,12 +68,11 @@ class ShopRbacRole extends BaseModel
]; ];
} }
//获取角色列表 //获取角色列表
public function getAuthorityRoles($ID) public function getAuthorityRoles($ID = '')
{ {
$query = static::find() $query = static::find()
->select('ID,NAME,PID,STATUS,REMARK,SORT') ->select('ID,NAME,PID,STATUS,REMARK,SORT')
->where(['STATUS' => 1]) ->where(['STATUS' => 1]);
->orderBy('SORT ASC');
if (isset($ID) && !empty($ID)) { if (isset($ID) && !empty($ID)) {
$query->andFilterWhere(['=', 'ID', $ID]); $query->andFilterWhere(['=', 'ID', $ID]);
...@@ -83,6 +82,11 @@ class ShopRbacRole extends BaseModel ...@@ -83,6 +82,11 @@ class ShopRbacRole extends BaseModel
'query' => $query->asArray(), 'query' => $query->asArray(),
'pagination' => [ 'pagination' => [
'pageSize' => PAGE_SIZE 'pageSize' => PAGE_SIZE
],
'sort' => [
'defaultOrder' => [
'SORT' => SORT_ASC
]
] ]
]); ]);
} }
......
...@@ -58,8 +58,7 @@ class ShopRbacRoleAccess extends BaseModel ...@@ -58,8 +58,7 @@ class ShopRbacRoleAccess extends BaseModel
//删除角色原有权限 //删除角色原有权限
public function delRoleAuthorities($ROLE_ID) public function delRoleAuthorities($ROLE_ID)
{ {
$roleAuth = static::find()->where(['ROLE_ID' => $ROLE_ID])->one(); $roleAuth = static::deleteAll(['ROLE_ID' => $ROLE_ID]);
$res = $roleAuth->delete(); return $roleAuth;
return $res;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment