View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.time.ZonedDateTime;
21  import java.time.format.DateTimeFormatter;
22  import java.util.stream.Collectors;
23  
24  import javax.annotation.PostConstruct;
25  
26  import org.apache.logging.log4j.LogManager;
27  import org.apache.logging.log4j.Logger;
28  import org.codelibs.core.lang.StringUtil;
29  import org.codelibs.fess.app.web.base.login.FessCredential;
30  import org.codelibs.fess.mylasta.action.FessUserBean;
31  import org.codelibs.fess.util.ComponentUtil;
32  import org.dbflute.optional.OptionalThing;
33  import org.lastaflute.web.login.credential.LoginCredential;
34  import org.lastaflute.web.util.LaRequestUtil;
35  
36  /**
37   * @author shinsuke
38   *
39   */
40  public class ActivityHelper {
41      protected Logger logger = null;
42  
43      protected String loggerName = "fess.log.audit";
44  
45      protected String permissionSeparator = "|";
46  
47      @PostConstruct
48      public void init() {
49          logger = LogManager.getLogger(loggerName);
50      }
51  
52      public void login(final OptionalThing<FessUserBean> user) {
53          final StringBuilder buf = new StringBuilder(100);
54          buf.append("action:");
55          buf.append(Action.LOGIN);
56          buf.append('\t');
57          buf.append("user:");
58          buf.append(user.map(FessUserBean::getUserId).orElse("-"));
59          buf.append('\t');
60          buf.append("permissions:");
61          buf.append(user.map(u -> stream(u.getPermissions()).get(stream -> stream.collect(Collectors.joining(permissionSeparator))))
62                  .filter(StringUtil::isNotBlank).orElse("-"));
63          log(buf);
64      }
65  
66      public void loginFailure(final OptionalThing<LoginCredential> credential) {
67          final StringBuilder buf = new StringBuilder(100);
68          buf.append("action:");
69          buf.append(Action.LOGIN_FAILURE);
70          credential.map(c -> {
71              final StringBuilder buffer = new StringBuilder(100);
72              buffer.append('\t');
73              buffer.append("class:");
74              buffer.append(c.getClass().getSimpleName());
75              if (c instanceof FessCredential) {
76                  buffer.append('\t');
77                  buffer.append("user:");
78                  buffer.append(((FessCredential) c).getUserId());
79              }
80              return buffer.toString();
81          }).ifPresent(buf::append);
82          log(buf);
83      }
84  
85      public void logout(final OptionalThing<FessUserBean> user) {
86          final StringBuilder buf = new StringBuilder(100);
87          buf.append("action:");
88          buf.append(Action.LOGOUT);
89          buf.append('\t');
90          buf.append("user:");
91          buf.append(user.map(FessUserBean::getUserId).orElse("-"));
92          buf.append('\t');
93          buf.append("permissions:");
94          buf.append(user.map(u -> stream(u.getPermissions()).get(stream -> stream.collect(Collectors.joining(permissionSeparator))))
95                  .filter(StringUtil::isNotBlank).orElse("-"));
96          log(buf);
97      }
98  
99      public void access(final OptionalThing<FessUserBean> user, final String path, final String execute) {
100         final StringBuilder buf = new StringBuilder(100);
101         buf.append("action:");
102         buf.append(Action.ACCESS);
103         buf.append('\t');
104         buf.append("user:");
105         buf.append(user.map(FessUserBean::getUserId).orElse("-"));
106         buf.append('\t');
107         buf.append("path:");
108         buf.append(path);
109         buf.append('\t');
110         buf.append("execute:");
111         buf.append(execute);
112         log(buf);
113     }
114 
115     public void permissionChanged(final OptionalThing<FessUserBean> user) {
116         final StringBuilder buf = new StringBuilder(100);
117         buf.append("action:");
118         buf.append(Action.UPDATE_PERMISSION);
119         buf.append('\t');
120         buf.append("user:");
121         buf.append(user.map(FessUserBean::getUserId).orElse("-"));
122         buf.append('\t');
123         buf.append("permissions:");
124         buf.append(user.map(u -> stream(u.getPermissions()).get(stream -> stream.collect(Collectors.joining(permissionSeparator))))
125                 .filter(StringUtil::isNotBlank).orElse("-"));
126         log(buf);
127     }
128 
129     private void log(final StringBuilder buf) {
130         buf.append('\t');
131         buf.append("ip:");
132         buf.append(getClientIp());
133         buf.append('\t');
134         buf.append("time:");
135         buf.append(DateTimeFormatter.ISO_INSTANT.format(ZonedDateTime.now()));
136         logger.info(buf.toString());
137     }
138 
139     protected static String getClientIp() {
140         return LaRequestUtil.getOptionalRequest().map(req -> ComponentUtil.getViewHelper().getClientIp(req)).orElse("-");
141     }
142 
143     protected enum Action {
144         LOGIN, LOGOUT, ACCESS, LOGIN_FAILURE, UPDATE_PERMISSION;
145     }
146 
147     public void setLoggerName(final String loggerName) {
148         this.loggerName = loggerName;
149     }
150 
151     public void setPermissionSeparator(final String permissionSeparator) {
152         this.permissionSeparator = permissionSeparator;
153     }
154 }