|
@@ -2,19 +2,24 @@ package com.inspur.customer.service.keycloak;
|
|
|
|
|
|
|
|
import com.inspur.customer.service.client.keycloak.KeycloakService;
|
|
import com.inspur.customer.service.client.keycloak.KeycloakService;
|
|
|
import com.inspur.customer.service.dto.KeycloakUserCO;
|
|
import com.inspur.customer.service.dto.KeycloakUserCO;
|
|
|
|
|
+import com.inspur.customer.service.dto.Pair;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.dubbo.config.annotation.DubboService;
|
|
import org.apache.dubbo.config.annotation.DubboService;
|
|
|
import org.keycloak.admin.client.Keycloak;
|
|
import org.keycloak.admin.client.Keycloak;
|
|
|
|
|
+import org.keycloak.admin.client.resource.RealmResource;
|
|
|
import org.keycloak.admin.client.resource.UserResource;
|
|
import org.keycloak.admin.client.resource.UserResource;
|
|
|
import org.keycloak.representations.idm.GroupRepresentation;
|
|
import org.keycloak.representations.idm.GroupRepresentation;
|
|
|
import org.keycloak.representations.idm.RoleRepresentation;
|
|
import org.keycloak.representations.idm.RoleRepresentation;
|
|
|
import org.keycloak.representations.idm.UserRepresentation;
|
|
import org.keycloak.representations.idm.UserRepresentation;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@DubboService
|
|
@DubboService
|
|
@@ -78,6 +83,7 @@ public class KeycloakServiceImpl implements KeycloakService {
|
|
|
if (userRepresentation != null) {
|
|
if (userRepresentation != null) {
|
|
|
KeycloakUserCO keycloakUserCo = new KeycloakUserCO();
|
|
KeycloakUserCO keycloakUserCo = new KeycloakUserCO();
|
|
|
keycloakUserCo.setId(userRepresentation.getId());
|
|
keycloakUserCo.setId(userRepresentation.getId());
|
|
|
|
|
+ keycloakUserCo.setUsername(userRepresentation.getUsername());
|
|
|
keycloakUserCo.setEmail(userRepresentation.getEmail());
|
|
keycloakUserCo.setEmail(userRepresentation.getEmail());
|
|
|
Map<String, List<String>> attributes = userRepresentation.getAttributes();
|
|
Map<String, List<String>> attributes = userRepresentation.getAttributes();
|
|
|
if (attributes != null) {
|
|
if (attributes != null) {
|
|
@@ -126,4 +132,81 @@ public class KeycloakServiceImpl implements KeycloakService {
|
|
|
return Collections.emptyList();
|
|
return Collections.emptyList();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<String> getAiAuditEnabledGroups() {
|
|
|
|
|
+ List<GroupRepresentation> groups = keycloak.realm(realm).groups().groups();
|
|
|
|
|
+ List<String> aiAudit = groups.stream().filter(groupRepresentation -> {
|
|
|
|
|
+ Map<String, List<String>> attributes = groupRepresentation.getAttributes();
|
|
|
|
|
+ if (attributes != null) {
|
|
|
|
|
+ List<String> list = attributes.get("aiAudit");
|
|
|
|
|
+ return !CollectionUtils.isEmpty(list) && "1".equals(list.get(0));
|
|
|
|
|
+ }
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }).map(GroupRepresentation::getPath).collect(Collectors.toList());
|
|
|
|
|
+ return aiAudit;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<KeycloakUserCO> getGroupSupervisor(String group) {
|
|
|
|
|
+ return getUserByRoleAndGroup(group, "ROLE_OPERATION_SUPERVISOR");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<KeycloakUserCO> getSuperAdmin() {
|
|
|
|
|
+ return keycloak.realm(realm).roles().get("ROLE_SUPER_ADMIN").getRoleUserMembers()
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .map(this::transfer)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<KeycloakUserCO> getGroupAdmin(String group) {
|
|
|
|
|
+ return getUserByRoleAndGroup(group, "ROLE_ADMIN");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String getSingleAttrByGroupPath(String group, String key) {
|
|
|
|
|
+ List<String> list = getAttrByGroupPath(group, key);
|
|
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return list.get(0);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<KeycloakUserCO> searchUserByAttrEntry(List<Pair> pairList) {
|
|
|
|
|
+ String search = pairList.stream()
|
|
|
|
|
+ .filter(t -> Objects.nonNull(t) && Objects.nonNull(t.getKey()) && Objects.nonNull(t.getValue()))
|
|
|
|
|
+ .map(t -> t.getKey() + ":" + t.getValue())
|
|
|
|
|
+ .collect(Collectors.joining(" "));
|
|
|
|
|
+ return keycloak.realm(realm).users().searchByAttributes(search).stream().map(this::transfer).collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<KeycloakUserCO> getUserByRoleAndGroup(String group, String role) {
|
|
|
|
|
+ if (!StringUtils.hasText(group) || !StringUtils.hasText(role)) {
|
|
|
|
|
+ return Collections.emptyList();
|
|
|
|
|
+ }
|
|
|
|
|
+ RealmResource realmResource = keycloak.realm(this.realm);
|
|
|
|
|
+ List<UserRepresentation> members = realmResource.groups().group(realmResource.getGroupByPath(group).getId()).members();
|
|
|
|
|
+ Set<String> set = realmResource.roles().get(role).getRoleUserMembers().stream().map(UserRepresentation::getId).collect(Collectors.toSet());
|
|
|
|
|
+ return members.stream().filter(t -> set.contains(t.getId())).map(this::transfer).collect(Collectors.toList());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private KeycloakUserCO transfer(UserRepresentation userRepresentation) {
|
|
|
|
|
+ if (userRepresentation == null) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ KeycloakUserCO keycloakUserCo = new KeycloakUserCO();
|
|
|
|
|
+ keycloakUserCo.setId(userRepresentation.getId());
|
|
|
|
|
+ keycloakUserCo.setUsername(userRepresentation.getUsername());
|
|
|
|
|
+ keycloakUserCo.setEmail(userRepresentation.getEmail());
|
|
|
|
|
+ Map<String, List<String>> attributes = userRepresentation.getAttributes();
|
|
|
|
|
+ if (attributes != null) {
|
|
|
|
|
+ keycloakUserCo.setPhone(attributes.get("phone"));
|
|
|
|
|
+ keycloakUserCo.setWechat(attributes.get("wechat"));
|
|
|
|
|
+ keycloakUserCo.setWechatApplet(attributes.get("wechat-applet-openid"));
|
|
|
|
|
+ }
|
|
|
|
|
+ return keycloakUserCo;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|