Przeglądaj źródła

fix:用户的有效数据能存入本地的均从keycloak移到本地用户表

wangbo 2 lat temu
rodzic
commit
9b873df747

+ 3 - 0
smsb-customer-manager-adapter/src/main/java/com/inspur/customer/web/controller/keyclaok/KeycloakDepartmentController.java

@@ -32,6 +32,7 @@ public class KeycloakDepartmentController {
     @DubboReference
     private SmsbDepartmentService departmentService;
 
+    @Deprecated(forRemoval=true)
     @SmsbMethodLog(description = "部门新增")
     @PostMapping("/admin/group/{id}/children")
     public Response addKeycloakGroup(@PathVariable("id") String id , @RequestBody GroupRepresentation groupRepresentation){
@@ -44,6 +45,7 @@ public class KeycloakDepartmentController {
         return keycloakService.addKeycloakGroup(id , groupRepresentation);
     }
 
+    @Deprecated(forRemoval=true)
     @SmsbMethodLog(description = "部门名称修改")
     @PutMapping("/admin/group/{id}")
     public Response updateKeycloakGroup(@PathVariable("id") String id , @RequestBody GroupRepresentation groupRepresentation){
@@ -56,6 +58,7 @@ public class KeycloakDepartmentController {
         return keycloakService.updateKeycloakGroup(id , groupRepresentation);
     }
 
+    @Deprecated(forRemoval=true)
     @SmsbMethodLog(description = "部门删除")
     @DeleteMapping("/admin/group/{id}")
     public Response removeGroup(@PathVariable("id") String id){

+ 11 - 0
smsb-customer-manager-adapter/src/main/java/com/inspur/customer/web/controller/keyclaok/KeycloakUserController.java

@@ -9,6 +9,7 @@ import com.inspur.customer.client.org.SmsbDepartmentService;
 import com.inspur.customer.client.org.SmsbDepartmentUserService;
 import com.inspur.customer.client.wechat.IWeChatService;
 import com.inspur.customer.constant.Constant;
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
 import com.inspur.customer.object.keycloak.SwitchDTO;
 import com.inspur.customer.object.keycloak.UsersRoleMappingDTO;
 import com.inspur.customer.object.org.*;
@@ -33,7 +34,9 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.validation.Valid;
 import java.time.LocalDateTime;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
 import java.util.stream.Collectors;
 
 /**
@@ -161,6 +164,14 @@ public class KeycloakUserController {
     @SmsbMethodLog(description = "更新用户属性" ,business = "用户属性更新")
     @PutMapping("/keycloak/users/attribute")
     public Response updateUserAttribute(@RequestHeader String userId , @RequestBody UserRepresentation representation){
+        KeycloakUserCO userCO = new KeycloakUserCO();
+        userCO.setEmail(representation.getEmail());
+        Map<String, List<String>> userAttribute = representation.getAttributes();
+        if(userAttribute != null){
+            userCO.setPhone(userAttribute.get("phone"));
+        }
+        userCO.setId(userId);
+        userService.updateUserAttribute(userCO);
         return keycloakService.updateUser(userId, representation, Constant.RESET_ATTRIBUTE);
     }
 

+ 12 - 1
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/keycloak/KeycloakServiceImpl.java

@@ -5,6 +5,7 @@ import com.alibaba.cola.dto.Response;
 import com.alibaba.cola.dto.SingleResponse;
 import com.google.common.collect.Lists;
 import com.inspur.customer.client.keycloak.KeycloakService;
+import com.inspur.customer.client.org.SmsbDepartmentUserService;
 import com.inspur.customer.constant.Constant;
 import com.inspur.customer.object.keycloak.KeycloakUserCO;
 import com.inspur.customer.object.keycloak.SwitchDTO;
@@ -13,12 +14,12 @@ import com.inspur.customer.object.org.SmsbDepartmentCmd;
 import com.inspur.customer.object.org.SmsbUserAdd;
 import com.inspur.customer.object.wechat.Pair;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.dubbo.config.annotation.DubboReference;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.keycloak.admin.client.resource.*;
 import org.keycloak.representations.idm.*;
 import org.springframework.cache.annotation.CacheEvict;
 import org.springframework.cache.annotation.Cacheable;
-import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
 
@@ -35,6 +36,8 @@ public class KeycloakServiceImpl implements KeycloakService {
 
     @Resource
     private RealmResource realmResource;
+    @DubboReference
+    private SmsbDepartmentUserService userService;
 
     @Override
     @Cacheable(value = "smsb:users")
@@ -122,6 +125,14 @@ public class KeycloakServiceImpl implements KeycloakService {
 
     @Override
     public void updateAttribute(String userId ,String property , String value) {
+        KeycloakUserCO userCO = new KeycloakUserCO();
+        userCO.setId(userId);
+        if(Objects.nonNull(property) && property.equals("wechat")){
+            userCO.setWechat(Collections.singletonList(value));
+        }else if(Objects.nonNull(property) && property.equals("wechat-applet-openid")){
+            userCO.setWechatAppletOpenId(value);
+        }
+        userService.updateUserAttribute(userCO);
         UserResource user = realmResource.users().get(userId);
         UserRepresentation userRepresentation = user.toRepresentation();
         if (userRepresentation.getAttributes() == null) {

+ 19 - 0
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/org/SmsbDepartmentUserServiceImpl.java

@@ -15,6 +15,7 @@ import com.inspur.customer.constant.Constant;
 import com.inspur.customer.infrastructure.convertor.SmsbDepartmentConvertor;
 import com.inspur.customer.infrastructure.mapper.org.SmsbDepartmentUserMapper;
 import com.inspur.customer.infrastructure.object.org.SmsbDepartmentUserDO;
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
 import com.inspur.customer.object.keycloak.SwitchDTO;
 import com.inspur.customer.object.org.*;
 import lombok.extern.slf4j.Slf4j;
@@ -212,4 +213,22 @@ public class SmsbDepartmentUserServiceImpl extends ServiceImpl<SmsbDepartmentUse
         });
         return smsbSimpleUserList;
     }
+
+    @Override
+    public Boolean updateUserAttribute(KeycloakUserCO userCO) {
+        if(StringUtils.isEmpty(userCO.getId()) && StringUtils.isEmpty(userCO.getUsername())){
+            return Boolean.FALSE;
+        }
+        if(StringUtils.isEmpty(userCO.getWechat()) && StringUtils.isEmpty(userCO.getWechatAppletOpenId()) &&
+            StringUtils.isEmpty(userCO.getPhone()) && StringUtils.isEmpty(userCO.getEmail())){
+            return Boolean.FALSE;
+        }
+        return super.update(new LambdaUpdateWrapper<>(SmsbDepartmentUserDO.class)
+            .set(StringUtils.isNotBlank(userCO.getWechat()) ,SmsbDepartmentUserDO::getWechat , userCO.getWechat())
+            .set(StringUtils.isNotBlank(userCO.getWechatAppletOpenId()) ,SmsbDepartmentUserDO::getWechatApplet , userCO.getWechatAppletOpenId())
+            .set(StringUtils.isNotBlank(userCO.getPhone()) ,SmsbDepartmentUserDO::getPhone , userCO.getPhone())
+            .set(StringUtils.isNotBlank(userCO.getEmail()) ,SmsbDepartmentUserDO::getEmail , userCO.getEmail())
+            .eq(StringUtils.isNotBlank(userCO.getUsername()) ,SmsbDepartmentUserDO::getUserName , userCO.getUsername())
+            .eq(StringUtils.isNotBlank(userCO.getId()) ,SmsbDepartmentUserDO::getUserId , userCO.getId()));
+    }
 }

+ 10 - 0
smsb-customer-manager-client/src/main/java/com/inspur/customer/client/org/SmsbDepartmentUserService.java

@@ -2,6 +2,7 @@ package com.inspur.customer.client.org;
 
 import com.alibaba.cola.dto.PageResponse;
 import com.alibaba.cola.dto.Response;
+import com.inspur.customer.object.keycloak.KeycloakUserCO;
 import com.inspur.customer.object.keycloak.SwitchDTO;
 import com.inspur.customer.object.org.*;
 
@@ -103,4 +104,13 @@ public interface SmsbDepartmentUserService {
      * @return 指定部门下接收级别<= informLevel的用户
      */
     List<SmsbSimpleUserCO> listUserByInformLevel(String tenant, List<String> org, int informLevel);
+
+    /**
+     * 更新用户属性
+     * 用户id || 用户账号必传一个
+     *
+     * @param userCO
+     * @return boolean
+     */
+    Boolean updateUserAttribute(KeycloakUserCO userCO);
 }

+ 21 - 0
smsb-customer-manager-infrastructure/src/main/java/com/inspur/customer/infrastructure/object/org/SmsbDepartmentUserDO.java

@@ -77,4 +77,25 @@ public class SmsbDepartmentUserDO implements Serializable {
      */
     @TableField("inform_level")
     private Integer informLevel;
+
+    /**
+     * 微信公众号openId
+     */
+    private String wechat;
+
+    /**
+     * 小程序OpenId
+     */
+    @TableField("wechat_applet")
+    private String wechatApplet;
+
+    /**
+     * 手机
+     */
+    private String phone;
+
+    /**
+     * 邮箱
+     */
+    private String email;
 }