Merge pull request #334 from kw-udon/dev

Refactoring: file authentication, user
This commit is contained in:
Shinsuke Sugaya 2015-10-26 22:48:40 +09:00
commit 51c559d661
15 changed files with 532 additions and 363 deletions

View file

@ -29,6 +29,7 @@ import org.codelibs.fess.es.cbean.FileAuthenticationCB;
import org.codelibs.fess.es.exbhv.FileAuthenticationBhv;
import org.codelibs.fess.es.exentity.FileAuthentication;
import org.dbflute.cbean.result.PagingResultBean;
import org.dbflute.optional.OptionalEntity;
public class FileAuthenticationService implements Serializable {
@ -57,17 +58,8 @@ public class FileAuthenticationService implements Serializable {
return fileAuthenticationList;
}
public FileAuthentication getFileAuthentication(final Map<String, String> keys) {
final FileAuthentication fileAuthentication = fileAuthenticationBhv.selectEntity(cb -> {
cb.query().docMeta().setId_Equal(keys.get("id"));
setupEntityCondition(cb, keys);
}).orElse(null);//TODO
if (fileAuthentication == null) {
// TODO exception?
return null;
}
return fileAuthentication;
public OptionalEntity<FileAuthentication> getFileAuthentication(final String id) {
return fileAuthenticationBhv.selectByPK(id);
}
public void store(final FileAuthentication fileAuthentication) {

View file

@ -29,6 +29,7 @@ import org.codelibs.fess.es.cbean.UserCB;
import org.codelibs.fess.es.exbhv.UserBhv;
import org.codelibs.fess.es.exentity.User;
import org.dbflute.cbean.result.PagingResultBean;
import org.dbflute.optional.OptionalEntity;
public class UserService implements Serializable {
@ -56,18 +57,9 @@ public class UserService implements Serializable {
return userList;
}
public User getUser(final Map<String, String> keys) {
final User user = userBhv.selectEntity(cb -> {
cb.query().docMeta().setId_Equal(keys.get("id"));
setupEntityCondition(cb, keys);
}).orElse(null);//TODO
if (user == null) {
// TODO exception?
return null;
}
return user;
public OptionalEntity<User> getUser(final String id) {
return userBhv.selectByPK(id);
}
public void store(final User user) {

View file

@ -34,6 +34,7 @@ import org.codelibs.fess.es.exentity.FileAuthentication;
import org.codelibs.fess.es.exentity.FileConfig;
import org.codelibs.fess.helper.SystemHelper;
import org.codelibs.fess.util.ComponentUtil;
import org.dbflute.optional.OptionalEntity;
import org.lastaflute.web.Execute;
import org.lastaflute.web.callback.ActionRuntime;
import org.lastaflute.web.response.HtmlResponse;
@ -73,14 +74,14 @@ public class AdminFileauthenticationAction extends FessAdminAction {
// Search Execute
// ==============
@Execute
public HtmlResponse index(final FileAuthenticationSearchForm form) {
public HtmlResponse index(final SearchForm form) {
return asHtml(path_AdminFileauthentication_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
@Execute
public HtmlResponse list(final Integer pageNumber, final FileAuthenticationSearchForm form) {
public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
fileAuthenticationPager.setCurrentPageNumber(pageNumber);
return asHtml(path_AdminFileauthentication_IndexJsp).renderWith(data -> {
searchPaging(data, form);
@ -88,15 +89,15 @@ public class AdminFileauthenticationAction extends FessAdminAction {
}
@Execute
public HtmlResponse search(final FileAuthenticationSearchForm form) {
copyBeanToBean(form.searchParams, fileAuthenticationPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
public HtmlResponse search(final SearchForm form) {
copyBeanToBean(form, fileAuthenticationPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
return asHtml(path_AdminFileauthentication_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
@Execute
public HtmlResponse reset(final FileAuthenticationSearchForm form) {
public HtmlResponse reset(final SearchForm form) {
fileAuthenticationPager.clear();
return asHtml(path_AdminFileauthentication_IndexJsp).renderWith(data -> {
searchPaging(data, form);
@ -104,17 +105,17 @@ public class AdminFileauthenticationAction extends FessAdminAction {
}
@Execute
public HtmlResponse back(final FileAuthenticationSearchForm form) {
public HtmlResponse back(final SearchForm form) {
return asHtml(path_AdminFileauthentication_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
protected void searchPaging(final RenderData data, final FileAuthenticationSearchForm form) {
protected void searchPaging(final RenderData data, final SearchForm form) {
data.register("fileAuthenticationItems", fileAuthenticationService.getFileAuthenticationList(fileAuthenticationPager)); // page navi
data.register("displayCreateLink", !fileConfigService.getAllFileConfigList(false, false, false, null).isEmpty());
// restore from pager
copyBeanToBean(fileAuthenticationPager, form.searchParams, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
copyBeanToBean(fileAuthenticationPager, form, op -> op.include("id"));
}
// ===================================================================================
@ -124,9 +125,42 @@ public class AdminFileauthenticationAction extends FessAdminAction {
// Entry Page
// ----------
@Execute(token = TxToken.SAVE)
public HtmlResponse createpage(final FileAuthenticationEditForm form) {
form.initialize();
form.crudMode = CrudMode.CREATE;
public HtmlResponse createpage() {
return asHtml(path_AdminFileauthentication_EditJsp).useForm(CreateForm.class, op -> {
op.setup(form -> {
form.initialize();
form.crudMode = CrudMode.CREATE;
});
}).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editpage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.EDIT);
return asHtml(path_AdminFileauthentication_EditJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
form.crudMode = crudMode;
});
}).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse createagain(final CreateForm form) {
verifyCrudMode(form.crudMode, CrudMode.CREATE);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminFileauthentication_EditJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
@ -134,11 +168,9 @@ public class AdminFileauthenticationAction extends FessAdminAction {
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editpage(final int crudMode, final String id, final FileAuthenticationEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.EDIT);
loadFileAuthentication(form);
public HtmlResponse editagain(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.EDIT);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminFileauthentication_EditJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
@ -146,17 +178,15 @@ public class AdminFileauthenticationAction extends FessAdminAction {
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editagain(final FileAuthenticationEditForm form) {
return asHtml(path_AdminFileauthentication_EditJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editfromconfirm(final FileAuthenticationEditForm form) {
public HtmlResponse editfromconfirm(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.EDIT;
loadFileAuthentication(form);
final String id = form.id;
fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return asHtml(path_AdminFileauthentication_EditJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
@ -164,21 +194,35 @@ public class AdminFileauthenticationAction extends FessAdminAction {
}
@Execute(token = TxToken.SAVE)
public HtmlResponse deletepage(final int crudMode, final String id, final FileAuthenticationEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.DELETE);
loadFileAuthentication(form);
return asHtml(path_AdminFileauthentication_ConfirmJsp).renderWith(data -> {
public HtmlResponse deletepage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.DELETE);
return asHtml(path_AdminFileauthentication_ConfirmJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
form.crudMode = crudMode;
});
}).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse deletefromconfirm(final FileAuthenticationEditForm form) {
public HtmlResponse deletefromconfirm(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.DELETE;
loadFileAuthentication(form);
final String id = form.id;
fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return asHtml(path_AdminFileauthentication_ConfirmJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
@ -189,11 +233,29 @@ public class AdminFileauthenticationAction extends FessAdminAction {
// Confirm
// -------
@Execute
public HtmlResponse confirmpage(final int crudMode, final String id, final FileAuthenticationEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.CONFIRM);
loadFileAuthentication(form);
public HtmlResponse confirmpage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.CONFIRM);
return asHtml(path_AdminFileauthentication_ConfirmJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
form.crudMode = crudMode;
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
});
}).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
}
@Execute(token = TxToken.VALIDATE_KEEP)
public HtmlResponse confirmfromcreate(final CreateForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.CREATE;
return asHtml(path_AdminFileauthentication_ConfirmJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
@ -201,17 +263,9 @@ public class AdminFileauthenticationAction extends FessAdminAction {
}
@Execute(token = TxToken.VALIDATE_KEEP)
public HtmlResponse confirmfromcreate(final FileAuthenticationEditForm form) {
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminFileauthentication_ConfirmJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
}
@Execute(token = TxToken.VALIDATE_KEEP)
public HtmlResponse confirmfromupdate(final FileAuthenticationEditForm form) {
public HtmlResponse confirmfromupdate(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.EDIT;
return asHtml(path_AdminFileauthentication_ConfirmJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
@ -222,65 +276,77 @@ public class AdminFileauthenticationAction extends FessAdminAction {
// Actually Crud
// -------------
@Execute(token = TxToken.VALIDATE)
public HtmlResponse create(final FileAuthenticationEditForm form) {
public HtmlResponse create(final CreateForm form) {
verifyCrudMode(form.crudMode, CrudMode.CREATE);
validate(form, messages -> {}, toEditHtml());
fileAuthenticationService.store(createFileAuthentication(form));
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
createFileAuthentication(form).ifPresent(entity -> {
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
fileAuthenticationService.store(entity);
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL), toEditHtml());
});
return redirect(getClass());
}
@Execute(token = TxToken.VALIDATE)
public HtmlResponse update(final FileAuthenticationEditForm form) {
public HtmlResponse update(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.EDIT);
validate(form, messages -> {}, toEditHtml());
fileAuthenticationService.store(createFileAuthentication(form));
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
createFileAuthentication(form).ifPresent(entity -> {
copyBeanToBean(form, entity, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
fileAuthenticationService.store(entity);
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
});
return redirect(getClass());
}
@Execute
public HtmlResponse delete(final FileAuthenticationEditForm form) {
verifyCrudMode(form, CrudMode.DELETE);
fileAuthenticationService.delete(getFileAuthentication(form));
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
public HtmlResponse delete(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.DELETE);
validate(form, messages -> {}, toEditHtml());
final String id = form.id;
fileAuthenticationService.getFileAuthentication(id).ifPresent(entity -> {
fileAuthenticationService.delete(entity);
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return redirect(getClass());
}
//===================================================================================
// Assist Logic
// ============
protected void loadFileAuthentication(final FileAuthenticationEditForm form) {
copyBeanToBean(getFileAuthentication(form), form, op -> op.exclude("crudMode"));
}
protected FileAuthentication getFileAuthentication(final FileAuthenticationEditForm form) {
final FileAuthentication fileAuthentication = fileAuthenticationService.getFileAuthentication(createKeyMap(form));
if (fileAuthentication == null) {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
}
return fileAuthentication;
}
protected FileAuthentication createFileAuthentication(final FileAuthenticationEditForm form) {
FileAuthentication fileAuthentication;
protected OptionalEntity<FileAuthentication> createFileAuthentication(final CreateForm form) {
final String username = systemHelper.getUsername();
final long currentTime = systemHelper.getCurrentTimeAsLong();
if (form.crudMode == CrudMode.EDIT) {
fileAuthentication = getFileAuthentication(form);
} else {
fileAuthentication = new FileAuthentication();
fileAuthentication.setCreatedBy(username);
fileAuthentication.setCreatedTime(currentTime);
switch (form.crudMode) {
case CrudMode.CREATE:
if (form instanceof CreateForm) {
final FileAuthentication entity = new FileAuthentication();
entity.setCreatedBy(username);
entity.setCreatedTime(currentTime);
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
return OptionalEntity.of(entity);
}
break;
case CrudMode.EDIT:
if (form instanceof EditForm) {
return fileAuthenticationService.getFileAuthentication(((EditForm) form).id).map(entity -> {
entity.setUpdatedBy(username);
entity.setUpdatedTime(currentTime);
return entity;
});
}
break;
default:
break;
}
fileAuthentication.setUpdatedBy(username);
fileAuthentication.setUpdatedTime(currentTime);
copyBeanToBean(form, fileAuthentication, op -> op.exclude(Constants.COMMON_CONVERSION_RULE));
return fileAuthentication;
}
protected Map<String, String> createKeyMap(final FileAuthenticationEditForm form) {
final Map<String, String> keys = new HashMap<String, String>();
keys.put("id", form.id);
return keys;
return OptionalEntity.empty();
}
protected void registerProtocolSchemeItems(final RenderData data) {
@ -310,17 +376,20 @@ public class AdminFileauthenticationAction extends FessAdminAction {
// ===================================================================================
// Small Helper
// ============
protected void verifyCrudMode(final FileAuthenticationEditForm form, final int expectedMode) {
if (form.crudMode != expectedMode) {
protected void verifyCrudMode(final int crudMode, final int expectedMode) {
if (crudMode != expectedMode) {
throwValidationError(messages -> {
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(form.crudMode));
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
}, toEditHtml());
}
}
protected VaErrorHook toEditHtml() {
return () -> {
return asHtml(path_AdminFileauthentication_EditJsp);
return asHtml(path_AdminFileauthentication_EditJsp).renderWith(data -> {
registerProtocolSchemeItems(data);
registerFileConfigItems(data);
});
};
}
}

View file

@ -0,0 +1,76 @@
/*
* Copyright 2009-2015 the CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.admin.fileauthentication;
import java.io.Serializable;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.util.ComponentUtil;
import org.lastaflute.web.validation.Required;
/**
* @author codelibs
* @author Keiichi Watanabe
*/
public class CreateForm implements Serializable {
private static final long serialVersionUID = 1L;
public Integer crudMode;
@Size(max = 100)
public String hostname;
@Min(value = 0)
@Max(value = 2147483647)
public Integer port;
@Size(max = 10)
public String protocolScheme;
@Required
@Size(max = 100)
public String username;
@Size(max = 100)
public String password;
@Size(max = 1000)
public String parameters;
@Required
@Size(max = 1000)
public String fileConfigId;
@Required
@Size(max = 1000)
public String createdBy;
@Required
public Long createdTime;
public void initialize() {
crudMode = CrudMode.CREATE;
createdBy = ComponentUtil.getSystemHelper().getUsername();
createdTime = ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright 2009-2015 the CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.admin.fileauthentication;
import javax.validation.constraints.Size;
import org.lastaflute.web.validation.Required;
/**
* @author Keiichi Watanabe
*/
public class EditForm extends CreateForm {
private static final long serialVersionUID = 1L;
@Required
@Size(max = 1000)
public String id;
@Size(max = 255)
public String updatedBy;
public Long updatedTime;
@Required
public Integer versionNo;
}

View file

@ -1,106 +0,0 @@
/*
* Copyright 2009-2015 the CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.admin.fileauthentication;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.codelibs.fess.util.ComponentUtil;
/**
* @author codelibs
* @author Keiichi Watanabe
*/
public class FileAuthenticationEditForm implements Serializable {
private static final long serialVersionUID = 1L;
//@IntegerType
public String pageNumber;
public Map<String, String> searchParams = new HashMap<String, String>();
//@IntegerType
public int crudMode;
public String getCurrentPageNumber() {
return pageNumber;
}
//@Required(target = "confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 1000)
public String id;
//@Maxbytelength(maxbytelength = 100)
public String hostname;
//@IntRange(min = -1, max = 2147483647)
public String port;
//@Maxbytelength(maxbytelength = 10)
public String protocolScheme;
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 100)
public String username;
//@Maxbytelength(maxbytelength = 100)
public String password;
//@Maxbytelength(maxbytelength = 1000)
public String parameters;
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 1000)
public String fileConfigId;
//@Required(target = "confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 255)
public String createdBy;
//@Required(target = "confirmfromupdate,update,delete")
//@LongType
public String createdTime;
//@Maxbytelength(maxbytelength = 255)
public String updatedBy;
//@LongType
public String updatedTime;
//@Required(target = "confirmfromupdate,update,delete")
//@IntegerType
public String versionNo;
public void initialize() {
id = null;
hostname = null;
port = null;
protocolScheme = null;
username = null;
password = null;
parameters = null;
fileConfigId = null;
createdBy = "system";
createdTime = Long.toString(ComponentUtil.getSystemHelper().getCurrentTimeAsLong());
updatedBy = null;
updatedTime = null;
versionNo = null;
}
}

View file

@ -17,16 +17,14 @@
package org.codelibs.fess.app.web.admin.fileauthentication;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* @author codelibs
* @author Keiichi Watanabe
*/
public class FileAuthenticationSearchForm implements Serializable {
public class SearchForm implements Serializable {
private static final long serialVersionUID = 1L;
public Map<String, String> searchParams = new HashMap<String, String>();
public String id;
}

View file

@ -25,13 +25,14 @@ import javax.annotation.Resource;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.Constants;
import org.codelibs.fess.app.pager.UserPager;
import org.codelibs.fess.app.service.UserService;
import org.codelibs.fess.app.service.GroupService;
import org.codelibs.fess.app.service.RoleService;
import org.codelibs.fess.app.service.UserService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.base.FessAdminAction;
import org.codelibs.fess.es.exentity.User;
import org.codelibs.fess.helper.SystemHelper;
import org.dbflute.optional.OptionalEntity;
import org.lastaflute.web.Execute;
import org.lastaflute.web.callback.ActionRuntime;
import org.lastaflute.web.response.HtmlResponse;
@ -41,10 +42,12 @@ import org.lastaflute.web.validation.VaErrorHook;
/**
* @author shinsuke
* @author Keiichi Watanabe
*/
public class AdminUserAction extends FessAdminAction {
private static final String TEMPORARY_PASSWORD = "fess.temporary_password";
// ===================================================================================
// Attribute
// =========
@ -72,7 +75,7 @@ public class AdminUserAction extends FessAdminAction {
// Search Execute
// ==============
@Execute
public HtmlResponse index(final UserSearchForm form) {
public HtmlResponse index(final SearchForm form) {
clearStoredPassword();
return asHtml(path_AdminUser_IndexJsp).renderWith(data -> {
searchPaging(data, form);
@ -80,7 +83,7 @@ public class AdminUserAction extends FessAdminAction {
}
@Execute
public HtmlResponse list(final Integer pageNumber, final UserSearchForm form) {
public HtmlResponse list(final Integer pageNumber, final SearchForm form) {
clearStoredPassword();
userPager.setCurrentPageNumber(pageNumber);
return asHtml(path_AdminUser_IndexJsp).renderWith(data -> {
@ -89,16 +92,16 @@ public class AdminUserAction extends FessAdminAction {
}
@Execute
public HtmlResponse search(final UserSearchForm form) {
public HtmlResponse search(final SearchForm form) {
clearStoredPassword();
copyBeanToBean(form.searchParams, userPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
copyBeanToBean(form, userPager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
return asHtml(path_AdminUser_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
@Execute
public HtmlResponse reset(final UserSearchForm form) {
public HtmlResponse reset(final SearchForm form) {
clearStoredPassword();
userPager.clear();
return asHtml(path_AdminUser_IndexJsp).renderWith(data -> {
@ -107,18 +110,17 @@ public class AdminUserAction extends FessAdminAction {
}
@Execute
public HtmlResponse back(final UserSearchForm form) {
public HtmlResponse back(final SearchForm form) {
clearStoredPassword();
return asHtml(path_AdminUser_IndexJsp).renderWith(data -> {
searchPaging(data, form);
});
}
protected void searchPaging(final RenderData data, final UserSearchForm form) {
protected void searchPaging(final RenderData data, final SearchForm form) {
data.register("userItems", userService.getUserList(userPager)); // page navi
// restore from pager
copyBeanToBean(userPager, form.searchParams, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
copyBeanToBean(userPager, form, op -> op.include("id"));
}
private void registerForms(final RenderData data) {
@ -133,62 +135,109 @@ public class AdminUserAction extends FessAdminAction {
// Entry Page
// ----------
@Execute(token = TxToken.SAVE)
public HtmlResponse createpage(final UserEditForm form) {
public HtmlResponse createpage() {
clearStoredPassword();
form.initialize();
form.crudMode = CrudMode.CREATE;
return asHtml(path_AdminUser_EditJsp).useForm(CreateForm.class, op -> {
op.setup(form -> {
form.initialize();
form.crudMode = CrudMode.CREATE;
});
}).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editpage(final int crudMode, final String id) {
clearStoredPassword();
verifyCrudMode(crudMode, CrudMode.EDIT);
return asHtml(path_AdminUser_EditJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
userService.getUser(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
form.crudMode = crudMode;
resetPassword(form);
});
}).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse createagain(final CreateForm form) {
clearStoredPassword();
verifyCrudMode(form.crudMode, CrudMode.CREATE);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminUser_EditJsp).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editpage(final int crudMode, final String id, final UserEditForm form) {
public HtmlResponse editagain(final EditForm form) {
clearStoredPassword();
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.EDIT);
loadUser(form, false);
verifyCrudMode(form.crudMode, CrudMode.EDIT);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminUser_EditJsp).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editagain(final UserEditForm form) {
clearStoredPassword();
return asHtml(path_AdminUser_EditJsp).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse editfromconfirm(final UserEditForm form) {
public HtmlResponse editfromconfirm(final EditForm form) {
clearStoredPassword();
form.crudMode = CrudMode.EDIT;
loadUser(form, false);
final String id = form.id;
userService.getUser(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
resetPassword(form);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminUser_EditJsp).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse deletepage(final int crudMode, final String id, final UserEditForm form) {
public HtmlResponse deletepage(final int crudMode, final String id) {
clearStoredPassword();
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.DELETE);
loadUser(form, false);
return asHtml(path_AdminUser_ConfirmJsp).renderWith(data -> {
verifyCrudMode(crudMode, CrudMode.DELETE);
return asHtml(path_AdminUser_ConfirmJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
userService.getUser(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
form.crudMode = crudMode;
resetPassword(form);
});
}).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.SAVE)
public HtmlResponse deletefromconfirm(final UserEditForm form) {
public HtmlResponse deletefromconfirm(final EditForm form) {
clearStoredPassword();
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.DELETE;
loadUser(form, false);
final String id = form.id;
userService.getUser(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
resetPassword(form);
return asHtml(path_AdminUser_ConfirmJsp).renderWith(data -> {
registerForms(data);
});
@ -198,32 +247,42 @@ public class AdminUserAction extends FessAdminAction {
// Confirm
// -------
@Execute
public HtmlResponse confirmpage(final int crudMode, final String id, final UserEditForm form) {
form.crudMode = crudMode;
form.id = id;
verifyCrudMode(form, CrudMode.CONFIRM);
public HtmlResponse confirmpage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.CONFIRM);
return asHtml(path_AdminUser_ConfirmJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
userService.getUser(id).ifPresent(entity -> {
copyBeanToBean(entity, form, copyOp -> {
copyOp.excludeNull();
});
form.crudMode = crudMode;
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
resetPassword(form);
});
}).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.VALIDATE_KEEP)
public HtmlResponse confirmfromcreate(final CreateForm form) {
verifyPassword(form);
loadUser(form, false);
storePassword(form);
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.CREATE;
return asHtml(path_AdminUser_ConfirmJsp).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.VALIDATE_KEEP)
public HtmlResponse confirmfromcreate(final UserEditForm form) {
verifyPassword(form);
storePassword(form);
validate(form, messages -> {}, toEditHtml());
return asHtml(path_AdminUser_ConfirmJsp).renderWith(data -> {
registerForms(data);
});
}
@Execute(token = TxToken.VALIDATE_KEEP)
public HtmlResponse confirmfromupdate(final UserEditForm form) {
public HtmlResponse confirmfromupdate(final EditForm form) {
verifyPassword(form);
storePassword(form);
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.EDIT;
return asHtml(path_AdminUser_ConfirmJsp).renderWith(data -> {
registerForms(data);
});
@ -233,83 +292,99 @@ public class AdminUserAction extends FessAdminAction {
// Actually Crud
// -------------
@Execute(token = TxToken.VALIDATE)
public HtmlResponse create(final UserEditForm form) {
public HtmlResponse create(final CreateForm form) {
verifyCrudMode(form.crudMode, CrudMode.CREATE);
validate(form, messages -> {}, toEditHtml());
verifyPassword(form);
userService.store(createUser(form));
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
createUser(form).ifPresent(entity -> {
copyBeanToBean(form, entity, op -> op.exclude("crudMode", "password", "confirmPassword"));
entity.setId(Base64.getEncoder().encodeToString(entity.getName().getBytes(Constants.CHARSET_UTF_8)));
userService.store(entity);
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL), toEditHtml());
});
return redirect(getClass());
}
@Execute(token = TxToken.VALIDATE)
public HtmlResponse update(final UserEditForm form) {
public HtmlResponse update(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.EDIT);
validate(form, messages -> {}, toEditHtml());
verifyPassword(form);
userService.store(createUser(form));
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
//verifyPassword(form);
createUser(form).ifPresent(entity -> {
copyBeanToBean(form, entity, op -> op.exclude("crudMode", "password", "confirmPassword"));
entity.setId(Base64.getEncoder().encodeToString(entity.getName().getBytes(Constants.CHARSET_UTF_8)));
userService.store(entity);
saveInfo(messages -> messages.addSuccessCrudUpdateCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
});
return redirect(getClass());
}
@Execute
public HtmlResponse delete(final UserEditForm form) {
verifyCrudMode(form, CrudMode.DELETE);
userService.delete(getUser(form));
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
public HtmlResponse delete(final EditForm form) {
verifyCrudMode(form.crudMode, CrudMode.DELETE);
validate(form, messages -> {}, toEditHtml());
final String id = form.id;
userService.getUser(id).ifPresent(entity -> {
userService.delete(entity);
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
}).orElse(() -> {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id), toEditHtml());
});
return redirect(getClass());
}
// ===================================================================================
//===================================================================================
// Assist Logic
// ============
protected void loadUser(final UserEditForm form, final boolean hasPassword) {
copyBeanToBean(getUser(form), form, op -> op.exclude("crudMode"));
if (!hasPassword) {
form.password = null;
form.confirmPassword = null;
protected OptionalEntity<User> createUser(final CreateForm form) {
switch (form.crudMode) {
case CrudMode.CREATE:
if (form instanceof CreateForm) {
final User entity = new User();
sessionManager.getAttribute(TEMPORARY_PASSWORD, String.class).ifPresent(password -> {
entity.setPassword(password);
});
return OptionalEntity.of(entity);
}
break;
case CrudMode.EDIT:
if (form instanceof EditForm) {
return userService.getUser(((EditForm) form).id).map(entity -> {
sessionManager.getAttribute(TEMPORARY_PASSWORD, String.class).ifPresent(password -> {
entity.setPassword(password);
});
return entity;
});
}
break;
default:
break;
}
return OptionalEntity.empty();
}
protected User getUser(final UserEditForm form) {
final User user = userService.getUser(createKeyMap(form));
if (user == null) {
throwValidationError(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, form.id), toEditHtml());
}
return user;
}
protected User createUser(final UserEditForm form) {
User user;
if (form.crudMode == CrudMode.EDIT) {
user = getUser(form);
} else {
user = new User();
}
copyBeanToBean(form, user, op -> op.exclude("password", "confirmPassword"));
sessionManager.getAttribute(TEMPORARY_PASSWORD, String.class).ifPresent(password -> {
user.setPassword(password);
});
user.setId(Base64.getEncoder().encodeToString(user.getName().getBytes(Constants.CHARSET_UTF_8)));
return user;
}
protected Map<String, String> createKeyMap(final UserEditForm form) {
final Map<String, String> keys = new HashMap<String, String>();
keys.put("id", form.id);
return keys;
protected Map<String, String> createItem(final String label, final String value) {
final Map<String, String> map = new HashMap<String, String>(2);
map.put(Constants.ITEM_LABEL, label);
map.put(Constants.ITEM_VALUE, value);
return map;
}
// ===================================================================================
// Small Helper
// ============
protected void verifyCrudMode(final UserEditForm form, final int expectedMode) {
if (form.crudMode != expectedMode) {
protected void verifyCrudMode(final int crudMode, final int expectedMode) {
if (crudMode != expectedMode) {
throwValidationError(messages -> {
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(form.crudMode));
messages.addErrorsCrudInvalidMode(GLOBAL, String.valueOf(expectedMode), String.valueOf(crudMode));
}, toEditHtml());
}
}
protected void verifyPassword(final UserEditForm form) {
protected void verifyPassword(final CreateForm form) {
if (form.crudMode == CrudMode.CREATE && StringUtil.isBlank(form.password)) {
throwValidationError(messages -> {
messages.addErrorsBlankPassword(GLOBAL);
@ -322,7 +397,7 @@ public class AdminUserAction extends FessAdminAction {
}
}
protected void verifyStoredPassword(final UserEditForm form) {
protected void verifyStoredPassword(final CreateForm form) {
if (!sessionManager.getAttribute(TEMPORARY_PASSWORD, String.class).isPresent()) {
throwValidationError(messages -> {
messages.addErrorsInvalidConfirmPassword(GLOBAL);
@ -334,13 +409,18 @@ public class AdminUserAction extends FessAdminAction {
sessionManager.removeAttribute(TEMPORARY_PASSWORD);
}
private void storePassword(final UserEditForm form) {
private void storePassword(final CreateForm form) {
final String encodedPassword = fessLoginAssist.encryptPassword(form.password);
sessionManager.setAttribute(TEMPORARY_PASSWORD, encodedPassword);
form.password = null;
form.confirmPassword = null;
}
private void resetPassword(final CreateForm form) {
form.password = null;
form.confirmPassword = null;
}
protected VaErrorHook toEditHtml() {
return () -> {
return asHtml(path_AdminUser_EditJsp).renderWith(data -> {

View file

@ -18,43 +18,34 @@ package org.codelibs.fess.app.web.admin.user;
import java.io.Serializable;
import javax.validation.constraints.Size;
import org.lastaflute.web.validation.Required;
/**
* @author shinsuke
* @author Keiichi Watanabe
*/
public class UserEditForm implements Serializable {
public class CreateForm implements Serializable {
private static final long serialVersionUID = 1L;
//@IntegerType
public int crudMode;
public Integer crudMode;
//@Required(target = "confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 1000)
public String id;
//@Required(target = "confirmfromcreate,create,confirmfromupdate,update,delete")
//@Maxbytelength(maxbytelength = 100)
@Required
@Size(max = 100)
public String name;
//@Maxbytelength(maxbytelength = 100)
@Size(max = 100)
public String password;
//@Maxbytelength(maxbytelength = 100)
@Size(max = 100)
public String confirmPassword;
public String[] roles;
public String[] groups;
//@Required(target = "confirmfromupdate,update,delete")
//@IntegerType
public String versionNo;
public void initialize() {
id = null;
name = null;
roles = null;
groups = null;
versionNo = null;
}
}

View file

@ -0,0 +1,37 @@
/*
* Copyright 2009-2015 the CodeLibs Project and the Others.
*
* 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 org.codelibs.fess.app.web.admin.user;
import javax.validation.constraints.Size;
import org.lastaflute.web.validation.Required;
/**
* @author Keiichi Watanabe
*/
public class EditForm extends CreateForm {
private static final long serialVersionUID = 1L;
@Required
@Size(max = 1000)
public String id;
@Required
public Integer versionNo;
}

View file

@ -17,15 +17,13 @@
package org.codelibs.fess.app.web.admin.user;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* @author shinsuke
*/
public class UserSearchForm implements Serializable {
public class SearchForm implements Serializable {
private static final long serialVersionUID = 1L;
public Map<String, String> searchParams = new HashMap<String, String>();
public String id;
}

View file

@ -140,6 +140,7 @@ public class AdminWebauthenticationAction extends FessAdminAction {
@Execute(token = TxToken.SAVE)
public HtmlResponse editpage(final int crudMode, final String id) {
verifyCrudMode(crudMode, CrudMode.EDIT);
return asHtml(path_AdminWebauthentication_EditJsp).useForm(EditForm.class, op -> {
op.setup(form -> {
webAuthenticationService.getWebAuthentication(id).ifPresent(entity -> {
@ -181,7 +182,7 @@ public class AdminWebauthenticationAction extends FessAdminAction {
public HtmlResponse editfromconfirm(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.EDIT;
String id = form.id;
final String id = form.id;
webAuthenticationService.getWebAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {
@ -217,7 +218,7 @@ public class AdminWebauthenticationAction extends FessAdminAction {
public HtmlResponse deletefromconfirm(final EditForm form) {
validate(form, messages -> {}, toEditHtml());
form.crudMode = CrudMode.DELETE;
String id = form.id;
final String id = form.id;
webAuthenticationService.getWebAuthentication(id).ifPresent(entity -> {
copyBeanToBean(entity, form, op -> {});
}).orElse(() -> {

View file

@ -34,14 +34,14 @@ public class CreateForm implements Serializable {
private static final long serialVersionUID = 1L;
public int crudMode;
public Integer crudMode;
@Size(max = 100)
public String hostname;
@Min(value = 0)
@Max(value = 2147483647)
public String port;
public Integer port;
@Size(max = 100)
public String authRealm;
@ -64,7 +64,7 @@ public class CreateForm implements Serializable {
public String webConfigId;
@Required
@Size(max = 255)
@Size(max = 1000)
public String createdBy;
@Required
@ -72,7 +72,7 @@ public class CreateForm implements Serializable {
public void initialize() {
crudMode = CrudMode.CREATE;
createdBy = "system";
createdBy = ComponentUtil.getSystemHelper().getUsername();
createdTime = ComponentUtil.getSystemHelper().getCurrentTimeAsLong();
}
}

View file

@ -130,7 +130,7 @@
<%-- Box Footer --%>
<div class="box-footer">
<c:if test="${crudMode == 1}">
<input type="submit" class="btn" name="editagain" value="<la:message key="labels.file_authentication_button_back"/>" />
<input type="submit" class="btn" name="createagain" value="<la:message key="labels.file_authentication_button_back"/>" />
<input type="submit" class="btn btn-primary" name="create"
value="<la:message key="labels.file_authentication_button_create"/>"
/>

View file

@ -133,7 +133,7 @@
<%-- Box Footer --%>
<div class="box-footer">
<c:if test="${crudMode == 1}">
<input type="submit" class="btn" name="editagain" value="<la:message key="labels.user_button_back"/>" />
<input type="submit" class="btn" name="createagain" value="<la:message key="labels.user_button_back"/>" />
<input type="submit" class="btn btn-primary" name="create"
value="<la:message key="labels.user_button_create"/>"
/>
@ -173,4 +173,3 @@
<jsp:include page="/WEB-INF/view/common/admin/foot.jsp"></jsp:include>
</body>
</html>