Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/issue657 multiple ldap #748

Merged
merged 6 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.support-project</groupId>
<artifactId>knowledge</artifactId>
<version>1.9.1-SNAPSHOT</version>
<version>1.10.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>knowledge</name>
Expand Down Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>org.support-project</groupId>
<artifactId>web</artifactId>
<version>1.9.0</version>
<version>1.10.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

Expand All @@ -13,53 +14,88 @@
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.support.project.common.bean.ValidateError;
import org.support.project.common.config.INT_FLAG;
import org.support.project.common.log.Log;
import org.support.project.common.log.LogFactory;
import org.support.project.common.util.PasswordUtil;
import org.support.project.common.util.StringUtils;
import org.support.project.common.validate.Validator;
import org.support.project.common.validate.ValidatorFactory;
import org.support.project.di.DI;
import org.support.project.di.Instance;
import org.support.project.knowledge.config.AppConfig;
import org.support.project.knowledge.control.Control;
import org.support.project.web.annotation.Auth;
import org.support.project.web.bean.LdapInfo;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.common.HttpStatus;
import org.support.project.web.config.WebConfig;
import org.support.project.web.control.service.Get;
import org.support.project.web.control.service.Post;
import org.support.project.web.dao.LdapConfigsDao;
import org.support.project.web.dao.SystemConfigsDao;
import org.support.project.web.entity.LdapConfigsEntity;
import org.support.project.web.entity.SystemConfigsEntity;
import org.support.project.web.exception.InvalidParamException;
import org.support.project.web.logic.LdapLogic;

import net.arnx.jsonic.JSONException;

@DI(instance = Instance.Prototype)
public class LdapControl extends Control {
/** ログ */
private static final Log LOG = LogFactory.getLog(LdapControl.class);

private static final String CONFIG_TYPE2 = "config2";
private static final String CONFIG_TYPE1 = "config1";
private static final String NO_CHANGE_PASSWORD = "NO_CHANGE_PASSWORD-fXLSJ_V-ZJ2E-X6c2_iGCpkE"; // パスワードを更新しなかったことを表すパスワード


/**
* Ldap設定の一覧を表示
* @return
*/
@Get(publishToken = "admin")
@Auth(roles = "admin")
public Boundary list() {
List<LdapConfigsEntity> configs = LdapConfigsDao.get().selectAll();
setAttribute("configs", configs);
return forward("list.jsp");
}

/**
* 設定画面を表示
*
* @return
*/
@Get(publishToken = "admin")
@Auth(roles = "admin")
public Boundary config() {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
key = "";
}
return config(key);
}
/**
* 設定画面を表示
* @param key
* @return
*/
private Boundary config(String key) {
setAttribute("key", key);
LdapConfigsDao dao = LdapConfigsDao.get();
LdapConfigsEntity entity = dao.selectOnKey(AppConfig.get().getSystemName());
String configType = CONFIG_TYPE1;
LdapConfigsEntity entity = dao.selectOnKey(key);
String configType = CONFIG_TYPE2;
if (entity == null) {
entity = new LdapConfigsEntity();
} else {
entity.setBindPassword(NO_CHANGE_PASSWORD);
entity.setSalt("");

if (entity.getAuthType().intValue() == LdapConfigsEntity.AUTH_TYPE_LDAP_2) {
configType = CONFIG_TYPE2;
if (entity.getAuthType().intValue() == LdapConfigsEntity.AUTH_TYPE_LDAP) {
configType = CONFIG_TYPE1;
} else if (entity.getAuthType().intValue() == LdapConfigsEntity.AUTH_TYPE_BOTH) {
configType = CONFIG_TYPE1;
} else if (entity.getAuthType().intValue() == LdapConfigsEntity.AUTH_TYPE_LDAP_2) {
entity.setAuthType(LdapConfigsEntity.AUTH_TYPE_LDAP);
} else if (entity.getAuthType().intValue() == LdapConfigsEntity.AUTH_TYPE_BOTH_2) {
configType = CONFIG_TYPE2;
entity.setAuthType(LdapConfigsEntity.AUTH_TYPE_BOTH);
}
}
Expand All @@ -80,7 +116,7 @@ public Boundary config() {

return forward("config.jsp");
}

/**
* リクエストの情報からLdapの設定情報を抽出(共通処理)
*
Expand All @@ -95,10 +131,11 @@ public Boundary config() {
* @throws IllegalBlockSizeException
* @throws BadPaddingException
*/
private LdapConfigsEntity loadLdapConfig() throws InstantiationException, IllegalAccessException, IOException, InvalidParamException,
private LdapConfigsEntity loadLdapConfig(String key) throws InstantiationException, IllegalAccessException, IOException, InvalidParamException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
LdapConfigsDao dao = LdapConfigsDao.get();
LdapConfigsEntity entity = super.getParamOnProperty(LdapConfigsEntity.class);
entity.setAuthType(LdapConfigsEntity.AUTH_TYPE_BOTH);
String security = getParam("security");
if (!StringUtils.isEmpty(security)) {
if (security.toLowerCase().equals("usessl")) {
Expand All @@ -116,6 +153,7 @@ private LdapConfigsEntity loadLdapConfig() throws InstantiationException, Illega
entity.setAuthType(LdapConfigsEntity.AUTH_TYPE_BOTH_2);
}

entity.setDescription(getParam("description2"));
entity.setHost(getParam("host2"));
entity.setPort(getParam("port2", Integer.class));
security = getParam("security2");
Expand All @@ -139,15 +177,14 @@ private LdapConfigsEntity loadLdapConfig() throws InstantiationException, Illega

String password = entity.getBindPassword();
if (password.equals(NO_CHANGE_PASSWORD)) {
LdapConfigsEntity saved = dao.selectOnKey(AppConfig.get().getSystemName());
LdapConfigsEntity saved = dao.selectOnKey(key);
if (saved != null) {
String encPass = saved.getBindPassword();
String salt = saved.getSalt();
password = PasswordUtil.decrypt(encPass, salt);
entity.setBindPassword(password);
}
}

return entity;
}

Expand All @@ -171,9 +208,14 @@ private LdapConfigsEntity loadLdapConfig() throws InstantiationException, Illega
@Auth(roles = "admin")
public Boundary check() throws InstantiationException, IllegalAccessException, JSONException, IOException, InvalidParamException, LdapException,
InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
addMsgWarn("knowledge.ldap.msg.connect.error");
return config();
}
LdapConfigsDao dao = LdapConfigsDao.get();
LdapConfigsEntity entity = dao.selectOnKey(AppConfig.get().getSystemName());
String configType = CONFIG_TYPE1;
LdapConfigsEntity entity = dao.selectOnKey(key);
String configType = CONFIG_TYPE2;
if (entity == null) {
addMsgWarn("knowledge.ldap.msg.connect.error");
return forward("config.jsp");
Expand All @@ -184,27 +226,31 @@ public Boundary check() throws InstantiationException, IllegalAccessException, J
configType = CONFIG_TYPE2;
}
}

