fix #2639 add print
This commit is contained in:
parent
d039878cfb
commit
7d05a2885b
2 changed files with 157 additions and 1 deletions
|
@ -19,6 +19,8 @@ import static org.codelibs.core.stream.StreamUtil.stream;
|
|||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
@ -126,7 +128,21 @@ public class ActivityHelper {
|
|||
log(buf);
|
||||
}
|
||||
|
||||
private void log(final StringBuilder buf) {
|
||||
public void print(final String action, final OptionalThing<FessUserBean> user, final Map<String, String> params) {
|
||||
final StringBuilder buf = new StringBuilder(100);
|
||||
buf.append("action:");
|
||||
buf.append(action.replace('\t', '_').toUpperCase(Locale.ENGLISH));
|
||||
buf.append('\t');
|
||||
buf.append("user:");
|
||||
buf.append(user.map(FessUserBean::getUserId).orElse("-"));
|
||||
params.entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).map(s -> s.replace('\t', '_')).sorted().forEach(s -> {
|
||||
buf.append('\t');
|
||||
buf.append(s);
|
||||
});
|
||||
log(buf);
|
||||
}
|
||||
|
||||
protected void log(final StringBuilder buf) {
|
||||
buf.append('\t');
|
||||
buf.append("ip:");
|
||||
buf.append(getClientIp());
|
||||
|
|
140
src/test/java/org/codelibs/fess/helper/ActivityHelperTest.java
Normal file
140
src/test/java/org/codelibs/fess/helper/ActivityHelperTest.java
Normal file
|
@ -0,0 +1,140 @@
|
|||
/*
|
||||
* Copyright 2012-2022 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.helper;
|
||||
|
||||
import org.codelibs.fess.entity.FessUser;
|
||||
import org.codelibs.fess.mylasta.action.FessUserBean;
|
||||
import org.codelibs.fess.unit.UnitFessTestCase;
|
||||
import org.dbflute.optional.OptionalThing;
|
||||
import org.opensearch.common.collect.Map;
|
||||
|
||||
public class ActivityHelperTest extends UnitFessTestCase {
|
||||
|
||||
private ActivityHelper activityHelper;
|
||||
|
||||
private ThreadLocal<String> localLogMsg = new ThreadLocal<>();
|
||||
|
||||
@Override
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
activityHelper = new ActivityHelper() {
|
||||
@Override
|
||||
protected void log(final StringBuilder buf) {
|
||||
localLogMsg.set(buf.toString());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void test_login() {
|
||||
activityHelper.login(OptionalThing.empty());
|
||||
assertEquals("action:LOGIN\tuser:-\tpermissions:-", localLogMsg.get());
|
||||
|
||||
activityHelper.login(createUser("testuser", new String[0]));
|
||||
assertEquals("action:LOGIN\tuser:testuser\tpermissions:-", localLogMsg.get());
|
||||
|
||||
activityHelper.login(createUser("testuser", new String[] { "111", "222" }));
|
||||
assertEquals("action:LOGIN\tuser:testuser\tpermissions:111|222", localLogMsg.get());
|
||||
}
|
||||
|
||||
public void test_loginFailure() {
|
||||
activityHelper.loginFailure(OptionalThing.empty());
|
||||
assertEquals("action:LOGIN_FAILURE", localLogMsg.get());
|
||||
}
|
||||
|
||||
public void test_logout() {
|
||||
activityHelper.logout(OptionalThing.empty());
|
||||
assertEquals("action:LOGOUT\tuser:-\tpermissions:-", localLogMsg.get());
|
||||
|
||||
activityHelper.logout(createUser("testuser", new String[0]));
|
||||
assertEquals("action:LOGOUT\tuser:testuser\tpermissions:-", localLogMsg.get());
|
||||
|
||||
activityHelper.logout(createUser("testuser", new String[] { "111", "222" }));
|
||||
assertEquals("action:LOGOUT\tuser:testuser\tpermissions:111|222", localLogMsg.get());
|
||||
}
|
||||
|
||||
public void test_access() {
|
||||
activityHelper.access(OptionalThing.empty(), "/", "aaa");
|
||||
assertEquals("action:ACCESS\tuser:-\tpath:/\texecute:aaa", localLogMsg.get());
|
||||
|
||||
activityHelper.access(createUser("testuser", new String[0]), "/aaa", "bbb");
|
||||
assertEquals("action:ACCESS\tuser:testuser\tpath:/aaa\texecute:bbb", localLogMsg.get());
|
||||
|
||||
activityHelper.access(createUser("testuser", new String[] { "111", "222" }), "/aaa/bbb", "ccc");
|
||||
assertEquals("action:ACCESS\tuser:testuser\tpath:/aaa/bbb\texecute:ccc", localLogMsg.get());
|
||||
}
|
||||
|
||||
public void test_permissionChanged() {
|
||||
activityHelper.permissionChanged(OptionalThing.empty());
|
||||
assertEquals("action:UPDATE_PERMISSION\tuser:-\tpermissions:-", localLogMsg.get());
|
||||
|
||||
activityHelper.permissionChanged(createUser("testuser", new String[0]));
|
||||
assertEquals("action:UPDATE_PERMISSION\tuser:testuser\tpermissions:-", localLogMsg.get());
|
||||
|
||||
activityHelper.permissionChanged(createUser("testuser", new String[] { "111", "222" }));
|
||||
assertEquals("action:UPDATE_PERMISSION\tuser:testuser\tpermissions:111|222", localLogMsg.get());
|
||||
}
|
||||
|
||||
public void test_print() {
|
||||
activityHelper.print("aaa", OptionalThing.empty(), Map.of());
|
||||
assertEquals("action:AAA\tuser:-", localLogMsg.get());
|
||||
|
||||
activityHelper.print("aaa bbb", createUser("testuser", new String[0]), Map.of("111", "222"));
|
||||
assertEquals("action:AAA BBB\tuser:testuser\t111:222", localLogMsg.get());
|
||||
|
||||
activityHelper.print("ccc", createUser("testuser", new String[] { "111", "222" }), Map.of("111", "222", "333", "444"));
|
||||
assertEquals("action:CCC\tuser:testuser\t111:222\t333:444", localLogMsg.get());
|
||||
}
|
||||
|
||||
OptionalThing<FessUserBean> createUser(String name, String[] permissions) {
|
||||
return OptionalThing.of(new FessUserBean(new TestUser(name, permissions)));
|
||||
}
|
||||
|
||||
static class TestUser implements FessUser {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
|
||||
private String[] permissions;
|
||||
|
||||
TestUser(String name, String[] permissions) {
|
||||
this.name = name;
|
||||
this.permissions = permissions;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getRoleNames() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getGroupNames() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getPermissions() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue