1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.services.security;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import javax.servlet.ServletConfig;
25
26 import org.apache.jetspeed.om.security.JetspeedUser;
27 import org.apache.jetspeed.om.security.JetspeedUserFactory;
28 import org.apache.jetspeed.om.security.UserNamePrincipal;
29 import org.apache.jetspeed.portal.Portlet;
30 import org.apache.jetspeed.services.JetspeedPortalAccessController;
31 import org.apache.jetspeed.services.JetspeedSecurity;
32 import org.apache.jetspeed.services.JetspeedUserManagement;
33 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
34 import org.apache.jetspeed.services.logging.JetspeedLogger;
35 import org.apache.jetspeed.services.rundata.JetspeedRunData;
36 import org.apache.turbine.om.security.User;
37 import org.apache.turbine.services.InitializationException;
38 import org.apache.turbine.services.TurbineBaseService;
39 import org.apache.turbine.services.TurbineServices;
40 import org.apache.turbine.services.resources.ResourceService;
41
42 /***
43 * <p>This is an implementation of the <code>JetspeedSecurityService</code> interface.
44 *
45 *
46 * @author <a href="mailto:david@bluesunrise.com">David Sean Taylor</a>
47 * @author <a href="mailto:sgala@hisitech.com">Santiago Gala</a>
48 * @version $Id: JetspeedDBSecurityService.java,v 1.25 2004/03/31 04:49:10 morciuch Exp $
49 */
50
51 public class JetspeedDBSecurityService extends TurbineBaseService
52 implements JetspeedSecurityService
53 {
54 /***
55 * Static initialization of the logger for this class
56 */
57 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(JetspeedDBSecurityService.class.getName());
58
59 private final static String CONFIG_CASEINSENSITIVE_USERNAME = "caseinsensitive.username";
60 private final static String CONFIG_CASEINSENSITIVE_PASSWORD = "caseinsensitive.password";
61 private final static String CONFIG_CASEINSENSITIVE_UPPER = "caseinsensitive.upper";
62 private final static String CONFIG_LOGON_STRIKE_COUNT = "logon.strike.count";
63 private final static String CONFIG_LOGON_STRIKE_MAX = "logon.strike.max";
64 private final static String CONFIG_LOGON_STRIKE_INTERVAL = "logon.strike.interval";
65 private final static String CONFIG_LOGON_AUTO_DISABLE = "logon.auto.disable";
66 private final static String CONFIG_ACTIONS_ANON_DISABLE = "actions.anon.disable";
67 private final static String CONFIG_ACTIONS_ALLUSERS_DISABLE = "actions.allusers.disable";
68 private final static String CONFIG_ACTIONS_ADMIN_ROLES = "admin.roles";
69
70 private final static String CONFIG_NEWUSER_ROLES = "newuser.roles";
71 private final static String CONFIG_DEFAULT_PERMISSION_LOGGEDIN = "permission.default.loggedin";
72 private final static String CONFIG_DEFAULT_PERMISSION_ANONYMOUS = "permission.default.anonymous";
73 private final static String CONFIG_ANONYMOUS_USER = "user.anonymous";
74 private final static String [] DEFAULT_PERMISSIONS = {""};
75 private final static String [] DEFAULT_CONFIG_NEWUSER_ROLES =
76 { "user" };
77 private final static String [] DEFAULT_ADMIN_ROLES =
78 { "admin" };
79
80 String roles[] = null;
81 boolean caseInsensitiveUsername = false;
82 boolean caseInsensitivePassword = false;
83 boolean caseInsensitiveUpper = true;
84 boolean actionsAnonDisable = true;
85 boolean actionsAllUsersDisable = false;
86 String anonymousUser = "anon";
87 String[] adminRoles = null;
88
89 int strikeCount = 3;
90 int strikeMax = 20;
91 long strikeInterval = 300;
92
93 boolean autoLogonDisable = false;
94
95 private static HashMap users = new HashMap();
96
97 private static Object sem = new Object();
98
99 /***
100 * This is the early initialization method called by the
101 * Turbine <code>Service</code> framework
102 * @param conf The <code>ServletConfig</code>
103 * @exception throws a <code>InitializationException</code> if the service
104 * fails to initialize
105 */
106 public synchronized void init(ServletConfig conf) throws InitializationException
107 {
108
109 if (getInit()) return;
110
111 super.init(conf);
112
113
114 ResourceService serviceConf = ((TurbineServices)TurbineServices.getInstance())
115 .getResources(JetspeedSecurityService.SERVICE_NAME);
116
117 try
118 {
119 roles = serviceConf.getStringArray(CONFIG_NEWUSER_ROLES);
120 adminRoles = serviceConf.getStringArray(CONFIG_ACTIONS_ADMIN_ROLES);
121 }
122 catch (Exception e)
123 {}
124
125 if (null == roles || roles.length == 0)
126 {
127 roles = DEFAULT_CONFIG_NEWUSER_ROLES;
128 }
129
130 if (null == adminRoles || adminRoles.length == 0)
131 {
132 adminRoles = DEFAULT_ADMIN_ROLES;
133 }
134
135 caseInsensitiveUsername = serviceConf.getBoolean(CONFIG_CASEINSENSITIVE_USERNAME, caseInsensitiveUsername);
136 caseInsensitivePassword = serviceConf.getBoolean(CONFIG_CASEINSENSITIVE_PASSWORD, caseInsensitivePassword);
137 caseInsensitiveUpper = serviceConf.getBoolean(CONFIG_CASEINSENSITIVE_UPPER, caseInsensitiveUpper);
138
139 strikeCount = serviceConf.getInt(CONFIG_LOGON_STRIKE_COUNT, strikeCount);
140 strikeInterval = serviceConf.getLong(CONFIG_LOGON_STRIKE_INTERVAL, strikeInterval);
141 strikeMax = serviceConf.getInt(CONFIG_LOGON_STRIKE_MAX, strikeMax);
142
143 autoLogonDisable = serviceConf.getBoolean(CONFIG_LOGON_AUTO_DISABLE, autoLogonDisable);
144 actionsAnonDisable = serviceConf.getBoolean(CONFIG_ACTIONS_ANON_DISABLE, actionsAnonDisable);
145 actionsAllUsersDisable = serviceConf.getBoolean(CONFIG_ACTIONS_ALLUSERS_DISABLE, actionsAllUsersDisable);
146
147 anonymousUser = serviceConf.getString(CONFIG_ANONYMOUS_USER, anonymousUser);
148
149
150 setInit(true);
151 }
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 public JetspeedUser getUserInstance()
173 {
174 try
175 {
176 return JetspeedUserFactory.getInstance();
177 }
178 catch (UserException e)
179 {
180 return null;
181 }
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209 public String convertUserName(String username)
210 {
211 if (caseInsensitiveUsername)
212 {
213 username = (caseInsensitiveUpper) ? username.toUpperCase() : username.toLowerCase();
214 }
215 return username;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 public String convertPassword(String password)
236 {
237 if (caseInsensitivePassword)
238 {
239 password = (caseInsensitiveUpper) ? password.toUpperCase() : password.toLowerCase();
240 }
241 return password;
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 public boolean checkDisableAccount(String username)
272 {
273 username = convertUserName(username);
274
275
276 UserLogonStats stat = (UserLogonStats)users.get(username);
277 if (stat == null)
278 {
279 stat = new UserLogonStats(username);
280 synchronized (sem)
281 {
282 users.put(username, stat);
283 }
284 }
285 boolean disabled = stat.failCheck(strikeCount, strikeInterval, strikeMax);
286
287 if (disabled)
288 {
289 try
290 {
291
292 JetspeedUser user = (JetspeedUser)JetspeedSecurity.getUser(username);
293 if (user != null)
294 {
295 user.setDisabled(true);
296 JetspeedSecurity.saveUser(user);
297 }
298 }
299 catch (Exception e)
300 {
301 logger.error("Could not disable user: " + username, e);
302 }
303 }
304 return disabled;
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 public boolean isDisableAccountCheckEnabled()
321 {
322 return autoLogonDisable;
323 }
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339 public void resetDisableAccountCheck(String username)
340 {
341
342 username = convertUserName(username);
343 UserLogonStats stat = (UserLogonStats)users.get(username);
344 if (stat == null)
345 {
346 stat = new UserLogonStats(username);
347 synchronized (sem)
348 {
349 users.put(username, stat);
350 }
351 }
352 stat.reset();
353 }
354
355
356
357
358
359
360
361
362 /***
363 * Helper to UserManagement.
364 * Retrieves a <code>JetspeedUser</code> given the primary principle username.
365 * The principal can be any valid Jetspeed Security Principal:
366 * <code>org.apache.jetspeed.om.security.UserNamePrincipal</code>
367 * <code>org.apache.jetspeed.om.security.UserIdPrincipal</code>
368 *
369 * The security service may optionally check the current user context
370 * to determine if the requestor has permission to perform this action.
371 *
372 * @param username The username principal.
373 * @return a <code>JetspeedUser</code> associated to the principal identity.
374 * @exception UserException when the security provider has a general failure retrieving a user.
375 * @exception UnknownUserException when the security provider cannot match
376 * the principal identity to a user.
377 * @exception InsufficientPrivilegeException when the requestor is denied due to insufficient privilege
378 */
379
380 public JetspeedUser getUser(String username)
381 throws JetspeedSecurityException
382 {
383 return JetspeedUserManagement.getUser(new UserNamePrincipal(username));
384 }
385
386
387 /***
388 * Helper to PortalAuthorization.
389 * Gets a <code>JetspeedUser</code> from rundata, authorize user to perform the secured action on
390 * the given <code>Portlet</code> resource. If the user does not have
391 * sufficient privilege to perform the action on the resource, the check returns false,
392 * otherwise when sufficient privilege is present, checkPermission returns true.
393 *
394 * @param rundata request that the user is taken from rundatas
395 * @param action the secured action to be performed on the resource by the user.
396 * @param portlet the portlet resource.
397 * @return boolean true if the user has sufficient privilege.
398 */
399 public boolean checkPermission(JetspeedRunData runData, String action, Portlet portlet)
400 {
401 return JetspeedPortalAccessController.checkPermission(runData.getJetspeedUser(),
402 portlet,
403 action);
404 }
405
406 /***
407 * Helper to PortalAuthorization.
408 * Gets a <code>JetspeedUser</code> from rundata, authorize user to perform the secured action on
409 * the given <code>Entry</code> resource. If the user does not have
410 * sufficient privilege to perform the action on the resource, the check returns false,
411 * otherwise when sufficient privilege is present, checkPermission returns true.
412 *
413 * @param rundata request that the user is taken from rundatas
414 * @param action the secured action to be performed on the resource by the user.
415 * @param entry the portal entry resource.
416 * @return boolean true if the user has sufficient privilege.
417 public boolean checkPermission(JetspeedRunData runData, String action, RegistryEntry entry)
418 {
419 return JetspeedPortalAccessController.checkPermission(runData.getJetspeedUser(),
420 entry,
421 action);
422 }
423 */
424
425
426
427
428
429
430
431
432
433 public boolean areActionsDisabledForAnon()
434 {
435 return actionsAnonDisable;
436 }
437
438
439
440
441
442
443
444
445
446 public boolean areActionsDisabledForAllUsers()
447 {
448 return actionsAllUsersDisable;
449 }
450
451
452
453
454
455
456
457
458 public String getAnonymousUserName()
459 {
460 return anonymousUser;
461 }
462
463
464
465
466
467
468 public List getAdminRoles()
469 {
470 List result = new ArrayList();
471 for (int i = 0; i < adminRoles.length; i++)
472 {
473 result.add(adminRoles[i]);
474 }
475
476 return result;
477 }
478
479 /***
480 * Returns true if user has administrative role
481 *
482 * @param user
483 * @return true if user has administrative role
484 */
485 public boolean hasAdminRole(User user)
486 {
487 String username = user.getUserName();
488 try
489 {
490 List adminRoles = getAdminRoles();
491 for (Iterator it = adminRoles.iterator(); it.hasNext();)
492 {
493 if (JetspeedSecurity.hasRole(username, (String)it.next()))
494 {
495 return true;
496 }
497 }
498 }
499 catch (Exception e)
500 {
501 logger.error(e);
502 }
503
504 return false;
505 }
506
507 }
508