diff --git a/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionService.java b/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionService.java new file mode 100644 index 00000000..3aaf0657 --- /dev/null +++ b/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionService.java @@ -0,0 +1,43 @@ +/* + * Copyright By ZATI + * Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617 + * All rights reserved. + * + * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or + * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your + * system. + */ +package com.jmsoftware.maf.authcenter.permission.service; + +import com.jmsoftware.maf.authcenter.permission.response.GetServicesInfoResponse; +import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload; +import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse; +import com.jmsoftware.maf.common.exception.BizException; +import org.springframework.validation.annotation.Validated; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; + +/** + * Description: PermissionServiceImpl, change description here. + * + * @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com, date: 2/18/2022 11:22 PM + **/ +@Validated +public interface PermissionService { + /** + * Gets permission list by role id list. + * + * @param payload the payload + * @return the permission list by role id list + */ + GetPermissionListByRoleIdListResponse getPermissionListByRoleIdList(@Valid @NotNull GetPermissionListByRoleIdListPayload payload); + + /** + * Gets services info. + * + * @return the services info + * @throws BizException the business exception + */ + GetServicesInfoResponse getServicesInfo() throws BizException; +} diff --git a/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionServiceImpl.java b/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionServiceImpl.java new file mode 100644 index 00000000..aac45f36 --- /dev/null +++ b/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionServiceImpl.java @@ -0,0 +1,109 @@ +/* + * Copyright By ZATI + * Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617 + * All rights reserved. + * + * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or + * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your + * system. + */ +package com.jmsoftware.maf.authcenter.permission.service.impl; + +import cn.hutool.core.collection.CollUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jmsoftware.maf.authcenter.permission.configuration.PermissionConfiguration; +import com.jmsoftware.maf.authcenter.permission.converter.PermissionMapStructMapper; +import com.jmsoftware.maf.authcenter.permission.response.GetServicesInfoResponse; +import com.jmsoftware.maf.authcenter.permission.service.PermissionDomainService; +import com.jmsoftware.maf.authcenter.permission.service.PermissionService; +import com.jmsoftware.maf.authcenter.role.service.RoleDomainService; +import com.jmsoftware.maf.common.bean.ResponseBodyBean; +import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload; +import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse; +import com.jmsoftware.maf.common.domain.authcenter.permission.PermissionType; +import com.jmsoftware.maf.common.domain.springbootstarter.HttpApiResourcesResponse; +import com.jmsoftware.maf.common.exception.BizException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Description: PermissionServiceImpl, change description here. + * + * @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com, date: 2/18/2022 11:37 PM + **/ +@Slf4j +@Service +@RequiredArgsConstructor +public class PermissionServiceImpl implements PermissionService { + private final PermissionDomainService permissionDomainService; + private final RoleDomainService roleDomainService; + private final DiscoveryClient discoveryClient; + private final RestTemplate restTemplate; + private final PermissionConfiguration permissionConfiguration; + + @Override + public GetPermissionListByRoleIdListResponse getPermissionListByRoleIdList( + @Valid @NotNull GetPermissionListByRoleIdListPayload payload + ) { + val adminRole = this.roleDomainService.checkAdmin(payload.getRoleIdList()); + val response = new GetPermissionListByRoleIdListResponse(); + if (adminRole) { + log.warn("Admin role checked. The role can access any resources"); + val permission = new GetPermissionListByRoleIdListResponse.Permission(); + permission.setUrl("/**"); + permission.setType(PermissionType.BUTTON.getType()); + permission.setPermissionExpression("admin-permission"); + permission.setMethod("*"); + response.getPermissionList().add(permission); + return response; + } + val permissionList = + this.permissionDomainService.getPermissionListByRoleIdList( + payload.getRoleIdList(), payload.getPermissionTypeList()); + response.setPermissionList( + permissionList + .stream() + .map(PermissionMapStructMapper.INSTANCE::of) + .collect(Collectors.toList()) + ); + return response; + } + + @Override + public GetServicesInfoResponse getServicesInfo() throws BizException { + val serviceIdList = this.discoveryClient.getServices(); + log.info("Getting service info from Service ID list: {}", serviceIdList); + val response = new GetServicesInfoResponse(); + val mapper = new ObjectMapper(); + log.info("Ignored service ID: {}", this.permissionConfiguration.getIgnoredServiceIds()); + for (String serviceId : serviceIdList) { + if (CollUtil.contains(this.permissionConfiguration.getIgnoredServiceIds(), serviceId)) { + log.warn("Ignored service ID: {}", serviceId); + continue; + } + ResponseBodyBean responseBodyBean = Optional.ofNullable(this.restTemplate.getForObject( + String.format("http://%s/http-api-resources", serviceId), ResponseBodyBean.class)) + .orElseThrow(() -> new BizException("Internal service mustn't respond null")); + val data = Optional.of(responseBodyBean.getData()) + .orElseThrow(() -> new BizException("HttpApiResourcesResponse mustn't be null")); + val httpApiResourcesResponse = mapper.convertValue(data, HttpApiResourcesResponse.class); + val serviceInfo = new GetServicesInfoResponse.ServiceInfo(); + serviceInfo.setServiceId(serviceId); + serviceInfo.setHttpApiResources(httpApiResourcesResponse); + response.getList().add(serviceInfo); + } + if (CollUtil.isEmpty(response.getList())) { + log.warn("Got am empty ServiceInfo list"); + } + return response; + } +} diff --git a/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/package-info.java b/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/package-info.java new file mode 100644 index 00000000..556108d8 --- /dev/null +++ b/auth-center/auth-center-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/package-info.java @@ -0,0 +1 @@ +package com.jmsoftware.maf.authcenter.permission.service; diff --git a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionDomainService.java b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionDomainService.java index 3b3bf9f6..ca99adff 100644 --- a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionDomainService.java +++ b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionDomainService.java @@ -2,14 +2,9 @@ import com.baomidou.mybatisplus.extension.service.IService; import com.jmsoftware.maf.authcenter.permission.persistence.Permission; -import com.jmsoftware.maf.authcenter.permission.response.GetServicesInfoResponse; -import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload; -import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse; import com.jmsoftware.maf.common.domain.authcenter.permission.PermissionType; -import com.jmsoftware.maf.common.exception.BizException; import org.springframework.validation.annotation.Validated; -import javax.validation.Valid; import javax.validation.constraints.NotEmpty; import java.util.List; @@ -23,14 +18,6 @@ */ @Validated public interface PermissionDomainService extends IService { - /** - * Gets permission list by role id list. - * - * @param payload the payload - * @return the permission list by role id list - */ - GetPermissionListByRoleIdListResponse getPermissionListByRoleIdList(@Valid GetPermissionListByRoleIdListPayload payload); - /** * Gets permission list by role id list. * @@ -38,14 +25,8 @@ public interface PermissionDomainService extends IService { * @param permissionTypeList the permission type list * @return the permission list by role id list */ - List getPermissionListByRoleIdList(@NotEmpty List roleIdList, - @NotEmpty List permissionTypeList); - - /** - * Gets services info. - * - * @return the services info - * @throws BizException the business exception - */ - GetServicesInfoResponse getServicesInfo() throws BizException; + List getPermissionListByRoleIdList( + @NotEmpty List roleIdList, + @NotEmpty List permissionTypeList + ); } diff --git a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionDomainServiceImpl.java b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionDomainServiceImpl.java index b0d8b1b1..28da6396 100644 --- a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionDomainServiceImpl.java +++ b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionDomainServiceImpl.java @@ -1,41 +1,23 @@ package com.jmsoftware.maf.authcenter.permission.service.impl; -import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.jmsoftware.maf.authcenter.permission.configuration.PermissionConfiguration; -import com.jmsoftware.maf.authcenter.permission.converter.PermissionMapStructMapper; import com.jmsoftware.maf.authcenter.permission.mapper.PermissionMapper; import com.jmsoftware.maf.authcenter.permission.persistence.Permission; -import com.jmsoftware.maf.authcenter.permission.response.GetServicesInfoResponse; import com.jmsoftware.maf.authcenter.permission.service.PermissionDomainService; -import com.jmsoftware.maf.authcenter.role.service.RoleDomainService; -import com.jmsoftware.maf.common.bean.ResponseBodyBean; -import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload; -import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse; import com.jmsoftware.maf.common.domain.authcenter.permission.PermissionType; -import com.jmsoftware.maf.common.domain.springbootstarter.HttpApiResourcesResponse; -import com.jmsoftware.maf.common.exception.BizException; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import lombok.val; -import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; -import javax.validation.Valid; import javax.validation.constraints.NotEmpty; import java.util.List; -import java.util.Optional; -import java.util.stream.Collectors; /** *

PermissionDomainServiceImpl

*

* Service implementation of Permission.(Permission) * - * @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com - * @date 5/11/20 8:34 AM + * @author Johnny Miller (锺俊), e-mail: johnnysviva@outlook.com, date: 5/11/20 8:34 AM */ @Slf4j @Service @@ -43,68 +25,11 @@ public class PermissionDomainServiceImpl extends ServiceImpl implements PermissionDomainService { - private final RoleDomainService roleDomainService; - private final DiscoveryClient discoveryClient; - private final RestTemplate restTemplate; - private final PermissionConfiguration permissionConfiguration; - - @Override - public GetPermissionListByRoleIdListResponse getPermissionListByRoleIdList(@Valid GetPermissionListByRoleIdListPayload payload) { - val adminRole = this.roleDomainService.checkAdmin(payload.getRoleIdList()); - val response = new GetPermissionListByRoleIdListResponse(); - if (adminRole) { - log.warn("Admin role checked. The role can access any resources"); - val permission = new GetPermissionListByRoleIdListResponse.Permission(); - permission.setUrl("/**"); - permission.setType(PermissionType.BUTTON.getType()); - permission.setPermissionExpression("admin-permission"); - permission.setMethod("*"); - response.getPermissionList().add(permission); - return response; - } - val permissionList = - this.getPermissionListByRoleIdList(payload.getRoleIdList(), payload.getPermissionTypeList()); - response.setPermissionList( - permissionList - .stream() - .map(PermissionMapStructMapper.INSTANCE::of) - .collect(Collectors.toList()) - ); - return response; - } - @Override - public List getPermissionListByRoleIdList(@NotEmpty List roleIdList, - @NotEmpty List permissionTypeList) { + public List getPermissionListByRoleIdList( + @NotEmpty List roleIdList, + @NotEmpty List permissionTypeList + ) { return this.getBaseMapper().selectPermissionListByRoleIdList(roleIdList, permissionTypeList); } - - @Override - public GetServicesInfoResponse getServicesInfo() throws BizException { - val serviceIdList = this.discoveryClient.getServices(); - log.info("Getting service info from Service ID list: {}", serviceIdList); - val response = new GetServicesInfoResponse(); - val mapper = new ObjectMapper(); - log.info("Ignored service ID: {}", this.permissionConfiguration.getIgnoredServiceIds()); - for (String serviceId : serviceIdList) { - if (CollUtil.contains(this.permissionConfiguration.getIgnoredServiceIds(), serviceId)) { - log.warn("Ignored service ID: {}", serviceId); - continue; - } - ResponseBodyBean responseBodyBean = Optional.ofNullable(this.restTemplate.getForObject( - String.format("http://%s/http-api-resources", serviceId), ResponseBodyBean.class)) - .orElseThrow(() -> new BizException("Internal service mustn't respond null")); - val data = Optional.of(responseBodyBean.getData()) - .orElseThrow(() -> new BizException("HttpApiResourcesResponse mustn't be null")); - val httpApiResourcesResponse = mapper.convertValue(data, HttpApiResourcesResponse.class); - val serviceInfo = new GetServicesInfoResponse.ServiceInfo(); - serviceInfo.setServiceId(serviceId); - serviceInfo.setHttpApiResources(httpApiResourcesResponse); - response.getList().add(serviceInfo); - } - if (CollUtil.isEmpty(response.getList())) { - log.warn("Got am empty ServiceInfo list"); - } - return response; - } } diff --git a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterFeignService.java b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterFeignService.java index c0e09cca..370e400c 100644 --- a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterFeignService.java +++ b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/OssCenterFeignService.java @@ -9,7 +9,7 @@ /** * Description: OssCenterFeignService, change description here. * - * @author 钟俊 (za-zhongjun), email: jun.zhong@zatech.com, date: 2/5/2022 7:46 PM + * @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com, date: 2/5/2022 7:46 PM **/ @Validated public interface OssCenterFeignService { diff --git a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/impl/OssCenterFeignServiceImpl.java b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/impl/OssCenterFeignServiceImpl.java index 199bd945..f1ff432f 100644 --- a/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/impl/OssCenterFeignServiceImpl.java +++ b/auth-center/auth-center-domain/src/main/java/com/jmsoftware/maf/authcenter/remote/impl/OssCenterFeignServiceImpl.java @@ -15,7 +15,7 @@ /** * Description: OssCenterFeignServiceImpl, change description here. * - * @author 钟俊 (za-zhongjun), email: jun.zhong@zatech.com, date: 2/5/2022 7:47 PM + * @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com, date: 2/5/2022 7:47 PM **/ @Slf4j @Service diff --git a/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionController.java b/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionController.java index 4ec35b61..70eedb38 100644 --- a/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionController.java +++ b/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionController.java @@ -1,7 +1,7 @@ package com.jmsoftware.maf.authcenter.permission; import com.jmsoftware.maf.authcenter.permission.response.GetServicesInfoResponse; -import com.jmsoftware.maf.authcenter.permission.service.PermissionDomainService; +import com.jmsoftware.maf.authcenter.permission.service.PermissionService; import com.jmsoftware.maf.common.bean.ResponseBodyBean; import com.jmsoftware.maf.common.exception.BizException; import lombok.RequiredArgsConstructor; @@ -21,7 +21,7 @@ @RestController @RequiredArgsConstructor public class PermissionController { - private final PermissionDomainService permissionDomainService; + private final PermissionService permissionService; /** * Services info response body bean. @@ -33,6 +33,6 @@ public class PermissionController { */ @GetMapping("/permissions/services-info") public ResponseBodyBean getServicesInfo() throws BizException { - return ResponseBodyBean.ofSuccess(this.permissionDomainService.getServicesInfo()); + return ResponseBodyBean.ofSuccess(this.permissionService.getServicesInfo()); } } diff --git a/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionRemoteApiController.java b/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionRemoteApiController.java index ae1c7f82..2f394641 100644 --- a/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionRemoteApiController.java +++ b/auth-center/auth-center-web/src/main/java/com/jmsoftware/maf/authcenter/permission/PermissionRemoteApiController.java @@ -1,6 +1,6 @@ package com.jmsoftware.maf.authcenter.permission; -import com.jmsoftware.maf.authcenter.permission.service.PermissionDomainService; +import com.jmsoftware.maf.authcenter.permission.service.PermissionService; import com.jmsoftware.maf.common.bean.ResponseBodyBean; import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListPayload; import com.jmsoftware.maf.common.domain.authcenter.permission.GetPermissionListByRoleIdListResponse; @@ -23,10 +23,12 @@ @RequiredArgsConstructor @RequestMapping("/permission-remote-api") public class PermissionRemoteApiController { - private final PermissionDomainService permissionDomainService; + private final PermissionService permissionService; @GetMapping("/permissions") - public ResponseBodyBean getPermissionListByRoleIdList(@Valid GetPermissionListByRoleIdListPayload payload) { - return ResponseBodyBean.ofSuccess(this.permissionDomainService.getPermissionListByRoleIdList(payload)); + public ResponseBodyBean getPermissionListByRoleIdList( + @Valid GetPermissionListByRoleIdListPayload payload + ) { + return ResponseBodyBean.ofSuccess(this.permissionService.getPermissionListByRoleIdList(payload)); } }