feat(新增): 角色分配权限,根据角色查询已经分配好的权限,其它缺陷修复
This commit is contained in:
parent
f6270e80f3
commit
d22f2305e5
|
@ -22,7 +22,7 @@ public class DeptUpdateDto {
|
|||
private Long id;
|
||||
|
||||
@Schema(name = "parentId", title = "父级id")
|
||||
private String parentId;
|
||||
private Long parentId;
|
||||
|
||||
@Schema(name = "managerId", title = "管理者")
|
||||
@NotNull(message = "管理者不能为空")
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package cn.bunny.dao.dto.system.rolePower;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Schema(name = "AssignPowersToRoleDto对象", title = "为角色分配权限表单", description = "为角色分配权限")
|
||||
public class AssignPowersToRoleDto {
|
||||
|
||||
@Schema(name = "roleIds", title = "角色id")
|
||||
@NotNull(message = "角色id不能为空")
|
||||
private Long roleId;
|
||||
|
||||
@Schema(name = "powerIds", title = "权限id列表")
|
||||
@NotNull(message = "权限id列表不能为空")
|
||||
private List<Long> powerIds;
|
||||
|
||||
}
|
|
@ -26,7 +26,6 @@ public class PowerAddDto {
|
|||
private String powerName;
|
||||
|
||||
@Schema(name = "requestUrl", title = "请求路径")
|
||||
@NotBlank(message = "请求路径 不能为空")
|
||||
private String requestUrl;
|
||||
|
||||
}
|
||||
|
|
|
@ -31,7 +31,6 @@ public class PowerUpdateDto {
|
|||
private String powerName;
|
||||
|
||||
@Schema(name = "requestUrl", title = "请求路径")
|
||||
@NotBlank(message = "请求路径 不能为空")
|
||||
private String requestUrl;
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package cn.bunny.dao.dto.system.router;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
@ -16,9 +17,11 @@ import java.util.List;
|
|||
public class AssignRolesToRoutersDto {
|
||||
|
||||
@Schema(name = "routerId", title = "路由id")
|
||||
@NotNull(message = "路由id不能为空")
|
||||
private Long routerId;
|
||||
|
||||
@Schema(name = "roleIds", title = "角色id列表")
|
||||
@NotNull(message = "角色id列表不能为空")
|
||||
private List<Long> roleIds;
|
||||
|
||||
}
|
|
@ -24,11 +24,4 @@ public class RedisUserConstant {
|
|||
return ADMIN_EMAIL_CODE_PREFIX + adminUser;
|
||||
}
|
||||
|
||||
public static String getUserLoginInfoPrefix(String user) {
|
||||
return USER_LOGIN_INFO_PREFIX + user;
|
||||
}
|
||||
|
||||
public static String getUserEmailCodePrefix(String user) {
|
||||
return USER_EMAIL_CODE_PREFIX + user;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,6 +56,7 @@ public enum ResultCodeEnum {
|
|||
URL_ENCODE_ERROR(216, "URL编码失败"),
|
||||
ILLEGAL_CALLBACK_REQUEST_ERROR(217, "非法回调请求"),
|
||||
FETCH_USERINFO_ERROR(219, "获取用户信息失败"),
|
||||
ILLEGAL_DATA_REQUEST(219, "非法数据请求"),
|
||||
|
||||
// 无权访问 403
|
||||
FAIL_REQUEST_NOT_AUTH(403, "用户未认证"),
|
||||
|
|
|
@ -48,7 +48,7 @@ public class LoginVo extends BaseVo {
|
|||
private String lastLoginIpAddress;
|
||||
|
||||
@Schema(name = "status", title = "1:禁用 0:正常")
|
||||
private Byte status;
|
||||
private Boolean status;
|
||||
|
||||
@Schema(name = "token", title = "令牌")
|
||||
private String token;
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
package cn.bunny.services.controller;
|
||||
|
||||
import cn.bunny.dao.dto.system.rolePower.AssignPowersToRoleDto;
|
||||
import cn.bunny.dao.pojo.result.Result;
|
||||
import cn.bunny.services.service.RolePowerService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -34,4 +34,11 @@ public class RolePowerController {
|
|||
List<String> voList = rolePowerService.getPowerListByRoleId(id);
|
||||
return Mono.just(Result.success(voList));
|
||||
}
|
||||
|
||||
@Operation(summary = "为角色分配权限", description = "为角色分配权限")
|
||||
@PostMapping("assignPowersToRole")
|
||||
public Mono<Result<String>> assignPowersToRole(@Valid @RequestBody AssignPowersToRoleDto dto) {
|
||||
rolePowerService.assignPowersToRole(dto);
|
||||
return Mono.just(Result.success());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,9 +20,9 @@ public interface UserRoleMapper extends BaseMapper<UserRole> {
|
|||
/**
|
||||
* * 删除这个用户id下所有的角色信息
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @param userIds 用户id
|
||||
*/
|
||||
void deleteBatchIdsByUserIdsWithPhysics(List<Long> userId);
|
||||
void deleteBatchIdsByUserIdsWithPhysics(List<Long> userIds);
|
||||
|
||||
/**
|
||||
* * 根据角色id删除用户和角色
|
||||
|
|
|
@ -48,7 +48,7 @@ public class NoTokenAuthenticationFilter extends OncePerRequestFilter {
|
|||
LoginVo loginVo = JSON.parseObject(JSON.toJSONString(loginVoObject), LoginVo.class);
|
||||
|
||||
// 判断用户是否禁用
|
||||
if (loginVo != null && loginVo.getStatus() == 1) {
|
||||
if (loginVo != null && loginVo.getStatus()) {
|
||||
ResponseUtil.out(response, Result.error(ResultCodeEnum.FAIL_NO_ACCESS_DENIED_USER_LOCKED));
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -86,7 +86,7 @@ public class TokenLoginFilterService extends UsernamePasswordAuthenticationFilte
|
|||
LoginVo loginVo = customUserDetailsService.login(loginDto);
|
||||
|
||||
// 判断用户是否禁用
|
||||
if (loginVo.getStatus() == 1) {
|
||||
if (loginVo.getStatus()) {
|
||||
out(response, Result.error(ResultCodeEnum.FAIL_NO_ACCESS_DENIED_USER_LOCKED));
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package cn.bunny.services.service;
|
||||
|
||||
import cn.bunny.dao.dto.system.rolePower.AssignPowersToRoleDto;
|
||||
import cn.bunny.dao.entity.system.RolePower;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -22,4 +24,11 @@ public interface RolePowerService extends IService<RolePower> {
|
|||
* @return 已选择的权限列表
|
||||
*/
|
||||
List<String> getPowerListByRoleId(Long id);
|
||||
|
||||
/**
|
||||
* * 为角色分配权限
|
||||
*
|
||||
* @param powerIds 权限id
|
||||
*/
|
||||
void assignPowersToRole(@Valid AssignPowersToRoleDto powerIds);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import cn.bunny.common.service.exception.BunnyException;
|
||||
import cn.bunny.dao.dto.system.dept.DeptAddDto;
|
||||
import cn.bunny.dao.dto.system.dept.DeptDto;
|
||||
import cn.bunny.dao.dto.system.dept.DeptUpdateDto;
|
||||
import cn.bunny.dao.entity.system.Dept;
|
||||
import cn.bunny.dao.pojo.result.PageResult;
|
||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||
import cn.bunny.dao.vo.system.dept.DeptVo;
|
||||
import cn.bunny.services.mapper.DeptMapper;
|
||||
import cn.bunny.services.mapper.UserDeptMapper;
|
||||
|
@ -12,7 +14,6 @@ import cn.bunny.services.service.DeptService;
|
|||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
@ -73,7 +74,8 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
|||
* @param dto 部门添加
|
||||
*/
|
||||
@Override
|
||||
public void addDept(@Valid DeptAddDto dto) {
|
||||
public void addDept(DeptAddDto dto) {
|
||||
// 整理管理者人员
|
||||
String mangerList = dto.getManager().stream().map(String::trim).collect(Collectors.joining(","));
|
||||
|
||||
// 保存数据
|
||||
|
@ -90,8 +92,10 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
|||
* @param dto 部门更新
|
||||
*/
|
||||
@Override
|
||||
public void updateDept(@Valid DeptUpdateDto dto) {
|
||||
public void updateDept(DeptUpdateDto dto) {
|
||||
// 判断所更新的部门是否存在
|
||||
String mangerList = dto.getManager().stream().map(String::trim).collect(Collectors.joining(","));
|
||||
if (dto.getId().equals(dto.getParentId())) throw new BunnyException(ResultCodeEnum.ILLEGAL_DATA_REQUEST);
|
||||
|
||||
// 更新内容
|
||||
Dept dept = new Dept();
|
||||
|
@ -110,6 +114,7 @@ public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements De
|
|||
public void deleteDept(List<Long> ids) {
|
||||
// 删除当前部门
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
|
||||
// 删除用户部门关联
|
||||
userDeptMapper.deleteBatchIdsByDeptIdWithPhysics(ids);
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public class MenuIconServiceImpl extends ServiceImpl<MenuIconMapper, MenuIcon> i
|
|||
* @param dto 系统菜单图标添加
|
||||
*/
|
||||
@Override
|
||||
public void addMenuIcon(@Valid MenuIconAddDto dto) {
|
||||
public void addMenuIcon(MenuIconAddDto dto) {
|
||||
// 保存数据
|
||||
MenuIcon menuIcon = new MenuIcon();
|
||||
BeanUtils.copyProperties(dto, menuIcon);
|
||||
|
|
|
@ -96,7 +96,8 @@ public class PowerServiceImpl extends ServiceImpl<PowerMapper, Power> implements
|
|||
Long id = dto.getId();
|
||||
List<Power> powerList = list(Wrappers.<Power>lambdaQuery().eq(Power::getId, id));
|
||||
if (powerList.isEmpty()) throw new BunnyException(ResultCodeEnum.DATA_NOT_EXIST);
|
||||
|
||||
if (dto.getId().equals(dto.getParentId())) throw new BunnyException(ResultCodeEnum.ILLEGAL_DATA_REQUEST);
|
||||
|
||||
// 更新内容
|
||||
Power power = new Power();
|
||||
BeanUtils.copyProperties(dto, power);
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
package cn.bunny.services.service.impl;
|
||||
|
||||
import cn.bunny.dao.dto.system.rolePower.AssignPowersToRoleDto;
|
||||
import cn.bunny.dao.entity.system.RolePower;
|
||||
import cn.bunny.services.mapper.RolePowerMapper;
|
||||
import cn.bunny.services.service.RolePowerService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -18,11 +19,9 @@ import java.util.List;
|
|||
* @since 2024-09-26
|
||||
*/
|
||||
@Service
|
||||
@Transactional
|
||||
public class RolePowerServiceImpl extends ServiceImpl<RolePowerMapper, RolePower> implements RolePowerService {
|
||||
|
||||
@Autowired
|
||||
private RolePowerMapper rolePowerMapper;
|
||||
|
||||
/**
|
||||
* * 根据角色id获取权限内容
|
||||
*
|
||||
|
@ -31,7 +30,31 @@ public class RolePowerServiceImpl extends ServiceImpl<RolePowerMapper, RolePower
|
|||
*/
|
||||
@Override
|
||||
public List<String> getPowerListByRoleId(Long id) {
|
||||
List<RolePower> rolePowerList = rolePowerMapper.selectPowerListByRoleId(id);
|
||||
List<RolePower> rolePowerList = baseMapper.selectPowerListByRoleId(id);
|
||||
return rolePowerList.stream().map(rolePower -> rolePower.getPowerId().toString()).toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* * 为角色分配权限
|
||||
*
|
||||
* @param dto 为角色分配权限表单
|
||||
*/
|
||||
@Override
|
||||
public void assignPowersToRole(AssignPowersToRoleDto dto) {
|
||||
List<Long> powerIds = dto.getPowerIds();
|
||||
Long roleId = dto.getRoleId();
|
||||
|
||||
// 删除所有权限
|
||||
baseMapper.deleteBatchPowerIdsWithPhysics(powerIds);
|
||||
|
||||
// 保存分配数据
|
||||
List<RolePower> rolePowerList = powerIds.stream().map(powerId -> {
|
||||
RolePower rolePower = new RolePower();
|
||||
rolePower.setRoleId(roleId);
|
||||
rolePower.setPowerId(powerId);
|
||||
return rolePower;
|
||||
}).toList();
|
||||
|
||||
saveBatch(rolePowerList);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,6 +95,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
|
|||
*/
|
||||
@Override
|
||||
public void updateRole(@Valid RoleUpdateDto dto) {
|
||||
// 查询更新的角色是否存在
|
||||
List<Role> roleList = list(Wrappers.<Role>lambdaQuery().eq(Role::getId, dto.getId()));
|
||||
if (roleList.isEmpty()) throw new BunnyException(ResultCodeEnum.DATA_NOT_EXIST);
|
||||
|
||||
|
|
|
@ -187,9 +187,8 @@ public class RouterServiceImpl extends ServiceImpl<RouterMapper, Router> impleme
|
|||
public void updateMenu(RouterUpdateDto dto) {
|
||||
Long id = dto.getId();
|
||||
Router router = getOne(Wrappers.<Router>lambdaQuery().eq(Router::getId, id));
|
||||
if (router == null) {
|
||||
throw new BunnyException(ResultCodeEnum.DATA_NOT_EXIST);
|
||||
}
|
||||
if (router == null) throw new BunnyException(ResultCodeEnum.DATA_NOT_EXIST);
|
||||
if (dto.getId().equals(dto.getParentId())) throw new BunnyException(ResultCodeEnum.ILLEGAL_DATA_REQUEST);
|
||||
|
||||
router = new Router();
|
||||
BeanUtils.copyProperties(dto, router);
|
||||
|
|
|
@ -306,7 +306,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implemen
|
|||
* @param dto 用户信息更新
|
||||
*/
|
||||
@Override
|
||||
public void updateAdminUser(@Valid AdminUserUpdateDto dto) {
|
||||
public void updateAdminUser(AdminUserUpdateDto dto) {
|
||||
// 判断更新内容是否存在
|
||||
List<AdminUser> adminUserList = list(Wrappers.<AdminUser>lambdaQuery().eq(AdminUser::getId, dto.getId()));
|
||||
if (adminUserList.isEmpty()) throw new BunnyException(ResultCodeEnum.DATA_NOT_EXIST);
|
||||
|
||||
AdminUser adminUser = new AdminUser();
|
||||
BeanUtils.copyProperties(dto, adminUser);
|
||||
updateById(adminUser);
|
||||
|
|
|
@ -77,8 +77,8 @@ mybatis-plus:
|
|||
global-config:
|
||||
db-config:
|
||||
logic-delete-field: isDelete
|
||||
# configuration:
|
||||
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志
|
||||
configuration:
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 查看日志
|
||||
|
||||
logging:
|
||||
level:
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
delete
|
||||
from sys_role_power
|
||||
where power_id in
|
||||
<foreach collection="deptIds" item="id" open="(" close=")" separator=",">
|
||||
<foreach collection="powerIds" item="id" open="(" close=")" separator=",">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
|
|
@ -23,7 +23,10 @@
|
|||
<delete id="deleteBatchIdsByUserIdsWithPhysics">
|
||||
delete
|
||||
from sys_user_role
|
||||
where user_id = #{userId}
|
||||
where user_id in
|
||||
<foreach collection="userIds" item="id" open="(" close=")" separator=",">
|
||||
#{id}
|
||||
</foreach>
|
||||
</delete>
|
||||
|
||||
<!-- 根据角色id删除用户和角色 -->
|
||||
|
|
Loading…
Reference in New Issue