LdapLogic ldapLogic = LdapLogic.get();
if (CONFIG_TYPE2.equals(configType)) {
boolean check = ldapLogic.check(entity);
if (!check) {
addMsgWarn("knowledge.ldap.msg.connect.error");
} else {
addMsgSuccess("knowledge.ldap.msg.connect.success2");
}
} else {
String pass = entity.getBindPassword();
if (StringUtils.isNotEmpty(entity.getSalt())) {
pass = PasswordUtil.decrypt(pass, entity.getSalt());
}
LdapInfo result = ldapLogic.auth(entity, entity.getBindDn(), pass);
if (result == null) {
addMsgWarn("knowledge.ldap.msg.connect.error");
try {
LdapLogic ldapLogic = LdapLogic.get();
if (CONFIG_TYPE2.equals(configType)) {
boolean check = ldapLogic.check(entity);
if (!check) {
addMsgWarn("knowledge.ldap.msg.connect.error");
} else {
addMsgSuccess("knowledge.ldap.msg.connect.success2");
}
} else {
addMsgSuccess("knowledge.ldap.msg.connect.success", result.getId(), result.getName(), result.getMail(),
String.valueOf(result.isAdmin()));
String pass = entity.getBindPassword();
if (StringUtils.isNotEmpty(entity.getSalt())) {
pass = PasswordUtil.decrypt(pass, entity.getSalt());
}
LdapInfo result = ldapLogic.auth(entity, entity.getBindDn(), pass);
if (result == null) {
addMsgWarn("knowledge.ldap.msg.connect.error");
} else {
addMsgSuccess("knowledge.ldap.msg.connect.success", result.getId(), result.getName(), result.getMail(),
String.valueOf(result.isAdmin()));
}
}
} catch (Exception e) {
LOG.warn(e);
addMsgWarn("knowledge.ldap.msg.connect.error");
}
return config();
}
Expand All @@ -229,18 +275,30 @@ public Boundary check() throws InstantiationException, IllegalAccessException, J
@Auth(roles = "admin")
public Boundary save() throws InstantiationException, IllegalAccessException, JSONException, IOException, InvalidParamException, LdapException,
InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
String key = getParam("key");
Map<String, String> params = getParams();
String configType = getParam("configType");
if (CONFIG_TYPE2.equals(configType)) {
params.put("host", params.get("host2"));
params.put("port", params.get("port2"));
params.put("description", params.get("description2"));
}
params.put("authType", "" + LdapConfigsEntity.AUTH_TYPE_BOTH);
List<ValidateError> errors = LdapConfigsEntity.get().validate(params);
Validator validator = ValidatorFactory.getInstance(Validator.REQUIRED);
ValidateError error = validator.validate(params.get("description"), getResource("knowledge.ldap.label.description"));
if (error != null) {
if (errors == null) {
errors = new ArrayList<>();
}
errors.add(error);
}
if (errors != null && !errors.isEmpty()) {
super.setResult("", errors);
return forward("config.jsp");
}
LdapConfigsEntity entity = loadLdapConfig();
LdapConfigsEntity entity = loadLdapConfig(key);
/*
LdapLogic ldapLogic = LdapLogic.get();
boolean check = false;
if (CONFIG_TYPE2.equals(configType)) {
Expand All @@ -259,23 +317,33 @@ public Boundary save() throws InstantiationException, IllegalAccessException, JS
check = true;
}
}
*/
boolean check = true; // 保存時の接続チェックをやめた(別途ボタンでチェック)
if (!check) {
addMsgWarn("knowledge.ldap.msg.save.error");
} else {
// Ldap設定を保存
LdapConfigsDao dao = LdapConfigsDao.get();
entity.setSystemName(AppConfig.get().getSystemName());

if (StringUtils.isEmpty(key)) {
int count = dao.selectCountAll();
count++;
key = "Ldap" + "-" + count;
}

entity.setSystemName(key);
String salt = PasswordUtil.getSalt();
String passHash = PasswordUtil.encrypt(entity.getBindPassword(), salt);
entity.setBindPassword(passHash);
entity.setSalt(salt);
dao.save(entity);

entity.setBindPassword(NO_CHANGE_PASSWORD);
setAttributeOnProperty(entity);
addMsgSuccess("knowledge.ldap.msg.save.success");
setAttribute("key", key);
}
return config();
return config(key);
}

