1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.jetspeed.layout.impl; |
18 |
|
|
19 |
|
import java.security.Principal; |
20 |
|
import java.util.ArrayList; |
21 |
|
import java.util.Collection; |
22 |
|
import java.util.HashMap; |
23 |
|
import java.util.Iterator; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
import java.util.prefs.BackingStoreException; |
27 |
|
import java.util.prefs.Preferences; |
28 |
|
|
29 |
|
import org.apache.commons.logging.Log; |
30 |
|
import org.apache.commons.logging.LogFactory; |
31 |
|
import org.apache.jetspeed.JetspeedActions; |
32 |
|
import org.apache.jetspeed.ajax.AJAXException; |
33 |
|
import org.apache.jetspeed.ajax.AjaxAction; |
34 |
|
import org.apache.jetspeed.ajax.AjaxBuilder; |
35 |
|
import org.apache.jetspeed.container.session.PortalSessionsManager; |
36 |
|
import org.apache.jetspeed.request.RequestContext; |
37 |
|
import org.apache.jetspeed.security.SecurityException; |
38 |
|
import org.apache.jetspeed.security.SecurityHelper; |
39 |
|
import org.apache.jetspeed.security.User; |
40 |
|
import org.apache.jetspeed.security.UserManager; |
41 |
|
import org.apache.jetspeed.security.UserPrincipal; |
42 |
|
import org.apache.jetspeed.statistics.PortalStatistics; |
43 |
|
import org.apache.jetspeed.statistics.UserStats; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
public class GetUserListAction |
66 |
|
extends BaseUserAction |
67 |
|
implements AjaxAction, AjaxBuilder, Constants |
68 |
|
{ |
69 |
0 |
protected Log log = LogFactory.getLog(GetUserListAction.class); |
70 |
0 |
private PortalStatistics pstats = null; |
71 |
0 |
private PortalSessionsManager psm = null; |
72 |
|
|
73 |
0 |
private String protectionScope = "all"; |
74 |
|
|
75 |
0 |
private final String PARAM_GUEST = "guest"; |
76 |
0 |
private final String PARAM_USERINFO = "userinfo"; |
77 |
0 |
private final String PARAM_OFFILE = "offline"; |
78 |
0 |
private final String PARAM_ALL = "all"; |
79 |
|
|
80 |
|
public GetUserListAction(String template, |
81 |
|
String errorTemplate, |
82 |
|
UserManager um, |
83 |
|
PortalStatistics pstats, |
84 |
|
PortalSessionsManager psm) |
85 |
|
{ |
86 |
0 |
this(template, errorTemplate, um, pstats, psm, null); |
87 |
0 |
} |
88 |
|
|
89 |
|
public GetUserListAction(String template, |
90 |
|
String errorTemplate, |
91 |
|
UserManager um, |
92 |
|
PortalStatistics pstats, |
93 |
|
PortalSessionsManager psm, |
94 |
|
RolesSecurityBehavior securityBehavior) |
95 |
|
{ |
96 |
0 |
super(template, errorTemplate, um, securityBehavior); |
97 |
0 |
this.pstats = pstats; |
98 |
0 |
this.psm = psm; |
99 |
0 |
} |
100 |
|
|
101 |
|
public GetUserListAction(String template, |
102 |
|
String errorTemplate, |
103 |
|
UserManager um, |
104 |
|
PortalStatistics pstats, |
105 |
|
PortalSessionsManager psm, |
106 |
|
RolesSecurityBehavior securityBehavior, |
107 |
|
String protectionScope) |
108 |
|
{ |
109 |
0 |
super(template, errorTemplate, um, securityBehavior); |
110 |
0 |
this.pstats = pstats; |
111 |
0 |
this.psm = psm; |
112 |
0 |
this.protectionScope = protectionScope; |
113 |
0 |
} |
114 |
|
|
115 |
|
public boolean run(RequestContext requestContext, Map resultMap) |
116 |
|
throws AJAXException |
117 |
|
{ |
118 |
0 |
boolean success = true; |
119 |
0 |
String status = "success"; |
120 |
|
|
121 |
|
boolean includeGuests; |
122 |
|
boolean includeUserInfo; |
123 |
|
boolean includeOffline; |
124 |
0 |
boolean includeAll = isTrue(getActionParameter(requestContext, PARAM_ALL)); |
125 |
|
|
126 |
|
|
127 |
0 |
if(includeAll){ |
128 |
0 |
includeGuests = true; |
129 |
0 |
includeUserInfo = true; |
130 |
0 |
includeOffline = true; |
131 |
|
} |
132 |
|
else |
133 |
|
{ |
134 |
0 |
includeOffline = isTrue(getActionParameter(requestContext, PARAM_OFFILE)); |
135 |
0 |
includeGuests = isTrue(getActionParameter(requestContext, PARAM_GUEST)); |
136 |
0 |
includeUserInfo = isTrue(getActionParameter(requestContext, PARAM_USERINFO)); |
137 |
|
} |
138 |
|
|
139 |
|
|
140 |
0 |
if(securityBehavior != null) |
141 |
|
{ |
142 |
|
|
143 |
0 |
if(!checkAccess(requestContext, JetspeedActions.EDIT) && !this.protectionScope.equals("none")) |
144 |
|
{ |
145 |
|
|
146 |
|
|
147 |
0 |
if(this.protectionScope.equals("private-offline")) |
148 |
|
{ |
149 |
|
|
150 |
0 |
includeUserInfo = false; |
151 |
0 |
includeOffline = false; |
152 |
|
} |
153 |
0 |
else if(this.protectionScope.equals("private")) |
154 |
|
{ |
155 |
|
|
156 |
0 |
includeUserInfo = false; |
157 |
|
} |
158 |
|
else |
159 |
|
{ |
160 |
|
|
161 |
0 |
success = false; |
162 |
0 |
resultMap.put(REASON, "Insufficient access see user details."); |
163 |
0 |
return success; |
164 |
|
} |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
0 |
int numberOfCurrentUsers = 0; |
169 |
0 |
int numberOfCurrentLoggedInUsers = 0; |
170 |
|
|
171 |
0 |
Collection users = new ArrayList(); |
172 |
0 |
Collection loggedInUsers = new ArrayList(); |
173 |
0 |
Collection offlineUsers = new ArrayList(); |
174 |
|
|
175 |
|
try |
176 |
|
{ |
177 |
0 |
resultMap.put(ACTION, "getuserlist"); |
178 |
|
|
179 |
0 |
if(pstats != null) |
180 |
|
{ |
181 |
|
|
182 |
0 |
numberOfCurrentUsers = psm.sessionCount(); |
183 |
0 |
numberOfCurrentLoggedInUsers = pstats.getNumberOfLoggedInUsers(); |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
0 |
List addedUserNames = new ArrayList(); |
193 |
|
|
194 |
|
|
195 |
0 |
if(numberOfCurrentLoggedInUsers > 0) |
196 |
|
{ |
197 |
|
|
198 |
|
|
199 |
0 |
Iterator usersIter = pstats.getListOfLoggedInUsers().iterator(); |
200 |
0 |
while(usersIter.hasNext()) |
201 |
|
{ |
202 |
0 |
Map userMap = (Map)usersIter.next(); |
203 |
0 |
if(userMap != null && userMap.size() > 0) |
204 |
|
{ |
205 |
0 |
Iterator userKeyIter = userMap.keySet().iterator(); |
206 |
0 |
while(userKeyIter.hasNext()) |
207 |
|
{ |
208 |
0 |
String userStatKey = String.valueOf(userKeyIter.next()); |
209 |
0 |
UserStats userStat = (UserStats)userMap.get(userStatKey); |
210 |
|
|
211 |
0 |
Map singleUserMap = new HashMap(); |
212 |
0 |
singleUserMap.put(USERNAME, userStat.getUsername()); |
213 |
0 |
singleUserMap.put(SESSIONS, new Integer(userStat.getNumberOfSessions())); |
214 |
0 |
singleUserMap.put(STATUS, ONLINE); |
215 |
0 |
singleUserMap.put(IPADDRESS, userStat.getInetAddress().getHostAddress()); |
216 |
0 |
if(includeUserInfo) |
217 |
|
{ |
218 |
0 |
singleUserMap.put(USERINFO, getUserInfo(userStat.getUsername())); |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
0 |
if(!addedUserNames.contains(userStat.getUsername())) |
223 |
0 |
addedUserNames.add(userStat.getUsername()); |
224 |
|
|
225 |
0 |
loggedInUsers.add(singleUserMap); |
226 |
0 |
} |
227 |
|
|
228 |
|
} |
229 |
0 |
} |
230 |
|
|
231 |
|
|
232 |
0 |
users.addAll(loggedInUsers); |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
0 |
if(includeOffline) |
237 |
|
{ |
238 |
0 |
Iterator allUusers = userManager.getUsers(""); |
239 |
0 |
while(allUusers.hasNext()) |
240 |
|
{ |
241 |
0 |
User user = (User)allUusers.next(); |
242 |
0 |
Principal userPrincipal = SecurityHelper.getPrincipal(user.getSubject(), UserPrincipal.class); |
243 |
0 |
if(userPrincipal != null) |
244 |
|
{ |
245 |
|
|
246 |
0 |
if(!addedUserNames.contains(userPrincipal.getName())) |
247 |
|
{ |
248 |
0 |
Map userMap = new HashMap(); |
249 |
0 |
userMap.put(USERNAME, userPrincipal.getName()); |
250 |
0 |
userMap.put(STATUS, OFFLINE); |
251 |
0 |
if(includeUserInfo) |
252 |
|
{ |
253 |
0 |
userMap.put(USERINFO, getUserInfo(userPrincipal.getName())); } |
254 |
|
|
255 |
0 |
offlineUsers.add(userMap); |
256 |
|
} |
257 |
|
} |
258 |
0 |
} |
259 |
|
|
260 |
|
|
261 |
0 |
users.addAll(offlineUsers); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
0 |
resultMap.put(USERS, users); |
266 |
|
|
267 |
0 |
if(includeGuests) |
268 |
|
{ |
269 |
|
|
270 |
0 |
int guestUserCount = numberOfCurrentUsers - numberOfCurrentLoggedInUsers; |
271 |
0 |
resultMap.put(GUESTUSERS, new Integer(guestUserCount)); |
272 |
|
} |
273 |
|
|
274 |
0 |
} |
275 |
|
else |
276 |
|
{ |
277 |
0 |
status = "failure"; |
278 |
0 |
resultMap.put(REASON, "Statistics not available"); |
279 |
0 |
return false; |
280 |
|
} |
281 |
0 |
resultMap.put(STATUS, status); |
282 |
|
} |
283 |
0 |
catch (Exception e) |
284 |
|
{ |
285 |
0 |
log.error("exception statistics access", e); |
286 |
0 |
resultMap.put(REASON, e.toString()); |
287 |
0 |
success = false; |
288 |
0 |
} |
289 |
0 |
return success; |
290 |
|
} |
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
private Map getUserInfo(String username) throws SecurityException, BackingStoreException |
302 |
|
{ |
303 |
0 |
Map userInfo = new HashMap(); |
304 |
0 |
User user = userManager.getUser(username); |
305 |
0 |
if(user != null) |
306 |
|
{ |
307 |
0 |
Preferences userPrefs = user.getUserAttributes(); |
308 |
0 |
String[] userPrefKeys = userPrefs.keys(); |
309 |
|
|
310 |
0 |
for(int i = 0; i<userPrefKeys.length; i++) |
311 |
|
{ |
312 |
0 |
userInfo.put(userPrefKeys[i], userPrefs.get(userPrefKeys[i], "No value")); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
0 |
return userInfo; |
317 |
|
} |
318 |
|
|
319 |
|
} |