fix: 修复ldap登录时存在本地同名用户的报错提示

This commit is contained in:
CaptainB 2022-10-25 18:42:05 +08:00 committed by 刘瑞斌
parent 92719a7c34
commit bd51abfe1f
3 changed files with 15 additions and 10 deletions

View File

@ -3,6 +3,7 @@ package io.metersphere.gateway.service;
import io.metersphere.base.domain.User; import io.metersphere.base.domain.User;
import io.metersphere.commons.constants.ParamConstants; import io.metersphere.commons.constants.ParamConstants;
import io.metersphere.commons.constants.SessionConstants;
import io.metersphere.commons.constants.UserSource; import io.metersphere.commons.constants.UserSource;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.user.SessionUser;
@ -102,7 +103,7 @@ public class LdapService {
} }
user.setSource(UserSource.LDAP.name()); user.setSource(UserSource.LDAP.name());
u = userLoginService.addLdapUser(user); userLoginService.addLdapUser(user);
} else { } else {
// 更新 // 更新
u.setName(name); u.setName(name);
@ -111,14 +112,16 @@ public class LdapService {
userLoginService.updateUser(u); userLoginService.updateUser(u);
} }
session.getAttributes().put("authenticate", UserSource.LDAP.name());
session.getAttributes().put("email", email);
// 执行 LocalRealm LDAP 登录逻辑 // 执行 LocalRealm LDAP 登录逻辑
LoginRequest loginRequest = new LoginRequest(); LoginRequest loginRequest = new LoginRequest();
loginRequest.setUsername(userId); loginRequest.setUsername(userId);
loginRequest.setAuthenticate(UserSource.LDAP.name()); loginRequest.setAuthenticate(UserSource.LDAP.name());
return userLoginService.login(loginRequest, session, locale);
Optional<SessionUser> sessionUser = userLoginService.login(loginRequest, session, locale);
session.getAttributes().put("authenticate", UserSource.LDAP.name());
session.getAttributes().put("email", email);
return sessionUser;
} }
private boolean authenticate(String dn, String credentials, LdapTemplate ldapTemplate) throws AuthenticationException { private boolean authenticate(String dn, String credentials, LdapTemplate ldapTemplate) throws AuthenticationException {

View File

@ -156,7 +156,6 @@ public class SSOService {
Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale); Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale);
session.getAttributes().put("authenticate", authSource.getType()); session.getAttributes().put("authenticate", authSource.getType());
session.getAttributes().put("authId", authSource.getId()); session.getAttributes().put("authId", authSource.getId());
session.getAttributes().put(SessionConstants.ATTR_USER, userOptional.get());
return userOptional; return userOptional;
} }
@ -196,7 +195,6 @@ public class SSOService {
Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale); Optional<SessionUser> userOptional = userLoginService.login(loginRequest, session, locale);
session.getAttributes().put("authenticate", authSource.getType()); session.getAttributes().put("authenticate", authSource.getType());
session.getAttributes().put("authId", authSource.getId()); session.getAttributes().put("authId", authSource.getId());
session.getAttributes().put(SessionConstants.ATTR_USER, userOptional.get());
session.getAttributes().put("casTicket", ticket); session.getAttributes().put("casTicket", ticket);
// 记录cas对应关系 // 记录cas对应关系
Long timeout = env.getProperty("spring.session.timeout", Long.class); Long timeout = env.getProperty("spring.session.timeout", Long.class);

View File

@ -50,7 +50,7 @@ public class UserLoginService {
userDTO = loginSsoMode(request.getUsername(), request.getAuthenticate()); userDTO = loginSsoMode(request.getUsername(), request.getAuthenticate());
break; break;
case "LDAP": case "LDAP":
userDTO = loginLdapMode(request.getUsername(), request.getAuthenticate()); userDTO = loginLdapMode(request.getUsername());
break; break;
default: default:
userDTO = loginLocalMode(request.getUsername(), request.getPassword()); userDTO = loginLocalMode(request.getUsername(), request.getPassword());
@ -62,8 +62,12 @@ public class UserLoginService {
return Optional.of(sessionUser); return Optional.of(sessionUser);
} }
private UserDTO loginLdapMode(String userId, String authenticate) { private UserDTO loginLdapMode(String userId) {
return getLoginUser(userId, Collections.singletonList(authenticate)); UserDTO loginUser = getLoginUser(userId, Collections.singletonList(UserSource.LDAP.name()));
if (loginUser == null) {
MSException.throwException(Translator.get("user_not_found_or_not_unique"));
}
return loginUser;
} }
private UserDTO loginSsoMode(String userId, String authType) { private UserDTO loginSsoMode(String userId, String authType) {