|
|
@@ -1,6 +1,7 @@
|
|
|
package com.inspur.customer.service.keycloak;
|
|
|
|
|
|
import com.inspur.customer.service.client.keycloak.KeycloakService;
|
|
|
+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;
|
|
|
@@ -11,10 +12,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
+import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
@Slf4j
|
|
|
@@ -40,6 +38,52 @@ public class KeycloakServiceImpl implements KeycloakService {
|
|
|
return userMap;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * get users in role (xuzhou environment)
|
|
|
+ * @param role role
|
|
|
+ * @return users
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<KeycloakUserCO> getUsersInRole(String role) {
|
|
|
+ List<KeycloakUserCO> keycloakUserCos = new ArrayList<>();
|
|
|
+ keycloak.realm(realm).roles().get(role).getRoleUserMembers().forEach(user -> {
|
|
|
+ log.info("name: {}", user.getUsername());
|
|
|
+ KeycloakUserCO keycloakUserCo = new KeycloakUserCO();
|
|
|
+ keycloakUserCo.setId(user.getId());
|
|
|
+ keycloakUserCo.setEmail(user.getEmail());
|
|
|
+ keycloakUserCo.setPhone(user.getAttributes().get("phone"));
|
|
|
+ keycloakUserCo.setWechat(user.getAttributes().get("wechat"));
|
|
|
+ keycloakUserCo.setWechatApplet(user.getAttributes().get("wechat-applet-openid"));
|
|
|
+ keycloakUserCos.add(keycloakUserCo);
|
|
|
+ });
|
|
|
+ return keycloakUserCos;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * get users by ids
|
|
|
+ * according to device's related users is no too much, temporary use that way to get users
|
|
|
+ * @deprecated
|
|
|
+ * @param userIds ids
|
|
|
+ * @return users
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<KeycloakUserCO> getUsersByIds(List<String> userIds) {
|
|
|
+ List<KeycloakUserCO> keycloakUserCos = new ArrayList<>(userIds.size());
|
|
|
+ userIds.forEach(id -> {
|
|
|
+ UserRepresentation userRepresentation = keycloak.realm(realm).users().get(id).toRepresentation();
|
|
|
+ if (userRepresentation != null) {
|
|
|
+ KeycloakUserCO keycloakUserCo = new KeycloakUserCO();
|
|
|
+ keycloakUserCo.setId(userRepresentation.getId());
|
|
|
+ keycloakUserCo.setEmail(userRepresentation.getEmail());
|
|
|
+ keycloakUserCo.setPhone(userRepresentation.getAttributes().get("phone"));
|
|
|
+ keycloakUserCo.setWechat(userRepresentation.getAttributes().get("wechat"));
|
|
|
+ keycloakUserCo.setWechatApplet(userRepresentation.getAttributes().get("wechat-applet-openid"));
|
|
|
+ keycloakUserCos.add(keycloakUserCo);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return keycloakUserCos;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public Boolean checkUserRole(String userId, String role) {
|
|
|
AtomicReference<Boolean> result = new AtomicReference<>(false);
|