1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.audit;
35
36 import info.magnolia.cms.security.UserManager;
37 import info.magnolia.cms.security.auth.login.FormLogin;
38 import info.magnolia.cms.security.auth.login.LoginResult;
39 import info.magnolia.context.Context;
40 import info.magnolia.context.MgnlContext;
41 import info.magnolia.context.SystemContext;
42 import info.magnolia.context.UserContext;
43
44 import javax.jcr.nodetype.NodeType;
45 import javax.servlet.http.HttpServletRequest;
46
47
48
49
50 public class AuditLoggingUtil {
51
52 public static final String ACTION_CREATE = "create";
53 public static final String ACTION_MODIFY = "modify";
54 public static final String ACTION_DELETE = "delete";
55 public static final String ACTION_COPY = "copy";
56 public static final String ACTION_MOVE = "move";
57 public static final String ACTION_ACTIVATE = "activate";
58 public static final String ACTION_DEACTIVATE = "deactivate";
59 public static final String ACTION_LOGIN = "login";
60 public static final String ACTION_LOGOUT = "logout";
61 public static final String ACTION_SECURITY = "security";
62
63
64
65
66 public static void log(String action, String workspaceName, NodeType nodeType, String nodePath) {
67 AuditLoggingUtil.log(action, new String[]{AuditLoggingUtil.getUser(), workspaceName, nodeType == null ? "" : nodeType.getName(), nodePath});
68 }
69
70
71
72
73 public static void log(String action, String userName, String workspaceName, NodeType nodeType, String nodePathTo) {
74 AuditLoggingUtil.log(action, new String[]{userName, workspaceName, nodeType == null ? "" : nodeType.getName(), nodePathTo});
75 }
76
77
78
79
80 public static void log(String action, String workspaceName, String nodePathFrom, String nodePathTo) {
81 AuditLoggingUtil.log(action, new String[]{AuditLoggingUtil.getUser(), workspaceName, nodePathFrom, nodePathTo});
82 }
83
84
85
86
87 public static void log(String action, long timeStamp, String workspaceName, NodeType nodeType, String path, String pathTo) {
88 AuditLoggingUtil.log(action, new String[]{String.valueOf(timeStamp), AuditLoggingUtil.getUser(), workspaceName, nodeType == null ? "" : nodeType.getName(), path, pathTo == null ? "" : pathTo});
89 }
90
91
92
93
94 public static void log(String action, String workspaceFrom, String workspaceTo, String nodePathFrom, String nodePathTo) {
95 AuditLoggingUtil.log(action, new String[]{AuditLoggingUtil.getUser(), workspaceFrom, workspaceTo, nodePathFrom, nodePathTo});
96 }
97
98
99
100
101 public static void log(final UserContext userContext) {
102 AuditLoggingUtil.log(AuditLoggingUtil.ACTION_LOGOUT, null, (String) null, null);
103 }
104
105
106
107
108 public static void log(final LoginResult loginResult, final HttpServletRequest request) {
109 int loginStatus = loginResult.getStatus();
110
111 if (loginStatus == LoginResult.STATUS_SUCCEEDED_REDIRECT_REQUIRED || loginStatus == LoginResult.STATUS_SUCCEEDED || loginStatus == LoginResult.STATUS_FAILED) {
112
113 String userId = request.getParameter(FormLogin.PARAMETER_USER_ID);
114 if (UserManager.ANONYMOUS_USER.equals(userId)) {
115
116 return;
117 }
118
119 String result;
120 if (loginStatus == LoginResult.STATUS_SUCCEEDED || loginStatus == LoginResult.STATUS_SUCCEEDED_REDIRECT_REQUIRED) {
121 result = "Success";
122 } else {
123 result = "Failure " + loginResult.getLoginException().getLocalizedMessage();
124 }
125
126 AuditLoggingUtil.log(AuditLoggingUtil.ACTION_LOGIN, new String[]{userId, request.getRemoteAddr(), result});
127 }
128 }
129
130
131
132
133 public static void logSecurity(String ipAddress, String securityEventType, String securityEventdetails) {
134 AuditLoggingUtil.log(AuditLoggingUtil.ACTION_SECURITY, new String[]{getUser(), ipAddress, securityEventType, securityEventdetails});
135 }
136
137 private static void log(String action, String[] data) {
138 AuditLoggingManager manager = AuditLoggingManager.getInstance();
139 if (manager != null) {
140 manager.log(action, data);
141 }
142 }
143
144 private static String getUser() {
145 try {
146 Context ctx = null;
147 if (MgnlContext.isSystemInstance()) {
148
149 ctx = ((SystemContext) MgnlContext.getInstance()).getOriginalContext();
150 if (ctx == null) {
151
152 return "SystemUser";
153 }
154 if (ctx.getUser() != null) {
155
156 return "System [" + ctx.getUser().getName() + "]";
157 }
158 } else if (MgnlContext.hasInstance()) {
159
160 ctx = MgnlContext.getInstance();
161 if (ctx.getUser() != null) {
162 return ctx.getUser().getName();
163 }
164 }
165 } catch (Exception e) {
166
167 return "system user";
168 }
169
170 return "user not set";
171 }
172
173 }