diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java index 79ba2e73..75c1144a 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/AdminController.java @@ -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> 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 assignRole(@RequestParam("id") Integer id, + @RequestParam("roleIds")Set roleIds) { + return adminService.assignRole(AdminSecurityContextHolder.getContext().getAdminId(), id, roleIds); + } + } \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/RoleConvert.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/RoleConvert.java index 61d0ee35..1d8b4700 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/RoleConvert.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/RoleConvert.java @@ -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 convert(List roleBO); + @Mappings({}) CommonResult convert(CommonResult resourceBO); diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/AdminService.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/AdminService.java index 0e2cd796..78e5e675 100644 --- a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/AdminService.java +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/AdminService.java @@ -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 getAdminPage(AdminPageDTO adminPageDTO); @@ -19,4 +21,6 @@ public interface AdminService { CommonResult deleteAdmin(Integer adminId, Integer updateAdminId); + CommonResult assignRole(Integer adminId, Integer updateAdminId, Set roleIds); + } \ No newline at end of file diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java index ee5c64a1..7b65cbd6 100644 --- a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/RoleService.java @@ -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 getRolePage(RolePageDTO rolePageDTO); + CommonResult> getRoleList(Integer adminId); + CommonResult addRole(Integer adminId, RoleAddDTO roleAddDTO); CommonResult updateRole(Integer adminId, RoleUpdateDTO roleUpdateDTO); diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminRoleMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminRoleMapper.java index eb1307f6..3c69199c 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminRoleMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminRoleMapper.java @@ -15,4 +15,6 @@ public interface AdminRoleMapper { int updateToDeletedByRoleId(@Param("roleId") Integer roleId); + void insertList(@Param("adminRoleDOs") List adminRoleDOs); + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java index 7b628bd4..c5753010 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/RoleMapper.java @@ -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 selectListByIds(@Param("ids") Set ids); + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java index 0abe5613..09cd24bd 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/AdminServiceImpl.java @@ -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 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 assignRole(Integer adminId, Integer updateAdminId, Set roleIds) { + // 校验账号存在 + AdminDO admin = adminMapper.selectById(updateAdminId); + if (admin == null) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); + } + // 校验是否有不存在的角色 + List 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 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()); } diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java index 629a1007..918c112c 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/RoleServiceImpl.java @@ -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> getRoleList(Integer adminId) { + return null; + } + @Override public CommonResult addRole(Integer adminId, RoleAddDTO roleAddDTO) { // TODO 芋艿,角色名是否要唯一呢?貌似一般系统都是允许的。 @@ -136,4 +143,11 @@ public class RoleServiceImpl implements RoleService { return CommonResult.success(true); } + public List getRoles(Set roleIds) { + if (CollectionUtil.isEmpty(roleIds)) { + return Collections.emptyList(); + } + return roleMapper.selectListByIds(roleIds); + } + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/resources/mapper/AdminRoleMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/AdminRoleMapper.xml index 28587d87..f677b8a5 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/AdminRoleMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/AdminRoleMapper.xml @@ -32,4 +32,13 @@ AND deleted = 0 + + INSERT INTO admin_role ( + admin_id, role_id, create_time, deleted + ) VALUES + + (#{adminRole.adminId}, #{adminRole.roleId}, #{adminRole.createTime}, #{adminRole.deleted}) + + + \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml index 86a51821..f053c1a3 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/RoleMapper.xml @@ -2,6 +2,10 @@ + + id, name, create_time + + INSERT INTO role ( name, create_time, deleted @@ -25,7 +29,7 @@ SELECT - id, name, create_time + FROM role @@ -56,4 +60,15 @@ + + \ No newline at end of file diff --git a/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java new file mode 100644 index 00000000..fd3c5bbd --- /dev/null +++ b/common/common-framework/src/main/java/cn/iocoder/common/framework/util/CollectionUtil.java @@ -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(); + } + +}