新增LDAP的部门同步

This commit is contained in:
xxx
2023-09-20 16:33:58 +08:00
parent a82e2992b4
commit b685a21717
10 changed files with 176 additions and 18 deletions

View File

@@ -41,5 +41,4 @@ public class ConfigConstant {
public static final String LDAP_ADMIN_USER = "ldap.admin_user";
public static final String LDAP_ADMIN_PASS = "ldap.admin_pass";
public static final String LDAP_BASE_DN = "ldap.base_dn";
public static final String LDAP_USER_DN_PREFIX = "ldap.user_dn_prefix";
}

View File

@@ -18,6 +18,7 @@ package xyz.playedu.common.service;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.playedu.common.domain.AppConfig;
import xyz.playedu.common.types.LdapConfig;
import xyz.playedu.common.types.config.MinioConfig;
import java.util.HashMap;
@@ -39,4 +40,6 @@ public interface AppConfigService extends IService<AppConfig> {
boolean enabledLdapLogin();
String defaultAvatar();
LdapConfig ldapConfig();
}

View File

@@ -66,4 +66,6 @@ public interface DepartmentService extends IService<Department> {
List<Department> chunk(List<Integer> ids);
Integer createWithChainList(List<String> ou);
Department findByName(String name, Integer parentId);
}

View File

@@ -21,8 +21,10 @@ import org.springframework.stereotype.Service;
import xyz.playedu.common.constant.ConfigConstant;
import xyz.playedu.common.domain.AppConfig;
import xyz.playedu.common.exception.ServiceException;
import xyz.playedu.common.mapper.AppConfigMapper;
import xyz.playedu.common.service.AppConfigService;
import xyz.playedu.common.types.LdapConfig;
import xyz.playedu.common.types.config.MinioConfig;
import java.util.ArrayList;
@@ -117,4 +119,29 @@ public class AppConfigServiceImpl extends ServiceImpl<AppConfigMapper, AppConfig
getOne(query().getWrapper().eq("key_name", ConfigConstant.MEMBER_DEFAULT_AVATAR));
return appConfig.getKeyValue();
}
@Override
public LdapConfig ldapConfig() {
Map<String, String> config = keyValues();
LdapConfig ldapConfig = new LdapConfig();
ldapConfig.setEnabled(config.get(ConfigConstant.LDAP_ENABLED).equals("1"));
ldapConfig.setUrl(config.get(ConfigConstant.LDAP_URL));
ldapConfig.setAdminUser(config.get(ConfigConstant.LDAP_ADMIN_USER));
ldapConfig.setAdminPass(config.get(ConfigConstant.LDAP_ADMIN_PASS));
ldapConfig.setBaseDN(config.get(ConfigConstant.LDAP_BASE_DN));
if (!ldapConfig.getEnabled()) {
throw new ServiceException("LDAP服务未启用");
}
if (ldapConfig.getUrl().isEmpty()
|| ldapConfig.getAdminUser().isEmpty()
|| ldapConfig.getAdminPass().isEmpty()
|| ldapConfig.getBaseDN().isEmpty()) {
throw new ServiceException("LDAP服务未配置");
}
return ldapConfig;
}
}

View File

@@ -288,4 +288,9 @@ public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Departm
}
return department.getId();
}
@Override
public Department findByName(String name, Integer parentId) {
return getOne(query().getWrapper().eq("name", name).eq("parent_id", parentId));
}
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2023 杭州白书科技有限公司
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.playedu.common.types;
import lombok.Data;
@Data
public class LdapConfig {
private Boolean enabled;
private String url;
private String adminUser;
private String adminPass;
private String baseDN;
}

View File

@@ -110,8 +110,10 @@ public class LdapUtil {
return users;
}
public static List<String> departments(LdapContext ldapContext, String baseDN)
throws NamingException {
public static List<String> departments(
String url, String adminUser, String adminPass, String baseDN) throws NamingException {
LdapContext ldapContext = initContext(url, adminUser, adminPass);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningAttributes(OU_RETURN_ATTRS);
@@ -132,16 +134,42 @@ public class LdapUtil {
return null;
}
List<String> ouScopes = new ArrayList<>();
String[] rdnList = baseDN.toLowerCase().split(",");
for (int i = 0; i < rdnList.length; i++) {
if (rdnList[i].startsWith("ou=")) {
ouScopes.add(rdnList[i]);
}
}
String ouScopesStr = String.join(",", ouScopes);
List<String> units = new ArrayList<>();
while (result.hasMoreElements()) {
SearchResult item = result.nextElement();
if (item == null) {
continue;
}
units.add(item.getName());
String name = item.getName();
if (name.isEmpty()) {
name = ouScopesStr;
} else {
name = name + (ouScopesStr.isEmpty() ? "" : "," + ouScopesStr);
}
units.add(name);
}
return units;
List<String> reverseUnits = new ArrayList<>();
if (!units.isEmpty()) {
units.forEach(
item -> {
List<String> tmp = new ArrayList<>(List.of(item.split(",")));
Collections.reverse(tmp);
reverseUnits.add(String.join(",", tmp));
});
}
return reverseUnits;
}
public static LdapTransformUser loginByMailOrUid(