Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Eladmin
Commits
7a2fbae8
You need to sign in or sign up before continuing.
Commit
7a2fbae8
authored
May 23, 2020
by
ZhengJie
Browse files
[代码完善](v2.5): v2.5 beta 角色、部门、岗位删除优化
更新详情:
https://www.ydyno.com/archives/1225.html
parent
9de236d6
Changes
12
Hide whitespace changes
Inline
Side-by-side
eladmin-common/src/main/java/me/zhengjie/utils/ThrowableUtil.java
View file @
7a2fbae8
...
...
@@ -15,8 +15,6 @@
*/
package
me.zhengjie.utils
;
import
me.zhengjie.exception.BadRequestException
;
import
org.hibernate.exception.ConstraintViolationException
;
import
java.io.PrintWriter
;
import
java.io.StringWriter
;
...
...
@@ -36,16 +34,4 @@ public class ThrowableUtil {
return
sw
.
toString
();
}
}
public
static
void
throwForeignKeyException
(
Throwable
e
,
String
msg
){
Throwable
t
=
e
.
getCause
();
while
((
t
!=
null
)
&&
!(
t
instanceof
ConstraintViolationException
))
{
t
=
t
.
getCause
();
}
if
(
t
!=
null
)
{
throw
new
BadRequestException
(
msg
);
}
assert
false
;
throw
new
BadRequestException
(
"删除失败"
);
}
}
eladmin-system/src/main/java/me/zhengjie/modules/system/repository/RoleRepository.java
View file @
7a2fbae8
...
...
@@ -57,4 +57,13 @@ public interface RoleRepository extends JpaRepository<Role, Long>, JpaSpecificat
@Modifying
@Query
(
value
=
"delete from sys_roles_menus where menu_id = ?1"
,
nativeQuery
=
true
)
void
untiedMenu
(
Long
id
);
/**
* 根据部门查询
* @param deptIds /
* @return /
*/
@Query
(
value
=
"select count(1) from sys_role r, sys_roles_depts d where "
+
"r.role_id = d.role_id and d.dept_id in ?1"
,
nativeQuery
=
true
)
int
countByDepts
(
Set
<
Long
>
deptIds
);
}
eladmin-system/src/main/java/me/zhengjie/modules/system/repository/UserRepository.java
View file @
7a2fbae8
...
...
@@ -95,4 +95,28 @@ public interface UserRepository extends JpaRepository<User, Long>, JpaSpecificat
* @param ids /
*/
void
deleteAllByIdIn
(
Set
<
Long
>
ids
);
/**
* 根据岗位查询
* @param ids /
* @return /
*/
@Query
(
value
=
"SELECT count(1) FROM sys_user u, sys_users_jobs j WHERE u.user_id = j.user_id AND j.job_id IN ?1"
,
nativeQuery
=
true
)
int
countByJobs
(
Set
<
Long
>
ids
);
/**
* 根据部门查询
* @param deptIds /
* @return /
*/
@Query
(
value
=
"SELECT count(1) FROM sys_user u WHERE u.dept_id IN ?1"
,
nativeQuery
=
true
)
int
countByDepts
(
Set
<
Long
>
deptIds
);
/**
* 根据角色查询
* @return /
*/
@Query
(
value
=
"SELECT count(1) FROM sys_user u, sys_users_roles r WHERE "
+
"u.user_id = r.user_id AND r.role_id in ?1"
,
nativeQuery
=
true
)
int
countByRoles
(
Set
<
Long
>
ids
);
}
eladmin-system/src/main/java/me/zhengjie/modules/system/rest/DeptController.java
View file @
7a2fbae8
...
...
@@ -113,11 +113,9 @@ public class DeptController {
deptDtos
=
deptService
.
getDeleteDepts
(
deptList
,
deptDtos
);
}
}
try
{
deptService
.
delete
(
deptDtos
);
}
catch
(
Throwable
e
){
ThrowableUtil
.
throwForeignKeyException
(
e
,
"所选部门中存在岗位或者角色关联,请取消关联后再试"
);
}
// 验证是否被角色或用户关联
deptService
.
verification
(
deptDtos
);
deptService
.
delete
(
deptDtos
);
return
new
ResponseEntity
<>(
HttpStatus
.
OK
);
}
}
\ No newline at end of file
eladmin-system/src/main/java/me/zhengjie/modules/system/rest/JobController.java
View file @
7a2fbae8
...
...
@@ -89,11 +89,9 @@ public class JobController {
@DeleteMapping
@PreAuthorize
(
"@el.check('job:del')"
)
public
ResponseEntity
<
Object
>
delete
(
@RequestBody
Set
<
Long
>
ids
){
try
{
jobService
.
delete
(
ids
);
}
catch
(
Throwable
e
){
ThrowableUtil
.
throwForeignKeyException
(
e
,
"所选岗位存在用户关联,请取消关联后再试"
);
}
// 验证是否被用户关联
jobService
.
verification
(
ids
);
jobService
.
delete
(
ids
);
return
new
ResponseEntity
<>(
HttpStatus
.
OK
);
}
}
\ No newline at end of file
eladmin-system/src/main/java/me/zhengjie/modules/system/rest/RoleController.java
View file @
7a2fbae8
...
...
@@ -136,11 +136,9 @@ public class RoleController {
RoleDto
role
=
roleService
.
findById
(
id
);
getLevels
(
role
.
getLevel
());
}
try
{
roleService
.
delete
(
ids
);
}
catch
(
Throwable
e
){
ThrowableUtil
.
throwForeignKeyException
(
e
,
"所选角色存在用户关联,请取消关联后再试"
);
}
// 验证是否被用户关联
roleService
.
verification
(
ids
);
roleService
.
delete
(
ids
);
return
new
ResponseEntity
<>(
HttpStatus
.
OK
);
}
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/service/DeptService.java
View file @
7a2fbae8
...
...
@@ -116,4 +116,10 @@ public interface DeptService {
* @return
*/
List
<
Long
>
getDeptChildren
(
Long
deptId
,
List
<
Dept
>
deptList
);
/**
* 验证是否被角色或用户关联
* @param deptDtos /
*/
void
verification
(
Set
<
DeptDto
>
deptDtos
);
}
\ No newline at end of file
eladmin-system/src/main/java/me/zhengjie/modules/system/service/JobService.java
View file @
7a2fbae8
...
...
@@ -79,4 +79,10 @@ public interface JobService {
* @throws IOException /
*/
void
download
(
List
<
JobDto
>
queryAll
,
HttpServletResponse
response
)
throws
IOException
;
/**
* 验证是否被用户关联
* @param ids /
*/
void
verification
(
Set
<
Long
>
ids
);
}
\ No newline at end of file
eladmin-system/src/main/java/me/zhengjie/modules/system/service/RoleService.java
View file @
7a2fbae8
...
...
@@ -120,4 +120,10 @@ public interface RoleService {
* @return 权限信息
*/
List
<
GrantedAuthority
>
mapToGrantedAuthorities
(
UserDto
user
);
/**
* 验证是否被用户关联
* @param ids /
*/
void
verification
(
Set
<
Long
>
ids
);
}
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/DeptServiceImpl.java
View file @
7a2fbae8
...
...
@@ -21,6 +21,7 @@ import lombok.RequiredArgsConstructor;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.modules.system.domain.Dept
;
import
me.zhengjie.modules.system.domain.User
;
import
me.zhengjie.modules.system.repository.RoleRepository
;
import
me.zhengjie.modules.system.repository.UserRepository
;
import
me.zhengjie.modules.system.service.dto.DeptDto
;
import
me.zhengjie.modules.system.service.dto.DeptQueryCriteria
;
...
...
@@ -57,6 +58,7 @@ public class DeptServiceImpl implements DeptService {
private
final
DeptMapper
deptMapper
;
private
final
UserRepository
userRepository
;
private
final
RedisUtils
redisUtils
;
private
final
RoleRepository
roleRepository
;
@Override
public
List
<
DeptDto
>
queryAll
(
DeptQueryCriteria
criteria
,
Boolean
isQuery
)
throws
Exception
{
...
...
@@ -240,6 +242,17 @@ public class DeptServiceImpl implements DeptService {
deptRepository
.
updateSubCntById
(
count
,
deptId
);
}
@Override
public
void
verification
(
Set
<
DeptDto
>
deptDtos
)
{
Set
<
Long
>
deptIds
=
deptDtos
.
stream
().
map
(
DeptDto:
:
getId
).
collect
(
Collectors
.
toSet
());
if
(
userRepository
.
countByDepts
(
deptIds
)
>
0
){
throw
new
BadRequestException
(
"所选部门存在用户关联,请解除后再试!"
);
}
if
(
roleRepository
.
countByDepts
(
deptIds
)
>
0
){
throw
new
BadRequestException
(
"所选部门存在角色关联,请解除后再试!"
);
}
}
/**
* 清理缓存
* @param id /
...
...
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/JobServiceImpl.java
View file @
7a2fbae8
...
...
@@ -16,8 +16,10 @@
package
me.zhengjie.modules.system.service.impl
;
import
lombok.RequiredArgsConstructor
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.exception.EntityExistException
;
import
me.zhengjie.modules.system.domain.Job
;
import
me.zhengjie.modules.system.repository.UserRepository
;
import
me.zhengjie.modules.system.service.dto.JobQueryCriteria
;
import
me.zhengjie.utils.*
;
import
me.zhengjie.modules.system.repository.JobRepository
;
...
...
@@ -49,6 +51,7 @@ public class JobServiceImpl implements JobService {
private
final
JobRepository
jobRepository
;
private
final
JobMapper
jobMapper
;
private
final
RedisUtils
redisUtils
;
private
final
UserRepository
userRepository
;
@Override
public
Map
<
String
,
Object
>
queryAll
(
JobQueryCriteria
criteria
,
Pageable
pageable
)
{
...
...
@@ -114,4 +117,11 @@ public class JobServiceImpl implements JobService {
}
FileUtil
.
downloadExcel
(
list
,
response
);
}
@Override
public
void
verification
(
Set
<
Long
>
ids
)
{
if
(
userRepository
.
countByJobs
(
ids
)
>
0
){
throw
new
BadRequestException
(
"所选的岗位中存在用户关联,请解除关联再试!"
);
}
}
}
\ No newline at end of file
eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/RoleServiceImpl.java
View file @
7a2fbae8
...
...
@@ -16,6 +16,7 @@
package
me.zhengjie.modules.system.service.impl
;
import
lombok.RequiredArgsConstructor
;
import
me.zhengjie.exception.BadRequestException
;
import
me.zhengjie.modules.system.domain.Menu
;
import
me.zhengjie.modules.system.domain.Role
;
import
me.zhengjie.exception.EntityExistException
;
...
...
@@ -202,4 +203,11 @@ public class RoleServiceImpl implements RoleService {
redisUtils
.
delByKeys
(
"menu::user:"
,
userIds
);
redisUtils
.
delByKeys
(
"role::auth:"
,
userIds
);
}
@Override
public
void
verification
(
Set
<
Long
>
ids
)
{
if
(
userRepository
.
countByRoles
(
ids
)
>
0
){
throw
new
BadRequestException
(
"所选角色存在用户关联,请解除关联再试!"
);
}
}
}
Write
Preview
Supports
Markdown
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