From 9c78ed62d00f09d00f1ba0f213d4cc869d9771e4 Mon Sep 17 00:00:00 2001 From: zhuyijun Date: Sat, 24 Sep 2022 23:17:21 +0800 Subject: [PATCH] =?UTF-8?q?rbac=E7=94=A8=E6=88=B7=E8=A7=92=E8=89=B2?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=8E=A7=E5=88=B6=E6=8E=A5=E5=8F=A3=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/zyjblogs/server/user/po/UserPo.java | 7 +- .../PermissionResourcesController.java | 86 ++++++++++ .../authority/dto/PermissionResourcesDto.java | 122 ++++++++++++++ .../mapper/PermissionResourcesMapper.java | 12 ++ .../authority/po/PermissionResourcesPo.java | 121 ++++++++++++++ .../service/PermissionResourcesService.java | 75 +++++++++ .../impl/PermissionResourcesServiceImpl.java | 153 ++++++++++++++++++ .../authority/vo/PermissionResourcesVo.java | 115 +++++++++++++ .../role/controller/RoleController.java | 35 ++++ .../RolePermissionRelController.java | 147 +++++++++++++++++ .../role/mapper/RolePermissionRelMapper.java | 34 ++++ .../cn/zyjblogs/server/role/po/RoleDto.java | 63 ++++++++ .../server/role/po/RolePermissionRelPo.java | 49 ++++++ .../cn/zyjblogs/server/role/po/RolePo.java | 7 +- .../service/RolePermissionRelService.java | 77 +++++++++ .../server/role/service/RoleService.java | 17 ++ .../impl/RolePermissionRelServiceImpl.java | 138 ++++++++++++++++ .../role/service/impl/RoleServiceImpl.java | 53 ++++++ .../server/role/vo/RolePermissionRelVo.java | 47 ++++++ .../cn/zyjblogs/server/role/vo/RoleVo.java | 7 +- .../cn/zyjblogs/server/user/dto/UserDto.java | 3 + .../cn/zyjblogs/server/user/po/UserPo.java | 7 +- .../mysql/role/RolePermissionRelMapper.xml | 25 +++ .../entity/constant/LengthConstant.java | 2 +- .../exception/CommonBusinessException.java | 6 +- 25 files changed, 1392 insertions(+), 16 deletions(-) create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/controller/PermissionResourcesController.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/dto/PermissionResourcesDto.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/mapper/PermissionResourcesMapper.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/po/PermissionResourcesPo.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/PermissionResourcesService.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/impl/PermissionResourcesServiceImpl.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/vo/PermissionResourcesVo.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RolePermissionRelController.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/mapper/RolePermissionRelMapper.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RoleDto.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePermissionRelPo.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RolePermissionRelService.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RolePermissionRelServiceImpl.java create mode 100644 server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RolePermissionRelVo.java create mode 100644 server/zyjblogs-rbac/src/main/resources/mapper/mysql/role/RolePermissionRelMapper.xml diff --git a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/po/UserPo.java b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/po/UserPo.java index c81d609..28d4e7d 100644 --- a/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/po/UserPo.java +++ b/server/zyjblogs-oauth/src/main/java/cn/zyjblogs/server/user/po/UserPo.java @@ -1,10 +1,11 @@ package cn.zyjblogs.server.user.po; +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import lombok.AllArgsConstructor; @@ -65,7 +66,7 @@ public class UserPo implements Serializable { private String createUserId; @JsonSerialize(using = LocalDateTimeSerializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) @TableField("create_time") private LocalDateTime createTime; @@ -74,7 +75,7 @@ public class UserPo implements Serializable { @JsonSerialize(using = LocalDateTimeSerializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) @TableField("edit_time") private LocalDateTime editTime; diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/controller/PermissionResourcesController.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/controller/PermissionResourcesController.java new file mode 100644 index 0000000..77ba837 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/controller/PermissionResourcesController.java @@ -0,0 +1,86 @@ +package cn.zyjblogs.server.authority.controller; + +import cn.zyjblogs.server.authority.dto.PermissionResourcesDto; +import cn.zyjblogs.server.authority.service.PermissionResourcesService; +import cn.zyjblogs.server.authority.vo.PermissionResourcesVo; +import cn.zyjblogs.starter.common.entity.context.BaseContext; +import cn.zyjblogs.starter.common.entity.response.ResponseObject; +import cn.zyjblogs.starter.common.entity.response.ResponseResult; +import cn.zyjblogs.starter.web.apiversion.ApiVersion; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +/** + * @author zhuyijun + */ +@Api(value = "权限资源控制层") +@RequestMapping("/{v}/menu") +@RestController +@ResponseBody +@ApiVersion(1) +@RequiredArgsConstructor +public class PermissionResourcesController { + private final PermissionResourcesService permissionResourcesService; + + @ApiOperation(value = "新增权限组", notes = "新增权限组") + @PutMapping("/save") + @ApiVersion(1) + public ResponseObject save(@Validated @RequestBody PermissionResourcesDto dto) { + permissionResourcesService.save(dto); + return ResponseResult.success(); + } + + + @ApiOperation(value = "修改权限组", notes = "修改权限组") + @PutMapping("/update") + @ApiVersion(1) + public ResponseObject update(@Validated @RequestBody PermissionResourcesDto dto) { + permissionResourcesService.update(dto); + return ResponseResult.success(); + } + + + @ApiOperation(value = "删除权限组", notes = "删除权限组") + @DeleteMapping("/delete") + @ApiVersion(1) + public ResponseObject deleteById(@RequestParam("id") String id) { + permissionResourcesService.removeById(id); + return ResponseResult.success(); + } + + + @ApiOperation(value = "查询所有权限组", notes = "查询所有权限组") + @GetMapping("/list/all") + @ApiVersion(1) + public ResponseObject> listAll() { + return ResponseResult.success(permissionResourcesService.listVoInTenant()); + } + + + @ApiOperation(value = "查询用户权限组", notes = "查询用户权限组") + @GetMapping("/list/userId") + @ApiVersion(1) + public ResponseObject> listUser() { + return ResponseResult.success(permissionResourcesService.listByUserId(BaseContext.getUserId())); + } + + @ApiOperation(value = "查询用户权限组", notes = "查询用户权限组") + @GetMapping("/list/roleId") + @ApiVersion(1) + public ResponseObject> listRoleId(String roleId) { + return ResponseResult.success(permissionResourcesService.listRoleId(roleId)); + } + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/dto/PermissionResourcesDto.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/dto/PermissionResourcesDto.java new file mode 100644 index 0000000..135a612 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/dto/PermissionResourcesDto.java @@ -0,0 +1,122 @@ +package cn.zyjblogs.server.authority.dto; + +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * @author zhuyijun + */ +@ApiModel(description = "权限资源dto对象") +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Data +public class PermissionResourcesDto implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键,uuid + */ + @ApiModelProperty(value = "主键id") + private String id; + /** + * 权限资源编码 + */ + @ApiModelProperty(value = "编码") + private String code; + + /** + * 名称 + */ + @ApiModelProperty(value = "名称") + private String name; + + + /** + * 父记录主键,uuid + */ + @ApiModelProperty(value = "父级节点id") + private String parentId; + /** + * 资源类型 0页面 1 按钮 + */ + @ApiModelProperty(value = "资源类型 0 页面 1 按钮") + private Integer resourceType; + + /** + * 描述 + */ + @ApiModelProperty(value = "描述") + private String description; + + /** + * 前端是否可见:0 隐藏,1 显示 + */ + @ApiModelProperty(value = "前端是否可见 0 显示 1 隐藏") + private Integer uiShow; + + /** + * 前端用路径 + */ + @ApiModelProperty(value = "前端用路径") + private String uiPath; + + /** + * 前端用编码 + */ + @ApiModelProperty(value = "前端用编码") + private String uiCode; + + /** + * 前端用排序 + */ + @ApiModelProperty(value = "前端用排序") + private Integer uiSort; + + /** + * 前端用模板 + */ + @ApiModelProperty(value = "前端模板") + private String uiTemplate; + /** + * 前端面包屑,1显示 0不显示 + */ + @ApiModelProperty(value = "前端面包屑,1显示 0不显示") + private Integer uiIsShowBreadcrumb; + + /** + * 创建时间 + */ + @ApiModelProperty(value = "创建时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JsonDeserialize(using = LocalDateTimeDeserializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = CommonConstant.DATETIME_PATTERN) + private LocalDateTime createTime; + + /** + * 最后修改的时间 + */ + @ApiModelProperty(value = "最后修改的时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JsonDeserialize(using = LocalDateTimeDeserializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = CommonConstant.DATETIME_PATTERN) + private LocalDateTime editTime; + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/mapper/PermissionResourcesMapper.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/mapper/PermissionResourcesMapper.java new file mode 100644 index 0000000..b6cf664 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/mapper/PermissionResourcesMapper.java @@ -0,0 +1,12 @@ +package cn.zyjblogs.server.authority.mapper; + +import cn.zyjblogs.server.authority.po.PermissionResourcesPo; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; + +/** + * @author zhuyijun + */ +@Mapper +public interface PermissionResourcesMapper extends BaseMapper { +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/po/PermissionResourcesPo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/po/PermissionResourcesPo.java new file mode 100644 index 0000000..7dcefe4 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/po/PermissionResourcesPo.java @@ -0,0 +1,121 @@ +package cn.zyjblogs.server.authority.po; + +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * @author zhuyijun + */ +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Data +@TableName("permission_resources") +public class PermissionResourcesPo implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键,uuid + */ + @TableId(value = "id", type = IdType.ASSIGN_UUID) + private String id; + /** + * 权限资源编码 + */ + @TableField("code") + private String code; + + /** + * 名称 + */ + @TableField("name") + private String name; + + + /** + * 父记录主键,uuid + */ + @TableField("parent_id") + private String parentId; + /** + * 资源类型 0页面 1 按钮 + */ + @TableField("resource_type") + private Integer resourceType; + + /** + * 描述 + */ + @TableField("description") + private String description; + + /** + * 前端是否可见:0 隐藏,1 显示 + */ + @TableField("ui_show") + private Integer uiShow; + + /** + * 前端用路径 + */ + @TableField("ui_path") + private String uiPath; + + /** + * 前端用编码 + */ + @TableField("ui_code") + private String uiCode; + + /** + * 前端用排序 + */ + @TableField("ui_sort") + private Integer uiSort; + + /** + * 前端用模板 + */ + @TableField("ui_template") + private String uiTemplate; + /** + * 前端面包屑,1显示 0不显示 + */ + @TableField("ui_is_show_breadcrumb") + private Integer uiIsShowBreadcrumb; + + /** + * 创建时间 + */ + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JsonDeserialize(using = LocalDateTimeDeserializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + @TableField("create_time") + private LocalDateTime createTime; + + /** + * 最后修改的时间 + */ + @JSONField(format = CommonConstant.DATETIME_PATTERN) + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JsonDeserialize(using = LocalDateTimeDeserializer.class) + @TableField("edit_time") + private LocalDateTime editTime; + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/PermissionResourcesService.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/PermissionResourcesService.java new file mode 100644 index 0000000..68e17d4 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/PermissionResourcesService.java @@ -0,0 +1,75 @@ +package cn.zyjblogs.server.authority.service; + +import cn.zyjblogs.server.authority.dto.PermissionResourcesDto; +import cn.zyjblogs.server.authority.po.PermissionResourcesPo; +import cn.zyjblogs.server.authority.vo.PermissionResourcesVo; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.List; + +/** + * @author zhuyijun + */ +public interface PermissionResourcesService extends IService { + + /** + * 保存 + * + * @param dto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + Boolean save(PermissionResourcesDto dto); + + /** + * 更新 + * + * @param dto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + Boolean update(PermissionResourcesDto dto); + + /** + * 查询当前租户权限资源 + * + * @param + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + List listVoInTenant(); + + /** + * 通过用户id查询权限资源 + * + * @param userId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + List listByUserId(String userId); + + /** + * 通过角色id查询权限资源 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + List listRoleId(String roleId); + + /** + * 通过资源父级节点查询限资源 + * + * @param + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + List listVoInTenant(String parentId); + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/impl/PermissionResourcesServiceImpl.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/impl/PermissionResourcesServiceImpl.java new file mode 100644 index 0000000..f5f5e2d --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/service/impl/PermissionResourcesServiceImpl.java @@ -0,0 +1,153 @@ +package cn.zyjblogs.server.authority.service.impl; + +import cn.zyjblogs.server.authority.dto.PermissionResourcesDto; +import cn.zyjblogs.server.authority.mapper.PermissionResourcesMapper; +import cn.zyjblogs.server.authority.po.PermissionResourcesPo; +import cn.zyjblogs.server.authority.service.PermissionResourcesService; +import cn.zyjblogs.server.authority.vo.PermissionResourcesVo; +import cn.zyjblogs.server.role.service.RolePermissionRelService; +import cn.zyjblogs.server.user.service.UserRoleService; +import cn.zyjblogs.starter.common.entity.response.HttpCode; +import cn.zyjblogs.starter.common.exception.CommonBusinessException; +import cn.zyjblogs.starter.common.utils.bean.BeanUtils; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; + +/** + * @author zhuyijun + */ +@Service +@RequiredArgsConstructor +public class PermissionResourcesServiceImpl extends ServiceImpl implements PermissionResourcesService { + private final PermissionResourcesMapper permissionResourcesMapper; + private final UserRoleService userRoleService; + private final RolePermissionRelService rolePermissionRelService; + + /** + * 权限资源保存 + * + * @param dto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public Boolean save(PermissionResourcesDto dto) { + dto.setId(null); + String parentId = dto.getParentId(); + PermissionResourcesPo newParentPo = this.getById(parentId); + if (Objects.isNull(newParentPo)) { + throw new CommonBusinessException(HttpCode.NOT_FOUND, "该资源父级节点不存在"); + } + PermissionResourcesPo po = BeanUtils.map(dto, PermissionResourcesPo.class); + po.setCreateTime(LocalDateTime.now()); + po.setCode(IdWorker.get32UUID()); + return this.save(po); + } + + /** + * 权限资源更新 + * + * @param dto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Transactional(rollbackFor = Exception.class) + @Override + public Boolean update(PermissionResourcesDto dto) { + PermissionResourcesPo po = BeanUtils.map(dto, PermissionResourcesPo.class); + po.setEditTime(LocalDateTime.now()); + return this.updateById(po); + } + + /** + * 查询当前租户所有权限 + * + * @param + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + public List listVoInTenant() { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.orderByAsc(PermissionResourcesPo::getCreateTime, PermissionResourcesPo::getId); + List permissionResourcesPos = this.list(queryWrapper); + return BeanUtils.map(permissionResourcesPos, PermissionResourcesVo.class); + } + + /** + * 用过用户id查询 + * + * @param userId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + public List listByUserId(String userId) { + // 获取用户所有角色信息 + List roleIdList = userRoleService.findRoleListByUserId(userId); + if (CollectionUtils.isEmpty(roleIdList)) { + throw new CommonBusinessException(HttpCode.BAD_REQUEST, "未查询到该用户关联角色"); + } + List permissionIds = rolePermissionRelService.findListByRoleIds(roleIdList); + if (CollectionUtils.isEmpty(permissionIds)) { + throw new CommonBusinessException(HttpCode.BAD_REQUEST, "未查询到该用户角色权限"); + } + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.in(PermissionResourcesPo::getId, permissionIds) + .orderByAsc(PermissionResourcesPo::getCreateTime, PermissionResourcesPo::getId); + List permissionResourcesPos = this.list(queryWrapper); + return BeanUtils.map(permissionResourcesPos, PermissionResourcesVo.class); + } + + /** + * 通过角色id查询 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + public List listRoleId(String roleId) { + List permissionIds = rolePermissionRelService.findListByRoleIds(List.of(roleId)); + if (CollectionUtils.isEmpty(permissionIds)) { + throw new CommonBusinessException(HttpCode.BAD_REQUEST, "未查询到该用户角色权限"); + } + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.in(PermissionResourcesPo::getId, permissionIds) + .orderByAsc(PermissionResourcesPo::getCreateTime, PermissionResourcesPo::getId); + List permissionResourcesPos = this.list(queryWrapper); + return BeanUtils.map(permissionResourcesPos, PermissionResourcesVo.class); + } + + /** + * 通过父级节点查询下级组织 + * + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + public List listVoInTenant(String parentId) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.eq(PermissionResourcesPo::getParentId, parentId) + .orderByAsc(PermissionResourcesPo::getCreateTime, PermissionResourcesPo::getId); + List permissionResourcesPos = this.list(queryWrapper); + return BeanUtils.map(permissionResourcesPos, PermissionResourcesVo.class); + } +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/vo/PermissionResourcesVo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/vo/PermissionResourcesVo.java new file mode 100644 index 0000000..1d8fbdf --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/authority/vo/PermissionResourcesVo.java @@ -0,0 +1,115 @@ +package cn.zyjblogs.server.authority.vo; + +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.time.LocalDateTime; + +/** + * @author zhuyijun + */ +@ApiModel(description = "权限资源vo对象") +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Data +public class PermissionResourcesVo implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键,uuid + */ + @ApiModelProperty(value = "主键id") + private String id; + /** + * 权限资源编码 + */ + @ApiModelProperty(value = "编码") + private String code; + + /** + * 名称 + */ + @ApiModelProperty(value = "名称") + private String name; + + + /** + * 父记录主键,uuid + */ + @ApiModelProperty(value = "父级节点id") + private String parentId; + /** + * 资源类型 0页面 1 按钮 + */ + @ApiModelProperty(value = "资源类型 0 页面 1 按钮") + private Integer resourceType; + + /** + * 描述 + */ + @ApiModelProperty(value = "描述") + private String description; + + /** + * 前端是否可见:0 隐藏,1 显示 + */ + @ApiModelProperty(value = "前端是否可见 0 显示 1 隐藏") + private Integer uiShow; + + /** + * 前端用路径 + */ + @ApiModelProperty(value = "前端用路径") + private String uiPath; + + /** + * 前端用编码 + */ + @ApiModelProperty(value = "前端用编码") + private String uiCode; + + /** + * 前端用排序 + */ + @ApiModelProperty(value = "前端用排序") + private Integer uiSort; + + /** + * 前端用模板 + */ + @ApiModelProperty(value = "前端模板") + private String uiTemplate; + /** + * 前端面包屑,1显示 0不显示 + */ + @ApiModelProperty(value = "前端面包屑,1显示 0不显示") + private Integer uiIsShowBreadcrumb; + + /** + * 创建时间 + */ + @ApiModelProperty(value = "创建时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + private LocalDateTime createTime; + + /** + * 最后修改的时间 + */ + @ApiModelProperty(value = "最后修改的时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + private LocalDateTime editTime; + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RoleController.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RoleController.java index 3c43c3d..ef831c1 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RoleController.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RoleController.java @@ -1,6 +1,7 @@ package cn.zyjblogs.server.role.controller; import cn.zyjblogs.server.role.dto.RolePageDto; +import cn.zyjblogs.server.role.po.RoleDto; import cn.zyjblogs.server.role.po.RolePo; import cn.zyjblogs.server.role.service.RoleService; import cn.zyjblogs.server.role.vo.RoleVo; @@ -13,8 +14,10 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.RequiredArgsConstructor; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; @@ -92,4 +95,36 @@ public class RoleController { public ResponseObject> findByIds(List ids) { return ResponseResult.success(roleService.findByIds(ids)); } + + /** + * 角色保存 + * + * @param roleDto + * @return cn.zyjblogs.starter.common.entity.response.ResponseObject> + * @author zhuyijun + * @date 2022/9/19 上午1:14 + */ + @ApiOperation(value = "通过角色id查询", notes = "通过角色id查询") + @ApiVersion(1) + @PostMapping("/save") + public ResponseObject save(@Validated @RequestBody RoleDto roleDto) { + roleService.save(roleDto); + return ResponseResult.success(); + } + + /** + * 角色更新 + * + * @param roleDto + * @return cn.zyjblogs.starter.common.entity.response.ResponseObject> + * @author zhuyijun + * @date 2022/9/19 上午1:14 + */ + @ApiOperation(value = "通过角色id查询", notes = "通过角色id查询") + @ApiVersion(1) + @PostMapping("/update") + public ResponseObject update(@Validated @RequestBody RoleDto roleDto) { + roleService.update(roleDto); + return ResponseResult.success(); + } } diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RolePermissionRelController.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RolePermissionRelController.java new file mode 100644 index 0000000..17314c0 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/controller/RolePermissionRelController.java @@ -0,0 +1,147 @@ +package cn.zyjblogs.server.role.controller; + +import cn.zyjblogs.server.role.service.RolePermissionRelService; +import cn.zyjblogs.server.role.vo.RolePermissionRelVo; +import cn.zyjblogs.starter.common.entity.response.ResponseObject; +import cn.zyjblogs.starter.common.entity.response.ResponseResult; +import cn.zyjblogs.starter.web.apiversion.ApiVersion; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Collection; +import java.util.List; + +/** + * @author zhuyijun + */ +@Api(value = "角色权限关联控制层") +@RestController +@RequestMapping("/{v}/role/rel") +@ApiVersion(1) +@ResponseBody +@RequiredArgsConstructor +public class RolePermissionRelController { + private final RolePermissionRelService rolePermissionRelService; + + /** + * @param roleId 角色id + * @author zhuyijun + * @date 2022/9/24 上午11:56 + * return 权限id + */ + @ApiOperation(value = "通过角色id查询权限集合", notes = "通过角色id查询权限集合") + @ApiVersion(1) + @GetMapping("/findListByRoleId") + public ResponseObject> findListByRoleId(String roleId) { + return ResponseResult.success(rolePermissionRelService.findListByRoleId(roleId)); + } + + /** + * @param roleIds 角色id + * @author zhuyijun + * @date 2022/9/24 上午11:56 + * return 权限id + */ + @ApiOperation(value = "通过角色id集合查询权限id集合", notes = "通过角色id集合查询权限id集合") + @ApiVersion(1) + @PostMapping("/findListByRoleIds") + public ResponseObject> findListByRoleIds(List roleIds) { + return ResponseResult.success(rolePermissionRelService.findListByRoleIds(roleIds)); + } + + /** + * @param roleId 角色id + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + @ApiOperation(value = "通过角色id查询关联对象集合", notes = "通过角色id查询关联对象集合") + @ApiVersion(1) + @GetMapping("/findListVoByRoleId") + public ResponseObject> findListVoByRoleId(String roleId) { + return ResponseResult.success(rolePermissionRelService.findListVoByRoleId(roleId)); + } + + /** + * @param roleIds 角色id + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + @ApiOperation(value = "通过角色id集合查询关联对象集合", notes = "通过角色id集合查询关联对象集合") + @ApiVersion(1) + @PostMapping("/findListVoByRoleIds") + public ResponseObject> findListVoByRoleIds(List roleIds) { + return ResponseResult.success(rolePermissionRelService.findListVoByRoleIds(roleIds)); + } + + /** + * 通过角色id删除权限 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @ApiOperation(value = "通过角色id删除角色", notes = "通过角色id删除角色") + @ApiVersion(1) + @PostMapping("/deleteByRoleId") + public ResponseObject deleteByRoleId(String roleId) { + return ResponseResult.success(rolePermissionRelService.deleteByRoleId(roleId)); + } + + /** + * 通过角色id删除权限 + * + * @param roleIds + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @ApiOperation(value = "通过角色id集合删除角色", notes = "通过角色id集合删除角色") + @ApiVersion(1) + @PostMapping("/deleteByRoleIds") + public ResponseObject deleteByRoleIds(List roleIds) { + return ResponseResult.success(rolePermissionRelService.deleteByRoleIds(roleIds)); + } + + /** + * 保存或更新角色权限关联 + * + * @param roleId + * @param permissionIds + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @ApiOperation(value = "角色权限保存", notes = "角色权限关联保存") + @ApiVersion(1) + @PutMapping("/save") + public ResponseObject save(String roleId, Collection permissionIds) { + return ResponseResult.success(rolePermissionRelService.saveUpdate(roleId, permissionIds, false)); + } + + + /** + * 保存或更新角色权限关联 + * + * @param roleId + * @param permissionIds + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @ApiOperation(value = "角色权限更新", notes = "角色权限更新") + @ApiVersion(1) + @PutMapping("/update") + public ResponseObject update(String roleId, Collection permissionIds) { + return ResponseResult.success(rolePermissionRelService.saveUpdate(roleId, permissionIds, false)); + } +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/mapper/RolePermissionRelMapper.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/mapper/RolePermissionRelMapper.java new file mode 100644 index 0000000..2810ed6 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/mapper/RolePermissionRelMapper.java @@ -0,0 +1,34 @@ +package cn.zyjblogs.server.role.mapper; + +import cn.zyjblogs.server.role.po.RolePermissionRelPo; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +/** + * @author zhuyijun + */ +@Mapper +public interface RolePermissionRelMapper extends BaseMapper { + /** + * 通过角色id查询权限id集合 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + List findListByRoleId(@Param("roleId") String roleId, @Param("tenantId") String tenantId); + + /** + * 通过角色id集合查询权限id集合 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + List findListByRoleIds(@Param("roleIds") List roleId, @Param("tenantId") String tenantId); +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RoleDto.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RoleDto.java new file mode 100644 index 0000000..74271a6 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RoleDto.java @@ -0,0 +1,63 @@ +package cn.zyjblogs.server.role.po; + +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; +import java.time.LocalDateTime; +import java.util.List; + +/** + * @author zhuyijun + */ +@ApiModel(description = "角色实体类") +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class RoleDto implements Serializable { + @ApiModelProperty(value = "主键id") + private String id; + @ApiModelProperty(value = "名称") + @NotBlank(message = "角色名称不能为空") + private String name; + @ApiModelProperty(value = "状态") + private Integer status; + @ApiModelProperty(value = "类型") + private Integer roleType; + @ApiModelProperty(value = "是否删除") + private Integer deleted; + + @ApiModelProperty(value = "描述") + private String description; + + @ApiModelProperty(value = "创建人") + private String createUserId; + + @ApiModelProperty(value = "创建时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + private LocalDateTime createTime; + + @ApiModelProperty(value = "编辑人") + private String editUserId; + + @ApiModelProperty(value = "编辑时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) + @JSONField(format = CommonConstant.DATETIME_PATTERN) + private LocalDateTime editTime; + @ApiModelProperty(value = "租户id") + private String tenantId; + @ApiModelProperty(value = "权限集合") + private List permissionIds; + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePermissionRelPo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePermissionRelPo.java new file mode 100644 index 0000000..83c47a7 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePermissionRelPo.java @@ -0,0 +1,49 @@ +package cn.zyjblogs.server.role.po; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * @author zhuyijun + */ +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Data +@TableName("role_permission_rel") +public class RolePermissionRelPo implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键,uuid + */ + @TableId(value = "id", type = IdType.ASSIGN_UUID) + private String id; + + /** + * 角色表主键,uuid + */ + @TableField("role_id") + private String roleId; + /** + * 权限组表主键,uuid + */ + @TableField("permission_id") + private String permissionId; + + /** + * 租户id + */ + @TableField("tenant_id") + private String tenantId; + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePo.java index 5eb7f3b..5a50089 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePo.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/po/RolePo.java @@ -1,9 +1,10 @@ package cn.zyjblogs.server.role.po; +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; @@ -44,7 +45,7 @@ public class RolePo implements Serializable { @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) @TableField("create_time") private LocalDateTime createTime; @@ -53,7 +54,7 @@ public class RolePo implements Serializable { @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) @TableField("edit_time") private LocalDateTime editTime; diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RolePermissionRelService.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RolePermissionRelService.java new file mode 100644 index 0000000..ef20c8e --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RolePermissionRelService.java @@ -0,0 +1,77 @@ +package cn.zyjblogs.server.role.service; + +import cn.zyjblogs.server.role.po.RolePermissionRelPo; +import cn.zyjblogs.server.role.vo.RolePermissionRelVo; +import com.baomidou.mybatisplus.extension.service.IService; + +import java.util.Collection; +import java.util.List; + +/** + * @author zhuyijun + */ +public interface RolePermissionRelService extends IService { + /** + * @param roleId 角色id + * @author zhuyijun + * @date 2022/9/24 上午11:56 + * return 权限id + */ + List findListByRoleId(String roleId); + + /** + * @param roleIds 角色id + * @author zhuyijun + * @date 2022/9/24 上午11:56 + * return 权限id + */ + List findListByRoleIds(List roleIds); + + /** + * @param roleId 角色id + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + List findListVoByRoleId(String roleId); + + /** + * @param roleIds 角色id + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + List findListVoByRoleIds(List roleIds); + + /** + * 通过角色id删除权限 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + boolean deleteByRoleId(String roleId); + + /** + * 通过角色id集合删除权限 + * + * @param roleIds + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + boolean deleteByRoleIds(List roleIds); + + /** + * 保存或更新角色权限关联 + * + * @param roleId + * @param permissionIds + * @param needClear + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + boolean saveUpdate(String roleId, Collection permissionIds, boolean needClear); +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RoleService.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RoleService.java index c64e83e..83358b3 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RoleService.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/RoleService.java @@ -1,6 +1,7 @@ package cn.zyjblogs.server.role.service; import cn.zyjblogs.server.role.dto.RolePageDto; +import cn.zyjblogs.server.role.po.RoleDto; import cn.zyjblogs.server.role.po.RolePo; import cn.zyjblogs.server.role.vo.RoleVo; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -37,4 +38,20 @@ public interface RoleService extends IService { */ List findList(String tenantId); + /** + * @param roleDto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + void save(RoleDto roleDto); + + /** + * @param roleDto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + void update(RoleDto roleDto); + } diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RolePermissionRelServiceImpl.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RolePermissionRelServiceImpl.java new file mode 100644 index 0000000..5bd75c4 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RolePermissionRelServiceImpl.java @@ -0,0 +1,138 @@ +package cn.zyjblogs.server.role.service.impl; + +import cn.zyjblogs.server.role.mapper.RolePermissionRelMapper; +import cn.zyjblogs.server.role.po.RolePermissionRelPo; +import cn.zyjblogs.server.role.service.RolePermissionRelService; +import cn.zyjblogs.server.role.vo.RolePermissionRelVo; +import cn.zyjblogs.starter.common.entity.context.BaseContext; +import cn.zyjblogs.starter.common.utils.bean.BeanUtils; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +/** + * 角色权限关联业务层 + * + * @author zhuyijun + */ +@Service +@RequiredArgsConstructor +public class RolePermissionRelServiceImpl extends ServiceImpl implements RolePermissionRelService { + private final RolePermissionRelMapper rolePermissionRelMapper; + + /** + * @param roleId 角色id + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + @Override + public List findListByRoleId(String roleId) { + return rolePermissionRelMapper.findListByRoleId(roleId, BaseContext.getTenantId()); + } + + /** + * @param roleIds 角色id集合查询权限id集合 + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + @Override + public List findListByRoleIds(List roleIds) { + return rolePermissionRelMapper.findListByRoleIds(roleIds, BaseContext.getTenantId()); + } + + /** + * @param roleId 角色id + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + @Override + public List findListVoByRoleId(String roleId) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.eq(RolePermissionRelPo::getRoleId, roleId) + .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId()); + List rolePermissionRelPos = rolePermissionRelMapper.selectList(queryWrapper); + return BeanUtils.map(rolePermissionRelPos, RolePermissionRelVo.class); + } + + /** + * @param roleIds 角色id集合 + * @return 角色和权限关联表 + * @author zhuyijun + * @date 2022/9/24 上午11:56 + */ + @Override + public List findListVoByRoleIds(List roleIds) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.in(RolePermissionRelPo::getRoleId, roleIds) + .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId()); + List rolePermissionRelPos = rolePermissionRelMapper.selectList(queryWrapper); + return BeanUtils.map(rolePermissionRelPos, RolePermissionRelVo.class); + } + + /** + * 通过角色id删除 + * + * @param roleId + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public boolean deleteByRoleId(String roleId) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.eq(RolePermissionRelPo::getRoleId, roleId) + .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId()); + return rolePermissionRelMapper.delete(queryWrapper) > 0; + } + + @Override + @Transactional(rollbackFor = Exception.class) + public boolean deleteByRoleIds(List roleIds) { + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); + queryWrapper.in(RolePermissionRelPo::getRoleId, roleIds) + .eq(RolePermissionRelPo::getTenantId, BaseContext.getTenantId()); + return rolePermissionRelMapper.delete(queryWrapper) > 0; + } + + /** + * 保存或更新角色权限关联 + * + * @param roleId + * @param permissionIds + * @param needClear + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public boolean saveUpdate(String roleId, Collection permissionIds, boolean needClear) { + if (needClear) { + deleteByRoleId(roleId); + } + if (CollectionUtils.isEmpty(permissionIds)) { + return true; + } + String tenantId = BaseContext.getTenantId(); + List list = new ArrayList<>(permissionIds.size()); + permissionIds.forEach(permissionId -> list.add(RolePermissionRelPo.builder() + .roleId(roleId) + .permissionId(permissionId) + .tenantId(tenantId) + .build())); + return this.saveBatch(list); + } + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RoleServiceImpl.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RoleServiceImpl.java index 549fdd3..c49b427 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RoleServiceImpl.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/service/impl/RoleServiceImpl.java @@ -2,13 +2,18 @@ package cn.zyjblogs.server.role.service.impl; import cn.zyjblogs.server.role.dto.RolePageDto; import cn.zyjblogs.server.role.mapper.RoleMapper; +import cn.zyjblogs.server.role.po.RoleDto; import cn.zyjblogs.server.role.po.RolePo; +import cn.zyjblogs.server.role.service.RolePermissionRelService; import cn.zyjblogs.server.role.service.RoleService; import cn.zyjblogs.server.role.vo.RoleVo; import cn.zyjblogs.starter.common.entity.context.BaseContext; +import cn.zyjblogs.starter.common.entity.response.HttpCode; +import cn.zyjblogs.starter.common.exception.CommonBusinessException; import cn.zyjblogs.starter.common.utils.bean.BeanUtils; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -16,6 +21,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -27,6 +33,7 @@ import java.util.List; @RequiredArgsConstructor public class RoleServiceImpl extends ServiceImpl implements RoleService { private final RoleMapper roleMapper; + private final RolePermissionRelService rolePermissionRelService; /** * 分页查询 @@ -77,4 +84,50 @@ public class RoleServiceImpl extends ServiceImpl implements } return BeanUtils.map(rolePos, RoleVo.class); } + + /** + * 保存角色 + * + * @param roleDto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + public void save(RoleDto roleDto) { + List permissionIds = roleDto.getPermissionIds(); + if (CollectionUtils.isEmpty(permissionIds)) { + throw new CommonBusinessException(HttpCode.BAD_REQUEST, "角色未关联权限资源"); + } + roleDto.setPermissionIds(null); + RolePo rolePo = BeanUtils.map(roleDto, RolePo.class); + rolePo.setId(IdWorker.get32UUID()); + rolePo.setCreateTime(LocalDateTime.now()); + rolePo.setCreateUserId(BaseContext.getUserId()); + rolePo.setTenantId(BaseContext.getTenantId()); + roleMapper.insert(rolePo); + rolePermissionRelService.saveUpdate(rolePo.getId(), permissionIds, false); + } + + /** + * 更新角色 + * + * @param roleDto + * @return + * @author zhuyijun + * @date 2022/9/24 + */ + @Override + public void update(RoleDto roleDto) { + List permissionIds = roleDto.getPermissionIds(); + if (CollectionUtils.isEmpty(permissionIds)) { + throw new CommonBusinessException(HttpCode.BAD_REQUEST, "角色未关联权限资源"); + } + roleDto.setPermissionIds(null); + RolePo rolePo = BeanUtils.map(roleDto, RolePo.class); + rolePo.setEditTime(LocalDateTime.now()); + rolePo.setEditUserId(BaseContext.getUserId()); + roleMapper.updateById(rolePo); + rolePermissionRelService.saveUpdate(rolePo.getId(), permissionIds, true); + } } diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RolePermissionRelVo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RolePermissionRelVo.java new file mode 100644 index 0000000..ab092cd --- /dev/null +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RolePermissionRelVo.java @@ -0,0 +1,47 @@ +package cn.zyjblogs.server.role.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * @author zhuyijun + */ +@ApiModel(description = "角色权限关联表") +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Data +public class RolePermissionRelVo implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 主键,uuid + */ + @ApiModelProperty(value = "主键id") + private String id; + + /** + * 角色表主键,uuid + */ + @ApiModelProperty(value = "角色id") + private String roleId; + /** + * 权限资源主键,uuid + */ + @ApiModelProperty(value = "权限id") + private String permissionId; + + /** + * 租户id + */ + @ApiModelProperty(value = "租户id") + private String tenantId; + +} diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RoleVo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RoleVo.java index 80e39d5..54b804e 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RoleVo.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/role/vo/RoleVo.java @@ -1,6 +1,7 @@ package cn.zyjblogs.server.role.vo; -import com.fasterxml.jackson.annotation.JsonFormat; +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; import io.swagger.annotations.ApiModel; @@ -41,7 +42,7 @@ public class RoleVo implements Serializable { @ApiModelProperty(value = "创建时间") @JsonSerialize(using = LocalDateTimeSerializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) private LocalDateTime createTime; @ApiModelProperty(value = "编辑人") @@ -49,7 +50,7 @@ public class RoleVo implements Serializable { @ApiModelProperty(value = "编辑时间") @JsonSerialize(using = LocalDateTimeSerializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) private LocalDateTime editTime; @ApiModelProperty(value = "租户id") private String tenantId; diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/dto/UserDto.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/dto/UserDto.java index 8c5908e..6f49b24 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/dto/UserDto.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/dto/UserDto.java @@ -56,11 +56,13 @@ public class UserDto { @ApiModelProperty(value = "创建人") private String createUserId; @ApiModelProperty(value = "创建时间") + @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) @JSONField(format = CommonConstant.DATETIME_PATTERN) @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = CommonConstant.DATETIME_PATTERN) private LocalDateTime createTime; + @ApiModelProperty(value = "编辑人") private String editUserId; @@ -70,6 +72,7 @@ public class UserDto { @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) private LocalDateTime editTime; + @ApiModelProperty(value = "角色id集合") @NotNull(message = "角色不能为空") private Set roleIds; diff --git a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/po/UserPo.java b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/po/UserPo.java index d72900e..748a84c 100644 --- a/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/po/UserPo.java +++ b/server/zyjblogs-rbac/src/main/java/cn/zyjblogs/server/user/po/UserPo.java @@ -1,10 +1,11 @@ package cn.zyjblogs.server.user.po; +import cn.zyjblogs.starter.common.entity.constant.CommonConstant; +import com.alibaba.fastjson.annotation.JSONField; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; @@ -68,7 +69,7 @@ public class UserPo implements Serializable { @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) @TableField("create_time") private LocalDateTime createTime; @@ -77,7 +78,7 @@ public class UserPo implements Serializable { @JsonSerialize(using = LocalDateTimeSerializer.class) @JsonDeserialize(using = LocalDateTimeDeserializer.class) - @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JSONField(format = CommonConstant.DATETIME_PATTERN) @TableField("edit_time") private LocalDateTime editTime; diff --git a/server/zyjblogs-rbac/src/main/resources/mapper/mysql/role/RolePermissionRelMapper.xml b/server/zyjblogs-rbac/src/main/resources/mapper/mysql/role/RolePermissionRelMapper.xml new file mode 100644 index 0000000..c50d669 --- /dev/null +++ b/server/zyjblogs-rbac/src/main/resources/mapper/mysql/role/RolePermissionRelMapper.xml @@ -0,0 +1,25 @@ + + + + + + \ No newline at end of file diff --git a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/constant/LengthConstant.java b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/constant/LengthConstant.java index b4f1dde..185450c 100644 --- a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/constant/LengthConstant.java +++ b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/entity/constant/LengthConstant.java @@ -1,7 +1,7 @@ package cn.zyjblogs.starter.common.entity.constant; public class LengthConstant { - public static final int ID_SIZE = 32; + public static final int ID_SIZE = 64; public static final int MIN_SIZE = 1; public static final int SHORT_SIZE = 16; public static final int NORMAL_SIZE = 100; diff --git a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/exception/CommonBusinessException.java b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/exception/CommonBusinessException.java index 18efaee..89b2529 100644 --- a/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/exception/CommonBusinessException.java +++ b/stater/zyjblogs-common-spring-boot-starter/src/main/java/cn/zyjblogs/starter/common/exception/CommonBusinessException.java @@ -7,12 +7,12 @@ import cn.zyjblogs.starter.common.entity.response.HttpCode; * * @author zhuyijun */ -public class CommonBusinessExcetion extends AbstractBusinessException { - public CommonBusinessExcetion() { +public class CommonBusinessException extends AbstractBusinessException { + public CommonBusinessException() { super(); } - public CommonBusinessExcetion(HttpCode responseCode, String message) { + public CommonBusinessException(HttpCode responseCode, String message) { super(responseCode, message); } }