✨ 为用户分配角色
This commit is contained in:
parent
27e3b6772f
commit
b01f915415
|
@ -46,6 +46,13 @@ public class RoleController {
|
|||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取全部角色列表", description = "获取全部角色列表")
|
||||
@GetMapping("all")
|
||||
public Result<List<RoleVo>> getRoleList() {
|
||||
List<RoleVo> roleVoList = roleService.getRoleList();
|
||||
return Result.success(roleVoList);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加系统角色表", description = "添加系统角色表")
|
||||
@PostMapping()
|
||||
public Result<String> addRole(@Valid @RequestBody RoleDto dto) {
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.spring.step2.controller;
|
|||
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.spring.step2.domain.dto.UserDto;
|
||||
import com.spring.step2.domain.dto.user.UserDto;
|
||||
import com.spring.step2.domain.entity.UserEntity;
|
||||
import com.spring.step2.domain.vo.UserVo;
|
||||
import com.spring.step2.domain.vo.result.PageResult;
|
||||
|
|
|
@ -2,7 +2,8 @@ package com.spring.step2.controller;
|
|||
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.spring.step2.domain.dto.UserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.AssignUserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.UserRoleDto;
|
||||
import com.spring.step2.domain.entity.UserRoleEntity;
|
||||
import com.spring.step2.domain.vo.UserRoleVo;
|
||||
import com.spring.step2.domain.vo.result.PageResult;
|
||||
|
@ -47,6 +48,13 @@ public class UserRoleController {
|
|||
return Result.success(pageResult);
|
||||
}
|
||||
|
||||
@Operation(summary = "根据用户id获取当前用户角色列表", description = "根据用户id获取当前用户角色列表")
|
||||
@GetMapping("roles")
|
||||
public Result<List<UserRoleVo>> getRoleListByUserId(Long userId) {
|
||||
List<UserRoleVo> voList = userRoleService.getRoleListByUserId(userId);
|
||||
return Result.success(voList);
|
||||
}
|
||||
|
||||
@Operation(summary = "添加用户角色关联表", description = "添加用户角色关联表")
|
||||
@PostMapping()
|
||||
public Result<String> addUserRole(@Valid @RequestBody UserRoleDto dto) {
|
||||
|
@ -54,6 +62,13 @@ public class UserRoleController {
|
|||
return Result.success(ResultCodeEnum.ADD_SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "为用户分配角色id", description = "根据用户id分配用户角色")
|
||||
@PostMapping("assign-role")
|
||||
public Result<String> assignUserRole(@Valid @RequestBody AssignUserRoleDto dto) {
|
||||
userRoleService.assignUserRole(dto);
|
||||
return Result.success(ResultCodeEnum.SUCCESS);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新用户角色关联表", description = "更新用户角色关联表")
|
||||
@PutMapping()
|
||||
public Result<String> updateUserRole(@Valid @RequestBody UserRoleDto dto) {
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
package com.spring.step2.domain.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
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 = "AssignUserRoleDTO对象", title = "用户分配角色DTO", description = "根据用户id分配用户角色")
|
||||
public class AssignUserRoleDto {
|
||||
|
||||
@Schema(name = "userId", title = "用户ID")
|
||||
@NotNull(message = "用户id为空")
|
||||
private Long userId;
|
||||
|
||||
@Schema(name = "roleId", title = "角色ID")
|
||||
@NotEmpty(message = "角色ID为空")
|
||||
private List<Long> roleIds;
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.spring.step2.domain.dto;
|
||||
package com.spring.step2.domain.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
|
@ -1,4 +1,4 @@
|
|||
package com.spring.step2.domain.dto;
|
||||
package com.spring.step2.domain.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -6,8 +6,6 @@ import lombok.Builder;
|
|||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
|
@ -24,19 +22,4 @@ public class UserRoleDto {
|
|||
@Schema(name = "userId", title = "用户ID")
|
||||
private Long userId;
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
@Schema(name = "isDeleted", title = "是否删除:0-未删除,1-已删除")
|
||||
private Boolean isDeleted;
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.spring.step2.domain.entity;
|
|||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.spring.step2.domain.entity.base.BaseEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.spring.step2.domain.entity;
|
|||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.spring.step2.domain.entity.base.BaseEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.spring.step2.domain.entity;
|
|||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.spring.step2.domain.entity.base.BaseEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.spring.step2.domain.entity;
|
|||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.spring.step2.domain.entity.base.BaseEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
|
@ -2,6 +2,7 @@ package com.spring.step2.domain.entity;
|
|||
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.spring.step2.domain.entity.base.BaseEntity;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package com.spring.step2.domain.entity;
|
||||
package com.spring.step2.domain.entity.base;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
|
@ -1,19 +1,25 @@
|
|||
package com.spring.step2.domain.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.spring.step2.domain.vo.base.BaseVo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(name = "PermissionVO对象", title = "系统权限表", description = "系统权限表的VO对象")
|
||||
public class PermissionVo {
|
||||
public class PermissionVo extends BaseVo {
|
||||
|
||||
@Schema(name = "id", title = "主键ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
@Schema(name = "permissionCode", title = "权限编码")
|
||||
|
@ -25,20 +31,5 @@ public class PermissionVo {
|
|||
@Schema(name = "remark", title = "备注信息")
|
||||
private String remark;
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
@Schema(name = "isDeleted", title = "是否删除:0-未删除,1-已删除")
|
||||
private Boolean isDeleted;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +1,25 @@
|
|||
package com.spring.step2.domain.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.spring.step2.domain.vo.base.BaseVo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(name = "RolePermissionVO对象", title = "角色权限关联表", description = "角色权限关联表的VO对象")
|
||||
public class RolePermissionVo {
|
||||
public class RolePermissionVo extends BaseVo {
|
||||
|
||||
@Schema(name = "id", title = "主键ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
@Schema(name = "roleId", title = "角色ID")
|
||||
|
@ -22,20 +28,5 @@ public class RolePermissionVo {
|
|||
@Schema(name = "permissionId", title = "权限ID")
|
||||
private Long permissionId;
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
@Schema(name = "isDeleted", title = "是否删除:0-未删除,1-已删除")
|
||||
private Boolean isDeleted;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3,18 +3,19 @@ package com.spring.step2.domain.vo;
|
|||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.spring.step2.domain.vo.base.BaseVo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(name = "RoleVO对象", title = "系统角色表", description = "系统角色表的VO对象")
|
||||
public class RoleVo {
|
||||
public class RoleVo extends BaseVo {
|
||||
|
||||
@Schema(name = "id", title = "主键ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
|
@ -30,20 +31,5 @@ public class RoleVo {
|
|||
@Schema(name = "remark", title = "备注信息")
|
||||
private String remark;
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
@Schema(name = "isDeleted", title = "是否删除:0-未删除,1-已删除")
|
||||
private Boolean isDeleted;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,41 +1,36 @@
|
|||
package com.spring.step2.domain.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.spring.step2.domain.vo.base.BaseVo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(name = "UserRoleVO对象", title = "用户角色关联表", description = "用户角色关联表的VO对象")
|
||||
public class UserRoleVo {
|
||||
public class UserRoleVo extends BaseVo {
|
||||
|
||||
@Schema(name = "id", title = "主键")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long id;
|
||||
|
||||
@Schema(name = "roleId", title = "角色ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long roleId;
|
||||
|
||||
@Schema(name = "userId", title = "用户ID")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
@JsonSerialize(using = ToStringSerializer.class)
|
||||
private Long userId;
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
@Schema(name = "isDeleted", title = "是否删除:0-未删除,1-已删除")
|
||||
private Boolean isDeleted;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3,18 +3,19 @@ package com.spring.step2.domain.vo;
|
|||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
||||
import com.spring.step2.domain.vo.base.BaseVo;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Schema(name = "UserVO对象", title = "用户基本信息表", description = "用户基本信息表的VO对象")
|
||||
public class UserVo {
|
||||
public class UserVo extends BaseVo {
|
||||
|
||||
@Schema(name = "id", title = "主键")
|
||||
@JsonFormat(shape = JsonFormat.Shape.STRING)
|
||||
|
@ -27,17 +28,5 @@ public class UserVo {
|
|||
@Schema(name = "email", title = "邮箱")
|
||||
private String email;
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.spring.step2.domain.vo.base;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class BaseVo {
|
||||
|
||||
@Schema(name = "createTime", title = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
@Schema(name = "updateTime", title = "更新时间")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@Schema(name = "createUser", title = "创建用户ID")
|
||||
private Long createUser;
|
||||
|
||||
@Schema(name = "updateUser", title = "更新用户ID")
|
||||
private Long updateUser;
|
||||
|
||||
}
|
|
@ -13,7 +13,7 @@ public enum ResultCodeEnum {
|
|||
ADD_SUCCESS(200, "添加成功"),
|
||||
UPDATE_SUCCESS(200, "修改成功"),
|
||||
DELETE_SUCCESS(200, "删除成功"),
|
||||
SORT_SUCCESS(200, "排序成功"),
|
||||
ASSIGN_SUCCESS(200, "排序成功"),
|
||||
SUCCESS_UPLOAD(200, "上传成功"),
|
||||
SUCCESS_LOGOUT(200, "退出成功"),
|
||||
EMAIL_CODE_REFRESH(200, "邮箱验证码已刷新"),
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.spring.step2.mapper;
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.spring.step2.domain.dto.UserDto;
|
||||
import com.spring.step2.domain.dto.user.UserDto;
|
||||
import com.spring.step2.domain.entity.PermissionEntity;
|
||||
import com.spring.step2.domain.entity.UserEntity;
|
||||
import com.spring.step2.domain.vo.UserVo;
|
||||
|
|
|
@ -3,12 +3,15 @@ package com.spring.step2.mapper;
|
|||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.spring.step2.domain.dto.UserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.AssignUserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.UserRoleDto;
|
||||
import com.spring.step2.domain.entity.UserRoleEntity;
|
||||
import com.spring.step2.domain.vo.UserRoleVo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户角色关联表 Mapper 接口
|
||||
|
@ -29,4 +32,19 @@ public interface UserRoleMapper extends BaseMapper<UserRoleEntity> {
|
|||
*/
|
||||
IPage<UserRoleVo> selectListByPage(@Param("page") Page<UserRoleEntity> pageParams, @Param("dto") UserRoleDto dto);
|
||||
|
||||
/**
|
||||
* 根据用户id获取当前用户角色列表
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 用户和角色列表
|
||||
*/
|
||||
List<UserRoleEntity> getRoleListByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据用户id删除用户相关分配角色
|
||||
*
|
||||
* @param dto 用户分配角色DTO {@link AssignUserRoleDto}
|
||||
*/
|
||||
void deleteByUserId(AssignUserRoleDto dto);
|
||||
|
||||
}
|
||||
|
|
|
@ -47,4 +47,12 @@ public interface RoleService extends IService<RoleEntity> {
|
|||
* @param ids 删除id列表
|
||||
*/
|
||||
void deleteRole(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 获取全部角色列表
|
||||
*
|
||||
* @return 角色列表
|
||||
*/
|
||||
List<RoleVo> getRoleList();
|
||||
|
||||
}
|
||||
|
|
|
@ -2,10 +2,12 @@ package com.spring.step2.service;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.spring.step2.domain.dto.UserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.AssignUserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.UserRoleDto;
|
||||
import com.spring.step2.domain.entity.UserRoleEntity;
|
||||
import com.spring.step2.domain.vo.UserRoleVo;
|
||||
import com.spring.step2.domain.vo.result.PageResult;
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -46,4 +48,19 @@ public interface UserRoleService extends IService<UserRoleEntity> {
|
|||
* @param ids 删除id列表
|
||||
*/
|
||||
void deleteUserRole(List<Long> ids);
|
||||
|
||||
/**
|
||||
* 根据用户id获取当前用户角色列表
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 用户和角色列表
|
||||
*/
|
||||
List<UserRoleVo> getRoleListByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据用户id分配用户角色
|
||||
*
|
||||
* @param dto 用户分配角色DTO {@link AssignUserRoleDto}
|
||||
*/
|
||||
void assignUserRole(@Valid AssignUserRoleDto dto);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.spring.step2.service;
|
|||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.spring.step2.domain.dto.UserDto;
|
||||
import com.spring.step2.domain.dto.user.UserDto;
|
||||
import com.spring.step2.domain.entity.UserEntity;
|
||||
import com.spring.step2.domain.vo.UserVo;
|
||||
import com.spring.step2.domain.vo.result.PageResult;
|
||||
|
|
|
@ -84,4 +84,20 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> impleme
|
|||
public void deleteRole(List<Long> ids) {
|
||||
removeByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取全部角色列表
|
||||
*
|
||||
* @return 角色列表
|
||||
*/
|
||||
@Override
|
||||
public List<RoleVo> getRoleList() {
|
||||
return list().stream()
|
||||
.map(roleEntity -> {
|
||||
RoleVo roleVo = new RoleVo();
|
||||
BeanUtils.copyProperties(roleEntity, roleVo);
|
||||
return roleVo;
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
}
|
|
@ -4,7 +4,8 @@ import com.baomidou.dynamic.datasource.annotation.DS;
|
|||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.spring.step2.domain.dto.UserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.AssignUserRoleDto;
|
||||
import com.spring.step2.domain.dto.user.UserRoleDto;
|
||||
import com.spring.step2.domain.entity.UserRoleEntity;
|
||||
import com.spring.step2.domain.vo.UserRoleVo;
|
||||
import com.spring.step2.domain.vo.result.PageResult;
|
||||
|
@ -84,4 +85,44 @@ public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRoleEnt
|
|||
public void deleteUserRole(List<Long> ids) {
|
||||
removeByIds(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户id获取当前用户角色列表
|
||||
*
|
||||
* @param userId 用户id
|
||||
* @return 用户和角色列表
|
||||
*/
|
||||
@Override
|
||||
public List<UserRoleVo> getRoleListByUserId(Long userId) {
|
||||
List<UserRoleEntity> userRoleEntityList = baseMapper.getRoleListByUserId(userId);
|
||||
return userRoleEntityList.stream().map(userRoleEntity -> {
|
||||
UserRoleVo userRoleVo = new UserRoleVo();
|
||||
BeanUtils.copyProperties(userRoleEntity, userRoleVo);
|
||||
return userRoleVo;
|
||||
})
|
||||
.toList();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户id分配用户角色
|
||||
*
|
||||
* @param dto 用户分配角色DTO {@link AssignUserRoleDto}
|
||||
*/
|
||||
@Override
|
||||
public void assignUserRole(AssignUserRoleDto dto) {
|
||||
Long userId = dto.getUserId();
|
||||
List<Long> roleIds = dto.getRoleIds();
|
||||
|
||||
// 先删除已经分配的角色
|
||||
baseMapper.deleteByUserId(dto);
|
||||
|
||||
// 为用户分配角色
|
||||
List<UserRoleEntity> entityList = roleIds.stream().map(roleId -> {
|
||||
UserRoleEntity userRoleEntity = new UserRoleEntity();
|
||||
userRoleEntity.setUserId(userId);
|
||||
userRoleEntity.setRoleId(roleId);
|
||||
return userRoleEntity;
|
||||
}).toList();
|
||||
saveBatch(entityList);
|
||||
}
|
||||
}
|
|
@ -4,7 +4,7 @@ import com.baomidou.dynamic.datasource.annotation.DS;
|
|||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.spring.step2.domain.dto.UserDto;
|
||||
import com.spring.step2.domain.dto.user.UserDto;
|
||||
import com.spring.step2.domain.entity.UserEntity;
|
||||
import com.spring.step2.domain.vo.UserVo;
|
||||
import com.spring.step2.domain.vo.result.PageResult;
|
||||
|
|
|
@ -19,6 +19,13 @@
|
|||
id,role_id,user_id,create_time,update_time,create_user,update_user,is_deleted
|
||||
</sql>
|
||||
|
||||
<!-- 根据用户id删除用户相关分配角色 -->
|
||||
<delete id="deleteByUserId">
|
||||
delete
|
||||
from t_user_role
|
||||
where user_id = #{userId}
|
||||
</delete>
|
||||
|
||||
<!-- 分页查询用户角色关联表内容 -->
|
||||
<select id="selectListByPage" resultType="com.spring.step2.domain.vo.UserRoleVo">
|
||||
select
|
||||
|
@ -29,4 +36,16 @@
|
|||
</where>
|
||||
</select>
|
||||
|
||||
<!-- 根据用户id获取当前用户角色列表 -->
|
||||
<select id="getRoleListByUserId" resultType="com.spring.step2.domain.entity.UserRoleEntity">
|
||||
select *
|
||||
from t_user_role
|
||||
<where>
|
||||
<if test="userId != null">
|
||||
user_id = #{userId}
|
||||
</if>
|
||||
</where>
|
||||
order by create_time desc
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
const AssignRoles = defineComponent({
|
||||
name: "AssignRoles",
|
||||
template: `
|
||||
<div class="offcanvas offcanvas-start" data-bs-scroll="true" id="assignRoleOffCanvas"
|
||||
aria-labelledby="assignRoleOffCanvasLabel" ref="assignRoleOffCanvasRef">
|
||||
<div class="offcanvas-header">
|
||||
<h5 class="offcanvas-title" id="assignRoleOffCanvasLabel">
|
||||
为用户分配角色
|
||||
<a href="JavaScript:" class="icon-link icon-link-hover text-decoration-none" @click="onSave">
|
||||
<i class="fas fa-save"></i>
|
||||
保存
|
||||
</a>
|
||||
</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="offcanvas" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="offcanvas-body">
|
||||
<div class="form-check" v-for="(role,index) in roleList" :key="role.id">
|
||||
<input class="form-check-input" type="checkbox" v-model="role.checked" :id="role.roleCode">
|
||||
<label class="form-check-label" :for="role.roleCode">
|
||||
{{role.remark}}
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`,
|
||||
props: {
|
||||
userinfo: {type: Object, required: true},
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
// 所有的角色列表
|
||||
roleList: ref([]),
|
||||
// 用户角色列表
|
||||
userRoleIds: ref([]),
|
||||
modalInstance: ref(null)
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
/* 获取角色列表 */
|
||||
async getRoleList() {
|
||||
const {data} = await axiosInstance.get("/role/all");
|
||||
this.roleList = data;
|
||||
},
|
||||
|
||||
/* 保存分配用户角色 */
|
||||
async onSave() {
|
||||
// 过滤出已经被选择的角色
|
||||
const checkedRoleList = this.roleList
|
||||
.filter(role => role.checked)
|
||||
.map(role => role.id);
|
||||
|
||||
// 分配的数据内容
|
||||
const data = {
|
||||
userId: this.userinfo.id,
|
||||
roleIds: checkedRoleList,
|
||||
}
|
||||
|
||||
// 为用户分配角色
|
||||
const {code, message} = await axiosInstance.post("/user-role/assign-role", data)
|
||||
if (code === 200) {
|
||||
this.modalInstance.hide();
|
||||
this.userRoleIds = [];
|
||||
antd.message.success(message);
|
||||
}
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
/* 监视用户信息 */
|
||||
async userinfo(value) {
|
||||
const {id} = value;
|
||||
// 如果没有id直接返回
|
||||
if (!id) return;
|
||||
console.log(id)
|
||||
// 获取角色列表
|
||||
await this.getRoleList();
|
||||
|
||||
// 获取用户拥有的角色
|
||||
const {data} = await axiosInstance.get("/user-role/roles", {params: {userId: id}});
|
||||
// 提取用户拥有的角色ID数组
|
||||
const userRoleIds = data.map(role => role.roleId + "");
|
||||
|
||||
// 遍历所有角色,检查用户是否拥有该角色
|
||||
this.roleList.forEach(role => {
|
||||
const hasRole = userRoleIds.includes(role.id);
|
||||
if (hasRole) role.checked = true;
|
||||
})
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
// 初始化模态框实例
|
||||
const modalEl = this.$refs.assignRoleOffCanvasRef;
|
||||
this.modalInstance = new bootstrap.Offcanvas(modalEl);
|
||||
}
|
||||
});
|
|
@ -378,10 +378,9 @@
|
|||
<span>Spring Security 6</span>
|
||||
</div>
|
||||
<ul class="nav-links">
|
||||
<li><a href="#features" target="_blank">特性</a></li>
|
||||
<li><a href="#features">特性</a></li>
|
||||
<li><a href="#docs" target="_blank">文档</a></li>
|
||||
<li><a href="/doc.html" target="_blank">API 文档</a></li>
|
||||
<li><a href="/swagger-ui/index.html" target="_blank">Swagger UI</a></li>
|
||||
<li><a href="/user" target="_blank">用户管理</a></li>
|
||||
<li><a href="/login" target="_blank">登录</a></li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
|
|
@ -35,7 +35,12 @@
|
|||
<div class="container-fluid" id="app">
|
||||
<dialog-user :is-add="dialogFormFlag" :on-search="onSearch" :userinfo="userinfo"></dialog-user>
|
||||
|
||||
<!-- 头部导航 -->
|
||||
<header-navs></header-navs>
|
||||
|
||||
<!-- 分配角色抽屉 -->
|
||||
<assign-roles :userinfo="userinfo"></assign-roles>
|
||||
|
||||
<!-- 头部 -->
|
||||
<div class="card">
|
||||
<div class="card-header d-flex justify-content-between align-items-center">
|
||||
|
@ -103,6 +108,10 @@
|
|||
data-bs-target="#userBackdrop" data-bs-toggle="modal">
|
||||
<i class="fas fa-edit"></i> 修改
|
||||
</button>
|
||||
<button @click="onAssignRoles(user)" class="btn btn-outline-primary btn-action"
|
||||
data-bs-target="#assignRoleOffCanvas" data-bs-toggle="offcanvas">
|
||||
<i class="fas fa-cloud"></i> 分配角色
|
||||
</button>
|
||||
<button @click="onDeleted(user)" class="btn btn-outline-danger btn-action">
|
||||
<i class="fas fa-trash"></i> 删除
|
||||
</button>
|
||||
|
@ -130,6 +139,8 @@
|
|||
<script th:src="@{/src/components/Pagination.js}"></script>
|
||||
<!-- 用户表单 -->
|
||||
<script th:src="@{/src/views/user/DialogUser.js}"></script>
|
||||
<!-- 分配角色 -->
|
||||
<script th:src="@{/src/views/user/AssignRoles.js}"></script>
|
||||
<script>
|
||||
const {createApp, ref} = Vue;
|
||||
|
||||
|
@ -193,6 +204,11 @@
|
|||
this.userinfo = user;
|
||||
},
|
||||
|
||||
/* 为用户分配角色 */
|
||||
onAssignRoles(user) {
|
||||
this.userinfo = user;
|
||||
},
|
||||
|
||||
/* 删除 */
|
||||
async onDeleted(user) {
|
||||
const result = confirm("确认删除?");
|
||||
|
@ -216,6 +232,7 @@
|
|||
app.component('HeaderNavs', HeaderNavs);
|
||||
app.component('Pagination', Pagination);
|
||||
app.component('DialogUser', DialogUser);
|
||||
app.component('AssignRoles', AssignRoles);
|
||||
app.mount('#app');
|
||||
</script>
|
||||
</html>
|
Loading…
Reference in New Issue