diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/UserInfoController.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/UserInfoController.java index e12d07e..9c4e3c8 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/UserInfoController.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/controller/UserInfoController.java @@ -3,10 +3,7 @@ package com.ningdatech.pmapi.user.controller; import com.ningdatech.basic.model.PageVo; import com.ningdatech.pmapi.user.manage.UserInfoManage; -import com.ningdatech.pmapi.user.model.po.ReqUserDetailEditPO; -import com.ningdatech.pmapi.user.model.po.ReqUserDetailPO; -import com.ningdatech.pmapi.user.model.po.ReqUserDisableOrEnablePO; -import com.ningdatech.pmapi.user.model.po.ReqUserInfoListPO; +import com.ningdatech.pmapi.user.model.po.*; import com.ningdatech.pmapi.user.model.vo.ResUserDetailVO; import com.ningdatech.pmapi.user.model.vo.ResUserInfoListVO; import io.swagger.annotations.Api; @@ -17,6 +14,7 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; /** @@ -61,4 +59,10 @@ public class UserInfoController { return userInfoManage.currentUserInfo(); } + @ApiOperation(value = "代登陆", notes = "代登陆") + @PostMapping("/generation-login") + public void generationLogin(@Valid @RequestBody ReqGenerationLoginPO reqGenerationLoginPO, HttpServletRequest httpServletRequest) { + userInfoManage.generationLogin(reqGenerationLoginPO); + } + } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/manage/UserInfoManage.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/manage/UserInfoManage.java index b0a3237..d74ae3f 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/manage/UserInfoManage.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/manage/UserInfoManage.java @@ -15,19 +15,24 @@ import com.ningdatech.pmapi.sys.model.entity.Role; import com.ningdatech.pmapi.sys.model.entity.UserRole; import com.ningdatech.pmapi.sys.service.IRoleService; import com.ningdatech.pmapi.sys.service.IUserRoleService; +import com.ningdatech.pmapi.user.constant.LoginTypeEnum; import com.ningdatech.pmapi.user.constant.UserAvailableEnum; import com.ningdatech.pmapi.user.entity.UserInfo; -import com.ningdatech.pmapi.user.model.po.ReqUserDetailEditPO; -import com.ningdatech.pmapi.user.model.po.ReqUserDetailPO; -import com.ningdatech.pmapi.user.model.po.ReqUserDisableOrEnablePO; -import com.ningdatech.pmapi.user.model.po.ReqUserInfoListPO; +import com.ningdatech.pmapi.user.model.po.*; import com.ningdatech.pmapi.user.model.vo.ResUserDetailVO; import com.ningdatech.pmapi.user.model.vo.ResUserInfoListVO; import com.ningdatech.pmapi.user.model.vo.UserRoleVO; +import com.ningdatech.pmapi.user.security.auth.constants.UserDeatilsServiceConstant; +import com.ningdatech.pmapi.user.security.auth.credential.CredentialAuthToken; import com.ningdatech.pmapi.user.security.auth.model.UserFullInfoDTO; import com.ningdatech.pmapi.user.service.IUserInfoService; import com.ningdatech.pmapi.user.util.LoginUserUtil; import lombok.RequiredArgsConstructor; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.web.authentication.WebAuthenticationDetails; +import org.springframework.security.web.context.HttpSessionSecurityContextRepository; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -50,6 +55,7 @@ public class UserInfoManage { private final IUserRoleService iUserRoleService; private final IRoleService iRoleService; private final UserInfoHelper userInfoHelper; + private final UserDetailsService userDetailsService; public PageVo list(ReqUserInfoListPO reqUserInfoListPO) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(DingEmployeeInfo.class) @@ -100,7 +106,7 @@ public class UserInfoManage { resListVO.setUpdateTime(r.getUpdateOn()); // 从用户信息中获取 resListVO.setEmployeeCode(r.getEmployeeCode()); - resListVO.setUserRoleList(new ArrayList<>()); + resListVO.setUserRoleList(getUserRoleVOList(resListVO.getUserId())); return resListVO; }).collect(Collectors.toList()); } @@ -111,6 +117,27 @@ public class UserInfoManage { return pageVo; } + + private List getUserRoleVOList(Long userId) { + List userRoleInfoList = new ArrayList<>(); + if (Objects.isNull(userId)) { + return userRoleInfoList; + } + List userRoleList = iUserRoleService.list(Wrappers.lambdaQuery(UserRole.class) + .eq(UserRole::getUserId, userId)); + if (CollUtil.isNotEmpty(userRoleList)) { + List roleIdList = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList()); + List roleList = iRoleService.list(Wrappers.lambdaQuery(Role.class).in(Role::getId, roleIdList)); + userRoleInfoList = roleList.stream().map(r -> { + UserRoleVO userRoleVO = new UserRoleVO(); + userRoleVO.setId(r.getId()); + userRoleVO.setName(r.getName()); + return userRoleVO; + }).collect(Collectors.toList()); + } + return userRoleInfoList; + } + public void disableOrEnable(ReqUserDisableOrEnablePO reqUserDisableOrEnablePO) { Long userId = generateUserId(reqUserDisableOrEnablePO.getEmployeeCode()); UserInfo userInfo = iUserInfoService.getById(userId); @@ -131,7 +158,8 @@ public class UserInfoManage { resUserDetailVO.setStatus(userInfo.getAvailable()); // 装配用户角色信息列表 - List userRoleList = iUserRoleService.list(Wrappers.lambdaQuery(UserRole.class).eq(UserRole::getUserId, userId)); + List userRoleList = iUserRoleService.list(Wrappers.lambdaQuery(UserRole.class) + .eq(UserRole::getUserId, userId)); List userRoleInfoList = new ArrayList<>(); if (CollUtil.isNotEmpty(userRoleList)) { List roleIdList = userRoleList.stream().map(UserRole::getRoleId).collect(Collectors.toList()); @@ -208,4 +236,40 @@ public class UserInfoManage { UserFullInfoDTO userFullInfo = userInfoHelper.getUserFullInfo(userId); return null; } + + public void generationLogin(ReqGenerationLoginPO reqGenerationLoginPO) { + Long userId = reqGenerationLoginPO.getUserId(); + UserInfo userInfo = iUserInfoService.getById(userId); + if (Objects.isNull(userInfo)) { + throw new BizException("该员工账号处于禁用状态中,无法使用"); + } + if (!UserAvailableEnum.ENABLE.name().equals(userInfo.getAvailable())) { + throw new BizException("该员工账号处于禁用状态中,无法使用"); + } + UserFullInfoDTO userFullInfo = userInfoHelper.getUserFullInfo(userId); + + +// ReqGenerationLoginPO reqGenerationLoginPO + } + +// public void autoLogin(Long userId){ +// userDetailsService.loadUserByUsername(userId + UserDeatilsServiceConstant.USER_DETAILS_SERVICE_SEPARATOR + LoginTypeEnum.USERNAME_PASSWORD_LOGIN.name()); +// +// CredentialAuthToken token = new CredentialAuthToken(email, password); +// try { +// token.setDetails(new WebAuthenticationDetails(httpServletRequest)); +// UsernamePasswordAuthToken authenticatedUser = (UsernamePasswordAuthToken)usernamePasswordAuthSecurityConfig +// .getAuthenticationManager().authenticate(token); +// SecurityContextHolder.getContext().setAuthentication(authenticatedUser); +// httpServletRequest.getSession().setAttribute( +// HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext()); +// String sessionId = httpServletRequest.getSession().getId(); +// putSessionIdToCache(LoginUserUtil.getUserId(), sessionId); +// } catch ( +// AuthenticationException e) { +// throw new RuntimeException("autoLogIn Authentication failed!", e); +// } +// } + + } diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/model/po/ReqGenerationLoginPO.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/model/po/ReqGenerationLoginPO.java new file mode 100644 index 0000000..91ab08b --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/model/po/ReqGenerationLoginPO.java @@ -0,0 +1,21 @@ +package com.ningdatech.pmapi.user.model.po; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +/** + * @author liuxinxin + * @date 2023/2/21 下午3:39 + */ +@Data +@ApiModel("代登陆请求PO") +public class ReqGenerationLoginPO { + + @NotNull(message = "用户id 不能为空") + @ApiModelProperty("用户id") + private Long userId; + +} diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/user/model/vo/ResUserInfoListVO.java b/pmapi/src/main/java/com/ningdatech/pmapi/user/model/vo/ResUserInfoListVO.java index 8b189ca..f552f0a 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/user/model/vo/ResUserInfoListVO.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/user/model/vo/ResUserInfoListVO.java @@ -1,6 +1,5 @@ package com.ningdatech.pmapi.user.model.vo; -import com.ningdatech.pmapi.sys.model.entity.Role; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -38,7 +37,7 @@ public class ResUserInfoListVO { private Long regionId; @ApiModelProperty("用户角色") - private List userRoleList; + private List userRoleList; @ApiModelProperty("状态") private String status; @@ -46,5 +45,4 @@ public class ResUserInfoListVO { @ApiModelProperty("更新时间") private LocalDateTime updateTime; - }