1 20 21 package golfShop.business.user; 22 23 import java.io.*; 24 import java.net.*; 25 import java.security.*; 26 import java.util.*; 27 import com.lutris.appserver.server.user.*; 28 import com.lutris.appserver.server.session.*; 29 import com.lutris.util.*; 30 import golfShop.data.user.UserDOImpl; 31 32 import golfShop.business.item.ItemQuery; 33 import golfShop.business.cart.CartImpl; 34 import golfShop.spec.user.*; 35 import golfShop.spec.LoginException; 36 43 public class GolfShopUserManagerImpl implements GolfShopUserManager { 44 47 public final int AUTH_OK = 0; 48 49 59 public final int AUTH_FAILED = 1; 60 61 67 public final int UNKNOWN_ERROR = 2; 68 69 74 public final int IO_ERROR = 3; 75 76 81 public final int PERMISSION_DENIED = 4; 82 83 87 public final int MULTIPLE_LOGIN = 5; 88 89 93 public final int ACCOUNT_DISABLED = 6; 94 95 100 public final int PASSWORD_TYPO = 7; 101 102 109 public final int PASSWORD_WEAK = 8; 110 111 116 public final int UNKNOWN_HOST = 9; 117 118 122 public final int USERNAME_ALREADY_EXISTS = 10; 123 124 128 129 130 140 public int authenticate(String username, String password) { 141 UserDO theUser = UserDOImpl.lookupUser(username); 143 if (theUser == null) { 144 return AUTH_FAILED; 146 } 147 return theUser.authenticate(password); 149 } 150 151 164 public void login(String username, String password, Session session) 165 throws LoginException, SessionException { 166 Session standardSession = (Session)session; 167 SessionManager standardSessionManager = 168 (SessionManager)session.getSessionManager(); 169 SessionData sd = standardSession.getSessionData(); 170 UserDO theUser = UserDOImpl.lookupUser(username); 173 if (theUser == null) 174 refuse(AUTH_FAILED); 176 if (theUser.authenticate(password) != AUTH_OK) 178 refuse(AUTH_FAILED); 179 180 194 if (standardSession.getUser() != null) { 195 standardSession.clearUser(); 196 try { 197 sd.remove("cart"); } catch (KeywordValueException e) { 199 throw new FatalExceptionError(e); 200 } 201 } 202 203 212 standardSession.setUser((UserDOImpl)theUser); 213 214 218 try { 219 sd.set("cart", new CartImpl(ItemQuery.global)); 220 } catch (KeywordValueException e) { 221 throw new LoginException(UNKNOWN_ERROR); 222 } 223 } 224 225 229 public void logout(Session session) throws SessionException { 230 if (session != null) { 231 232 try{ 233 SessionData sessionData=session.getSessionData(); 234 String [] sessinDataKeys=sessionData.leafKeys(); 235 for (int i=0;i<sessinDataKeys.length;i++){ 236 sessionData.remove(sessinDataKeys[i]); 237 } 238 }catch (Exception e){ 239 throw new SessionException(e.getMessage()); 240 } 241 242 } 244 } 245 246 251 private static final void refuse(int why) throws LoginException { 252 throw new LoginException(why); 253 } 254 255 256 257 266 public void createAccount(String username, 267 String password, 268 String address1, 269 String address2, 270 String city, 271 String state, 272 String zip, 273 String creditCard, 274 String email, 275 Session session) throws LoginException, SessionException { 276 277 UserDO theUser = UserDOImpl.lookupUser(username); 280 if (theUser != null) { 281 refuse(USERNAME_ALREADY_EXISTS); 283 } 284 UserDO newUser = UserDOImpl.createUser(username, 286 password, address1, address2, city, 287 state, zip, creditCard, email); 288 if (newUser == null) 289 refuse(UNKNOWN_ERROR); 290 login(username, password, session); 292 } 293 294 295 300 public User getUser(String username) { 301 return (User) UserDOImpl.lookupUser(username); 302 } 303 } 304 305 | Popular Tags |