|
@@ -1,22 +1,20 @@
|
|
|
package com.dragoninfo.dcuc.auth.business.impl.zerotrust;
|
|
|
|
|
|
import com.dragoninfo.dcuc.auth.auth.dto.zerotrust.approval.ApprovalBaseRespDto;
|
|
|
-import com.dragoninfo.dcuc.auth.auth.entity.RoleInfo;
|
|
|
-import com.dragoninfo.dcuc.auth.auth.entity.StaffAssignAuthInfo;
|
|
|
import com.dragoninfo.dcuc.auth.auth.entity.zerotrust.RoleAuthApply;
|
|
|
import com.dragoninfo.dcuc.auth.auth.enumresources.zerotrust.approval.AuthApplyStatusEnum;
|
|
|
-import com.dragoninfo.dcuc.auth.auth.service.IRoleInfoService;
|
|
|
import com.dragoninfo.dcuc.auth.auth.service.IStaffAssignAuthInfoService;
|
|
|
import com.dragoninfo.dcuc.auth.auth.service.zerotrust.IRoleAuthApplyService;
|
|
|
import com.dragoninfo.dcuc.auth.business.zerotrust.IRoleAuthCallbackHandler;
|
|
|
-import com.dragoninfo.dcuc.auth.sub.entity.AuthUserInfo;
|
|
|
-import com.dragoninfo.dcuc.auth.sub.service.IAuthUserInfoService;
|
|
|
+import com.dragonsoft.duceap.base.entity.security.SecurityUser;
|
|
|
+import com.dragonsoft.duceap.base.enums.BooleanEnum;
|
|
|
+import com.dragonsoft.duceap.base.utils.UserContextUtils;
|
|
|
import com.dragonsoft.duceap.commons.util.collections.CollectionUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
@@ -32,20 +30,6 @@ public class RoleAuthCallbackHandler implements IRoleAuthCallbackHandler {
|
|
|
|
|
|
private IStaffAssignAuthInfoService staffAssignAuthInfoService;
|
|
|
|
|
|
- private IRoleInfoService roleInfoService;
|
|
|
-
|
|
|
- private IAuthUserInfoService userInfoService;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- public void setUserInfoService(IAuthUserInfoService userInfoService) {
|
|
|
- this.userInfoService = userInfoService;
|
|
|
- }
|
|
|
-
|
|
|
- @Autowired
|
|
|
- public void setRoleInfoService(IRoleInfoService roleInfoService) {
|
|
|
- this.roleInfoService = roleInfoService;
|
|
|
- }
|
|
|
-
|
|
|
@Autowired
|
|
|
public void setStaffAssignAuthInfoService(IStaffAssignAuthInfoService staffAssignAuthInfoService) {
|
|
|
this.staffAssignAuthInfoService = staffAssignAuthInfoService;
|
|
@@ -56,6 +40,7 @@ public class RoleAuthCallbackHandler implements IRoleAuthCallbackHandler {
|
|
|
this.roleAuthApplyService = roleAuthApplyService;
|
|
|
}
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public ApprovalBaseRespDto callBackHandle(String processInstId, String type, String applyStatus) {
|
|
|
log.info("开始处理角色权限申请");
|
|
@@ -70,80 +55,54 @@ public class RoleAuthCallbackHandler implements IRoleAuthCallbackHandler {
|
|
|
if (!AuthApplyStatusEnum.AGREE.getValue().equals(applyStatus)) {
|
|
|
return new ApprovalBaseRespDto().success();
|
|
|
}
|
|
|
+ Date now = new Date();
|
|
|
+
|
|
|
+ // 将未到授权时间的申请设置位待授权
|
|
|
+ List<RoleAuthApply> readyToAuth = filterToBeAuthed(authApplies, now);
|
|
|
+
|
|
|
+ // 过滤有效的权限申请
|
|
|
+ List<RoleAuthApply> activeApplies = getEndTimeActiveApply(readyToAuth, now);
|
|
|
|
|
|
// 保存角色授权
|
|
|
- saveRoleAuth(authApplies);
|
|
|
+ if (CollectionUtils.isNotEmpty(activeApplies)) {
|
|
|
+ staffAssignAuthInfoService.startRoleAuthApply(activeApplies);
|
|
|
+ }
|
|
|
|
|
|
return new ApprovalBaseRespDto().success();
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 保存角色授权申请
|
|
|
- * @param authApplies
|
|
|
- */
|
|
|
- private void saveRoleAuth(List<RoleAuthApply> authApplies) {
|
|
|
- // 查询申请人员
|
|
|
- List<String> userIds = authApplies.stream().map(RoleAuthApply::getUserId).distinct().collect(Collectors.toList());
|
|
|
- List<AuthUserInfo> userInfos = userInfoService.findByIds(userIds);
|
|
|
- Map<String, AuthUserInfo> userMap = userInfos.stream().collect(Collectors.toMap(AuthUserInfo::getId, e -> e));
|
|
|
-
|
|
|
- // 查询应用中的角色
|
|
|
- List<String> appIds = authApplies.stream().map(RoleAuthApply::getAppId).distinct().collect(Collectors.toList());
|
|
|
- List<RoleInfo> roles = roleInfoService.getRolesByAppIds(appIds);
|
|
|
- Map<String, Map<String, String>> appRoleMap = roles.stream().collect(Collectors.groupingBy(RoleInfo::getAppId,
|
|
|
- Collectors.collectingAndThen(Collectors.toList(),
|
|
|
- e -> e.stream().collect(Collectors.toMap(RoleInfo::getCode, RoleInfo::getId)))));
|
|
|
-
|
|
|
- List<StaffAssignAuthInfo> roleAuths = getStaffAssignAuthInfos(authApplies, appRoleMap, userMap);
|
|
|
- if (CollectionUtils.isNotEmpty(roleAuths)) {
|
|
|
- staffAssignAuthInfoService.batchSave(roleAuths);
|
|
|
+ private List<RoleAuthApply> filterToBeAuthed(List<RoleAuthApply> authApplies, Date now) {
|
|
|
+ Map<Boolean, List<RoleAuthApply>> collect = authApplies.stream().collect(Collectors.partitioningBy(e -> {
|
|
|
+ Date startTime = e.getStartTime();
|
|
|
+ // 有效的
|
|
|
+ return Objects.isNull(startTime) || now.after(startTime);
|
|
|
+ }));
|
|
|
+ List<RoleAuthApply> tobeAuthed = collect.get(Boolean.FALSE);
|
|
|
+ if (CollectionUtils.isNotEmpty(tobeAuthed)) {
|
|
|
+ roleAuthApplyService.setToBeAuthed(tobeAuthed, BooleanEnum.TRUE.getValue());
|
|
|
}
|
|
|
+ return collect.get(Boolean.TRUE);
|
|
|
}
|
|
|
|
|
|
- private List<StaffAssignAuthInfo> getStaffAssignAuthInfos(List<RoleAuthApply> authApplies, Map<String, Map<String, String>> appRoleMap,
|
|
|
- Map<String, AuthUserInfo> userMap) {
|
|
|
- Date createTime = new Date();
|
|
|
- // 过滤申请中的但还未审批通过的角色和已过期的申请
|
|
|
- authApplies = authApplies.stream().filter(e-> {
|
|
|
+
|
|
|
+ private List<RoleAuthApply> getEndTimeActiveApply(List<RoleAuthApply> authApplies, Date now) {
|
|
|
+ return authApplies.stream().filter(e -> {
|
|
|
Date endTime = e.getEndTime();
|
|
|
- if (Objects.nonNull(endTime) && createTime.after(endTime)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- String appId = e.getAppId();
|
|
|
- Map<String, String> roleCods = appRoleMap.get(appId);
|
|
|
- return Objects.nonNull(roleCods) && roleCods.containsKey(e.getRoleCode());
|
|
|
+ return Objects.isNull(endTime) || now.before(endTime);
|
|
|
}).collect(Collectors.toList());
|
|
|
-
|
|
|
- if (CollectionUtils.isEmpty(authApplies)) {
|
|
|
- return Collections.emptyList();
|
|
|
- }
|
|
|
- String activeTime = getActiveTime(authApplies);
|
|
|
-
|
|
|
- return authApplies.stream().map(e -> {
|
|
|
- StaffAssignAuthInfo authInfo = new StaffAssignAuthInfo();
|
|
|
- String roleId = appRoleMap.get(e.getAppId()).get(e.getRoleCode());
|
|
|
- authInfo.setRoleId(roleId);
|
|
|
- authInfo.setAppId(e.getAppId());
|
|
|
-
|
|
|
- AuthUserInfo authUserInfo = userMap.get(e.getUserId());
|
|
|
- authInfo.setStaffId(e.getUserId());
|
|
|
- authInfo.setStaffOrgId(authUserInfo.getOrgId());
|
|
|
-
|
|
|
- authInfo.setCreateTime(createTime);
|
|
|
- authInfo.setActiveTime(activeTime);
|
|
|
- return authInfo;
|
|
|
- }).filter(Objects::nonNull).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
- private String getActiveTime(List<RoleAuthApply> authApplies) {
|
|
|
- RoleAuthApply roleAuthApply = authApplies.get(0);
|
|
|
- Date endTime = roleAuthApply.getEndTime();
|
|
|
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
- if (Objects.isNull(endTime)) {
|
|
|
- return "长期";
|
|
|
- } else {
|
|
|
- return format.format(endTime);
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public void startRoleAuth() {
|
|
|
+ log.info("授权将要开始的服务授权开始");
|
|
|
+ List<RoleAuthApply> starts = roleAuthApplyService.getStartToAuth();
|
|
|
+ List<RoleAuthApply> activeApply = getEndTimeActiveApply(starts, new Date());
|
|
|
+ if (CollectionUtils.isNotEmpty(activeApply)) {
|
|
|
+ staffAssignAuthInfoService.startRoleAuthApply(activeApply);
|
|
|
+ roleAuthApplyService.setToBeAuthed(activeApply, BooleanEnum.FALSE.getValue());
|
|
|
}
|
|
|
+ log.info("授权将要开始的服务授权结束");
|
|
|
}
|
|
|
|
|
|
}
|