Unverified Commit 3de42d73 authored by gaoyan's avatar gaoyan Committed by GitHub

[Optimized ] Optimized the logic for obtaining users from LDAP (#3050)

parent e57093b6
...@@ -34,6 +34,9 @@ import org.springframework.ldap.core.ContextMapper; ...@@ -34,6 +34,9 @@ import org.springframework.ldap.core.ContextMapper;
import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.core.support.LdapContextSource;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LdapContext { public class LdapContext {
private static final SystemConfiguration configuration = SystemConfiguration.getInstances(); private static final SystemConfiguration configuration = SystemConfiguration.getInstances();
...@@ -65,7 +68,9 @@ public class LdapContext { ...@@ -65,7 +68,9 @@ public class LdapContext {
return controls; return controls;
} }
/** Context mapper for LDAP user identification. */ /**
* Context mapper for LDAP user identification.
*/
public static class UserContextMapper implements ContextMapper<LdapUserIdentification> { public static class UserContextMapper implements ContextMapper<LdapUserIdentification> {
/** /**
...@@ -81,7 +86,9 @@ public class LdapContext { ...@@ -81,7 +86,9 @@ public class LdapContext {
} }
} }
/** Attributes mapper from LDAP user to Local user. */ /**
* Attributes mapper from LDAP user to Local user.
*/
public static class UserAttributesMapperMapper implements AttributesMapper<User> { public static class UserAttributesMapperMapper implements AttributesMapper<User> {
/** /**
...@@ -98,16 +105,19 @@ public class LdapContext { ...@@ -98,16 +105,19 @@ public class LdapContext {
Attribute nicknameAttr = Attribute nicknameAttr =
attributes.get(configuration.getLdapCastNickname().getValue()); attributes.get(configuration.getLdapCastNickname().getValue());
if (usernameAttr != null && nicknameAttr != null) { if (usernameAttr != null) {
User user = new User(); User user = new User();
user.setUsername(usernameAttr.get().toString()); user.setUsername(usernameAttr.get().toString());
user.setNickname(nicknameAttr.get().toString()); if (nicknameAttr != null) {
user.setNickname(nicknameAttr.get().toString());
}
user.setUserType(UserType.LDAP.getCode()); user.setUserType(UserType.LDAP.getCode());
user.setEnabled(true); user.setEnabled(true);
return user; return user;
} else {
log.error("LDAP user mapping failed, username attribute is null");
return null;
} }
return null;
} }
} }
} }
...@@ -87,6 +87,7 @@ public class LdapController { ...@@ -87,6 +87,7 @@ public class LdapController {
List<User> users = ldapService.listUsers(); List<User> users = ldapService.listUsers();
List<User> localUsers = userService.list(); List<User> localUsers = userService.list();
// 已经存在的用户不可导入 | Existing users cannot be imported
users.stream() users.stream()
.filter(ldapUser -> .filter(ldapUser ->
localUsers.stream().anyMatch(user -> user.getUsername().equals(ldapUser.getUsername()))) localUsers.stream().anyMatch(user -> user.getUsername().equals(ldapUser.getUsername())))
......
...@@ -30,6 +30,7 @@ import org.dinky.service.LdapService; ...@@ -30,6 +30,7 @@ import org.dinky.service.LdapService;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.naming.directory.Attributes; import javax.naming.directory.Attributes;
...@@ -87,10 +88,10 @@ public class LdapServiceImpl implements LdapService { ...@@ -87,10 +88,10 @@ public class LdapServiceImpl implements LdapService {
// Build the User with cast // Build the User with cast
User user = new User(); User user = new User();
user.setUsername(loginDTO.getUsername()); user.setUsername(loginDTO.getUsername());
user.setNickname(attributes Optional.of(attributes
.get(configuration.getLdapCastNickname().getValue()) .get(configuration.getLdapCastNickname().getValue())
.get() .get())
.toString()); .ifPresent(obj -> user.setNickname(obj.toString()));
return user; return user;
} catch (Exception e) { } catch (Exception e) {
if (e instanceof AuthenticationException) { if (e instanceof AuthenticationException) {
......
...@@ -276,7 +276,6 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -276,7 +276,6 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
// User doesn't exist locally // User doesn't exist locally
// Check if LDAP user autoload is enabled // Check if LDAP user autoload is enabled
if (!SystemConfiguration.getInstances().getLdapAutoload().getValue()) { if (!SystemConfiguration.getInstances().getLdapAutoload().getValue()) {
loginLogService.saveLoginLog(userFromLocal, Status.USER_NAME_PASSWD_ERROR);
throw new AuthException(Status.LDAP_USER_AUTOLOAD_FORBAID); throw new AuthException(Status.LDAP_USER_AUTOLOAD_FORBAID);
} }
...@@ -285,7 +284,6 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen ...@@ -285,7 +284,6 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
SystemConfiguration.getInstances().getLdapDefaultTeant().getValue(); SystemConfiguration.getInstances().getLdapDefaultTeant().getValue();
Tenant tenant = tenantService.getTenantByTenantCode(defaultTeantCode); Tenant tenant = tenantService.getTenantByTenantCode(defaultTeantCode);
if (Asserts.isNull(tenant)) { if (Asserts.isNull(tenant)) {
loginLogService.saveLoginLog(userFromLocal, Status.LDAP_DEFAULT_TENANT_NOFOUND);
throw new AuthException(Status.LDAP_DEFAULT_TENANT_NOFOUND); throw new AuthException(Status.LDAP_DEFAULT_TENANT_NOFOUND);
} }
......
...@@ -169,6 +169,16 @@ public class SystemConfiguration { ...@@ -169,6 +169,16 @@ public class SystemConfiguration {
.defaultValue("") .defaultValue("")
.note(Status.SYS_LDAP_SETTINGS_BASEDN_NOTE); .note(Status.SYS_LDAP_SETTINGS_BASEDN_NOTE);
private final Configuration<String> ldapCastUsername = key(Status.SYS_LDAP_SETTINGS_CASTUSERNAME)
.stringType()
.defaultValue("cn")
.note(Status.SYS_LDAP_SETTINGS_CASTUSERNAME_NOTE);
private final Configuration<String> ldapCastNickname = key(Status.SYS_LDAP_SETTINGS_CASTNICKNAME)
.stringType()
.defaultValue("sn")
.note(Status.SYS_LDAP_SETTINGS_CASTNICKNAME_NOTE);
private final Configuration<String> ldapFilter = key(Status.SYS_LDAP_SETTINGS_FILTER) private final Configuration<String> ldapFilter = key(Status.SYS_LDAP_SETTINGS_FILTER)
.stringType() .stringType()
.defaultValue("") .defaultValue("")
...@@ -184,16 +194,6 @@ public class SystemConfiguration { ...@@ -184,16 +194,6 @@ public class SystemConfiguration {
.defaultValue("DefaultTenant") .defaultValue("DefaultTenant")
.note(Status.SYS_LDAP_SETTINGS_DEFAULTTEANT_NOTE); .note(Status.SYS_LDAP_SETTINGS_DEFAULTTEANT_NOTE);
private final Configuration<String> ldapCastUsername = key(Status.SYS_LDAP_SETTINGS_CASTUSERNAME)
.stringType()
.defaultValue("cn")
.note(Status.SYS_LDAP_SETTINGS_CASTUSERNAME_NOTE);
private final Configuration<String> ldapCastNickname = key(Status.SYS_LDAP_SETTINGS_CASTNICKNAME)
.stringType()
.defaultValue("sn")
.note(Status.SYS_LDAP_SETTINGS_CASTNICKNAME_NOTE);
private final Configuration<Boolean> ldapEnable = key(Status.SYS_LDAP_SETTINGS_ENABLE) private final Configuration<Boolean> ldapEnable = key(Status.SYS_LDAP_SETTINGS_ENABLE)
.booleanType() .booleanType()
.defaultValue(false) .defaultValue(false)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment