1 25 package org.ofbiz.guiapp.xui; 26 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.GeneralException; 32 import org.ofbiz.base.util.UtilMisc; 33 import org.ofbiz.base.util.UtilValidate; 34 import org.ofbiz.webapp.control.LoginWorker; 35 import org.ofbiz.entity.GenericDelegator; 36 import org.ofbiz.entity.GenericEntityException; 37 import org.ofbiz.entity.GenericValue; 38 import org.ofbiz.service.GenericServiceException; 39 import org.ofbiz.service.LocalDispatcher; 40 import org.ofbiz.service.ServiceUtil; 41 42 48 public class XuiSession { 49 50 public static final String module = XuiSession.class.getName(); 51 52 protected GenericDelegator delegator = null; 53 protected LocalDispatcher dispatcher = null; 54 protected GenericValue userLogin = null; 55 protected XuiContainer container = null; 56 protected Map attributes = new HashMap (); 57 protected String id = null; 58 59 60 public XuiSession(String id, GenericDelegator delegator, LocalDispatcher dispatcher, XuiContainer container) { 61 this.id = id; 62 this.delegator = delegator; 63 this.dispatcher = dispatcher; 64 this.container = container; 65 Debug.logInfo("Created XuiSession [" + id + "]", module); 66 } 67 68 public XuiContainer getContainer() { 69 return this.container; 70 } 71 72 public GenericDelegator getDelegator() { 73 return this.delegator; 74 } 75 76 public LocalDispatcher getDispatcher() { 77 return this.dispatcher; 78 } 79 80 public GenericValue getUserLogin() { 81 return this.userLogin; 82 } 83 84 public void setAttribute(String name, Object value) { 85 this.attributes.put(name, value); 86 } 87 88 public Object getAttribute(String name) { 89 return this.attributes.get(name); 90 } 91 92 public String getId() { 93 return this.id; 94 } 95 96 public String getUserId() { 97 if (this.userLogin == null) { 98 return null; 99 } else { 100 return this.userLogin.getString("userLoginId"); 101 } 102 } 103 104 public void logout() { 105 if (this.userLogin != null) { 106 LoginWorker.setLoggedOut(this.userLogin.getString("userLoginId"), this.getDelegator()); 107 this.userLogin = null; 108 } 109 } 110 111 public void login(String username, String password) throws UserLoginFailure { 112 if (this.userLogin != null) { 114 if (!userLogin.getString("userLoginId").equals(username)) { 115 throw new UserLoginFailure("Username does not match already logged in user!"); 116 } 117 } 118 this.userLogin = this.checkLogin(username, password); 119 } 120 121 public GenericValue checkLogin(String username, String password) throws UserLoginFailure { 122 if (dispatcher == null) { 124 throw new UserLoginFailure("Unable to log in; XUI not configured propertly"); 125 } 126 if (UtilValidate.isEmpty(username)) { 127 throw new UserLoginFailure("Username is missing"); 128 } 129 if (UtilValidate.isEmpty(password)) { 130 throw new UserLoginFailure("Password is missing"); 131 } 132 133 Map result = null; 135 try { 136 result = dispatcher.runSync("userLogin", UtilMisc.toMap("login.username", username, "login.password", password)); 137 } catch (GenericServiceException e) { 138 Debug.logError(e, module); 139 throw new UserLoginFailure(e); 140 } catch (Throwable t) { 141 Debug.logError(t, "Thowable caught!", module); 142 } 143 144 if (ServiceUtil.isError(result)) { 146 throw new UserLoginFailure(ServiceUtil.getErrorMessage(result)); 147 } else { 148 GenericValue ul = (GenericValue) result.get("userLogin"); 149 if (ul == null) { 150 throw new UserLoginFailure("UserLogin return was not valid (null)"); 151 } 152 return ul; 153 } 154 } 155 156 public boolean hasRole(GenericValue userLogin, String roleTypeId) { 157 if (userLogin == null || roleTypeId == null) { 158 return false; 159 } 160 String partyId = userLogin.getString("partyId"); 161 GenericValue partyRole = null; 162 try { 163 partyRole = delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap("partyId", partyId, "roleTypeId", roleTypeId)); 164 } catch (GenericEntityException e) { 165 Debug.logError(e, module); 166 return false; 167 } 168 169 if (partyRole == null) { 170 return false; 171 } 172 173 return true; 174 } 175 176 public class UserLoginFailure extends GeneralException { 177 public UserLoginFailure() { 178 super(); 179 } 180 181 public UserLoginFailure(String str) { 182 super(str); 183 } 184 185 public UserLoginFailure(String str, Throwable nested) { 186 super(str, nested); 187 } 188 189 public UserLoginFailure(Throwable nested) { 190 super(nested); 191 } 192 } 193 } 194 | Popular Tags |