瀏覽代碼

Merge branch '71-integrate' into 84-integrate

linwenhua 2 年之前
父節點
當前提交
a56cf08df8

+ 6 - 13
smsb-customer-manager-adapter/src/main/java/com/inspur/customer/web/controller/keyclaok/KeycloakController.java

@@ -6,16 +6,13 @@ import com.alibaba.fastjson.JSONObject;
 import com.inspur.customer.client.keycloak.KeycloakService;
 import com.inspur.customer.client.wechat.IWeChatService;
 import com.inspur.customer.constant.Constant;
-import com.inspur.customer.object.keycloak.SwitchDTO;
 import com.inspur.customer.object.keycloak.UsersRoleMappingDTO;
 import com.inspur.customer.object.wechat.SubscribeDto;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.dubbo.config.Constants;
 import org.apache.dubbo.config.annotation.DubboReference;
 import org.keycloak.representations.idm.CredentialRepresentation;
 import org.keycloak.representations.idm.GroupRepresentation;
-import org.keycloak.representations.idm.RoleRepresentation;
 import org.keycloak.representations.idm.UserRepresentation;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.EnableScheduling;
@@ -23,10 +20,6 @@ import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
-
 /**
  * 微信模块
  * @author wangbo13
@@ -122,8 +115,8 @@ public class KeycloakController {
     }
 
     @PutMapping("/admin/users/role/configure")
-    public Response roleMapping(@RequestBody UsersRoleMappingDTO ssersRoleMappingDTO){
-        return keycloakService.roleMapping(ssersRoleMappingDTO);
+    public Response roleMapping(@RequestBody UsersRoleMappingDTO usersRoleMappingDTO){
+        return keycloakService.roleMapping(usersRoleMappingDTO);
     }
 
     @GetMapping("/admin/users/{userId}/credentials")
@@ -139,22 +132,22 @@ public class KeycloakController {
 
     @PutMapping("/keycloak/users/attribute")
     public Response updateUserAttribute(@RequestHeader String userId , @RequestBody UserRepresentation representation){
-        return keycloakService.updateUser(userId , representation ,Constant.RESET_ATTRBUTE);
+        return keycloakService.updateUser(userId, representation, Constant.RESET_ATTRIBUTE);
     }
 
     @PostMapping("/super/admin/tenant")
-    public Response addKeyclaokTenant(@RequestBody GroupRepresentation groupRepresentation){
+    public Response addKeycloakTenant(@RequestBody GroupRepresentation groupRepresentation){
         if(StringUtils.isEmpty(groupRepresentation.getName())){
             return Response.buildFailure("500","name不能为空!");
         }
         if(CollectionUtils.isEmpty(groupRepresentation.getAttributes())){
             return Response.buildFailure("500","attributes不能为空!");
         }
-        return keycloakService.addKeyclaokTenant(groupRepresentation);
+        return keycloakService.addKeycloakTenant(groupRepresentation);
     }
 
     @DeleteMapping("/super/admin/tenant/{id}")
-    public Response addKeyclaokTenant(@PathVariable("id")String id){
+    public Response removeKeycloakTenant(@PathVariable("id")String id){
         return keycloakService.removeGroup(id);
     }
 

+ 9 - 2
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/authcode/AuthCodeServiceImpl.java

@@ -52,10 +52,15 @@ public class AuthCodeServiceImpl implements AuthCodeService {
         try{
 
             //生成一个6位验证码
-            String authCode = String.valueOf(((Math.random() * 9 + 1) * 100000));
+            String authCode = StringUtils.left(String.valueOf(((Math.random() * 9 + 1) * 100000)), 6);
             Boolean sendResult = commonInformService.sendMessage(AUTH_CODE_MESSAGE_TEMPLATE, getMessage(authCode, authCodeDTO));
             if (Boolean.TRUE.equals(sendResult)) {
-                redisTemplate.opsForValue().set(AUTH_CODE_CACHE_KEY + authCodeDTO.getEmail(), authCode, 5, TimeUnit.MINUTES);
+                if (StringUtils.isNotBlank(authCodeDTO.getEmail())) {
+                    redisTemplate.opsForValue().set(AUTH_CODE_CACHE_KEY + authCodeDTO.getEmail(), authCode, 5, TimeUnit.MINUTES);
+                } else {
+                    redisTemplate.opsForValue().set(AUTH_CODE_CACHE_KEY + authCodeDTO.getPhoneNum(), authCode, 5, TimeUnit.MINUTES);
+                }
+
                 return Response.buildSuccess();
             } else {
                 return Response.buildFailure("500", "send auth code fail");
@@ -69,6 +74,7 @@ public class AuthCodeServiceImpl implements AuthCodeService {
 
     private AbstractBaseInformMessage getMessage(String authCode, AuthCodeDTO authCodeDTO) {
         if (StringUtils.isNotBlank(authCodeDTO.getEmail())) {
+            log.info("email auth code");
             EmailInformMessage emailInformMessage = new EmailInformMessage();
             Map<String, String> params = new HashMap<>(1);
             params.put("\\{MSR_AUTH_CODE}", String.valueOf(authCode));
@@ -76,6 +82,7 @@ public class AuthCodeServiceImpl implements AuthCodeService {
             emailInformMessage.setAddressees(Collections.singletonList(authCodeDTO.getEmail()));
             return emailInformMessage;
         } else {
+            log.info("sms auth code");
             SmsInformMessage smsInformMessage = new SmsInformMessage();
             Map<String, String> params = new HashMap<>(1);
             params.put("MSRcode", String.valueOf(authCode));

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

@@ -5,13 +5,13 @@ 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.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.SmsbDepartmentCmd;
 import com.inspur.customer.object.org.SmsbUserAdd;
 import com.inspur.customer.object.wechat.Pair;
-import com.inspur.customer.constant.Constant;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.keycloak.admin.client.resource.*;
@@ -121,7 +121,6 @@ public class KeycloakServiceImpl implements KeycloakService {
         roles.forEach(n ->{
             if(n.getName().equalsIgnoreCase(role)){
                 result.set(true);
-                return;
             }
         });
         return result.get();
@@ -323,16 +322,16 @@ public class KeycloakServiceImpl implements KeycloakService {
     private String getUserIdByName(String username){
         Optional<String> optional = realmResource.users().search(username)
             .stream().filter(user -> user.getUsername().equals(username))
-            .map(i -> i.getId())
+            .map(UserRepresentation::getId)
             .findFirst();
         return optional.isEmpty()?null:optional.get();
     }
 
     @Override
-    public Response updateUser(String userId, Object representation ,Integer oprateType) {
+    public Response updateUser(String userId, Object representation, Integer operateType) {
         UserResource userResource = realmResource.users().get(userId);
         UserRepresentation user = userResource.toRepresentation();
-        switch (oprateType){
+        switch (operateType) {
             case Constant.ENABLE:
                 SwitchDTO switchDTO = (SwitchDTO) representation;
                 user.setEnabled(switchDTO.getEnabled());
@@ -341,13 +340,15 @@ public class KeycloakServiceImpl implements KeycloakService {
                 CredentialRepresentation credential = (CredentialRepresentation) representation;
                 user.setCredentials(Stream.of(credential).collect(Collectors.toList()));
                 break;
-            case Constant.RESET_ATTRBUTE:
+            case Constant.RESET_ATTRIBUTE:
                 UserRepresentation userRepresentation = (UserRepresentation) representation;
                 user.setEmail(Optional.ofNullable(userRepresentation.getEmail()).orElse(null));
                 user.setFirstName(userRepresentation.getFirstName());
                 user.setAttributes(userRepresentation.getAttributes());
+                break;
             case Constant.UPDATE_USER_FIRSTNAME:
                 user.setFirstName((String) representation);
+                break;
             default:
                 break;
         }
@@ -366,13 +367,12 @@ public class KeycloakServiceImpl implements KeycloakService {
         UserResource userResource = realmResource.users().get(userId);
         List<GroupRepresentation> groups = userResource.groups();
         // 移除
-        groups.forEach(groupRepresentation->{userResource.leaveGroup(groupRepresentation.getId());});
+        groups.forEach(groupRepresentation-> userResource.leaveGroup(groupRepresentation.getId()));
         // 添加
         GroupsResource initGroups = realmResource.groups();
-        List<String> targetGroups = Arrays.asList(initGroups.group(groupId).toRepresentation().getPath().split("/"))
-            .stream().filter(t -> Objects.nonNull(t)).collect(Collectors.toList());
+        List<String> targetGroups = Arrays.stream(initGroups.group(groupId).toRepresentation().getPath().split("/")).filter(Objects::nonNull).collect(Collectors.toList());
 
-        getGroupId(targetGroups ,initGroups.groups()).forEach(targetGroupId->{userResource.joinGroup(targetGroupId);});
+        getGroupId(targetGroups ,initGroups.groups()).forEach(userResource::joinGroup);
         return SingleResponse.buildSuccess();
     }
 
@@ -387,10 +387,10 @@ public class KeycloakServiceImpl implements KeycloakService {
     }
 
     @Override
-    public Response roleMapping(UsersRoleMappingDTO ssersRoleMappingDTO) {
-        RoleScopeResource roleScopeResource = realmResource.users().get(ssersRoleMappingDTO.getUserId()).roles().realmLevel();
-        roleScopeResource.add(Optional.ofNullable(ssersRoleMappingDTO.getAddRoleList()).orElse(Lists.newArrayList()));
-        roleScopeResource.remove(Optional.ofNullable(ssersRoleMappingDTO.getRemoveRoleList()).orElse(Lists.newArrayList()));
+    public Response roleMapping(UsersRoleMappingDTO usersRoleMappingDTO) {
+        RoleScopeResource roleScopeResource = realmResource.users().get(usersRoleMappingDTO.getUserId()).roles().realmLevel();
+        roleScopeResource.add(Optional.ofNullable(usersRoleMappingDTO.getAddRoleList()).orElse(Lists.newArrayList()));
+        roleScopeResource.remove(Optional.ofNullable(usersRoleMappingDTO.getRemoveRoleList()).orElse(Lists.newArrayList()));
         return SingleResponse.buildSuccess();
     }
 
@@ -406,14 +406,14 @@ public class KeycloakServiceImpl implements KeycloakService {
     }
 
     @Override
-    public Response addKeyclaokTenant(GroupRepresentation groupRepresentation) {
+    public Response addKeycloakTenant(GroupRepresentation groupRepresentation) {
         return SingleResponse.of(realmResource.groups().add(groupRepresentation).getDate());
     }
 
     @Override
     public Boolean isExitTargetRole(String userId ,String role) {
         List<String> roleList = realmResource.users().get(userId).roles().realmLevel().listAll()
-            .stream().map(i -> i.getName()).collect(Collectors.toList());
+            .stream().map(RoleRepresentation::getName).collect(Collectors.toList());
         return roleList.contains(role);
     }
 

+ 1 - 0
smsb-customer-manager-app/src/main/java/com/inspur/customer/service/tenant/UserInformServiceImpl.java

@@ -48,6 +48,7 @@ public class UserInformServiceImpl implements UserInformService {
         switch (informLevel) {
             case HINT: {
                 log.info("hint");
+                users.addAll(keycloakService.getUsersInRole("ROLE_SUPER_ADMIN"));
                 break;
             }
             case INTERMEDIATE: {

+ 1 - 1
smsb-customer-manager-client/src/main/java/com/inspur/customer/client/keycloak/KeycloakService.java

@@ -185,7 +185,7 @@ public interface KeycloakService {
      * @param groupRepresentation
      * @return
      */
-    Response addKeyclaokTenant(GroupRepresentation groupRepresentation);
+    Response addKeycloakTenant(GroupRepresentation groupRepresentation);
 
     /**
      * 判断用户是否具有某角色

+ 1 - 1
smsb-customer-manager-client/src/main/java/com/inspur/customer/constant/Constant.java

@@ -20,7 +20,7 @@ public final class Constant {
     /**
      * 重置用户属性
      */
-    public static final int RESET_ATTRBUTE = 3;
+    public static final int RESET_ATTRIBUTE = 3;
 
     /**
      * 更新用户名称