This commit is contained in:
yfujita 2017-03-28 02:19:03 +09:00
parent c5e76875fe
commit 5b76d69886
12 changed files with 277 additions and 15 deletions

View file

@ -30,7 +30,9 @@ 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.app.web.base.login.FessLoginAssist;
import org.codelibs.fess.es.user.exentity.User;
import org.codelibs.fess.util.ComponentUtil;
import org.codelibs.fess.util.RenderDataUtil;
import org.dbflute.optional.OptionalEntity;
import org.dbflute.optional.OptionalThing;
@ -263,7 +265,7 @@ public class AdminUserAction extends FessAdminAction {
//===================================================================================
// Assist Logic
// ============
private OptionalEntity<User> getEntity(final CreateForm form) {
private static OptionalEntity<User> getEntity(final CreateForm form) {
switch (form.crudMode) {
case CrudMode.CREATE:
return OptionalEntity.of(new User()).map(entity -> {
@ -272,7 +274,7 @@ public class AdminUserAction extends FessAdminAction {
});
case CrudMode.EDIT:
if (form instanceof EditForm) {
return userService.getUser(((EditForm) form).id);
return ComponentUtil.getComponent(UserService.class).getUser(((EditForm) form).id);
}
break;
default:
@ -281,11 +283,11 @@ public class AdminUserAction extends FessAdminAction {
return OptionalEntity.empty();
}
protected OptionalEntity<User> getUser(final CreateForm form) {
public static OptionalEntity<User> getUser(final CreateForm form) {
return getEntity(form).map(entity -> {
copyBeanToBean(form, entity, op -> op.exclude(ArrayUtils.addAll(Constants.COMMON_CONVERSION_RULE, "password")));
if (form.crudMode.intValue() == CrudMode.CREATE || StringUtil.isNotBlank(form.password)) {
final String encodedPassword = fessLoginAssist.encryptPassword(form.password);
final String encodedPassword = ComponentUtil.getComponent(FessLoginAssist.class).encryptPassword(form.password);
entity.setOriginalPassword(form.password);
entity.setPassword(encodedPassword);
}
@ -327,7 +329,7 @@ public class AdminUserAction extends FessAdminAction {
}
}
private void resetPassword(final CreateForm form) {
public static void resetPassword(final CreateForm form) {
form.password = null;
form.confirmPassword = null;
}

View file

@ -33,6 +33,7 @@ import javax.annotation.Resource;
import org.codelibs.fess.app.pager.BadWordPager;
import org.codelibs.fess.app.service.BadWordService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.admin.badword.UploadForm;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
@ -69,6 +70,7 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
@Execute
public JsonResponse<ApiResult> put$setting(final CreateBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.CREATE;
final BadWord entity = getBadWord(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
@ -84,13 +86,14 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
}
// POST /api/admin/badword/setting
// POST /api/admin/user/setting
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.EDIT;
final BadWord entity = getBadWord(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
});
return null;
});
@ -100,7 +103,7 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
}
// DELETE /api/admin/badword/setting/{id}
@ -121,7 +124,7 @@ public class ApiAdminBadwordAction extends FessApiAdminAction {
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(id).created(true).status(ApiResult.Status.OK).result());
return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
}
// POST /api/admin/badword/upload

View file

@ -1,5 +1,3 @@
package org.codelibs.fess.app.web.api.admin.elevateword;
/*
* Copyright 2012-2017 CodeLibs Project and the Others.
*
@ -15,10 +13,12 @@ package org.codelibs.fess.app.web.api.admin.elevateword;
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.codelibs.fess.app.web.api.admin.elevateword;
import org.codelibs.core.lang.StringUtil;
import org.codelibs.fess.app.pager.ElevateWordPager;
import org.codelibs.fess.app.service.ElevateWordService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.admin.elevateword.UploadForm;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
@ -85,6 +85,7 @@ public class ApiAdminElevatewordAction extends FessApiAdminAction {
@Execute
public JsonResponse<ApiResult> put$setting(final CreateBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.CREATE;
final ElevateWord entity = getElevateWord(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
@ -105,9 +106,10 @@ public class ApiAdminElevatewordAction extends FessApiAdminAction {
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.EDIT;
final ElevateWord entity = getElevateWord(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
});
return null;
});
@ -118,7 +120,7 @@ public class ApiAdminElevatewordAction extends FessApiAdminAction {
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
}
// DELETE /api/admin/elevateword/setting/{id}
@ -139,7 +141,7 @@ public class ApiAdminElevatewordAction extends FessApiAdminAction {
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(id).created(true).status(ApiResult.Status.OK).result());
return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
}
// POST /api/admin/elevateword/upload

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 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.api.admin.elevateword;
import org.codelibs.fess.app.web.admin.elevateword.CreateForm;

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 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.api.admin.elevateword;
import org.codelibs.fess.app.web.admin.elevateword.DownloadForm;

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 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.api.admin.elevateword;
import org.codelibs.fess.app.web.admin.elevateword.EditForm;

View file

@ -1,3 +1,18 @@
/*
* Copyright 2012-2017 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.api.admin.elevateword;
import org.codelibs.fess.app.web.api.admin.BaseSearchBody;

View file

@ -0,0 +1,132 @@
/*
* Copyright 2012-2017 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.api.admin.user;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.codelibs.fess.Constants;
import org.codelibs.fess.app.pager.UserPager;
import org.codelibs.fess.app.service.UserService;
import org.codelibs.fess.app.web.CrudMode;
import org.codelibs.fess.app.web.api.ApiResult;
import org.codelibs.fess.app.web.api.admin.FessApiAdminAction;
import org.codelibs.fess.es.user.exentity.User;
import org.lastaflute.web.Execute;
import org.lastaflute.web.response.JsonResponse;
import static org.codelibs.fess.app.web.admin.user.AdminUserAction.*;
public class ApiAdminUserAction extends FessApiAdminAction {
@Resource
private UserService userService;
// GET /api/admin/user
// POST /api/admin/user
@Execute
public JsonResponse<ApiResult> settings(final SearchBody body) {
validateApi(body, messages -> {});
final UserPager pager = new UserPager();
copyBeanToBean(body, pager, op -> op.exclude(Constants.PAGER_CONVERSION_RULE));
final List<User> list = userService.getUserList(pager);
return asJson(new ApiResult.ApiConfigsResponse<EditBody>()
.settings(list.stream().map(entity -> createEditBody(entity)).collect(Collectors.toList()))
.total(pager.getAllRecordCount()).status(ApiResult.Status.OK).result());
}
// GET /api/admin/user/setting/{id}
@Execute
public JsonResponse<ApiResult> get$setting(final String id) {
return asJson(new ApiResult.ApiConfigResponse()
.setting(userService.getUser(id).map(entity -> createEditBody(entity)).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
return null;
})).status(ApiResult.Status.OK).result());
}
// PUT /api/admin/user/setting
@Execute
public JsonResponse<ApiResult> put$setting(final CreateBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.CREATE;
final User entity = getUser(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudFailedToCreateInstance(GLOBAL);
});
return null;
});
try {
userService.store(entity);
saveInfo(messages -> messages.addSuccessCrudCreateCrudTable(GLOBAL));
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToCreateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(true).status(ApiResult.Status.OK).result());
}
// POST /api/admin/user/setting
@Execute
public JsonResponse<ApiResult> post$setting(final EditBody body) {
validateApi(body, messages -> {});
body.crudMode = CrudMode.EDIT;
final User entity = getUser(body).orElseGet(() -> {
throwValidationErrorApi(messages -> {
messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, body.id);
});
return null;
});
try {
userService.store(entity);
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToUpdateCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(entity.getId()).created(false).status(ApiResult.Status.OK).result());
}
// DELETE /api/admin/user/setting/{id}
@Execute
public JsonResponse<ApiResult> delete$setting(final String id) {
final User entity = userService.getUser(id).orElseGet(() -> {
throwValidationErrorApi(messages -> messages.addErrorsCrudCouldNotFindCrudTable(GLOBAL, id));
return null;
});
getUserBean().ifPresent(u -> {
if (u.getFessUser() instanceof User && entity.getName().equals(u.getUserId())) {
throwValidationErrorApi(messages -> messages.addErrorsCouldNotDeleteLoggedInUser(GLOBAL));
}
});
try {
userService.delete(entity);
saveInfo(messages -> messages.addSuccessCrudDeleteCrudTable(GLOBAL));
} catch (final Exception e) {
throwValidationErrorApi(messages -> messages.addErrorsCrudFailedToDeleteCrudTable(GLOBAL, buildThrowableMessage(e)));
}
return asJson(new ApiResult.ApiUpdateResponse().id(id).created(false).status(ApiResult.Status.OK).result());
}
protected EditBody createEditBody(final User entity) {
final EditBody body = new EditBody();
copyBeanToBean(entity, body, copyOp -> {
copyOp.excludeNull();
});
body.password = null;
body.confirmPassword = null;
return body;
}
}

View file

@ -0,0 +1,21 @@
/*
* Copyright 2012-2017 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.api.admin.user;
import org.codelibs.fess.app.web.admin.user.CreateForm;
public class CreateBody extends CreateForm {
}

View file

@ -0,0 +1,21 @@
/*
* Copyright 2012-2017 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.api.admin.user;
import org.codelibs.fess.app.web.admin.user.EditForm;
public class EditBody extends EditForm {
}

View file

@ -0,0 +1,21 @@
/*
* Copyright 2012-2017 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.api.admin.user;
import org.codelibs.fess.app.web.admin.user.SearchForm;
public class SearchBody extends SearchForm {
}

View file

@ -189,7 +189,7 @@ public abstract class FessBaseAction extends TypicalAction // has several interf
sessionManager.errors().saveMessages(messages);
}
protected void copyBeanToBean(final Object src, final Object dest, final Consumer<CopyOptions> option) {
protected static void copyBeanToBean(final Object src, final Object dest, final Consumer<CopyOptions> option) {
BeanUtil.copyBeanToBean(src, dest, option);
}