feat: 添加角色用户缓存及权限构建优化,前端用户管理表单验证优化
This commit is contained in:
parent
c763aef763
commit
b3cbfa562b
@ -63,7 +63,11 @@
|
||||
<el-table-column prop="name" label="名称" />
|
||||
<el-table-column prop="dataScope" label="数据权限" />
|
||||
<el-table-column prop="level" label="角色级别" />
|
||||
<el-table-column :show-overflow-tooltip="true" prop="description" label="描述" />
|
||||
<el-table-column :show-overflow-tooltip="true" prop="description" label="描述">
|
||||
<template slot-scope="scope">
|
||||
{{ scope.row.description == null ? '-' : scope.row.description }}
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column :show-overflow-tooltip="true" width="135px" prop="createTime" label="创建日期" />
|
||||
<el-table-column v-if="checkPer(['admin','roles:edit','roles:del'])" label="操作" width="130px" align="center" fixed="right">
|
||||
<template slot-scope="scope">
|
||||
@ -152,9 +156,6 @@ export default {
|
||||
rules: {
|
||||
name: [
|
||||
{ required: true, message: '请输入名称', trigger: 'blur' }
|
||||
],
|
||||
permission: [
|
||||
{ required: true, message: '请输入权限', trigger: 'blur' }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -72,7 +72,7 @@
|
||||
</crudOperation>
|
||||
</div>
|
||||
<!--表单渲染-->
|
||||
<el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="570px">
|
||||
<el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="555px">
|
||||
<el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="66px">
|
||||
<el-form-item label="用户名" prop="username">
|
||||
<el-input v-model="form.username" @keydown.native="keydown($event)" />
|
||||
@ -91,14 +91,14 @@
|
||||
v-model="form.dept.id"
|
||||
:options="depts"
|
||||
:load-options="loadDepts"
|
||||
style="width: 178px"
|
||||
style="width: 173px"
|
||||
placeholder="选择部门"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="岗位" prop="jobs">
|
||||
<el-form-item label="岗位" prop="jobDatas" class="is-required">
|
||||
<el-select
|
||||
v-model="jobDatas"
|
||||
style="width: 178px"
|
||||
style="width: 172px"
|
||||
multiple
|
||||
placeholder="请选择"
|
||||
@remove-tag="deleteTag"
|
||||
@ -127,10 +127,11 @@
|
||||
>{{ item.label }}</el-radio>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
<el-form-item style="margin-bottom: 0;" label="角色" prop="roles">
|
||||
<el-form-item style="margin-bottom: 0;" label="角色" prop="roleDatas" class="is-required">
|
||||
<el-select
|
||||
v-model="roleDatas"
|
||||
style="width: 437px"
|
||||
:disabled="form.id === user.id"
|
||||
style="width: 426px"
|
||||
multiple
|
||||
placeholder="请选择"
|
||||
@remove-tag="deleteTag"
|
||||
@ -267,6 +268,35 @@ export default {
|
||||
],
|
||||
phone: [
|
||||
{ required: true, trigger: 'blur', validator: validPhone }
|
||||
],
|
||||
'dept.id': [
|
||||
{ required: true, message: '部门不能为空', trigger: 'blur' }
|
||||
],
|
||||
jobDatas: [
|
||||
{
|
||||
validator: (rule, value, callback) => {
|
||||
value = this.jobDatas
|
||||
if (!value || value.length === 0) {
|
||||
callback(new Error('请选择至少一个岗位'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
trigger: 'change'
|
||||
}
|
||||
],
|
||||
roleDatas: [
|
||||
{
|
||||
validator: (rule, value, callback) => {
|
||||
value = this.roleDatas
|
||||
if (!value || value.length === 0) {
|
||||
callback(new Error('请选择至少一个角色'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
},
|
||||
trigger: 'change'
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@ -351,27 +381,9 @@ export default {
|
||||
},
|
||||
// 提交前做的操作
|
||||
[CRUD.HOOK.afterValidateCU](crud) {
|
||||
if (!crud.form.dept.id) {
|
||||
this.$message({
|
||||
message: '部门不能为空',
|
||||
type: 'warning'
|
||||
})
|
||||
return false
|
||||
} else if (this.jobDatas.length === 0) {
|
||||
this.$message({
|
||||
message: '岗位不能为空',
|
||||
type: 'warning'
|
||||
})
|
||||
return false
|
||||
} else if (this.roleDatas.length === 0) {
|
||||
this.$message({
|
||||
message: '角色不能为空',
|
||||
type: 'warning'
|
||||
})
|
||||
return false
|
||||
}
|
||||
crud.form.roles = userRoles
|
||||
crud.form.jobs = userJobs
|
||||
console.log(this.jobDatas)
|
||||
return true
|
||||
},
|
||||
// 获取左侧部门数据
|
||||
|
@ -28,6 +28,11 @@ import java.util.stream.Collectors;
|
||||
@Service(value = "el")
|
||||
public class AuthorityConfig {
|
||||
|
||||
/**
|
||||
* 判断接口是否有权限
|
||||
* @param permissions 权限
|
||||
* @return /
|
||||
*/
|
||||
public Boolean check(String ...permissions){
|
||||
// 获取当前用户的所有权限
|
||||
List<String> elPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
|
||||
|
@ -42,6 +42,7 @@ public interface CacheKey {
|
||||
* 角色授权
|
||||
*/
|
||||
String ROLE_AUTH = "role::auth:";
|
||||
String ROLE_USER = "role::user:";
|
||||
|
||||
/**
|
||||
* 角色信息
|
||||
|
@ -18,6 +18,7 @@ package me.zhengjie.modules.security.service;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.zhengjie.exception.BadRequestException;
|
||||
import me.zhengjie.modules.security.service.dto.AuthorityDto;
|
||||
import me.zhengjie.modules.security.service.dto.JwtUserDto;
|
||||
import me.zhengjie.modules.system.domain.User;
|
||||
import me.zhengjie.modules.system.service.DataService;
|
||||
@ -25,6 +26,7 @@ import me.zhengjie.modules.system.service.RoleService;
|
||||
import me.zhengjie.modules.system.service.UserService;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Zheng Jie
|
||||
@ -50,7 +52,10 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||
if (!user.getEnabled()) {
|
||||
throw new BadRequestException("账号未激活!");
|
||||
}
|
||||
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), roleService.buildAuthorities(user), user.getPassword());
|
||||
// 获取用户的权限
|
||||
List<AuthorityDto> authorities = roleService.buildPermissions(user);
|
||||
// 初始化JwtUserDto
|
||||
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), authorities, user.getPassword());
|
||||
// 添加缓存数据
|
||||
userCacheManager.addUserCache(username, jwtUserDto);
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ public interface RoleService extends IService<Role> {
|
||||
* @param user 用户信息
|
||||
* @return 权限信息
|
||||
*/
|
||||
List<AuthorityDto> buildAuthorities(User user);
|
||||
List<AuthorityDto> buildPermissions(User user);
|
||||
|
||||
/**
|
||||
* 验证是否被用户关联
|
||||
|
@ -152,7 +152,13 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||
|
||||
@Override
|
||||
public List<Role> findByUsersId(Long userId) {
|
||||
return roleMapper.findByUserId(userId);
|
||||
String key = CacheKey.ROLE_USER + userId;
|
||||
List<Role> roles = redisUtils.getList(key, Role.class);
|
||||
if (CollUtil.isEmpty(roles)) {
|
||||
roles = roleMapper.findByUserId(userId);
|
||||
redisUtils.set(key, roles, 1, TimeUnit.DAYS);
|
||||
}
|
||||
return roles;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -168,7 +174,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AuthorityDto> buildAuthorities(User user) {
|
||||
public List<AuthorityDto> buildPermissions(User user) {
|
||||
String key = CacheKey.ROLE_AUTH + user.getId();
|
||||
List<AuthorityDto> authorityDtos = redisUtils.getList(key, AuthorityDto.class);
|
||||
if (CollUtil.isEmpty(authorityDtos)) {
|
||||
@ -228,6 +234,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
||||
redisUtils.delByKeys(CacheKey.DATA_USER, userIds);
|
||||
redisUtils.delByKeys(CacheKey.MENU_USER, userIds);
|
||||
redisUtils.delByKeys(CacheKey.ROLE_AUTH, userIds);
|
||||
redisUtils.delByKeys(CacheKey.ROLE_USER, userIds);
|
||||
}
|
||||
redisUtils.del(CacheKey.ROLE_ID + id);
|
||||
}
|
||||
|
@ -125,6 +125,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
||||
redisUtils.del(CacheKey.DATA_USER + resources.getId());
|
||||
redisUtils.del(CacheKey.MENU_USER + resources.getId());
|
||||
redisUtils.del(CacheKey.ROLE_AUTH + resources.getId());
|
||||
redisUtils.del(CacheKey.ROLE_USER + resources.getId());
|
||||
}
|
||||
// 修改部门会影响 数据权限
|
||||
if (!Objects.equals(resources.getDept(),user.getDept())) {
|
||||
|
Loading…
Reference in New Issue
Block a user