/**
Expand All @@ -286,18 +354,33 @@ public Boundary save() throws InstantiationException, IllegalAccessException, JS
@Post(subscribeToken = "admin")
@Auth(roles = "admin")
public Boundary delete() {
String key = getParam("key");
if (StringUtils.isEmpty(key)) {
return sendError(HttpStatus.SC_400_BAD_REQUEST, "BAD_REQUEST");
}
LdapConfigsDao dao = LdapConfigsDao.get();
LdapConfigsEntity entity = dao.selectOnKey(AppConfig.get().getSystemName());
LdapConfigsEntity entity = dao.selectOnKey(key);
if (entity != null) {
dao.physicalDelete(AppConfig.get().getSystemName());
dao.physicalDelete(key);
}
entity = new LdapConfigsEntity();
entity.setSystemName(AppConfig.get().getSystemName());
entity.setSystemName(key);
setAttributeOnProperty(entity);


SystemConfigsEntity config = SystemConfigsDao.get().selectOnKey(WebConfig.KEY_LDAP_CONFIG, AppConfig.get().getSystemName());
if (config != null) {
if (config.getConfigValue().indexOf(key) != -1) {
if (config.getConfigValue().indexOf(key) == 0) {
key = config.getConfigValue().substring(config.getConfigValue().indexOf(key));
} else {
key = config.getConfigValue().substring(0, config.getConfigValue().indexOf(key) -1).concat(key.substring(config.getConfigValue().indexOf(key) + key.length()));
}
config.setConfigValue(key);
SystemConfigsDao.get().save(config);
}
}
addMsgInfo("message.success.delete.target", getResource("knowledge.ldap.title"));

return config();
return list();
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.support.project.knowledge.control.protect;

import java.util.List;

import org.support.project.di.DI;
import org.support.project.di.Instance;
import org.support.project.knowledge.control.Control;
import org.support.project.web.boundary.Boundary;
import org.support.project.web.control.service.Get;
import org.support.project.web.dao.LdapConfigsDao;
import org.support.project.web.entity.LdapConfigsEntity;

@DI(instance = Instance.Prototype)
public class ConfigControl extends Control {

@Get
public Boundary index() {
List<LdapConfigsEntity> ldapConfigs = LdapConfigsDao.get().selectAll();
Boolean ldapExists = ldapConfigs.size() > 0;
setAttribute("ldapExists", ldapExists);
return forward("index.jsp");
}
}
Loading