首次登录随机选中工作区间和组织

This commit is contained in:
shiziyuan9527 2020-03-10 18:17:52 +08:00
parent 79a39e5773
commit 6f64057e3b
1 changed files with 23 additions and 1 deletions

View File

@ -1,19 +1,27 @@
package io.metersphere.controller; package io.metersphere.controller;
import io.metersphere.base.domain.UserRole;
import io.metersphere.controller.request.LoginRequest; import io.metersphere.controller.request.LoginRequest;
import io.metersphere.dto.UserDTO;
import io.metersphere.i18n.Translator; import io.metersphere.i18n.Translator;
import io.metersphere.user.SessionUtils; import io.metersphere.service.UserService;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*; import org.apache.shiro.authc.*;
import org.apache.shiro.authz.UnauthorizedException; import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping @RequestMapping
public class LoginController { public class LoginController {
@Resource
private UserService userService;
@GetMapping(value = "/isLogin") @GetMapping(value = "/isLogin")
public ResultHolder isLogin() { public ResultHolder isLogin() {
if (SecurityUtils.getSubject().isAuthenticated()) { if (SecurityUtils.getSubject().isAuthenticated()) {
@ -37,6 +45,20 @@ public class LoginController {
try { try {
subject.login(token); subject.login(token);
if (subject.isAuthenticated()) { if (subject.isAuthenticated()) {
UserDTO user = (UserDTO) subject.getSession().getAttribute("user");
// 自动选中组织工作空间
if (StringUtils.isBlank(user.getLastOrganizationId())) {
List<UserRole> userRoles = user.getUserRoles();
List<UserRole> test = userRoles.stream().filter(ur -> ur.getRoleId().indexOf("test") > -1).collect(Collectors.toList());
List<UserRole> org = userRoles.stream().filter(ur -> ur.getRoleId().indexOf("org") > -1).collect(Collectors.toList());
if (test.size() > 0) {
String wsId = test.get(0).getSourceId();
userService.switchUserRole(user, "workspace", wsId);
} else if (org.size() > 0) {
String orgId = org.get(0).getSourceId();
userService.switchUserRole(user, "organization", orgId);
}
}
// 返回 userDTO // 返回 userDTO
return ResultHolder.success(subject.getSession().getAttribute("user")); return ResultHolder.success(subject.getSession().getAttribute("user"));
} else { } else {