增加管理员分配角色

This commit is contained in:
YunaiV 2019-03-02 18:17:09 +08:00
parent 36f62b2459
commit b3b3b8154b
11 changed files with 130 additions and 3 deletions

View File

@ -3,6 +3,7 @@ package cn.iocoder.mall.admin.application.controller;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.AdminService;
import cn.iocoder.mall.admin.api.ResourceService;
import cn.iocoder.mall.admin.api.RoleService;
import cn.iocoder.mall.admin.api.bo.AdminPageBO;
import cn.iocoder.mall.admin.api.bo.ResourceBO;
import cn.iocoder.mall.admin.api.constant.ResourceConstants;
@ -14,6 +15,7 @@ import cn.iocoder.mall.admin.application.convert.ResourceConvert;
import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO;
import cn.iocoder.mall.admin.application.vo.AdminPageVO;
import cn.iocoder.mall.admin.application.vo.AdminVO;
import cn.iocoder.mall.admin.application.vo.RoleVO;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
@ -34,6 +36,8 @@ public class AdminController {
private ResourceService resourceService;
@Reference(validation = "true")
private AdminService adminService;
@Reference(validation = "true")
private RoleService roleService;
// =========== 当前管理员相关的资源 API ===========
@ -136,4 +140,24 @@ public class AdminController {
return adminService.deleteAdmin(AdminSecurityContextHolder.getContext().getAdminId(), id);
}
@GetMapping("/role_list")
@ApiOperation(value = "指定管理员拥有的角色列表")
@ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1")
public CommonResult<List<RoleVO>> roleList(@RequestParam("id") Integer id) {
// return RoleConvert.INSTANCE.convert()
// TODO 需要讨论下 api 提供的方式
return null;
}
@PostMapping("/assign_role")
@ApiOperation(value = "分配给管理员角色")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1"),
@ApiImplicitParam(name = "roleIds", value = "角色编号集合", required = true, example = "1,2,3"),
})
public CommonResult<Boolean> assignRole(@RequestParam("id") Integer id,
@RequestParam("roleIds")Set<Integer> roleIds) {
return adminService.assignRole(AdminSecurityContextHolder.getContext().getAdminId(), id, roleIds);
}
}

View File

