1 /*
2 * Copyright 2012-2025 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.app.logic;
17
18 import org.codelibs.fess.mylasta.action.FessUserBean;
19 import org.dbflute.hook.AccessContext;
20 import org.dbflute.optional.OptionalThing;
21 import org.lastaflute.core.time.TimeManager;
22 import org.lastaflute.db.dbflute.accesscontext.AccessContextResource;
23
24 import jakarta.annotation.Resource;
25
26 /**
27 * The logic for access context.
28 */
29 public class AccessContextLogic {
30
31 /**
32 * Default constructor.
33 */
34 public AccessContextLogic() {
35 // nothing
36 }
37
38 // ===================================================================================
39 // Attribute
40 // =========
41 @Resource
42 private TimeManager timeManager;
43
44 // ===================================================================================
45 // Resource Interface
46 // ==================
47 /**
48 * The supplier of user type.
49 */
50 @FunctionalInterface
51 public interface UserTypeSupplier {
52 /**
53 * Supply the user type.
54 * @return The user type.
55 */
56 OptionalThing<String> supply();
57 }
58
59 /**
60 * The supplier of user bean.
61 */
62 @FunctionalInterface
63 public interface UserBeanSupplier {
64 /**
65 * Supply the user bean.
66 * @return The user bean.
67 */
68 OptionalThing<FessUserBean> supply();
69 }
70
71 /**
72 * The supplier of application type.
73 */
74 @FunctionalInterface
75 public interface AppTypeSupplier {
76 /**
77 * Supply the application type.
78 * @return The application type.
79 */
80 String supply();
81 }
82
83 // ===================================================================================
84 // Create Context
85 // ==============
86 /**
87 * Create the access context.
88 * @param resource The access context resource.
89 * @param userTypeSupplier The supplier of user type.
90 * @param userBeanSupplier The supplier of user bean.
91 * @param appTypeSupplier The supplier of application type.
92 * @return The access context.
93 */
94 public AccessContext create(final AccessContextResource resource, final UserTypeSupplier userTypeSupplier,
95 final UserBeanSupplier userBeanSupplier, final AppTypeSupplier appTypeSupplier) {
96 final AccessContext context = new AccessContext();
97 context.setAccessLocalDateTimeProvider(() -> timeManager.currentDateTime());
98 context.setAccessUserProvider(() -> buildAccessUserTrace(resource, userTypeSupplier, appTypeSupplier));
99 return context;
100 }
101
102 private String buildAccessUserTrace(final AccessContextResource resource, final UserTypeSupplier userTypeSupplier,
103 final AppTypeSupplier appTypeSupplier) {
104 final StringBuilder sb = new StringBuilder();
105 sb.append(userTypeSupplier.supply().orElse("_"));
106 sb.append(",").append(appTypeSupplier.supply()).append(",").append(resource.getModuleName());
107 final String trace = sb.toString();
108 final int columnSize = 200;
109 return trace.length() > columnSize ? trace.substring(0, columnSize) : trace;
110 }
111 }