|
|
@@ -5,6 +5,7 @@ import com.inspur.customer.service.dto.KeycloakUserCO;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.dubbo.config.annotation.DubboService;
|
|
|
import org.keycloak.admin.client.Keycloak;
|
|
|
+import org.keycloak.admin.client.resource.RealmResource;
|
|
|
import org.keycloak.admin.client.resource.UserResource;
|
|
|
import org.keycloak.representations.idm.GroupRepresentation;
|
|
|
import org.keycloak.representations.idm.RoleRepresentation;
|
|
|
@@ -12,6 +13,7 @@ import org.keycloak.representations.idm.UserRepresentation;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
@@ -80,6 +82,7 @@ public class KeycloakServiceImpl implements KeycloakService {
|
|
|
if (userRepresentation != 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) {
|
|
|
@@ -129,7 +132,7 @@ public class KeycloakServiceImpl implements KeycloakService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public List<String> getAiAuditList() {
|
|
|
+ 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();
|
|
|
@@ -142,4 +145,58 @@ public class KeycloakServiceImpl implements KeycloakService {
|
|
|
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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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;
|
|
|
+ }
|
|
|
}
|