增加管理员模块的增删改查~

This commit is contained in:
YunaiV 2019-03-02 17:19:37 +08:00
parent ffeb866b91
commit 36f62b2459
25 changed files with 422 additions and 31 deletions

View File

@ -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.AdminPageBO;
import cn.iocoder.mall.admin.api.bo.ResourceBO; import cn.iocoder.mall.admin.api.bo.ResourceBO;
import cn.iocoder.mall.admin.api.constant.ResourceConstants; 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.AdminPageDTO;
import cn.iocoder.mall.admin.api.dto.AdminUpdateDTO;
import cn.iocoder.mall.admin.application.convert.AdminConvert; import cn.iocoder.mall.admin.application.convert.AdminConvert;
import cn.iocoder.mall.admin.application.convert.ResourceConvert; import cn.iocoder.mall.admin.application.convert.ResourceConvert;
import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO; import cn.iocoder.mall.admin.application.vo.AdminMenuTreeNodeVO;
import cn.iocoder.mall.admin.application.vo.AdminPageVO; 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 cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("admin/admin") @RequestMapping("admins/admin")
@Api("管理员模块") @Api("管理员模块")
public class AdminController { public class AdminController {
@ -88,4 +88,52 @@ public class AdminController {
return AdminConvert.INSTANCE.convert(result); 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<AdminVO> 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<Boolean> 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<Boolean> 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<Boolean> delete(@RequestParam("id") Integer id) {
return adminService.deleteAdmin(AdminSecurityContextHolder.getContext().getAdminId(), id);
}
} }

View File

@ -16,7 +16,7 @@ import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@RequestMapping("admin/passport") @RequestMapping("admins/passport")
@Api("Admin Passport 模块") @Api("Admin Passport 模块")
public class PassportController { public class PassportController {

View File

@ -24,7 +24,7 @@ import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("admin/resource") @RequestMapping("admins/resource")
@Api("资源模块") @Api("资源模块")
public class ResourceController { public class ResourceController {

View File

@ -25,7 +25,7 @@ import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("admin/role") @RequestMapping("admins/role")
public class RoleController { public class RoleController {
@Reference(validation = "true") @Reference(validation = "true")

View File

@ -22,6 +22,9 @@ public interface AdminConvert {
@Mappings({}) @Mappings({})
AdminVO convert(AdminBO adminBO); AdminVO convert(AdminBO adminBO);
@Mappings({})
CommonResult<AdminVO> convert2(CommonResult<AdminBO> result);
@Mappings({}) @Mappings({})
CommonResult<AdminPageVO> convert(CommonResult<AdminPageBO> result); CommonResult<AdminPageVO> convert(CommonResult<AdminPageBO> result);

View File

@ -5,3 +5,5 @@ spring:
# server # server
server: server:
port: 18083 port: 18083
servlet:
context-path: /admin-api/

View File

@ -1,11 +1,22 @@
package cn.iocoder.mall.admin.api; package cn.iocoder.mall.admin.api;
import cn.iocoder.common.framework.vo.CommonResult; 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.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.AdminPageDTO;
import cn.iocoder.mall.admin.api.dto.AdminUpdateDTO;
public interface AdminService { public interface AdminService {
CommonResult<AdminPageBO> getAdminPage(AdminPageDTO adminPageDTO); CommonResult<AdminPageBO> getAdminPage(AdminPageDTO adminPageDTO);
CommonResult<AdminBO> addAdmin(Integer adminId, AdminAddDTO adminAddDTO);
CommonResult<Boolean> updateAdmin(Integer adminId, AdminUpdateDTO adminUpdateDTO);
CommonResult<Boolean> updateAdminStatus(Integer adminId, Integer updateAdminId, Integer status);
CommonResult<Boolean> deleteAdmin(Integer adminId, Integer updateAdminId);
} }

View File

@ -30,6 +30,4 @@ public interface OAuth2Service {
// TODO @see 刷新 token // TODO @see 刷新 token
// TODO @see 移除 token
} }

View File

@ -24,6 +24,9 @@ public enum AdminErrorCodeEnum {
ADMIN_USERNAME_NOT_REGISTERED(1002002000, "账号不存在"), ADMIN_USERNAME_NOT_REGISTERED(1002002000, "账号不存在"),
ADMIN_PASSWORD_ERROR(1002002001, "密码不正确"), ADMIN_PASSWORD_ERROR(1002002001, "密码不正确"),
ADMIN_IS_DISABLE(1002002002, "账号被禁用"), ADMIN_IS_DISABLE(1002002002, "账号被禁用"),
ADMIN_USERNAME_EXISTS(1002002002, "账号已经存在"),
ADMIN_STATUS_EQUALS(1002002003, "账号已经是该状态"),
ADMIN_DELETE_ONLY_DISABLE(1002002004, "只有关闭的账号才可以删除"),
// ========== 资源模块 1002003000 ========== // ========== 资源模块 1002003000 ==========
RESOURCE_NAME_DUPLICATE(1002003000, "已经存在该名字的资源"), RESOURCE_NAME_DUPLICATE(1002003000, "已经存在该名字的资源"),

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,6 +1,8 @@
package cn.iocoder.mall.admin.convert; package cn.iocoder.mall.admin.convert;
import cn.iocoder.mall.admin.api.bo.AdminBO; 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 cn.iocoder.mall.admin.dataobject.AdminDO;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.Mappings; import org.mapstruct.Mappings;
@ -16,6 +18,12 @@ public interface AdminConvert {
@Mappings({}) @Mappings({})
AdminBO convert(AdminDO adminDO); AdminBO convert(AdminDO adminDO);
@Mappings({})
AdminDO convert(AdminAddDTO adminAddDTO);
@Mappings({})
AdminDO convert(AdminUpdateDTO adminUpdateDTO);
@Mappings({}) @Mappings({})
List<AdminBO> convert(List<AdminDO> adminBOs); List<AdminBO> convert(List<AdminDO> adminBOs);

View File

@ -9,6 +9,8 @@ import java.util.List;
@Repository @Repository
public interface AdminMapper { public interface AdminMapper {
AdminDO selectById(@Param("id") Integer id);
AdminDO selectByUsername(@Param("username") String username); AdminDO selectByUsername(@Param("username") String username);
List<AdminDO> selectListByNicknameLike(@Param("nickname") String nickname, List<AdminDO> selectListByNicknameLike(@Param("nickname") String nickname,
@ -17,4 +19,8 @@ public interface AdminMapper {
Integer selectCountByNicknameLike(@Param("nickname") String nickname); Integer selectCountByNicknameLike(@Param("nickname") String nickname);
void insert(AdminDO admin);
int update(AdminDO admin);
} }

View File

@ -11,4 +11,8 @@ public interface AdminRoleMapper {
List<AdminRoleDO> selectByAdminId(@Param("adminId") Integer adminId); List<AdminRoleDO> selectByAdminId(@Param("adminId") Integer adminId);
int updateToDeletedByAdminId(@Param("adminId") Integer adminId);
int updateToDeletedByRoleId(@Param("roleId") Integer roleId);
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.admin.dao; package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.OAuth2AccessTokenDO; import cn.iocoder.mall.admin.dataobject.OAuth2AccessTokenDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
@ -8,6 +9,8 @@ public interface OAuth2AccessTokenMapper {
void insert(OAuth2AccessTokenDO entity); void insert(OAuth2AccessTokenDO entity);
OAuth2AccessTokenDO selectByTokenId(String tokenId); OAuth2AccessTokenDO selectByTokenId(@Param("id") String id);
int updateToInvalidByAdminId(@Param("adminId") Integer adminId);
} }

View File

@ -1,6 +1,7 @@
package cn.iocoder.mall.admin.dao; package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.OAuth2RefreshTokenDO; import cn.iocoder.mall.admin.dataobject.OAuth2RefreshTokenDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
@Repository @Repository
@ -8,4 +9,6 @@ public interface OAuth2RefreshTokenMapper {
void insert(OAuth2RefreshTokenDO entity); void insert(OAuth2RefreshTokenDO entity);
int updateToInvalidByAdminId(@Param("adminId") Integer adminId);
} }

View File

@ -1,11 +1,11 @@
package cn.iocoder.mall.admin.dataobject; package cn.iocoder.mall.admin.dataobject;
import java.util.Date; import cn.iocoder.common.framework.dataobject.BaseDO;
/** /**
* {@link AdminDO} {@link RoleDO} 的关联表 * {@link AdminDO} {@link RoleDO} 的关联表
*/ */
public class AdminRoleDO { public class AdminRoleDO extends BaseDO {
/** /**
* 编号 * 编号
@ -19,12 +19,6 @@ public class AdminRoleDO {
* 角色编号(外键{@link RoleDO} * 角色编号(外键{@link RoleDO}
*/ */
private Integer roleId; private Integer roleId;
/**
* 创建时间
*/
private Date createTime;
// TODO 芋艿 删除状态
public Integer getId() { public Integer getId() {
return id; return id;
@ -53,13 +47,4 @@ public class AdminRoleDO {
return this; return this;
} }
public Date getCreateTime() {
return createTime;
}
public AdminRoleDO setCreateTime(Date createTime) {
this.createTime = createTime;
return this;
}
} }

View File

@ -1,20 +1,27 @@
package cn.iocoder.mall.admin.service; 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.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult; import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.AdminService; 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.bo.AdminPageBO;
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum; 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.AdminPageDTO;
import cn.iocoder.mall.admin.api.dto.AdminUpdateDTO;
import cn.iocoder.mall.admin.convert.AdminConvert; 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.AdminMapper;
import cn.iocoder.mall.admin.dao.AdminRoleMapper; 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.AdminRoleDO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import java.util.Date;
import java.util.List; import java.util.List;
@Service @Service
@ -25,6 +32,8 @@ public class AdminServiceImpl implements AdminService {
private AdminMapper adminMapper; private AdminMapper adminMapper;
@Autowired @Autowired
private AdminRoleMapper adminRoleMapper; private AdminRoleMapper adminRoleMapper;
@Autowired
private OAuth2ServiceImpl oAuth2Service;
public CommonResult<AdminDO> validAdmin(String username, String password) { public CommonResult<AdminDO> validAdmin(String username, String password) {
AdminDO admin = adminMapper.selectByUsername(username); AdminDO admin = adminMapper.selectByUsername(username);
@ -33,7 +42,7 @@ public class AdminServiceImpl implements AdminService {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_USERNAME_NOT_REGISTERED.getCode()); 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()); return ServiceExceptionUtil.error(AdminErrorCodeEnum.ADMIN_PASSWORD_ERROR.getCode());
} }
// 账号被禁用 // 账号被禁用
@ -62,4 +71,98 @@ public class AdminServiceImpl implements AdminService {
return CommonResult.success(adminPage); return CommonResult.success(adminPage);
} }
@Override
public CommonResult<AdminBO> 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<Boolean> 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<Boolean> 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<Boolean> 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);
}
} }

View File

@ -13,6 +13,7 @@ import cn.iocoder.mall.admin.dataobject.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -46,6 +47,7 @@ public class OAuth2ServiceImpl implements OAuth2Service {
private ResourceServiceImpl resourceService; private ResourceServiceImpl resourceService;
@Override @Override
@Transactional
public CommonResult<OAuth2AccessTokenBO> getAccessToken(String username, String password) { public CommonResult<OAuth2AccessTokenBO> getAccessToken(String username, String password) {
CommonResult<AdminDO> adminResult = adminService.validAdmin(username, password); CommonResult<AdminDO> adminResult = adminService.validAdmin(username, password);
// 校验失败返回错误结果 // 校验失败返回错误结果
@ -79,6 +81,19 @@ public class OAuth2ServiceImpl implements OAuth2Service {
return CommonResult.success(OAuth2Convert.INSTANCE.convertToAuthentication(accessTokenDO, adminRoleDOs)); 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 @Override
public CommonResult<Boolean> checkPermission(Integer adminId, Set<Integer> roleIds, String url) { public CommonResult<Boolean> checkPermission(Integer adminId, Set<Integer> roleIds, String url) {
// 如果未配置该资源说明无需权限控制 // 如果未配置该资源说明无需权限控制

View File

@ -16,6 +16,7 @@ import cn.iocoder.mall.admin.dao.RoleResourceMapper;
import cn.iocoder.mall.admin.dataobject.ResourceDO; import cn.iocoder.mall.admin.dataobject.ResourceDO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -116,6 +117,7 @@ public class ResourceServiceImpl implements ResourceService {
} }
@Override @Override
@Transactional
public CommonResult<Boolean> deleteResource(Integer adminId, Integer resourceId) { public CommonResult<Boolean> deleteResource(Integer adminId, Integer resourceId) {
// 校验更新的资源是否存在 // 校验更新的资源是否存在
if (resourceMapper.selectById(resourceId) == null) { if (resourceMapper.selectById(resourceId) == null) {

View File

@ -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.RolePageDTO;
import cn.iocoder.mall.admin.api.dto.RoleUpdateDTO; import cn.iocoder.mall.admin.api.dto.RoleUpdateDTO;
import cn.iocoder.mall.admin.convert.RoleConvert; 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.RoleMapper;
import cn.iocoder.mall.admin.dao.RoleResourceMapper; import cn.iocoder.mall.admin.dao.RoleResourceMapper;
import cn.iocoder.mall.admin.dataobject.ResourceDO; 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 cn.iocoder.mall.admin.dataobject.RoleResourceDO;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -31,6 +33,8 @@ public class RoleServiceImpl implements RoleService {
@Autowired @Autowired
private RoleResourceMapper roleResourceMapper; private RoleResourceMapper roleResourceMapper;
@Autowired @Autowired
private AdminRoleMapper adminRoleMapper;
@Autowired
private RoleMapper roleMapper; private RoleMapper roleMapper;
@Autowired @Autowired
@ -84,6 +88,7 @@ public class RoleServiceImpl implements RoleService {
} }
@Override @Override
@Transactional
public CommonResult<Boolean> deleteRole(Integer adminId, Integer roleId) { public CommonResult<Boolean> deleteRole(Integer adminId, Integer roleId) {
// 校验角色是否存在 // 校验角色是否存在
if (roleMapper.selectById(roleId) == null) { if (roleMapper.selectById(roleId) == null) {
@ -93,12 +98,17 @@ public class RoleServiceImpl implements RoleService {
RoleDO roleDO = new RoleDO().setId(roleId); RoleDO roleDO = new RoleDO().setId(roleId);
roleDO.setDeleted(RoleDO.DELETED_YES); roleDO.setDeleted(RoleDO.DELETED_YES);
roleMapper.update(roleDO); roleMapper.update(roleDO);
// 标记删除 RoleResource
roleResourceMapper.updateToDeletedByRoleId(roleId);
// 标记删除 AdminRole
adminRoleMapper.updateToDeletedByRoleId(roleId);
// TODO 插入操作日志 // TODO 插入操作日志
// 返回成功 // 返回成功
return CommonResult.success(true); return CommonResult.success(true);
} }
@Override @Override
@Transactional
public CommonResult<Boolean> assignResource(Integer adminId, Integer roleId, Set<Integer> resourceIds) { public CommonResult<Boolean> assignResource(Integer adminId, Integer roleId, Set<Integer> resourceIds) {
// 校验角色是否存在 // 校验角色是否存在
if (roleMapper.selectById(roleId) == null) { if (roleMapper.selectById(roleId) == null) {

View File

@ -40,4 +40,44 @@
</where> </where>
</select> </select>
<insert id="insert" parameterType="AdminDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO admin (
username, nickname, password, status,
create_time, deleted
) VALUES (
#{username}, #{nickname}, #{password}, #{status},
#{createTime}, #{deleted}
)
</insert>
<select id="selectById" parameterType="Integer" resultType="AdminDO">
SELECT
<include refid="FIELDS" />
FROM admin
WHERE id = #{id}
AND deleted = 0
</select>
<update id="update" parameterType="RoleDO">
UPDATE admin
<set>
<if test="username != null">
, username = #{username}
</if>
<if test="nickname != null">
, nickname = #{nickname}
</if>
<if test="password != null">
, password = #{password}
</if>
<if test="status != null">
, status = #{status}
</if>
<if test="deleted != null">
, deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper> </mapper>

View File

@ -18,4 +18,18 @@
AND a.id = ar.admin_id AND a.id = ar.admin_id
</select> </select>
<update id="updateToDeletedByAdminId" parameterType="Integer">
UPDATE admin_role
SET deleted = 1
WHERE admin_id = #{adminId}
AND deleted = 0
</update>
<update id="updateToDeletedByRoleId" parameterType="Integer">
UPDATE admin_role
SET deleted = 1
WHERE role_id = #{roleId}
AND deleted = 0
</update>
</mapper> </mapper>

View File

@ -19,4 +19,11 @@
WHERE id = #{id} WHERE id = #{id}
</select> </select>
<update id="updateToInvalidByAdminId" parameterType="Integer">
UPDATE oauth2_access_token
SET valid = 0
WHERE admin_id = #{adminId}
AND valid = 1
</update>
</mapper> </mapper>

View File

@ -10,4 +10,11 @@
) )
</insert> </insert>
<update id="updateToInvalidByAdminId" parameterType="Integer">
UPDATE oauth2_refresh_token
SET valid = 0
WHERE admin_id = #{adminId}
AND valid = 1
</update>
</mapper> </mapper>