Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
newShopBack
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
newshop
newShopBack
Commits
128dfd46
Commit
128dfd46
authored
Dec 11, 2019
by
孙磊
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
权限角色节点和添加权限
Signed-off-by:
sunlei
<
sunlei@romens.cn
>
parent
5cc29bd1
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
99 additions
and
101 deletions
+99
-101
urlmanager.php
backend/config/urlmanager.php
+1
-2
AuthorityNodeController.php
backend/controllers/v1/authority/AuthorityNodeController.php
+56
-0
AuthorityRoleController.php
backend/controllers/v1/authority/AuthorityRoleController.php
+24
-88
ShopRbacNode.php
backend/models/v1/rbacuser/ShopRbacNode.php
+9
-5
ShopRbacRole.php
backend/models/v1/rbacuser/ShopRbacRole.php
+7
-3
ShopRbacRoleAccess.php
backend/models/v1/rbacuser/ShopRbacRoleAccess.php
+2
-3
No files found.
backend/config/urlmanager.php
View file @
128dfd46
...
...
@@ -290,8 +290,6 @@ return [
'GET,OPTIONS authority-role-detail'
=>
'authority-role-detail'
,
'POST,OPTIONS add-authority-role'
=>
'add-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'
,
],
],
...
...
@@ -307,6 +305,7 @@ return [
'PUT,OPTIONS node-update'
=>
'node-update'
,
'POST,OPTIONS node-add'
=>
'node-add'
,
'DELETE,OPTIONS node-del'
=>
'node-del'
,
'GET,OPTIONS role-authorities'
=>
'role-authorities'
,
]
],
//权限-管理员列表
...
...
backend/controllers/v1/authority/AuthorityNodeController.php
View file @
128dfd46
...
...
@@ -8,6 +8,9 @@ use Yii;
use
yii\web\BadRequestHttpException
;
use
yii\web\HttpException
;
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
{
...
...
@@ -322,4 +325,57 @@ class AuthorityNodeController extends BaseController
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
;
}
}
}
backend/controllers/v1/authority/AuthorityRoleController.php
View file @
128dfd46
...
...
@@ -36,20 +36,11 @@ class AuthorityRoleController extends BaseController
public
function
actionAuthorityRoleList
()
{
$model
=
new
$this
->
modelClass
;
$params
=
Yii
::
$app
->
request
->
queryParams
;
$model
->
attributes
=
$params
;
if
(
$model
->
validate
())
{
//所有输入数据都有效 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
;
$info
=
$model
->
getAuthorityRoles
();
if
(
!
$info
->
getModels
())
{
throw
new
BadRequestHttpException
(
'未找到符合的角色信息'
);
}
return
$info
;
}
/**
* @OA\Get(
...
...
@@ -176,70 +167,6 @@ class AuthorityRoleController extends BaseController
Yii
::
$app
->
getResponse
()
->
setStatusCode
(
202
);
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(
* path="/backend/web/v1/authority/authority-roles/assign-authority-role",
...
...
@@ -276,17 +203,26 @@ class AuthorityRoleController extends BaseController
throw
new
BadRequestHttpException
(
'删除原有角色的权限失败,请稍后重试'
);
}
//然后添加角色的权限
$other_param
=
array
(
'ORG_GUID'
=>
Yii
::
$app
->
user
->
identity
->
ORG_GUID
);
$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.'
);
if
(
!
$params
[
'NODE_ID'
])
{
throw
new
BadRequestHttpException
(
'未给角色添加权限'
);
}
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
;
}
}
backend/models/v1/rbacuser/ShopRbacNode.php
View file @
128dfd46
...
...
@@ -70,12 +70,16 @@ class ShopRbacNode extends BaseModel
'LEVEL'
=>
'类型'
,
];
}
//获取全部的权限节点
public
function
getAuthorityNodes
()
/**
* 获取角色拥有的权限节点信息
*/
public
function
getRoleAuthrities
(
$auth_arr
)
{
return
$query
=
static
::
find
()
->
select
(
'ID,NAME,TITLE,STATUS,PID'
)
->
where
([
'ifnull(STATUS,0)'
=>
1
])
->
all
();
->
select
(
'ID,NAME,TITLE,STATUS,PID'
)
->
where
([
'ifnull(STATUS,0)'
=>
1
])
->
andFilterWhere
([
'ID'
=>
$auth_arr
])
->
asArray
()
->
all
();
}
}
backend/models/v1/rbacuser/ShopRbacRole.php
View file @
128dfd46
...
...
@@ -68,12 +68,11 @@ class ShopRbacRole extends BaseModel
];
}
//获取角色列表
public
function
getAuthorityRoles
(
$ID
)
public
function
getAuthorityRoles
(
$ID
=
''
)
{
$query
=
static
::
find
()
->
select
(
'ID,NAME,PID,STATUS,REMARK,SORT'
)
->
where
([
'STATUS'
=>
1
])
->
orderBy
(
'SORT ASC'
);
->
where
([
'STATUS'
=>
1
]);
if
(
isset
(
$ID
)
&&
!
empty
(
$ID
))
{
$query
->
andFilterWhere
([
'='
,
'ID'
,
$ID
]);
...
...
@@ -83,6 +82,11 @@ class ShopRbacRole extends BaseModel
'query'
=>
$query
->
asArray
(),
'pagination'
=>
[
'pageSize'
=>
PAGE_SIZE
],
'sort'
=>
[
'defaultOrder'
=>
[
'SORT'
=>
SORT_ASC
]
]
]);
}
...
...
backend/models/v1/rbacuser/ShopRbacRoleAccess.php
View file @
128dfd46
...
...
@@ -58,8 +58,7 @@ class ShopRbacRoleAccess extends BaseModel
//删除角色原有权限
public
function
delRoleAuthorities
(
$ROLE_ID
)
{
$roleAuth
=
static
::
find
()
->
where
([
'ROLE_ID'
=>
$ROLE_ID
])
->
one
();
$res
=
$roleAuth
->
delete
();
return
$res
;
$roleAuth
=
static
::
deleteAll
([
'ROLE_ID'
=>
$ROLE_ID
]);
return
$roleAuth
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment