将当前管理员相关的信息、权限等接口,迁移到 Passport 模块,更加统一
parent
74d05557e1
commit
2daea0060d
|
@ -1,10 +1,14 @@
|
|||
package cn.iocoder.common.framework.util;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 校验工具类
|
||||
*/
|
||||
public class ValidationUtil {
|
||||
|
||||
private static Pattern PATTERN_URL = Pattern.compile("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]");
|
||||
|
||||
public static boolean isMobile(String mobile) {
|
||||
if (mobile == null || mobile.length() != 11) {
|
||||
return false;
|
||||
|
@ -13,4 +17,13 @@ public class ValidationUtil {
|
|||
return true;
|
||||
}
|
||||
|
||||
public static boolean isURL(String url) {
|
||||
return StringUtils.hasText(url)
|
||||
&& PATTERN_URL.matcher(url).matches();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(isURL("http://www.iocoder.cn"));
|
||||
}
|
||||
|
||||
}
|
|
@ -8,7 +8,6 @@ import cn.iocoder.mall.managementweb.controller.admin.dto.AdminPageDTO;
|
|||
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateInfoDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.admin.dto.AdminUpdateStatusDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.admin.vo.AdminPageItemVO;
|
||||
import cn.iocoder.mall.managementweb.controller.admin.vo.AdminVO;
|
||||
import cn.iocoder.mall.managementweb.manager.admin.AdminManager;
|
||||
import cn.iocoder.mall.security.admin.core.context.AdminSecurityContextHolder;
|
||||
import cn.iocoder.security.annotations.RequiresPermissions;
|
||||
|
@ -62,12 +61,4 @@ public class AdminController {
|
|||
return success(true);
|
||||
}
|
||||
|
||||
// =========== 当前管理员 API ===========
|
||||
|
||||
@GetMapping("/info")
|
||||
@ApiOperation(value = "更新管理员状态")
|
||||
public CommonResult<AdminVO> info() {
|
||||
return success(adminManager.getAdmin(AdminSecurityContextHolder.getAdminId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
### /passport/login 成功
|
||||
POST {{baseUrl}}/passport/login
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
username=admin&password=buzhidao
|
||||
|
||||
### /passport/login 密码不正确
|
||||
POST {{baseUrl}}/passport/login
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
username=admin&password=1024
|
||||
|
||||
###
|
|
@ -1,36 +0,0 @@
|
|||
package cn.iocoder.mall.managementweb.controller.passport;
|
||||
|
||||
import cn.iocoder.common.framework.util.HttpUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.dto.AdminPassportLoginDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.AdminPassportVO;
|
||||
import cn.iocoder.mall.managementweb.manager.passport.AdminPassportManager;
|
||||
import cn.iocoder.security.annotations.RequiresNone;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
|
||||
@Api(tags = "管理员 Passport API")
|
||||
@RestController
|
||||
@RequestMapping("/passport")
|
||||
public class AdminPassportController {
|
||||
|
||||
@Autowired
|
||||
private AdminPassportManager adminPassportManager;
|
||||
|
||||
@ApiOperation("账号密码登陆")
|
||||
@PostMapping("/login")
|
||||
@RequiresNone
|
||||
public CommonResult<AdminPassportVO> login(AdminPassportLoginDTO loginDTO,
|
||||
HttpServletRequest request) {
|
||||
return success(adminPassportManager.login(loginDTO, HttpUtil.getIp(request)));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
### /passport/login 成功
|
||||
POST {{baseUrl}}/passport/login
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
username=admin&password=buzhidao
|
||||
|
||||
### /passport/login 密码不正确
|
||||
POST {{baseUrl}}/passport/login
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
username=admin&password=1024
|
||||
|
||||
### /passport/info 成功
|
||||
GET {{baseUrl}}/passport/info
|
||||
Authorization: Bearer {{accessToken}}
|
||||
|
||||
### /passport/tree-admin-menu 成功
|
||||
GET {{baseUrl}}/passport/tree-admin-menu
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{accessToken}}
|
||||
|
||||
### /passport/list-admin-permission 成功
|
||||
GET {{baseUrl}}/passport/list-admin-permission
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{accessToken}}
|
||||
|
||||
###
|
|
@ -0,0 +1,61 @@
|
|||
package cn.iocoder.mall.managementweb.controller.passport;
|
||||
|
||||
import cn.iocoder.common.framework.util.HttpUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.dto.PassportLoginDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAccessTokenVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAdminMenuTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAdminVO;
|
||||
import cn.iocoder.mall.managementweb.manager.passport.PassportManager;
|
||||
import cn.iocoder.mall.security.admin.core.context.AdminSecurityContextHolder;
|
||||
import cn.iocoder.security.annotations.RequiresNone;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
|
||||
@Api(tags = "管理员 Passport API")
|
||||
@RestController
|
||||
@RequestMapping("/passport")
|
||||
public class PassportController {
|
||||
|
||||
@Autowired
|
||||
private PassportManager passportManager;
|
||||
|
||||
@PostMapping("/login")
|
||||
@ApiOperation("账号密码登陆")
|
||||
@RequiresNone
|
||||
public CommonResult<PassportAccessTokenVO> login(PassportLoginDTO loginDTO,
|
||||
HttpServletRequest request) {
|
||||
return success(passportManager.login(loginDTO, HttpUtil.getIp(request)));
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
@ApiOperation(value = "获得当前管理员信息")
|
||||
public CommonResult<PassportAdminVO> info() {
|
||||
return success(passportManager.getAdmin(AdminSecurityContextHolder.getAdminId()));
|
||||
}
|
||||
|
||||
@GetMapping("/tree-admin-menu")
|
||||
@ApiOperation("获得当前管理员的菜单树")
|
||||
public CommonResult<List<PassportAdminMenuTreeNodeVO>> treeAdminMenu() {
|
||||
return success(passportManager.treeAdminMenu(AdminSecurityContextHolder.getAdminId()));
|
||||
}
|
||||
|
||||
@GetMapping("/list-admin-permission")
|
||||
@ApiOperation("获得当前管理员的权限列表")
|
||||
public CommonResult<Set<String>> listAdminPermission() {
|
||||
return success(passportManager.listAdminPermission(AdminSecurityContextHolder.getAdminId()));
|
||||
}
|
||||
|
||||
}
|
|
@ -13,7 +13,7 @@ import java.io.Serializable;
|
|||
@ApiModel("管理登录 DTO")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminPassportLoginDTO implements Serializable {
|
||||
public class PassportLoginDTO implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "用户名", required = true, example = "yudaoyuanma")
|
||||
@NotEmpty(message = "登陆账号不能为空")
|
|
@ -1,50 +0,0 @@
|
|||
package cn.iocoder.mall.managementweb.controller.passport.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("管理员通信证信息 VO")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminPassportVO {
|
||||
|
||||
@ApiModel("认证信息")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class Authentication {
|
||||
|
||||
@ApiModelProperty(value = "访问令牌", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String accessToken;
|
||||
@ApiModelProperty(value = "刷新令牌", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String refreshToken;
|
||||
@ApiModelProperty(value = "过期时间", required = true)
|
||||
private Date expiresTime;
|
||||
|
||||
}
|
||||
|
||||
@ApiModel("管理员信息")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class Admin {
|
||||
|
||||
@ApiModelProperty(value = "用户编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
@ApiModelProperty(value = "真实姓名", required = true, example = "小王")
|
||||
private String name;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员信息
|
||||
*/
|
||||
private Admin admin;
|
||||
/**
|
||||
* 认证信息
|
||||
*/
|
||||
private Authentication authorization;
|
||||
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package cn.iocoder.mall.managementweb.controller.passport.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@ApiModel("访问令牌信息 VO")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class PassportAccessTokenVO {
|
||||
|
||||
@ApiModelProperty(value = "访问令牌", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String accessToken;
|
||||
@ApiModelProperty(value = "刷新令牌", required = true, example = "001e8f49b20e47f7b3a2de774497cd50")
|
||||
private String refreshToken;
|
||||
@ApiModelProperty(value = "过期时间", required = true)
|
||||
private Date expiresTime;
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package cn.iocoder.mall.managementweb.controller.permission.vo;
|
||||
package cn.iocoder.mall.managementweb.controller.passport.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
|
@ -10,7 +10,7 @@ import java.util.List;
|
|||
@ApiModel(value = "管理员拥有的菜单树", description = "一般用于首页菜单")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class AdminMenuTreeNodeVO {
|
||||
public class PassportAdminMenuTreeNodeVO {
|
||||
|
||||
@ApiModelProperty(value = "菜单编号", required = true, example = "1")
|
||||
private Integer id;
|
||||
|
@ -28,6 +28,6 @@ public class AdminMenuTreeNodeVO {
|
|||
/**
|
||||
* 子节点数组
|
||||
*/
|
||||
private List<AdminMenuTreeNodeVO> children;
|
||||
private List<PassportAdminMenuTreeNodeVO> children;
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package cn.iocoder.mall.managementweb.controller.passport.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@ApiModel("管理员信息 VO")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class PassportAdminVO {
|
||||
|
||||
@ApiModelProperty(value = "真实名字", required = true, example = "小王")
|
||||
private String name;
|
||||
@ApiModelProperty(value = "头像", required = true, example = "http://www.iocoder.cn/xxx.jpg")
|
||||
private String avatar;
|
||||
|
||||
}
|
|
@ -34,14 +34,4 @@ GET {{baseUrl}}/resource/tree
|
|||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{accessToken}}
|
||||
|
||||
### /resource/tree-admin-menu 成功
|
||||
GET {{baseUrl}}/resource/tree-admin-menu
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{accessToken}}
|
||||
|
||||
### /resource/list-admin-permission 成功
|
||||
GET {{baseUrl}}/resource/list-admin-permission
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
Authorization: Bearer {{accessToken}}
|
||||
|
||||
###
|
||||
|
|
|
@ -3,7 +3,6 @@ package cn.iocoder.mall.managementweb.controller.permission;
|
|||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceCreateDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceUpdateDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.AdminMenuTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceVO;
|
||||
import cn.iocoder.mall.managementweb.manager.permission.ResourceManager;
|
||||
|
@ -17,7 +16,6 @@ import org.springframework.web.bind.annotation.*;
|
|||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import static cn.iocoder.common.framework.vo.CommonResult.success;
|
||||
|
||||
|
@ -73,18 +71,4 @@ public class ResourceController {
|
|||
return success(resourceManager.treeResource());
|
||||
}
|
||||
|
||||
// =========== 当前管理员相关 API ===========
|
||||
|
||||
@GetMapping("/tree-admin-menu")
|
||||
@ApiOperation("获得当前登陆的管理员的菜单树")
|
||||
public CommonResult<List<AdminMenuTreeNodeVO>> treeAdminMenu() {
|
||||
return success(resourceManager.treeAdminMenu(AdminSecurityContextHolder.getAdminId()));
|
||||
}
|
||||
|
||||
@GetMapping("/list-admin-permission")
|
||||
@ApiOperation("获得当前登陆的管理员的权限列表")
|
||||
public CommonResult<Set<String>> listAdminPermission() {
|
||||
return success(resourceManager.listAdminPermission(AdminSecurityContextHolder.getAdminId()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
package cn.iocoder.mall.managementweb.convert.passport;
|
||||
|
||||
import cn.iocoder.mall.managementweb.controller.passport.dto.AdminPassportLoginDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.AdminPassportVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.dto.PassportLoginDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAccessTokenVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAdminVO;
|
||||
import cn.iocoder.mall.systemservice.rpc.admin.dto.AdminVerifyPasswordDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.vo.OAuth2AccessTokenVO;
|
||||
|
@ -13,12 +14,10 @@ public interface AdminPassportConvert {
|
|||
|
||||
AdminPassportConvert INSTANCE = Mappers.getMapper(AdminPassportConvert.class);
|
||||
|
||||
AdminVerifyPasswordDTO convert(AdminPassportLoginDTO loginDTO);
|
||||
AdminVerifyPasswordDTO convert(PassportLoginDTO bean);
|
||||
|
||||
default AdminPassportVO convert(AdminVO adminVO, OAuth2AccessTokenVO accessTokenVO) {
|
||||
return new AdminPassportVO().setAdmin(convert(adminVO)).setAuthorization(convert(accessTokenVO));
|
||||
}
|
||||
AdminPassportVO.Admin convert(AdminVO adminVO);
|
||||
AdminPassportVO.Authentication convert(OAuth2AccessTokenVO accessTokenVO);
|
||||
PassportAccessTokenVO convert(OAuth2AccessTokenVO bean);
|
||||
|
||||
PassportAdminVO convert(AdminVO bean);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package cn.iocoder.mall.managementweb.convert.permission;
|
||||
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.AdminMenuTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAdminMenuTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceVO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.dto.ResourceCreateDTO;
|
||||
|
@ -25,6 +25,6 @@ public interface ResourceConvert {
|
|||
|
||||
ResourceTreeNodeVO convertTreeNode(cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO bean);
|
||||
|
||||
List<AdminMenuTreeNodeVO> convert(List<ResourceTreeNodeVO> list);
|
||||
List<PassportAdminMenuTreeNodeVO> convert(List<ResourceTreeNodeVO> list);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
package cn.iocoder.mall.managementweb.manager.passport;
|
||||
|
||||
import cn.iocoder.common.framework.enums.UserTypeEnum;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.dto.AdminPassportLoginDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.AdminPassportVO;
|
||||
import cn.iocoder.mall.managementweb.convert.passport.AdminPassportConvert;
|
||||
import cn.iocoder.mall.systemservice.rpc.admin.AdminRpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.OAuth2Rpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.dto.OAuth2CreateAccessTokenDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.vo.OAuth2AccessTokenVO;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AdminPassportManager {
|
||||
|
||||
@Reference(version = "${dubbo.consumer.AdminRpc.version}", validation = "false")
|
||||
private AdminRpc adminRpc;
|
||||
@Reference(version = "${dubbo.consumer.OAuth2Rpc.version}", validation = "false")
|
||||
private OAuth2Rpc oauth2Rpc;
|
||||
|
||||
public AdminPassportVO login(AdminPassportLoginDTO loginDTO, String ip) {
|
||||
// 校验管理员密码
|
||||
CommonResult<AdminVO> verifyPasswordResult = adminRpc.verifyPassword(AdminPassportConvert.INSTANCE.convert(loginDTO).setIp(ip));
|
||||
verifyPasswordResult.checkError();
|
||||
// 创建访问令牌
|
||||
CommonResult<OAuth2AccessTokenVO> createAccessTokenResult = oauth2Rpc.createAccessToken(
|
||||
new OAuth2CreateAccessTokenDTO().setUserId(verifyPasswordResult.getData().getId())
|
||||
.setUserType(UserTypeEnum.ADMIN.getValue()).setCreateIp(ip));
|
||||
createAccessTokenResult.checkError();
|
||||
// 返回
|
||||
return AdminPassportConvert.INSTANCE.convert(verifyPasswordResult.getData(), createAccessTokenResult.getData());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
package cn.iocoder.mall.managementweb.manager.passport;
|
||||
|
||||
import cn.iocoder.common.framework.enums.UserTypeEnum;
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.dto.PassportLoginDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAccessTokenVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAdminMenuTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.passport.vo.PassportAdminVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.convert.passport.AdminPassportConvert;
|
||||
import cn.iocoder.mall.managementweb.convert.permission.ResourceConvert;
|
||||
import cn.iocoder.mall.managementweb.manager.permission.ResourceManager;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
import cn.iocoder.mall.systemservice.rpc.admin.AdminRpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.admin.vo.AdminVO;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.OAuth2Rpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.dto.OAuth2CreateAccessTokenDTO;
|
||||
import cn.iocoder.mall.systemservice.rpc.oauth.vo.OAuth2AccessTokenVO;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.ResourceRpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.RoleRpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO;
|
||||
import org.apache.dubbo.config.annotation.Reference;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@Service
|
||||
public class PassportManager {
|
||||
|
||||
@Reference(version = "${dubbo.consumer.AdminRpc.version}", validation = "false")
|
||||
private AdminRpc adminRpc;
|
||||
@Reference(version = "${dubbo.consumer.OAuth2Rpc.version}", validation = "false")
|
||||
private OAuth2Rpc oauth2Rpc;
|
||||
@Reference(version = "${dubbo.consumer.RoleRpc.version}", validation = "false")
|
||||
private RoleRpc roleRpc;
|
||||
@Reference(version = "${dubbo.consumer.ResourceRpc.version}", validation = "false")
|
||||
private ResourceRpc resourceRpc;
|
||||
|
||||
public PassportAccessTokenVO login(PassportLoginDTO loginDTO, String ip) {
|
||||
// 校验管理员密码
|
||||
CommonResult<AdminVO> verifyPasswordResult = adminRpc.verifyPassword(AdminPassportConvert.INSTANCE.convert(loginDTO).setIp(ip));
|
||||
verifyPasswordResult.checkError();
|
||||
// 创建访问令牌
|
||||
CommonResult<OAuth2AccessTokenVO> createAccessTokenResult = oauth2Rpc.createAccessToken(
|
||||
new OAuth2CreateAccessTokenDTO().setUserId(verifyPasswordResult.getData().getId())
|
||||
.setUserType(UserTypeEnum.ADMIN.getValue()).setCreateIp(ip));
|
||||
createAccessTokenResult.checkError();
|
||||
// 返回
|
||||
return AdminPassportConvert.INSTANCE.convert(createAccessTokenResult.getData());
|
||||
}
|
||||
|
||||
public PassportAdminVO getAdmin(Integer adminId) {
|
||||
CommonResult<AdminVO> getAdminResult = adminRpc.getAdmin(adminId);
|
||||
getAdminResult.checkError();
|
||||
return AdminPassportConvert.INSTANCE.convert(getAdminResult.getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定管理员的权限列表
|
||||
*
|
||||
* @param adminId 管理员编号
|
||||
* @return 权限列表
|
||||
*/
|
||||
public Set<String> listAdminPermission(Integer adminId) {
|
||||
// 获得管理员拥有的角色编号列表
|
||||
CommonResult<Set<Integer>> listAdminRoleIdsResult = roleRpc.listAdminRoleIds(adminId);
|
||||
listAdminRoleIdsResult.checkError();
|
||||
if (CollectionUtils.isEmpty(listAdminRoleIdsResult.getData())) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
// 获得角色拥有的资源列表
|
||||
CommonResult<List<ResourceVO>> resourceVOResult = resourceRpc.listRoleResource(
|
||||
listAdminRoleIdsResult.getData(), null);
|
||||
resourceVOResult.checkError();
|
||||
return CollectionUtils.convertSet(resourceVOResult.getData(), cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO::getPermission);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得管理员的菜单树
|
||||
*
|
||||
* @param adminId 管理员编号
|
||||
* @return 菜单树
|
||||
*/
|
||||
public List<PassportAdminMenuTreeNodeVO> treeAdminMenu(Integer adminId) {
|
||||
// 获得管理员拥有的角色编号列表
|
||||
CommonResult<Set<Integer>> listAdminRoleIdsResult = roleRpc.listAdminRoleIds(adminId);
|
||||
listAdminRoleIdsResult.checkError();
|
||||
if (CollectionUtils.isEmpty(listAdminRoleIdsResult.getData())) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 获得角色拥有的资源(菜单)列表
|
||||
CommonResult<List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO>> resourceVOResult = resourceRpc.listRoleResource(
|
||||
listAdminRoleIdsResult.getData(), ResourceTypeEnum.MENU.getType());
|
||||
resourceVOResult.checkError();
|
||||
// 构建菜单树
|
||||
return this.buildAdminMenuTree(resourceVOResult.getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树
|
||||
*
|
||||
* @param resourceVOs 资源(都是菜单)列表
|
||||
* @return 菜单树
|
||||
*/
|
||||
private List<PassportAdminMenuTreeNodeVO> buildAdminMenuTree(List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO> resourceVOs) {
|
||||
List<ResourceTreeNodeVO> treeNodeVOS = ResourceManager.buildResourceTree(resourceVOs);
|
||||
// 虽然多了一层转换,但是可维护性更好。
|
||||
return ResourceConvert.INSTANCE.convert(treeNodeVOS);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,15 +1,12 @@
|
|||
package cn.iocoder.mall.managementweb.manager.permission;
|
||||
|
||||
import cn.iocoder.common.framework.util.CollectionUtils;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceCreateDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.dto.ResourceUpdateDTO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.AdminMenuTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceTreeNodeVO;
|
||||
import cn.iocoder.mall.managementweb.controller.permission.vo.ResourceVO;
|
||||
import cn.iocoder.mall.managementweb.convert.permission.ResourceConvert;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceIdEnum;
|
||||
import cn.iocoder.mall.systemservice.enums.permission.ResourceTypeEnum;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.ResourceRpc;
|
||||
import cn.iocoder.mall.systemservice.rpc.permission.RoleRpc;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -97,40 +94,7 @@ public class ResourceManager {
|
|||
CommonResult<List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO>> listResourceResult = resourceRpc.listResource();
|
||||
listResourceResult.checkError();
|
||||
// 构建菜单树
|
||||
return this.buildResourceTree(listResourceResult.getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得管理员的菜单树
|
||||
*
|
||||
* @param adminId 管理员编号
|
||||
* @return 菜单树
|
||||
*/
|
||||
public List<AdminMenuTreeNodeVO> treeAdminMenu(Integer adminId) {
|
||||
// 获得管理员拥有的角色编号列表
|
||||
CommonResult<Set<Integer>> listAdminRoleIdsResult = roleRpc.listAdminRoleIds(adminId);
|
||||
listAdminRoleIdsResult.checkError();
|
||||
if (CollectionUtils.isEmpty(listAdminRoleIdsResult.getData())) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
// 获得角色拥有的资源(菜单)列表
|
||||
CommonResult<List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO>> resourceVOResult = resourceRpc.listRoleResource(
|
||||
listAdminRoleIdsResult.getData(), ResourceTypeEnum.MENU.getType());
|
||||
resourceVOResult.checkError();
|
||||
// 构建菜单树
|
||||
return this.buildAdminMenuTree(resourceVOResult.getData());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建菜单树
|
||||
*
|
||||
* @param resourceVOs 资源(都是菜单)列表
|
||||
* @return 菜单树
|
||||
*/
|
||||
private List<AdminMenuTreeNodeVO> buildAdminMenuTree(List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO> resourceVOs) {
|
||||
List<ResourceTreeNodeVO> treeNodeVOS = this.buildResourceTree(resourceVOs);
|
||||
// 虽然多了一层转换,但是可维护性更好。
|
||||
return ResourceConvert.INSTANCE.convert(treeNodeVOS);
|
||||
return buildResourceTree(listResourceResult.getData());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,7 +103,7 @@ public class ResourceManager {
|
|||
* @param resourceVOs 资源列表
|
||||
* @return 资源树
|
||||
*/
|
||||
private List<ResourceTreeNodeVO> buildResourceTree(List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO> resourceVOs) {
|
||||
public static List<ResourceTreeNodeVO> buildResourceTree(List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO> resourceVOs) {
|
||||
// 排序,保证菜单的有序性
|
||||
resourceVOs.sort(Comparator.comparing(cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO::getSort));
|
||||
// 构建菜单树
|
||||
|
@ -164,24 +128,4 @@ public class ResourceManager {
|
|||
return treeNodeMap.values().stream().filter(node -> node.getPid().equals(ResourceIdEnum.ROOT.getId())).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得指定管理员的权限列表
|
||||
*
|
||||
* @param adminId 管理员编号
|
||||
* @return 权限列表
|
||||
*/
|
||||
public Set<String> listAdminPermission(Integer adminId) {
|
||||
// 获得管理员拥有的角色编号列表
|
||||
CommonResult<Set<Integer>> listAdminRoleIdsResult = roleRpc.listAdminRoleIds(adminId);
|
||||
listAdminRoleIdsResult.checkError();
|
||||
if (CollectionUtils.isEmpty(listAdminRoleIdsResult.getData())) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
// 获得角色拥有的资源列表
|
||||
CommonResult<List<cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO>> resourceVOResult = resourceRpc.listRoleResource(
|
||||
listAdminRoleIdsResult.getData(), null);
|
||||
resourceVOResult.checkError();
|
||||
return CollectionUtils.convertSet(resourceVOResult.getData(), cn.iocoder.mall.systemservice.rpc.permission.vo.ResourceVO::getPermission);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,10 @@ public class AdminDO extends BaseDO {
|
|||
* 真实名字
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 头像
|
||||
*/
|
||||
private String avatar;
|
||||
/**
|
||||
* 部门编号
|
||||
*
|
||||
|
|
|
@ -217,10 +217,11 @@ public class ResourceService {
|
|||
* @param resource 资源
|
||||
*/
|
||||
private void initResourceProperty(ResourceDO resource) {
|
||||
// 初始化资源为按钮类型时,无需 route 和 icon 属性
|
||||
// 资源为按钮类型时,无需 route、icon、view 属性,进行置空
|
||||
if (ResourceTypeEnum.BUTTON.getType().equals(resource.getType())) {
|
||||
resource.setRoute(null);
|
||||
resource.setIcon(null);
|
||||
resource.setView(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue