001: /*
002: * Created on 03.01.2005
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.tigris.scarab.util;
008:
009: import org.apache.fulcrum.security.TurbineSecurity;
010: import org.apache.fulcrum.security.entity.User;
011: import org.apache.fulcrum.security.util.DataBackendException;
012: import org.apache.fulcrum.security.util.UnknownEntityException;
013: import org.apache.turbine.RunData;
014: import org.apache.turbine.Turbine;
015: import org.tigris.scarab.om.ScarabUser;
016:
017: /**
018: * @author hdab
019: *
020: * To change the template for this generated type comment go to
021: * Window - Preferences - Java - Code Generation - Code and Comments
022: */
023: public class AnonymousUserUtil {
024:
025: /**
026: * Returns true if the user is the one set in scarab.anonymous.username, and
027: * false otherwise.
028: * Note: If anonymous access is denied per configuration, this method
029: * always returns false!
030: * @return
031: */
032: public static boolean isUserAnonymous(ScarabUser user) {
033: boolean brdo = false;
034: if (anonymousAccessAllowed()) {
035: String anonymous = getAnonymousUserId();
036: if (anonymous != null
037: && user.getUserName().equals(anonymous)) {
038: brdo = true;
039: }
040: }
041: return brdo;
042: }
043:
044: /**
045: * Returns true, when anonymous user access is explicitly allowed,.
046: * Otherwise returns false.
047: * @return
048: */
049: public static boolean anonymousAccessAllowed() {
050: boolean allowed = Turbine.getConfiguration().getBoolean(
051: "scarab.anonymous.enable", false);
052: return allowed;
053: }
054:
055: /**
056: * Returns the userid of the anonymous user
057: * Note: This method returns the anonymous userid
058: * independent from wether anonymous access is allowed or not.
059: * @return
060: */
061: public static String getAnonymousUserId() {
062: String anonymous = Turbine.getConfiguration().getString(
063: "scarab.anonymous.username", null);
064: return anonymous;
065: }
066:
067: /**
068: * Returns the name of the anonymous rolename
069: * Note: This method returns the anonymous userid
070: * independent from wether anonymous access is allowed or not.
071: * @return
072: */
073: public static String getAnonymousRolename() {
074: String anonymous = Turbine.getConfiguration().getString(
075: "scarab.anonymous.rolename", null);
076: return anonymous;
077: }
078:
079: /**
080: * Return an instanceof the Anonymous User.
081: * If Anonymous user has been switched off, this method
082: * returns a Turbine-anonymous user.
083: * @return
084: * @throws DataBackendException
085: * @throws UnknownEntityException
086: */
087: public static User getAnonymousUser() throws DataBackendException,
088: UnknownEntityException {
089: User user;
090: if (anonymousAccessAllowed()) {
091: String userid = getAnonymousUserId();
092: try {
093: user = TurbineSecurity.getUser(userid);
094: } catch (UnknownEntityException uee) {
095: Log.get().error(
096: "anonymous user does not exist: [" + userid
097: + "]");
098: Log.get().error(
099: "reported error was: [" + uee.getMessage()
100: + "]");
101: Log.get().warn("anonymous login temporarily disabled.");
102: user = TurbineSecurity.getAnonymousUser();
103: }
104: } else {
105: user = TurbineSecurity.getAnonymousUser();
106: }
107: return user;
108: }
109:
110: /**
111: * Login the Anonymous user and prepare the run data
112: * @param data
113: */
114: public static void anonymousLogin(RunData data) {
115: try {
116: User user;
117: user = AnonymousUserUtil.getAnonymousUser();
118: userLogin(data, user);
119: } catch (DataBackendException e) {
120: Log.get().error("anonymousLogin: " + e);
121: } catch (UnknownEntityException e) {
122: Log.get().error("anonymousLogin: " + e);
123: }
124: }
125:
126: /**
127: * Login a given user into the session updating the rundata.
128: * @param data
129: * @param user
130: */
131: public static void userLogin(RunData data, User user) {
132: data.setUser(user);
133: if (null == user || user.getUserName() == null
134: || user.getUserName().equals("")) {
135: user.setHasLoggedIn(Boolean.FALSE);
136: } else {
137: user.setHasLoggedIn(Boolean.TRUE);
138: try {
139: user.updateLastLogin();
140: } catch (Exception e) {
141: Log.get().error(
142: "userLogin: Error updating last login: " + e);
143: }
144: }
145: data.save();
146:
147: }
148:
149: }
|