-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf($DDD): decouple and aggregate permission
- Loading branch information
1 parent
6ef3fd2
commit 87845e7
Showing
9 changed files
with
173 additions
and
112 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/PermissionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
109 changes: 109 additions & 0 deletions
109
...ain/java/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...nter-biz/src/main/java/com/jmsoftware/maf/authcenter/permission/service/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package com.jmsoftware.maf.authcenter.permission.service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 5 additions & 80 deletions
85
...va/com/jmsoftware/maf/authcenter/permission/service/impl/PermissionDomainServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,35 @@ | ||
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; | ||
|
||
/** | ||
* <h1>PermissionDomainServiceImpl</h1> | ||
* <p> | ||
* 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 | ||
@RequiredArgsConstructor | ||
public class PermissionDomainServiceImpl | ||
extends ServiceImpl<PermissionMapper, Permission> | ||
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<Permission> getPermissionListByRoleIdList(@NotEmpty List<Long> roleIdList, | ||
@NotEmpty List<PermissionType> permissionTypeList) { | ||
public List<Permission> getPermissionListByRoleIdList( | ||
@NotEmpty List<Long> roleIdList, | ||
@NotEmpty List<PermissionType> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters