|
|
@@ -0,0 +1,260 @@
|
|
|
+package com.inspur.customer.web.controller.keyclaok;
|
|
|
+
|
|
|
+import com.alibaba.cola.dto.PageResponse;
|
|
|
+import com.alibaba.cola.dto.Response;
|
|
|
+import com.alibaba.cola.dto.SingleResponse;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.inspur.customer.client.keycloak.KeycloakService;
|
|
|
+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.SwitchDTO;
|
|
|
+import com.inspur.customer.object.keycloak.UsersRoleMappingDTO;
|
|
|
+import com.inspur.customer.object.org.*;
|
|
|
+import com.inspur.customer.object.wechat.SubscribeDto;
|
|
|
+import com.inspur.logging.annotation.SmsbMethodLog;
|
|
|
+import com.inspur.logging.annotation.SmsbSysLog;
|
|
|
+import com.inspur.logging.annotation.SysLogIgnore;
|
|
|
+import com.inspur.logging.client.SmsbLogService;
|
|
|
+import com.inspur.logging.object.log.SmsbLogDto;
|
|
|
+import com.inspur.logging.service.HttpContextUtils;
|
|
|
+import com.inspur.logging.service.NetUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.dubbo.config.annotation.DubboReference;
|
|
|
+import org.keycloak.representations.idm.CredentialRepresentation;
|
|
|
+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;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+
|
|
|
+import javax.validation.Valid;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author wangbo13
|
|
|
+ * @Date 2023/2/28 15:34
|
|
|
+ * @Version 1.0
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@SmsbSysLog(value = 3,description = "用户管理控制器")
|
|
|
+@EnableScheduling
|
|
|
+public class KeycloakUserController {
|
|
|
+
|
|
|
+ @DubboReference
|
|
|
+ private KeycloakService keycloakService;
|
|
|
+ @DubboReference
|
|
|
+ private IWeChatService iWeChatService;
|
|
|
+ @Value("${system.appKey}")
|
|
|
+ private String appKey;
|
|
|
+ @DubboReference
|
|
|
+ private SmsbDepartmentUserService userService;
|
|
|
+ @DubboReference
|
|
|
+ private SmsbDepartmentService departmentService;
|
|
|
+ @DubboReference
|
|
|
+ private SmsbLogService smsbLogService;
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "公众号扫描关注")
|
|
|
+ @PostMapping("/keycloak/update/user")
|
|
|
+ public Response updateUser(@RequestBody String json){
|
|
|
+ SubscribeDto subscribe = JSONObject.parseObject(json, SubscribeDto.class);
|
|
|
+ if(StringUtils.isEmpty(subscribe.getUserId())){
|
|
|
+ return Response.buildFailure("404","userId不能为空!");
|
|
|
+ }
|
|
|
+ keycloakService.updateAttribute(subscribe.getUserId(), "wechat", subscribe.getOppenId());
|
|
|
+ return Response.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "公众号二维码展示")
|
|
|
+ @GetMapping("/keycloak/query/ticket/{userId}")
|
|
|
+ public SingleResponse getTicket(@PathVariable("userId") String userId,
|
|
|
+ @RequestHeader("Authorization") String authorization) {
|
|
|
+ return SingleResponse.of(iWeChatService.queryTicket(appKey, userId, authorization));
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "密码重置")
|
|
|
+ @PutMapping("/keycloak/changePassword")
|
|
|
+ public Response changePassword(@RequestHeader("userId") String userId, String newPassword) {
|
|
|
+ if (StringUtils.isEmpty(userId)) {
|
|
|
+ return Response.buildFailure("400", "密码不能为空");
|
|
|
+ }
|
|
|
+ keycloakService.changePassword(userId, newPassword);
|
|
|
+ return Response.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(cron = "0 0/10 * * * ?")
|
|
|
+ public void askUserOppenId() {
|
|
|
+ log.info("update wechat openId");
|
|
|
+ for (String userId : keycloakService.getUsersMap().keySet()) {
|
|
|
+ String oppenId = iWeChatService.askUser(userId);
|
|
|
+ if (!StringUtils.isEmpty(oppenId)) {
|
|
|
+ SubscribeDto dto = new SubscribeDto();
|
|
|
+ dto.setUserId(userId);
|
|
|
+ dto.setOppenId(oppenId);
|
|
|
+ this.updateUser(JSONObject.toJSONString(dto));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "密码重置")
|
|
|
+ @PutMapping("/admin/users/{userId}/resetPassword")
|
|
|
+ public Response resetPassword(@PathVariable("userId") String userId , @RequestBody CredentialRepresentation credential){
|
|
|
+ if(StringUtils.isEmpty(credential.getType()) || !credential.getType().equals(Constant.type)){
|
|
|
+ return Response.buildFailure("500","type参数不合法!");
|
|
|
+ }
|
|
|
+ if(StringUtils.isEmpty(credential.getValue())){
|
|
|
+ return Response.buildFailure("500","value不能为空!");
|
|
|
+ }
|
|
|
+ return keycloakService.updateUser(userId , credential , Constant.RESET_PASSWORD);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SysLogIgnore
|
|
|
+ @PutMapping("/admin/users/role/configure")
|
|
|
+ public Response roleMapping(@RequestHeader String userId ,@RequestBody UsersRoleMappingDTO usersRoleMappingDTO){
|
|
|
+ // 注解的方式不好描述日志信息,接口方式存储
|
|
|
+ SmsbLogDto logDto = new SmsbLogDto();
|
|
|
+ logDto.setUserId(userId);
|
|
|
+ logDto.setBusiness(3);
|
|
|
+ logDto.setDescription("账号角色分配");
|
|
|
+ StringBuffer sbf = new StringBuffer("给用户:"+ usersRoleMappingDTO.getUserId());
|
|
|
+ if(!usersRoleMappingDTO.getAddRoleList().isEmpty()){
|
|
|
+ sbf.append(",分配角色:"+getRoleNameList(usersRoleMappingDTO.getAddRoleList()));
|
|
|
+ }
|
|
|
+ if(!usersRoleMappingDTO.getRemoveRoleList().isEmpty()){
|
|
|
+ sbf.append(",删除角色:"+getRoleNameList(usersRoleMappingDTO.getRemoveRoleList()));
|
|
|
+ }
|
|
|
+ logDto.setParams(sbf.toString());
|
|
|
+ logDto.setRequestMethod(1);
|
|
|
+ logDto.setMethod("roleMapping()");
|
|
|
+ logDto.setStatus(1);
|
|
|
+ logDto.setIp(NetUtils.getIpAddr(HttpContextUtils.getHttpServletRequest()));
|
|
|
+ logDto.setOperTime(LocalDateTime.now());
|
|
|
+ logDto.setTenant("unknown");
|
|
|
+ log.info("账号角色分配日志记录:{}" , sbf);
|
|
|
+ smsbLogService.saveLog(logDto);
|
|
|
+ return keycloakService.roleMapping(usersRoleMappingDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<String> getRoleNameList(List<RoleRepresentation> roleRepList){
|
|
|
+ return roleRepList.stream().map(RoleRepresentation::getDescription).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "获取账号凭证")
|
|
|
+ @GetMapping("/admin/users/{userId}/credentials")
|
|
|
+ public Response queryUserCredentials(@PathVariable("userId")String userId){
|
|
|
+ return keycloakService.queryUserCredentials(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "删除账号凭证")
|
|
|
+ @DeleteMapping("/admin/users/{userId}/credentials/{credentialId}")
|
|
|
+ public Response removeUserCredentials(@PathVariable("userId")String userId,
|
|
|
+ @PathVariable("credentialId")String credentialId){
|
|
|
+ return keycloakService.removeUserCredentials(userId ,credentialId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "更新用户属性")
|
|
|
+ @PutMapping("/keycloak/users/attribute")
|
|
|
+ public Response updateUserAttribute(@RequestHeader String userId , @RequestBody UserRepresentation representation){
|
|
|
+ return keycloakService.updateUser(userId, representation, Constant.RESET_ATTRIBUTE);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "新增账号",
|
|
|
+ business = "添加账号:${smsbUserAdd.username}/${smsbUserAdd.name}于部门:${smsbUserAdd.departmentId}")
|
|
|
+ @PostMapping("/admin/users")
|
|
|
+ public Response addKeyClaokUser(@RequestHeader String userId ,@Valid @RequestBody SmsbUserAdd smsbUserAdd){
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, smsbUserAdd.getTenant());
|
|
|
+ if(!checkResponse.isSuccess()){return checkResponse;}
|
|
|
+ return departmentService.addKeyClaokUser(userId ,smsbUserAdd);
|
|
|
+ }
|
|
|
+ @SmsbMethodLog(description = "账号分组",
|
|
|
+ business = "将账号:${smsbRegrouping.userId}添加到部门:${smsbRegrouping.departmentId}")
|
|
|
+ @PutMapping("/admin/users/regrouping")
|
|
|
+ public Response regrouping(@RequestHeader String userId,
|
|
|
+ @Valid @RequestBody SmsbRegrouping smsbRegrouping){
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, userService.getOneByUserId(
|
|
|
+ smsbRegrouping.getUserId()).getTenant());
|
|
|
+ if(!checkResponse.isSuccess()){return checkResponse;}
|
|
|
+ return departmentService.regrouping(smsbRegrouping);
|
|
|
+ }
|
|
|
+ @SmsbMethodLog(description = "账号注销", business = "账号:${uId}注销")
|
|
|
+ @DeleteMapping("/admin/users/{uId}")
|
|
|
+ public Response enableSwitch(@RequestHeader String userId,
|
|
|
+ @PathVariable("uId") String uId){
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, userService.getOneByUserId(uId).getTenant());
|
|
|
+ if(!checkResponse.isSuccess()){return checkResponse;}
|
|
|
+ if(userService.removeUser(uId)){
|
|
|
+ return keycloakService.removeUser(uId);
|
|
|
+ }else{
|
|
|
+ return Response.buildFailure("500","注销用户失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "部门用户分页列表展示", business = "获取部门:${departmentCmd.departmentId}用户列表")
|
|
|
+ @PostMapping("/admin/department/user/list")
|
|
|
+ public PageResponse<SmsbSimpleUserCO> queryDepartmentUserList(@RequestHeader String userId , @Valid @RequestBody SmsbDepartmentCmd departmentCmd){
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, departmentCmd.getTenant());
|
|
|
+ if(!checkResponse.isSuccess()){return PageResponse.buildFailure(checkResponse.getErrCode(),checkResponse.getErrMessage());}
|
|
|
+ return departmentService.queryDepartmentUserList(departmentCmd);
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "账号部门迁移", business = "将账号:${userMigration.userName}迁移到部门:${userMigration.path}")
|
|
|
+ @PostMapping("/admin/users/temporary/migration")
|
|
|
+ public Response migration(@RequestHeader String userId , @RequestBody SmsbUserMigration userMigration){
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, userMigration.getTenant());
|
|
|
+ if(!checkResponse.isSuccess()){return checkResponse;}
|
|
|
+ return userService.migration(userMigration);
|
|
|
+ }
|
|
|
+ @SmsbMethodLog(description = "账号启用/禁用", business = "账号:${switchDTO.userId}启用:${switchDTO.enabled}")
|
|
|
+ @PutMapping("/admin/users/enabled")
|
|
|
+ public Response enableSwitch(@RequestHeader String userId,
|
|
|
+ @RequestBody SwitchDTO switchDTO){
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, userService.getOneByUserId(switchDTO.getUserId())
|
|
|
+ .getTenant());
|
|
|
+ if(!checkResponse.isSuccess()){return checkResponse;}
|
|
|
+ Boolean flag = userService.enableSwitch(switchDTO);
|
|
|
+ if(flag){
|
|
|
+ return keycloakService.updateUser(switchDTO.getUserId() , switchDTO , Constant.ENABLE);
|
|
|
+ }
|
|
|
+ return Response.buildFailure("500","账号启用/禁用失败!");
|
|
|
+ }
|
|
|
+ @SmsbMethodLog(description = "账号名称修改", business = "用户:${userUpdate.userId}将名称修改为:${userUpdate.name}")
|
|
|
+ @PutMapping("/admin/users/update")
|
|
|
+ public Response updateUserFirstName(@RequestHeader String userId ,
|
|
|
+ @RequestBody SmsbUserUpdate userUpdate) {
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, userService.getOneByUserId(userUpdate.getUserId()).getTenant());
|
|
|
+ if (!checkResponse.isSuccess()) {
|
|
|
+ return checkResponse;
|
|
|
+ }
|
|
|
+ Boolean flag = userService.updateUserFirstName(userUpdate);
|
|
|
+ if (flag) {
|
|
|
+ return keycloakService.updateUser(userUpdate.getUserId(), userUpdate.getName(), Constant.UPDATE_USER_FIRSTNAME);
|
|
|
+ }
|
|
|
+ return Response.buildFailure("500", "用户名修改失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "消息推送预警级别", business = "将用户:${levelUpdate.userId}消息推送预警级别改为:${levelUpdate.informLevel}")
|
|
|
+ @PutMapping("/admin/users/update/deviceExceptionLevel")
|
|
|
+ public Response updateUserFirstName(@RequestHeader String userId,
|
|
|
+ @RequestBody SmsbUserExceptionLevelUpdate levelUpdate) {
|
|
|
+ Response checkResponse = departmentService.checkOperationAuthority(userId, userService.getOneByUserId(levelUpdate.getUserId()).getTenant());
|
|
|
+ if (!checkResponse.isSuccess()) {
|
|
|
+ return checkResponse;
|
|
|
+ }
|
|
|
+ if (userService.updateDeviceExceptionLevel(levelUpdate)) {
|
|
|
+ return Response.buildSuccess();
|
|
|
+ }
|
|
|
+ return Response.buildFailure("500", "接收级别修改失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ @SmsbMethodLog(description = "个人部门树展示")
|
|
|
+ @GetMapping("/keycloak/oneself/tree")
|
|
|
+ public Response queryOneselfDepartTree(@RequestHeader String userId) {
|
|
|
+ return departmentService.queryOneselfDepartTree(userId);
|
|
|
+ }
|
|
|
+}
|