KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > AnonymousUserUtil


1 /*
2  * Created on 03.01.2005
3  *
4  * To change the template for this generated file go to
5  * Window - Preferences - Java - Code Generation - Code and Comments
6  */

7 package org.tigris.scarab.util;
8
9 import org.apache.fulcrum.security.TurbineSecurity;
10 import org.apache.fulcrum.security.entity.User;
11 import org.apache.fulcrum.security.util.DataBackendException;
12 import org.apache.fulcrum.security.util.UnknownEntityException;
13 import org.apache.turbine.RunData;
14 import org.apache.turbine.Turbine;
15 import org.tigris.scarab.om.ScarabUser;
16
17 /**
18  * @author hdab
19  *
20  * To change the template for this generated type comment go to
21  * Window - Preferences - Java - Code Generation - Code and Comments
22  */

23 public class AnonymousUserUtil
24 {
25
26     /**
27      * Returns true if the user is the one set in scarab.anonymous.username, and
28      * false otherwise.
29      * Note: If anonymous access is denied per configuration, this method
30      * always returns false!
31      * @return
32      */

33     public static boolean isUserAnonymous(ScarabUser user)
34     {
35         boolean brdo = false;
36         if(anonymousAccessAllowed())
37         {
38             String JavaDoc anonymous = getAnonymousUserId();
39             if (anonymous != null && user.getUserName().equals(anonymous))
40             {
41                 brdo = true;
42             }
43         }
44         return brdo;
45     }
46
47     /**
48      * Returns true, when anonymous user access is explicitly allowed,.
49      * Otherwise returns false.
50      * @return
51      */

52     public static boolean anonymousAccessAllowed()
53     {
54         boolean allowed = Turbine.getConfiguration().getBoolean("scarab.anonymous.enable", false);
55         return allowed;
56     }
57     
58     /**
59      * Returns the userid of the anonymous user
60      * Note: This method returns the anonymous userid
61      * independent frm wether anonymous access is allowed or not.
62      * @return
63      */

64     public static String JavaDoc getAnonymousUserId()
65     {
66         String JavaDoc anonymous = Turbine.getConfiguration().getString("scarab.anonymous.username", null);
67         return anonymous;
68     }
69     
70     /**
71      * Return an instanceof the Anonymous User.
72      * If Anonymous user has been switched off, this method
73      * returns a Turbine-anonymous user.
74      * @return
75      * @throws DataBackendException
76      * @throws UnknownEntityException
77      */

78     public static User getAnonymousUser() throws DataBackendException, UnknownEntityException
79     {
80         User user;
81         if(anonymousAccessAllowed())
82         {
83             String JavaDoc userid = getAnonymousUserId();
84             try
85             {
86                 user = TurbineSecurity.getUser(userid);
87             }
88             catch (UnknownEntityException uee)
89             {
90                 Log.get().error("anonymous user does not exist: [" + userid + "]");
91                 Log.get().error("reported error was: ["+uee.getMessage() + "]");
92                 Log.get().warn("anonymous login temporarily disabled.");
93                 user = TurbineSecurity.getAnonymousUser();
94             }
95         }
96         else
97         {
98             user = TurbineSecurity.getAnonymousUser();
99         }
100         return user;
101     }
102     
103     /**
104      * Login the Anonymous user and prepare the run data
105      * @param data
106      */

107     public static void anonymousLogin(RunData data)
108     {
109         try
110         {
111             User user = AnonymousUserUtil.getAnonymousUser();
112             data.setUser(user);
113             if( null == user || user.getUserName() == null || user.getUserName().equals(""))
114             {
115                 user.setHasLoggedIn(Boolean.FALSE);
116             }
117             else
118             {
119                 user.setHasLoggedIn(Boolean.TRUE);
120                 user.updateLastLogin();
121             }
122             data.save();
123         }
124         catch (Exception JavaDoc e)
125         {
126             Log.get().error("anonymousLogin failed to login anonymously: " + e.getMessage());
127         }
128         
129     }
130
131 }
132
Popular Tags