01: package org.tigris.scarab.pipeline;
02:
03: import java.io.IOException;
04: import java.util.HashSet;
05: import java.util.Set;
06:
07: import org.apache.turbine.RunData;
08: import org.apache.turbine.TurbineException;
09: import org.apache.turbine.ValveContext;
10: import org.apache.turbine.pipeline.AbstractValve;
11: import org.tigris.scarab.om.ScarabUser;
12: import org.tigris.scarab.util.AnonymousUserUtil;
13: import org.tigris.scarab.util.Log;
14:
15: /*
16: * This valve will try to automatically login an Anonymous user if there is no user authenticated.
17: * The user and password will be set in scarab.user.anonymous and scarab.anonymous.password
18: * If scarab.anonymous.userid does not exists, the valve will just pass control to the following
19: * through the pipeline.
20: *
21: */
22: public class AnonymousLoginValve extends AbstractValve {
23: private final static Set nonAnonymousTargets = new HashSet();
24: private boolean anonymousAccessAllowed = false;
25:
26: /**
27: * Initilizes the templates that will not make an automatical
28: * anonymous login.
29: */
30: public void initialize() throws Exception {
31: anonymousAccessAllowed = AnonymousUserUtil
32: .anonymousAccessAllowed();
33: if (anonymousAccessAllowed) {
34: Log.get().info("anonymous Login enabled.");
35: //nonAnonymousTargets.add("Index.vm");
36: //nonAnonymousTargets.add("Logout.vm");
37: //nonAnonymousTargets.add(conf.getProperty("template.login"));
38: //nonAnonymousTargets.add(conf.getProperty("template.homepage"));
39: nonAnonymousTargets.add("Register.vm");
40: nonAnonymousTargets.add("ForgotPassword.vm");
41: } else {
42: Log.get().info("anonymous Login disabled.");
43: }
44: }
45:
46: /*
47: * Invoked by the Turbine's pipeline, as defined in scarab-pipeline.xml
48: * @see org.apache.turbine.pipeline.AbstractValve#invoke(org.apache.turbine.RunData, org.apache.turbine.ValveContext)
49: */
50: public void invoke(RunData data, ValveContext context)
51: throws IOException, TurbineException {
52: String target = data.getTarget();
53: if (anonymousAccessAllowed
54: && !nonAnonymousTargets.contains(target)
55: && target.indexOf("help,") == -1) {
56: // If there's no user, we will login as Anonymous.
57: ScarabUser user = (ScarabUser) data.getUserFromSession();
58: if (null == user || user.getUserId() == null
59: || !user.hasLoggedIn())
60: AnonymousUserUtil.anonymousLogin(data);
61: }
62: context.invokeNext(data);
63: }
64:
65: }
|