角色模块的 CRUD + 分配资源
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package cn.iocoder.mall.admin.convert;
|
||||
|
||||
import cn.iocoder.mall.admin.api.bo.RoleBO;
|
||||
import cn.iocoder.mall.admin.api.dto.RoleAddDTO;
|
||||
import cn.iocoder.mall.admin.api.dto.RoleUpdateDTO;
|
||||
import cn.iocoder.mall.admin.dataobject.RoleDO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappings;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Mapper
|
||||
public interface RoleConvert {
|
||||
|
||||
RoleConvert INSTANCE = Mappers.getMapper(RoleConvert.class);
|
||||
|
||||
@Mappings({})
|
||||
RoleDO convert(RoleAddDTO roleAddDTO);
|
||||
|
||||
@Mappings({})
|
||||
RoleDO convert(RoleUpdateDTO roleUpdateDTO);
|
||||
|
||||
@Mappings({})
|
||||
RoleBO convert(RoleDO roleDO);
|
||||
|
||||
@Mappings({})
|
||||
List<RoleBO> convert(List<RoleDO> roleDOs);
|
||||
|
||||
}
|
||||
@@ -22,6 +22,8 @@ public interface ResourceMapper {
|
||||
|
||||
ResourceDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<ResourceDO> selectListByIds(@Param("ids") Set<Integer> ids);
|
||||
|
||||
void insert(ResourceDO resource);
|
||||
|
||||
int update(ResourceDO resource);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package cn.iocoder.mall.admin.dao;
|
||||
|
||||
import cn.iocoder.mall.admin.dataobject.RoleDO;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface RoleMapper {
|
||||
|
||||
void insert(RoleDO roleDO);
|
||||
|
||||
int update(RoleDO roleDO);
|
||||
|
||||
RoleDO selectById(@Param("id") Integer id);
|
||||
|
||||
List<RoleDO> selectListByNameLike(@Param("name") String name,
|
||||
@Param("offset") Integer offset,
|
||||
@Param("limit") Integer limit);
|
||||
|
||||
Integer selectCountByNameLike(@Param("name") String name);
|
||||
|
||||
}
|
||||
@@ -9,6 +9,8 @@ import java.util.List;
|
||||
@Repository
|
||||
public interface RoleResourceMapper {
|
||||
|
||||
int insertList(@Param("roleResources") List<RoleResourceDO> resourceDOs);
|
||||
|
||||
List<RoleResourceDO> selectByResourceHandler(@Param("resourceHandler") String resourceHandler);
|
||||
|
||||
List<RoleResourceDO> selectByResourceId(@Param("resourceId") Integer resourceId);
|
||||
|
||||
@@ -1,20 +1,11 @@
|
||||
package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import java.util.Date;
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
|
||||
/**
|
||||
* 角色实体
|
||||
*/
|
||||
public class RoleDO {
|
||||
|
||||
/**
|
||||
* 账号状态 - 开启
|
||||
*/
|
||||
public static final Integer STATUS_ENABLE = 1;
|
||||
/**
|
||||
* 账号状态 - 禁用
|
||||
*/
|
||||
public static final Integer STATUS_DISABLE = 2;
|
||||
public class RoleDO extends BaseDO {
|
||||
|
||||
/**
|
||||
* 角色编号
|
||||
@@ -24,14 +15,6 @@ public class RoleDO {
|
||||
* 角色名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
@@ -51,22 +34,4 @@ public class RoleDO {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Date getCreateTime() {
|
||||
return createTime;
|
||||
}
|
||||
|
||||
public RoleDO setCreateTime(Date createTime) {
|
||||
this.createTime = createTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Integer getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public RoleDO setStatus(Integer status) {
|
||||
this.status = status;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,6 @@ public class ResourceServiceImpl implements ResourceService {
|
||||
@Autowired
|
||||
private RoleResourceMapper roleResourceMapper;
|
||||
|
||||
|
||||
public ResourceDO getResourceByTypeAndHandler(Integer type, String handler) {
|
||||
return resourceMapper.selectByTypeAndHandler(type, handler);
|
||||
}
|
||||
@@ -136,6 +135,13 @@ public class ResourceServiceImpl implements ResourceService {
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
public List<ResourceDO> getResources(Set<Integer> resourceIds) {
|
||||
if (resourceIds == null || resourceIds.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return resourceMapper.selectListByIds(resourceIds);
|
||||
}
|
||||
|
||||
private boolean isValidResourceType(Integer type) {
|
||||
return ResourceConstants.TYPE_MENU.equals(type)
|
||||
|| ResourceConstants.TYPE_URL.equals(type);
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
package cn.iocoder.mall.admin.service;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.BaseDO;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.admin.api.RoleService;
|
||||
import cn.iocoder.mall.admin.api.bo.RoleBO;
|
||||
import cn.iocoder.mall.admin.api.bo.RolePageBO;
|
||||
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum;
|
||||
import cn.iocoder.mall.admin.api.dto.RoleAddDTO;
|
||||
import cn.iocoder.mall.admin.api.dto.RolePageDTO;
|
||||
import cn.iocoder.mall.admin.api.dto.RoleUpdateDTO;
|
||||
import cn.iocoder.mall.admin.convert.RoleConvert;
|
||||
import cn.iocoder.mall.admin.dao.RoleMapper;
|
||||
import cn.iocoder.mall.admin.dao.RoleResourceMapper;
|
||||
import cn.iocoder.mall.admin.dataobject.ResourceDO;
|
||||
import cn.iocoder.mall.admin.dataobject.RoleDO;
|
||||
import cn.iocoder.mall.admin.dataobject.RoleResourceDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@com.alibaba.dubbo.config.annotation.Service
|
||||
@@ -14,6 +30,11 @@ public class RoleServiceImpl implements RoleService {
|
||||
|
||||
@Autowired
|
||||
private RoleResourceMapper roleResourceMapper;
|
||||
@Autowired
|
||||
private RoleMapper roleMapper;
|
||||
|
||||
@Autowired
|
||||
private ResourceServiceImpl resourceService;
|
||||
|
||||
public List<RoleResourceDO> getRoleByResourceHandler(String resourceHandler) {
|
||||
return roleResourceMapper.selectByResourceHandler(resourceHandler);
|
||||
@@ -23,4 +44,86 @@ public class RoleServiceImpl implements RoleService {
|
||||
return roleResourceMapper.selectByResourceId(resourceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<RolePageBO> getRolePage(RolePageDTO rolePageDTO) {
|
||||
RolePageBO rolePage = new RolePageBO();
|
||||
// 查询分页数据
|
||||
int offset = rolePageDTO.getPageNo() * rolePageDTO.getPageSize();
|
||||
rolePage.setRoles(RoleConvert.INSTANCE.convert(roleMapper.selectListByNameLike(rolePageDTO.getName(),
|
||||
offset, rolePageDTO.getPageSize())));
|
||||
// 查询分页总数
|
||||
rolePage.setCount(roleMapper.selectCountByNameLike(rolePageDTO.getName()));
|
||||
return CommonResult.success(rolePage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<RoleBO> addRole(Integer adminId, RoleAddDTO roleAddDTO) {
|
||||
// TODO 芋艿,角色名是否要唯一呢?貌似一般系统都是允许的。
|
||||
// 保存到数据库
|
||||
RoleDO role = RoleConvert.INSTANCE.convert(roleAddDTO);
|
||||
role.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
roleMapper.insert(role);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(RoleConvert.INSTANCE.convert(role));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> updateRole(Integer adminId, RoleUpdateDTO roleUpdateDTO) {
|
||||
// TODO 芋艿,角色名是否要唯一呢?貌似一般系统都是允许的。
|
||||
// 校验角色是否存在
|
||||
if (roleMapper.selectById(roleUpdateDTO.getId()) == null) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 更新到数据库
|
||||
RoleDO roleDO = RoleConvert.INSTANCE.convert(roleUpdateDTO);
|
||||
roleMapper.update(roleDO);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> deleteRole(Integer adminId, Integer roleId) {
|
||||
// 校验角色是否存在
|
||||
if (roleMapper.selectById(roleId) == null) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 更新到数据库,标记删除
|
||||
RoleDO roleDO = new RoleDO().setId(roleId);
|
||||
roleDO.setDeleted(RoleDO.DELETED_YES);
|
||||
roleMapper.update(roleDO);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Boolean> assignResource(Integer adminId, Integer roleId, Set<Integer> resourceIds) {
|
||||
// 校验角色是否存在
|
||||
if (roleMapper.selectById(roleId) == null) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.RESOURCE_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 校验是否有不存在的资源
|
||||
List<ResourceDO> resources = resourceService.getResources(resourceIds);
|
||||
if (resources.size() != resourceIds.size()) {
|
||||
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ROLE_ASSIGN_RESOURCE_NOT_EXISTS.getCode());
|
||||
}
|
||||
// TODO 芋艿,这里先简单实现。即方式是,删除老的分配的资源关系,然后添加新的分配的资源关系
|
||||
// 标记角色原资源关系都为删除
|
||||
roleResourceMapper.updateToDeletedByRoleId(roleId);
|
||||
// 创建 RoleResourceDO 数组,并插入到数据库
|
||||
if (!resourceIds.isEmpty()) {
|
||||
List<RoleResourceDO> roleResources = resourceIds.stream().map(resourceId -> {
|
||||
RoleResourceDO roleResource = new RoleResourceDO().setRoleId(roleId).setResourceId(resourceId);
|
||||
roleResource.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
|
||||
return roleResource;
|
||||
}).collect(Collectors.toList());
|
||||
roleResourceMapper.insertList(roleResources);
|
||||
}
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return CommonResult.success(true);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -42,12 +42,14 @@
|
||||
r.id, r.name, r.type, r.sort, r.display_name,
|
||||
r.create_time, r.pid, r.handler
|
||||
FROM resource r, role_resource rr
|
||||
WHERE r.type = #{type}
|
||||
AND deleted = 0
|
||||
WHERE r.deleted = 0
|
||||
<if test="type != null">
|
||||
AND r.type = #{type}
|
||||
</if>
|
||||
AND rr.role_id IN
|
||||
<foreach item="roleId" collection="roleIds" separator="," open="(" close=")" index="">
|
||||
#{roleId}
|
||||
</foreach>
|
||||
<foreach item="roleId" collection="roleIds" separator="," open="(" close=")" index="">
|
||||
#{roleId}
|
||||
</foreach>
|
||||
AND r.id = rr.resource_id
|
||||
</select>
|
||||
|
||||
@@ -68,6 +70,17 @@
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectListByIds" resultType="ResourceDO">
|
||||
SELECT
|
||||
<include refid="FIELDS"/>
|
||||
FROM resource
|
||||
WHERE id IN
|
||||
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
|
||||
#{id}
|
||||
</foreach>
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectCountByPid" resultType="int">
|
||||
SELECT
|
||||
COUNT(1)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="cn.iocoder.mall.admin.dao.RoleMapper">
|
||||
|
||||
<insert id="insert" parameterType="RoleDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
|
||||
INSERT INTO role (
|
||||
name, create_time, deleted
|
||||
) VALUES (
|
||||
#{name}, #{createTime}, #{deleted}
|
||||
)
|
||||
</insert>
|
||||
|
||||
<update id="update" parameterType="RoleDO">
|
||||
UPDATE role
|
||||
<set>
|
||||
<if test="name != null">
|
||||
, name = #{name}
|
||||
</if>
|
||||
<if test="deleted != null">
|
||||
, deleted = #{deleted}
|
||||
</if>
|
||||
</set>
|
||||
WHERE id = #{id}
|
||||
</update>
|
||||
|
||||
<select id="selectById" parameterType="Integer" resultType="RoleDO">
|
||||
SELECT
|
||||
id, name, create_time
|
||||
FROM role
|
||||
WHERE id = #{id}
|
||||
AND deleted = 0
|
||||
</select>
|
||||
|
||||
<select id="selectListByNameLike" resultType="RoleDO">
|
||||
SELECT
|
||||
id, name, create_time
|
||||
FROM role
|
||||
<where>
|
||||
<if test="name != null">
|
||||
name LIKE "%"#{name}"%"
|
||||
</if>
|
||||
</where>
|
||||
LIMIT #{offset}, #{limit}
|
||||
</select>
|
||||
|
||||
<select id="selectCountByNameLike" resultType="RoleDO">
|
||||
SELECT
|
||||
COUNT(1)
|
||||
FROM role
|
||||
<where>
|
||||
<if test="name != null">
|
||||
name LIKE "%"#{name}"%"
|
||||
</if>
|
||||
</where>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
@@ -41,4 +41,13 @@
|
||||
AND deleted = 0
|
||||
</update>
|
||||
|
||||
<insert id="insertList">
|
||||
INSERT INTO role_resource (
|
||||
resource_id, role_id, create_time, deleted
|
||||
) VALUES
|
||||
<foreach collection="roleResources" item="roleResource" separator=",">
|
||||
(#{roleResource.resourceId}, #{roleResource.roleId}, #{roleResource.createTime}, #{roleResource.deleted})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user