1 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 23 public class AnonymousUserUtil 24 { 25 26 33 public static boolean isUserAnonymous(ScarabUser user) 34 { 35 boolean brdo = false; 36 if(anonymousAccessAllowed()) 37 { 38 String anonymous = getAnonymousUserId(); 39 if (anonymous != null && user.getUserName().equals(anonymous)) 40 { 41 brdo = true; 42 } 43 } 44 return brdo; 45 } 46 47 52 public static boolean anonymousAccessAllowed() 53 { 54 boolean allowed = Turbine.getConfiguration().getBoolean("scarab.anonymous.enable", false); 55 return allowed; 56 } 57 58 64 public static String getAnonymousUserId() 65 { 66 String anonymous = Turbine.getConfiguration().getString("scarab.anonymous.username", null); 67 return anonymous; 68 } 69 70 78 public static User getAnonymousUser() throws DataBackendException, UnknownEntityException 79 { 80 User user; 81 if(anonymousAccessAllowed()) 82 { 83 String 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 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 e) 125 { 126 Log.get().error("anonymousLogin failed to login anonymously: " + e.getMessage()); 127 } 128 129 } 130 131 } 132 | Popular Tags |