@ -9,6 +9,8 @@ import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface RoleConvert {
@ -17,6 +19,9 @@ public interface RoleConvert {
@Mappings({})
RoleVO convert(RoleBO roleBO);
@Mappings({})
List<RoleVO> convert(List<RoleBO> roleBO);
@Mappings({})
CommonResult<RoleVO> convert(CommonResult<RoleBO> resourceBO);

View File

@ -7,6 +7,8 @@ import cn.iocoder.mall.admin.api.dto.AdminAddDTO;
import cn.iocoder.mall.admin.api.dto.AdminPageDTO;
import cn.iocoder.mall.admin.api.dto.AdminUpdateDTO;
import java.util.Set;
public interface AdminService {
CommonResult<AdminPageBO> getAdminPage(AdminPageDTO adminPageDTO);
@ -19,4 +21,6 @@ public interface AdminService {
CommonResult<Boolean> deleteAdmin(Integer adminId, Integer updateAdminId);
CommonResult<Boolean> assignRole(Integer adminId, Integer updateAdminId, Set<Integer> roleIds);
}

View File

@ -7,12 +7,15 @@ 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 java.util.List;
import java.util.Set;
public interface RoleService {
CommonResult<RolePageBO> getRolePage(RolePageDTO rolePageDTO);
CommonResult<List<RoleBO>> getRoleList(Integer adminId);
CommonResult<RoleBO> addRole(Integer adminId, RoleAddDTO roleAddDTO);
CommonResult<Boolean> updateRole(Integer adminId, RoleUpdateDTO roleUpdateDTO);

View File

@ -15,4 +15,6 @@ public interface AdminRoleMapper {
int updateToDeletedByRoleId(@Param("roleId") Integer roleId);
void insertList(@Param("adminRoleDOs") List<AdminRoleDO> adminRoleDOs);
}

View File

@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Set;
@Repository
public interface RoleMapper {
@ -21,4 +22,6 @@ public interface RoleMapper {
Integer selectCountByNameLike(@Param("name") String name);
List<RoleDO> selectListByIds(@Param("ids") Set<Integer> ids);
}

View File

@ -16,6 +16,7 @@ import cn.iocoder.mall.admin.dao.AdminMapper;
import cn.iocoder.mall.admin.dao.AdminRoleMapper;
import cn.iocoder.mall.admin.dataobject.AdminDO;
import cn.iocoder.mall.admin.dataobject.AdminRoleDO;
import cn.iocoder.mall.admin.dataobject.RoleDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -23,6 +24,8 @@ import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
@Service
@com.alibaba.dubbo.config.annotation.Service
@ -32,8 +35,11 @@ public class AdminServiceImpl implements AdminService {
private AdminMapper adminMapper;
@Autowired
private AdminRoleMapper adminRoleMapper;
@Autowired
private OAuth2ServiceImpl oAuth2Service;
@Autowired
private RoleServiceImpl roleService;
public CommonResult<AdminDO> validAdmin(String username, String password) {
AdminDO admin = adminMapper.selectByUsername(username);
@ -143,10 +149,11 @@ public class AdminServiceImpl implements AdminService {
if (admin == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
// 只有禁用的账号才可以删除
if (AdminDO.STATUS_ENABLE.equals(admin.getStatus())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_DELETE_ONLY_DISABLE.getCode());
}
// 只有禁用的账号才可以删除
// 标记删除 AdminDO
AdminDO updateAdmin = new AdminDO().setId(updateAdminId);
updateAdmin.setDeleted(BaseDO.DELETED_YES);
adminMapper.update(updateAdmin);
@ -157,6 +164,36 @@ public class AdminServiceImpl implements AdminService {
return CommonResult.success(true);
}
@Override
@Transactional
public CommonResult<Boolean> assignRole(Integer adminId, Integer updateAdminId, Set<Integer> roleIds) {
// 校验账号存在
AdminDO admin = adminMapper.selectById(updateAdminId);
if (admin == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode());
}
// 校验是否有不存在的角色
List<RoleDO> roles = roleService.getRoles(roleIds);
if (roles.size() != roleIds.size()) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ROLE_ASSIGN_RESOURCE_NOT_EXISTS.getCode());
}
// TODO 芋艿这里先简单实现即方式是删除老的分配的角色关系然后添加新的分配的角色关系
// 标记管理员角色源关系都为删除
adminRoleMapper.updateToDeletedByRoleId(updateAdminId);
// 创建 RoleResourceDO 数组并插入到数据库
if (!roleIds.isEmpty()) {
List<AdminRoleDO> adminRoleDOs = roleIds.stream().map(roleId -> {
AdminRoleDO roleResource = new AdminRoleDO().setAdminId(updateAdminId).setRoleId(roleId);
roleResource.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
return roleResource;
}).collect(Collectors.toList());
adminRoleMapper.insertList(adminRoleDOs);
}
// TODO 插入操作日志
// 返回成功
return CommonResult.success(true);
}
private String encodePassword(String password) {
return DigestUtils.md5DigestAsHex(password.getBytes());
}

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.dataobject.BaseDO;
import cn.iocoder.common.framework.util.CollectionUtil;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.RoleService;
@ -21,6 +22,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
@ -60,6 +62,11 @@ public class RoleServiceImpl implements RoleService {
return CommonResult.success(rolePage);
}
@Override
public CommonResult<List<RoleBO>> getRoleList(Integer adminId) {
return null;
}
@Override
public CommonResult<RoleBO> addRole(Integer adminId, RoleAddDTO roleAddDTO) {
// TODO 芋艿角色名是否要唯一呢貌似一般系统都是允许的
@ -136,4 +143,11 @@ public class RoleServiceImpl implements RoleService {
return CommonResult.success(true);
}
public List<RoleDO> getRoles(Set<Integer> roleIds) {
if (CollectionUtil.isEmpty(roleIds)) {
return Collections.emptyList();
}
return roleMapper.selectListByIds(roleIds);
}
}

View File

@ -32,4 +32,13 @@
AND deleted = 0
</update>
<insert id="insertList">
INSERT INTO admin_role (
admin_id, role_id, create_time, deleted
) VALUES
<foreach collection="adminRoleDOs" item="adminRole" separator=",">
(#{adminRole.adminId}, #{adminRole.roleId}, #{adminRole.createTime}, #{adminRole.deleted})
</foreach>
</insert>
</mapper>

View File

@ -2,6 +2,10 @@
<!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">
<sql id="FIELDS">
id, name, create_time
</sql>
<insert id="insert" parameterType="RoleDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO role (
name, create_time, deleted
@ -25,7 +29,7 @@
<select id="selectById" parameterType="Integer" resultType="RoleDO">
SELECT
id, name, create_time
<include refid="FIELDS"/>
FROM role
WHERE id = #{id}
AND deleted = 0
@ -33,7 +37,7 @@
<select id="selectListByNameLike" resultType="RoleDO">
SELECT
id, name, create_time
<include refid="FIELDS"/>
FROM role
<where>
<if test="name != null">
@ -56,4 +60,15 @@
</where>
</select>
<select id="selectListByIds" resultType="RoleDO">
SELECT
<include refid="FIELDS"/>
FROM resource
WHERE id IN
<foreach item="id" collection="ids" separator="," open="(" close=")" index="">
#{id}
</foreach>
AND deleted = 0
</select>
</mapper>

View File

@ -0,0 +1,11 @@
package cn.iocoder.common.framework.util;
import java.util.Collection;
public class CollectionUtil {
public static boolean isEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}
}