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 3a3aec8a9..79ba2e730 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 @@ -6,27 +6,27 @@ import cn.iocoder.mall.admin.api.ResourceService; import cn.iocoder.mall.admin.api.bo.AdminPageBO; import cn.iocoder.mall.admin.api.bo.ResourceBO; import cn.iocoder.mall.admin.api.constant.ResourceConstants; +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 cn.iocoder.mall.admin.application.convert.AdminConvert; 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.sdk.context.AdminSecurityContextHolder; import com.alibaba.dubbo.config.annotation.Reference; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.*; import java.util.stream.Collectors; @RestController -@RequestMapping("admin/admin") +@RequestMapping("admins/admin") @Api("管理员模块") public class AdminController { @@ -88,4 +88,52 @@ public class AdminController { return AdminConvert.INSTANCE.convert(result); } + @PostMapping("/add") + @ApiOperation(value = "创建管理员") + @ApiImplicitParams({ + @ApiImplicitParam(name = "username", value = "账号", required = true, example = "15601691300"), + @ApiImplicitParam(name = "nickname", value = "昵称", required = true, example = "小王"), + @ApiImplicitParam(name = "password", value = "密码", required = true, example = "buzhidao"), + }) + public CommonResult add(@RequestParam("username") String username, + @RequestParam("nickname") String nickname, + @RequestParam("password") String password) { + AdminAddDTO adminAddDTO = new AdminAddDTO().setUsername(username).setNickname(nickname).setPassword(password); + return AdminConvert.INSTANCE.convert2(adminService.addAdmin(AdminSecurityContextHolder.getContext().getAdminId(), adminAddDTO)); + } + + @PostMapping("/update") + @ApiOperation(value = "更新管理员") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1"), + @ApiImplicitParam(name = "username", value = "账号", required = true, example = "15601691300"), + @ApiImplicitParam(name = "nickname", value = "昵称", required = true, example = "小王"), + @ApiImplicitParam(name = "password", value = "密码", required = true, example = "buzhidao"), + }) + public CommonResult update(@RequestParam("id") Integer id, + @RequestParam("username") String username, + @RequestParam("nickname") String nickname, + @RequestParam("password") String password) { + AdminUpdateDTO adminUpdateDTO = new AdminUpdateDTO().setId(id).setUsername(username).setNickname(nickname).setPassword(password); + return adminService.updateAdmin(AdminSecurityContextHolder.getContext().getAdminId(), adminUpdateDTO); + } + + @PostMapping("/update_status") + @ApiOperation(value = "更新管理员状态") + @ApiImplicitParams({ + @ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1"), + @ApiImplicitParam(name = "status", value = "状态。1 - 开启;2 - 禁用", required = true, example = "1"), + }) + public CommonResult updateStatus(@RequestParam("id") Integer id, + @RequestParam("status") Integer status) { + return adminService.updateAdminStatus(AdminSecurityContextHolder.getContext().getAdminId(), id, status); + } + + @PostMapping("/delete") + @ApiOperation(value = "删除管理员") + @ApiImplicitParam(name = "id", value = "管理员编号", required = true, example = "1") + public CommonResult delete(@RequestParam("id") Integer id) { + return adminService.deleteAdmin(AdminSecurityContextHolder.getContext().getAdminId(), id); + } + } \ No newline at end of file diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/PassportController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/PassportController.java index 8d005c025..6b889fcb4 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/PassportController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/PassportController.java @@ -16,7 +16,7 @@ import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.*; @RestController -@RequestMapping("admin/passport") +@RequestMapping("admins/passport") @Api("Admin Passport 模块") public class PassportController { diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java index 48adab9b6..8a0de17be 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/ResourceController.java @@ -24,7 +24,7 @@ import java.util.Map; import java.util.stream.Collectors; @RestController -@RequestMapping("admin/resource") +@RequestMapping("admins/resource") @Api("资源模块") public class ResourceController { diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java index 5f1659aa2..e09a557b5 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/controller/RoleController.java @@ -25,7 +25,7 @@ import java.util.*; import java.util.stream.Collectors; @RestController -@RequestMapping("admin/role") +@RequestMapping("admins/role") public class RoleController { @Reference(validation = "true") diff --git a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java index 8ef6b6846..3c0a4ce51 100644 --- a/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java +++ b/admin/admin-application/src/main/java/cn/iocoder/mall/admin/application/convert/AdminConvert.java @@ -22,6 +22,9 @@ public interface AdminConvert { @Mappings({}) AdminVO convert(AdminBO adminBO); + @Mappings({}) + CommonResult convert2(CommonResult result); + @Mappings({}) CommonResult convert(CommonResult result); diff --git a/admin/admin-application/src/main/resources/application.yaml b/admin/admin-application/src/main/resources/application.yaml index ed6019ec3..a37e6a54f 100644 --- a/admin/admin-application/src/main/resources/application.yaml +++ b/admin/admin-application/src/main/resources/application.yaml @@ -4,4 +4,6 @@ spring: # server server: - port: 18083 \ No newline at end of file + port: 18083 + servlet: + context-path: /admin-api/ \ No newline at end of file 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 b21ad76cc..0e2cd7965 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 @@ -1,11 +1,22 @@ package cn.iocoder.mall.admin.api; import cn.iocoder.common.framework.vo.CommonResult; +import cn.iocoder.mall.admin.api.bo.AdminBO; import cn.iocoder.mall.admin.api.bo.AdminPageBO; +import cn.iocoder.mall.admin.api.dto.AdminAddDTO; import cn.iocoder.mall.admin.api.dto.AdminPageDTO; +import cn.iocoder.mall.admin.api.dto.AdminUpdateDTO; public interface AdminService { CommonResult getAdminPage(AdminPageDTO adminPageDTO); + CommonResult addAdmin(Integer adminId, AdminAddDTO adminAddDTO); + + CommonResult updateAdmin(Integer adminId, AdminUpdateDTO adminUpdateDTO); + + CommonResult updateAdminStatus(Integer adminId, Integer updateAdminId, Integer status); + + CommonResult deleteAdmin(Integer adminId, Integer updateAdminId); + } \ No newline at end of file diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/OAuth2Service.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/OAuth2Service.java index 1be3644da..cdf9a7531 100644 --- a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/OAuth2Service.java +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/OAuth2Service.java @@ -30,6 +30,4 @@ public interface OAuth2Service { // TODO @see 刷新 token - // TODO @see 移除 token - } \ No newline at end of file diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java index 42f90d10a..03efd89bd 100644 --- a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/constant/AdminErrorCodeEnum.java @@ -24,6 +24,9 @@ public enum AdminErrorCodeEnum { ADMIN_USERNAME_NOT_REGISTERED(1002002000, "账号不存在"), ADMIN_PASSWORD_ERROR(1002002001, "密码不正确"), ADMIN_IS_DISABLE(1002002002, "账号被禁用"), + ADMIN_USERNAME_EXISTS(1002002002, "账号已经存在"), + ADMIN_STATUS_EQUALS(1002002003, "账号已经是该状态"), + ADMIN_DELETE_ONLY_DISABLE(1002002004, "只有关闭的账号才可以删除"), // ========== 资源模块 1002003000 ========== RESOURCE_NAME_DUPLICATE(1002003000, "已经存在该名字的资源"), diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/AdminAddDTO.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/AdminAddDTO.java new file mode 100644 index 000000000..38d9d73eb --- /dev/null +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/AdminAddDTO.java @@ -0,0 +1,52 @@ +package cn.iocoder.mall.admin.api.dto; + +import javax.validation.constraints.NotEmpty; + +/** + * 管理员添加 DTO + */ +public class AdminAddDTO { + + /** + * 登陆账号 + */ + @NotEmpty(message = "登陆账号不能为空") + private String username; + /** + * 昵称 + */ + @NotEmpty(message = "昵称不能为空") + private String nickname; + /** + * 密码 + */ + @NotEmpty(message = "密码不能为空") + private String password; + + public String getUsername() { + return username; + } + + public AdminAddDTO setUsername(String username) { + this.username = username; + return this; + } + + public String getNickname() { + return nickname; + } + + public AdminAddDTO setNickname(String nickname) { + this.nickname = nickname; + return this; + } + + public String getPassword() { + return password; + } + + public AdminAddDTO setPassword(String password) { + this.password = password; + return this; + } +} \ No newline at end of file diff --git a/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/AdminUpdateDTO.java b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/AdminUpdateDTO.java new file mode 100644 index 000000000..3698d4aae --- /dev/null +++ b/admin/admin-service-api/src/main/java/cn/iocoder/mall/admin/api/dto/AdminUpdateDTO.java @@ -0,0 +1,67 @@ +package cn.iocoder.mall.admin.api.dto; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +/** + * 管理员更新 DTO + */ +public class AdminUpdateDTO { + + /** + * 管理员编号 + */ + @NotNull(message = "管理员编号不能为空") + private Integer id; + /** + * 登陆账号 + */ + @NotEmpty(message = "登陆账号不能为空") + private String username; + /** + * 昵称 + */ + @NotEmpty(message = "昵称不能为空") + private String nickname; + /** + * 密码 + */ + @NotEmpty(message = "密码不能为空") + private String password; + + public String getUsername() { + return username; + } + + public AdminUpdateDTO setUsername(String username) { + this.username = username; + return this; + } + + public String getNickname() { + return nickname; + } + + public AdminUpdateDTO setNickname(String nickname) { + this.nickname = nickname; + return this; + } + + public String getPassword() { + return password; + } + + public AdminUpdateDTO setPassword(String password) { + this.password = password; + return this; + } + + public Integer getId() { + return id; + } + + public AdminUpdateDTO setId(Integer id) { + this.id = id; + return this; + } +} \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/convert/AdminConvert.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/convert/AdminConvert.java index 0cae43c6f..b50774a63 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/convert/AdminConvert.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/convert/AdminConvert.java @@ -1,6 +1,8 @@ package cn.iocoder.mall.admin.convert; import cn.iocoder.mall.admin.api.bo.AdminBO; +import cn.iocoder.mall.admin.api.dto.AdminAddDTO; +import cn.iocoder.mall.admin.api.dto.AdminUpdateDTO; import cn.iocoder.mall.admin.dataobject.AdminDO; import org.mapstruct.Mapper; import org.mapstruct.Mappings; @@ -16,6 +18,12 @@ public interface AdminConvert { @Mappings({}) AdminBO convert(AdminDO adminDO); + @Mappings({}) + AdminDO convert(AdminAddDTO adminAddDTO); + + @Mappings({}) + AdminDO convert(AdminUpdateDTO adminUpdateDTO); + @Mappings({}) List convert(List adminBOs); diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminMapper.java index c5cf5e410..9c2c2b695 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/AdminMapper.java @@ -9,6 +9,8 @@ import java.util.List; @Repository public interface AdminMapper { + AdminDO selectById(@Param("id") Integer id); + AdminDO selectByUsername(@Param("username") String username); List selectListByNicknameLike(@Param("nickname") String nickname, @@ -17,4 +19,8 @@ public interface AdminMapper { Integer selectCountByNicknameLike(@Param("nickname") String nickname); + void insert(AdminDO admin); + + int update(AdminDO admin); + } \ No newline at end of file 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 3a0399e97..eb1307f6a 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 @@ -11,4 +11,8 @@ public interface AdminRoleMapper { List selectByAdminId(@Param("adminId") Integer adminId); + int updateToDeletedByAdminId(@Param("adminId") Integer adminId); + + int updateToDeletedByRoleId(@Param("roleId") Integer roleId); + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2AccessTokenMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2AccessTokenMapper.java index 4e1a1399a..7931d0130 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2AccessTokenMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2AccessTokenMapper.java @@ -1,6 +1,7 @@ package cn.iocoder.mall.admin.dao; import cn.iocoder.mall.admin.dataobject.OAuth2AccessTokenDO; +import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository @@ -8,6 +9,8 @@ public interface OAuth2AccessTokenMapper { void insert(OAuth2AccessTokenDO entity); - OAuth2AccessTokenDO selectByTokenId(String tokenId); + OAuth2AccessTokenDO selectByTokenId(@Param("id") String id); + + int updateToInvalidByAdminId(@Param("adminId") Integer adminId); } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2RefreshTokenMapper.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2RefreshTokenMapper.java index 73659b94b..8cd6e6166 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2RefreshTokenMapper.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dao/OAuth2RefreshTokenMapper.java @@ -1,6 +1,7 @@ package cn.iocoder.mall.admin.dao; import cn.iocoder.mall.admin.dataobject.OAuth2RefreshTokenDO; +import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository @@ -8,4 +9,6 @@ public interface OAuth2RefreshTokenMapper { void insert(OAuth2RefreshTokenDO entity); + int updateToInvalidByAdminId(@Param("adminId") Integer adminId); + } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/AdminRoleDO.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/AdminRoleDO.java index 048ac41e6..dc841ce97 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/AdminRoleDO.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/dataobject/AdminRoleDO.java @@ -1,11 +1,11 @@ package cn.iocoder.mall.admin.dataobject; -import java.util.Date; +import cn.iocoder.common.framework.dataobject.BaseDO; /** * {@link AdminDO} 和 {@link RoleDO} 的关联表 */ -public class AdminRoleDO { +public class AdminRoleDO extends BaseDO { /** * 编号 @@ -19,12 +19,6 @@ public class AdminRoleDO { * 角色编号(外键:{@link RoleDO} */ private Integer roleId; - /** - * 创建时间 - */ - private Date createTime; - - // TODO 芋艿 删除状态 public Integer getId() { return id; @@ -53,13 +47,4 @@ public class AdminRoleDO { return this; } - public Date getCreateTime() { - return createTime; - } - - public AdminRoleDO setCreateTime(Date createTime) { - this.createTime = createTime; - return this; - } - } \ 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 f6ba54c1c..0abe56131 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 @@ -1,20 +1,27 @@ package cn.iocoder.mall.admin.service; +import cn.iocoder.common.framework.constant.SysErrorCodeEnum; +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.AdminService; +import cn.iocoder.mall.admin.api.bo.AdminBO; import cn.iocoder.mall.admin.api.bo.AdminPageBO; import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; +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 cn.iocoder.mall.admin.convert.AdminConvert; -import cn.iocoder.mall.admin.dataobject.AdminDO; 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 org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.util.DigestUtils; +import java.util.Date; import java.util.List; @Service @@ -25,6 +32,8 @@ public class AdminServiceImpl implements AdminService { private AdminMapper adminMapper; @Autowired private AdminRoleMapper adminRoleMapper; + @Autowired + private OAuth2ServiceImpl oAuth2Service; public CommonResult validAdmin(String username, String password) { AdminDO admin = adminMapper.selectByUsername(username); @@ -33,7 +42,7 @@ public class AdminServiceImpl implements AdminService { return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); } // 密码不正确 - if (DigestUtils.md5DigestAsHex(password.getBytes()).equals(admin.getPassword())) { + if (encodePassword(password).equals(admin.getPassword())) { return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_PASSWORD_ERROR.getCode()); } // 账号被禁用 @@ -62,4 +71,98 @@ public class AdminServiceImpl implements AdminService { return CommonResult.success(adminPage); } + @Override + public CommonResult addAdmin(Integer adminId, AdminAddDTO adminAddDTO) { + // 校验账号唯一 + if (adminMapper.selectByUsername(adminAddDTO.getUsername()) != null) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_EXISTS.getCode()); + } + // 保存到数据库 + AdminDO admin = AdminConvert.INSTANCE.convert(adminAddDTO) + .setPassword(encodePassword(adminAddDTO.getPassword())) // 加密密码 + .setStatus(AdminDO.STATUS_ENABLE); + admin.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO); + adminMapper.insert(admin); + // TODO 插入操作日志 + // 返回成功 + return CommonResult.success(AdminConvert.INSTANCE.convert(admin)); + } + + @Override + public CommonResult updateAdmin(Integer adminId, AdminUpdateDTO adminUpdateDTO) { + // 校验账号存在 + if (adminMapper.selectById(adminUpdateDTO.getId()) == null) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); + } + // 校验账号唯一 + AdminDO usernameAdmin = adminMapper.selectByUsername(adminUpdateDTO.getUsername()); + if (usernameAdmin != null && !usernameAdmin.getId().equals(adminUpdateDTO.getId())) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_EXISTS.getCode()); + } + // 更新到数据库 + AdminDO updateAdmin = AdminConvert.INSTANCE.convert(adminUpdateDTO); + adminMapper.update(updateAdmin); + // TODO 插入操作日志 + // 返回成功 + return CommonResult.success(true); + } + + @Override + @Transactional + public CommonResult updateAdminStatus(Integer adminId, Integer updateAdminId, Integer status) { + // 校验参数 + if (!isValidStatus(status)) { + return CommonResult.error(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "变更状态必须是开启(1)或关闭(2)"); // TODO 有点搓 + } + // 校验账号存在 + AdminDO admin = adminMapper.selectById(updateAdminId); + if (admin == null) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); + } + // 如果状态相同,则返回错误 + if (status.equals(admin.getStatus())) { + return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_STATUS_EQUALS.getCode()); + } + // 更新管理员状态 + AdminDO updateAdmin = new AdminDO().setId(updateAdminId).setStatus(status); + adminMapper.update(updateAdmin); + // 如果是关闭管理员,则标记 token 失效。否则,管理员还可以继续蹦跶 + if (AdminDO.STATUS_DISABLE.equals(status)) { + oAuth2Service.removeToken(updateAdminId); + } + // TODO 插入操作日志 + // 返回成功 + return CommonResult.success(true); + } + + @Override + @Transactional + public CommonResult deleteAdmin(Integer adminId, Integer updateAdminId) { + // 校验账号存在 + AdminDO admin = adminMapper.selectById(updateAdminId); + 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 updateAdmin = new AdminDO().setId(updateAdminId); + updateAdmin.setDeleted(BaseDO.DELETED_YES); + adminMapper.update(updateAdmin); + // 标记删除 AdminRole + adminRoleMapper.updateToDeletedByAdminId(updateAdminId); + // TODO 插入操作日志 + // 返回成功 + return CommonResult.success(true); + } + + private String encodePassword(String password) { + return DigestUtils.md5DigestAsHex(password.getBytes()); + } + + private boolean isValidStatus(Integer status) { + return AdminDO.STATUS_ENABLE.equals(status) + || AdminDO.STATUS_DISABLE.equals(status); + } } \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/OAuth2ServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/OAuth2ServiceImpl.java index e827e2a43..6a1af316a 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/OAuth2ServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/OAuth2ServiceImpl.java @@ -13,6 +13,7 @@ import cn.iocoder.mall.admin.dataobject.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; @@ -46,6 +47,7 @@ public class OAuth2ServiceImpl implements OAuth2Service { private ResourceServiceImpl resourceService; @Override + @Transactional public CommonResult getAccessToken(String username, String password) { CommonResult adminResult = adminService.validAdmin(username, password); // 校验失败,返回错误结果 @@ -79,6 +81,19 @@ public class OAuth2ServiceImpl implements OAuth2Service { return CommonResult.success(OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO, adminRoleDOs)); } + /** + * 移除管理员对应的 Token + * + * @param adminId 管理员编号 + */ + @Transactional + public void removeToken(Integer adminId) { + // 设置 access token 失效 + oauth2AccessTokenMapper.updateToInvalidByAdminId(adminId); + // 设置 refresh token 失效 + oauth2RefreshTokenMapper.updateToInvalidByAdminId(adminId); + } + @Override public CommonResult checkPermission(Integer adminId, Set roleIds, String url) { // 如果未配置该资源,说明无需权限控制。 diff --git a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java index 10bbe2887..fa1772e23 100644 --- a/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java +++ b/admin/admin-service-impl/src/main/java/cn/iocoder/mall/admin/service/ResourceServiceImpl.java @@ -16,6 +16,7 @@ import cn.iocoder.mall.admin.dao.RoleResourceMapper; import cn.iocoder.mall.admin.dataobject.ResourceDO; 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; @@ -116,6 +117,7 @@ public class ResourceServiceImpl implements ResourceService { } @Override + @Transactional public CommonResult deleteResource(Integer adminId, Integer resourceId) { // 校验更新的资源是否存在 if (resourceMapper.selectById(resourceId) == null) { 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 8ffc40e00..629a10074 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 @@ -11,6 +11,7 @@ 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.AdminRoleMapper; import cn.iocoder.mall.admin.dao.RoleMapper; import cn.iocoder.mall.admin.dao.RoleResourceMapper; import cn.iocoder.mall.admin.dataobject.ResourceDO; @@ -18,6 +19,7 @@ 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 org.springframework.transaction.annotation.Transactional; import java.util.Date; import java.util.List; @@ -31,6 +33,8 @@ public class RoleServiceImpl implements RoleService { @Autowired private RoleResourceMapper roleResourceMapper; @Autowired + private AdminRoleMapper adminRoleMapper; + @Autowired private RoleMapper roleMapper; @Autowired @@ -84,6 +88,7 @@ public class RoleServiceImpl implements RoleService { } @Override + @Transactional public CommonResult deleteRole(Integer adminId, Integer roleId) { // 校验角色是否存在 if (roleMapper.selectById(roleId) == null) { @@ -93,12 +98,17 @@ public class RoleServiceImpl implements RoleService { RoleDO roleDO = new RoleDO().setId(roleId); roleDO.setDeleted(RoleDO.DELETED_YES); roleMapper.update(roleDO); + // 标记删除 RoleResource + roleResourceMapper.updateToDeletedByRoleId(roleId); + // 标记删除 AdminRole + adminRoleMapper.updateToDeletedByRoleId(roleId); // TODO 插入操作日志 // 返回成功 return CommonResult.success(true); } @Override + @Transactional public CommonResult assignResource(Integer adminId, Integer roleId, Set resourceIds) { // 校验角色是否存在 if (roleMapper.selectById(roleId) == null) { diff --git a/admin/admin-service-impl/src/main/resources/mapper/AdminMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/AdminMapper.xml index 83cc2d2f8..30f45cae5 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/AdminMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/AdminMapper.xml @@ -40,4 +40,44 @@ + + INSERT INTO admin ( + username, nickname, password, status, + create_time, deleted + ) VALUES ( + #{username}, #{nickname}, #{password}, #{status}, + #{createTime}, #{deleted} + ) + + + + + + UPDATE admin + + + , username = #{username} + + + , nickname = #{nickname} + + + , password = #{password} + + + , status = #{status} + + + , deleted = #{deleted} + + + WHERE id = #{id} + + \ 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 63a517de0..28587d879 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/AdminRoleMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/AdminRoleMapper.xml @@ -18,4 +18,18 @@ AND a.id = ar.admin_id + + UPDATE admin_role + SET deleted = 1 + WHERE admin_id = #{adminId} + AND deleted = 0 + + + + UPDATE admin_role + SET deleted = 1 + WHERE role_id = #{roleId} + AND deleted = 0 + + \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml index 47c1cf10c..e9b8ce6eb 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/OAuth2AccessTokenMapper.xml @@ -19,4 +19,11 @@ WHERE id = #{id} + + UPDATE oauth2_access_token + SET valid = 0 + WHERE admin_id = #{adminId} + AND valid = 1 + + \ No newline at end of file diff --git a/admin/admin-service-impl/src/main/resources/mapper/OAuth2RefreshTokenMapper.xml b/admin/admin-service-impl/src/main/resources/mapper/OAuth2RefreshTokenMapper.xml index 04cefd1b7..34472899c 100644 --- a/admin/admin-service-impl/src/main/resources/mapper/OAuth2RefreshTokenMapper.xml +++ b/admin/admin-service-impl/src/main/resources/mapper/OAuth2RefreshTokenMapper.xml @@ -10,4 +10,11 @@ ) + + UPDATE oauth2_refresh_token + SET valid = 0 + WHERE admin_id = #{adminId} + AND valid = 1 + + \ No newline at end of file