Skip to content

Commit

Permalink
[Optimized ] Optimized the logic for obtaining users from LDAP (DataL…
Browse files Browse the repository at this point in the history
…inkDC#3050)

Co-authored-by: gaoyan1998 <gaoyan1998@users.noreply.github.com>
  • Loading branch information
gaoyan1998 and gaoyan1998 authored Jan 23, 2024
1 parent e57093b commit 3de42d7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
22 changes: 16 additions & 6 deletions dinky-admin/src/main/java/org/dinky/context/LdapContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
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();

Expand Down Expand Up @@ -65,7 +68,9 @@ public static SearchControls getControls() {
return controls;
}

/** Context mapper for LDAP user identification. */
/**
* Context mapper for LDAP user identification.
*/
public static class UserContextMapper implements ContextMapper<LdapUserIdentification> {

/**
Expand All @@ -81,7 +86,9 @@ public LdapUserIdentification mapFromContext(Object ctx) {
}
}

/** Attributes mapper from LDAP user to Local user. */
/**
* Attributes mapper from LDAP user to Local user.
*/
public static class UserAttributesMapperMapper implements AttributesMapper<User> {

/**
Expand All @@ -98,16 +105,19 @@ public User mapFromAttributes(Attributes attributes) throws NamingException {
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public Result<List<User>> listUser() {
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())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.naming.directory.Attributes;
Expand Down Expand Up @@ -87,10 +88,10 @@ public User authenticate(LoginDTO loginDTO) throws AuthException {
// 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ private User ldapLogin(LoginDTO loginDTO) throws AuthException {
// 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);
}

Expand All @@ -285,7 +284,6 @@ private User ldapLogin(LoginDTO loginDTO) throws AuthException {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ public static Configuration.OptionBuilder key(Status status) {
.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("")
Expand All @@ -184,16 +194,6 @@ public static Configuration.OptionBuilder key(Status status) {
.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)
Expand Down

0 comments on commit 3de42d7

Please sign in to comment.