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;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.support.LdapContextSource;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LdapContext {
private static final SystemConfiguration configuration = SystemConfiguration.getInstances();
......@@ -65,7 +68,9 @@ public class LdapContext {
return controls;
}
/** Context mapper for LDAP user identification. */
/**
* Context mapper for LDAP user identification.
*/
public static class UserContextMapper implements ContextMapper<LdapUserIdentification> {
/**
......@@ -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> {
/**
......@@ -98,16 +105,19 @@ public class LdapContext {
Attribute nicknameAttr =
attributes.get(configuration.getLdapCastNickname().getValue());
if (usernameAttr != null && nicknameAttr != null) {
if (usernameAttr != null) {
User user = new User();
user.setUsername(usernameAttr.get().toString());
user.setNickname(nicknameAttr.get().toString());
if (nicknameAttr != null) {
user.setNickname(nicknameAttr.get().toString());
}
user.setUserType(UserType.LDAP.getCode());
user.setEnabled(true);
return user;
} else {
log.error("LDAP user mapping failed, username attribute is null");
return null;
}
return null;
}
}
}
......@@ -87,6 +87,7 @@ public class LdapController {
List<User> users = ldapService.listUsers();
List<User> localUsers = userService.list();
// 已经存在的用户不可导入 | Existing users cannot be imported
users.stream()
.filter(ldapUser ->
localUsers.stream().anyMatch(user -> user.getUsername().equals(ldapUser.getUsername())))
......
......@@ -30,6 +30,7 @@ import org.dinky.service.LdapService;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.naming.directory.Attributes;
......@@ -87,10 +88,10 @@ public class LdapServiceImpl implements LdapService {
// Build the User with cast
User user = new User();
user.setUsername(loginDTO.getUsername());
user.setNickname(attributes
.get(configuration.getLdapCastNickname().getValue())
.get()
.toString());
Optional.of(attributes
.get(configuration.getLdapCastNickname().getValue())
.get())
.ifPresent(obj -> user.setNickname(obj.toString()));
return user;
} catch (Exception e) {
if (e instanceof AuthenticationException) {
......
......@@ -276,7 +276,6 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
// User doesn't exist locally
// Check if LDAP user autoload is enabled
if (!SystemConfiguration.getInstances().getLdapAutoload().getValue()) {
loginLogService.saveLoginLog(userFromLocal, Status.USER_NAME_PASSWD_ERROR);
throw new AuthException(Status.LDAP_USER_AUTOLOAD_FORBAID);
}
......@@ -285,7 +284,6 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
SystemConfiguration.getInstances().getLdapDefaultTeant().getValue();
Tenant tenant = tenantService.getTenantByTenantCode(defaultTeantCode);
if (Asserts.isNull(tenant)) {
loginLogService.saveLoginLog(userFromLocal, Status.LDAP_DEFAULT_TENANT_NOFOUND);
throw new AuthException(Status.LDAP_DEFAULT_TENANT_NOFOUND);
}
......
......@@ -169,6 +169,16 @@ public class SystemConfiguration {
.defaultValue("")
.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)
.stringType()
.defaultValue("")
......@@ -184,16 +194,6 @@ public class SystemConfiguration {
.defaultValue("DefaultTenant")
.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)
.booleanType()
.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