1 package org.manentia.kasai; 2 3 import java.io.StringReader ; 4 import java.sql.ResultSet ; 5 import java.sql.SQLException ; 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.ResourceBundle ; 9 import java.util.logging.Level ; 10 11 import javax.xml.parsers.DocumentBuilder ; 12 import javax.xml.parsers.DocumentBuilderFactory ; 13 14 import org.apache.commons.lang.StringUtils; 15 import org.apache.commons.lang.exception.ExceptionUtils; 16 import org.manentia.kasai.exceptions.InvalidPasswordException; 17 import org.manentia.kasai.util.Constants; 18 import org.manentia.kasai.exceptions.InvalidAttributesException; 19 import org.manentia.kasai.exceptions.ServiceException; 20 import org.manentia.kasai.exceptions.ServiceNotAvailableException; 21 import org.manentia.kasai.services.AuthService; 22 import org.manentia.kasai.services.AuthServiceFactory; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.xml.sax.InputSource ; 26 import com.koala.commons.log.Log; 27 28 29 34 public class User{ 35 36 public static final int AUTH_OK = AuthService.AUTH_OK; 37 public static final int AUTH_BAD_USERNAME = AuthService.AUTH_BAD_USERNAME; 38 public static final int AUTH_BAD_PASSWORD = AuthService.AUTH_BAD_PASSWORD; 39 public static final int STATUS_ACTIVE = 1; 40 public static final int STATUS_BLOCKED = 2; 41 42 private String login; 43 44 private String firstName; 45 46 private String lastName; 47 48 private String email; 49 50 private boolean blocked; 51 52 private String description; 53 54 private Document data; 55 56 private String password; 57 58 private Collection groups; 59 60 private Collection objectsUsersRoles; 61 62 private String dataStr; 63 64 private boolean superUser; 65 66 67 public User() { 68 groups = new ArrayList (); 69 objectsUsersRoles = new ArrayList (); 70 superUser = false; 71 } 72 73 public User (ResultSet rs) throws SQLException { 74 login = rs.getString("id"); 75 firstName = StringUtils.defaultString(rs.getString ("first_name")); 76 lastName = StringUtils.defaultString(rs.getString("last_name")); 77 email = StringUtils.defaultString(rs.getString("email")); 78 blocked = (rs.getInt("blocked")!=0); 79 description = StringUtils.defaultString(rs.getString("description")); 80 superUser = (rs.getInt("super_user")!=0); 81 groups = new ArrayList (); 82 objectsUsersRoles = new ArrayList (); 83 } 84 85 86 public int checkPassword(String password) 87 throws ServiceNotAvailableException, ServiceException { 88 ResourceBundle res = ResourceBundle.getBundle(Constants.PROPERTY_FILE); 89 90 AuthService authService = AuthServiceFactory.getAuthService(res.getString( 91 "auth.service")); 92 93 return authService.checkPassword(this.login, StringUtils.defaultString(password)); 94 } 95 96 public void changePassword(String oldPassword, String newPassword, String confirmation) 97 throws ServiceNotAvailableException, ServiceException, InvalidAttributesException, InvalidPasswordException { 98 ResourceBundle res = ResourceBundle.getBundle(Constants.PROPERTY_FILE); 99 100 AuthService authService = AuthServiceFactory.getAuthService(res.getString( 101 "auth.service")); 102 if ((newPassword != null) && (newPassword.equals (confirmation))){ 103 authService.changePassword(this.login, oldPassword, newPassword); 104 this.setPassword(newPassword); 105 } 106 else{ 107 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "changePassword", 108 "Password and confirmation password don't match", java.util.logging.Level.INFO); 109 throw new InvalidPasswordException(User.class.getName() + ".changePassword.passwordMisMatch"); 110 } 111 } 112 113 public void overridePassword(String newPassword) 114 throws ServiceNotAvailableException, ServiceException, InvalidAttributesException, InvalidPasswordException { 115 ResourceBundle res = ResourceBundle.getBundle(Constants.PROPERTY_FILE); 116 117 AuthService authService = AuthServiceFactory.getAuthService(res.getString( 118 "auth.service")); 119 120 authService.setPassword(this.login, StringUtils.defaultString(newPassword)); 121 this.setPassword(newPassword); 122 } 123 124 public String getLogin() { 125 return this.login; 126 } 127 128 public void setLogin(String login) { 129 this.login = login; 130 } 131 132 public String getFirstName() { 133 return this.firstName; 134 } 135 136 public void setFirstName(String firstName) { 137 this.firstName = firstName; 138 } 139 140 public String getLastName() { 141 return this.lastName; 142 } 143 144 public void setLastName(String lastName) { 145 this.lastName = lastName; 146 } 147 148 public String getEmail() { 149 return this.email; 150 } 151 152 public void setEmail(String email) { 153 this.email = email; 154 } 155 156 public boolean getBlocked() { 157 return this.blocked; 158 } 159 160 public void setBlocked(boolean blocked) { 161 this.blocked = blocked; 162 } 163 164 public String getDescription() { 165 return this.description; 166 } 167 168 public void setDescription(String description) { 169 this.description = description; 170 } 171 172 public String getPassword() { 173 return this.password; 174 } 175 176 public void setPassword(String password) { 177 this.password = password; 178 } 179 180 public void setValue(String key, String value) throws com.koala.commons.xml.exceptions.XMLException { 181 try { 182 loadData(); 183 this.data.getDocumentElement().setAttribute(key, value); 184 this.dataStr = this.data.getDocumentElement().toString(); 185 } catch (Exception e) { 186 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "setValue", 187 "Error modifing value (" + ExceptionUtils.getStackTrace(e) + 188 ")", java.util.logging.Level.SEVERE); 189 throw new com.koala.commons.xml.exceptions.XMLException(KasaiFacade.class.getName() + ".xmlError", e); 190 } 191 } 192 193 public String getValue(String key) throws com.koala.commons.xml.exceptions.XMLException { 194 String value = null; 195 196 try { 197 loadData(); 198 value = this.data.getDocumentElement().getAttribute(key); 199 } catch (Exception e) { 200 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "getValue", 201 "Error getting value (" + ExceptionUtils.getStackTrace(e) + 202 ")", java.util.logging.Level.SEVERE); 203 204 throw new com.koala.commons.xml.exceptions.XMLException(KasaiFacade.class.getName() + ".xmlError", e); 205 } 206 207 return value; 208 } 209 210 public Collection getGroups() { 211 return groups; 212 } 213 214 public void setGroups(Collection groups) { 215 this.groups = groups; 216 } 217 218 public Collection getObjectsUsersRoles(){ 219 return objectsUsersRoles; 220 } 221 222 public void setObjectsUsersRoles(Collection objectUserRole){ 223 this.objectsUsersRoles = objectUserRole; 224 } 225 226 public void addObjectUserRole(ObjectUserRole objectUserRole) { 227 if (objectUserRole != null) { 228 if(!objectsUsersRoles.contains(objectUserRole)){ 229 this.objectsUsersRoles.add(objectUserRole); 230 } 231 } 232 } 233 234 public void removeObjectUserRole (ObjectUserRole objectUserRole){ 235 if (objectUserRole != null){ 236 this.objectsUsersRoles.remove(objectUserRole); 237 } 238 } 239 240 public void addGroup(Group group) { 241 if (group != null) { 242 if(!groups.contains(group)){ 243 this.groups.add(group); 244 } 245 } 246 } 247 248 public void removeGroup (Group group){ 249 if (group != null){ 250 this.groups.remove(group); 251 } 252 } 253 254 public String getDataStr() { 255 return this.dataStr; 256 } 257 258 public void setDataStr(String dataStr) { 259 this.dataStr = dataStr; 260 } 261 262 private void loadData() throws com.koala.commons.xml.exceptions.XMLException { 263 DocumentBuilder docBuilder; 264 Element root; 265 266 try { 267 if ((this.dataStr == null) || (this.dataStr.length()<=0)) { 268 docBuilder = DocumentBuilderFactory.newInstance() 269 .newDocumentBuilder(); 270 data = docBuilder.newDocument(); 271 root = data.createElement("data"); 272 data.appendChild(root); 273 } else { 274 if (data == null) { 275 try { 276 docBuilder = DocumentBuilderFactory.newInstance() 277 .newDocumentBuilder(); 278 this.data = docBuilder.parse(new InputSource ( 279 new StringReader (this.dataStr))); 280 } catch (Exception e) { 281 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), 282 "loadData", 283 "Error parsing xml document (" + 284 ExceptionUtils.getStackTrace(e) + ")", 285 java.util.logging.Level.SEVERE); 286 287 throw new com.koala.commons.xml.exceptions.XMLException(KasaiFacade.class.getName() + ".xmlError", e); 288 } 289 } 290 } 291 } catch (Exception e) { 292 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "loadData", 293 "Error loading data (" + ExceptionUtils.getStackTrace(e) + 294 ")", java.util.logging.Level.SEVERE); 295 296 throw new com.koala.commons.xml.exceptions.XMLException(KasaiFacade.class.getName() + ".xmlError", e); 297 } 298 } 299 300 public String resetPassword() 301 throws ServiceNotAvailableException, ServiceException { 302 ResourceBundle res = ResourceBundle.getBundle(Constants.PROPERTY_FILE); 303 ResourceBundle messages = ResourceBundle.getBundle(Constants.MESSAGES); 304 305 AuthService authService = AuthServiceFactory.getAuthService(res.getString( 306 "auth.service")); 307 308 this.setPassword(authService.resetPassword(this.login)); 309 310 String msg= StringUtils.replace(messages.getString("user.resetPassword.mail.body"), "<NEW_PASSWORD>", this.getPassword()); 311 String subject = messages.getString("user.resetPassword.mail.subject"); 312 313 try{ 314 org.manentia.kasai.util.MailUtil.send (subject, msg, this.getEmail()); 315 }catch (Exception e){ 316 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "resetPassword", 317 "Password was modified successfully (user: " + this.getLogin() + ") but email could not be sent (" + 318 ExceptionUtils.getStackTrace(e) + ")", java.util.logging.Level.SEVERE); 319 } 320 return this.getPassword(); 321 } 322 323 public void validate() throws InvalidAttributesException{ 324 325 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(),"validate","Enter",Level.INFO); 326 327 if ((this.getLogin() == null) || (this.getLogin().length()==0)){ 328 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "validate","Login was not specified",Level.WARNING); 329 throw new InvalidAttributesException(User.class.getName() + ".emptyLogin"); 330 } 331 else if ((this.getEmail() == null) || (this.getEmail().length() < 5)){ 332 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(), "validate","Email was not specified",Level.WARNING); 333 throw new InvalidAttributesException(User.class.getName() + ".emptyEmail"); 334 } 335 336 Log.getInstance(Constants.PROPERTY_FILE).write(User.class.getName(),"validate","Exit",Level.INFO); 337 } 338 339 public boolean equals (java.lang.Object obj){ 340 boolean result = false; 341 342 try{ 343 if (obj instanceof User){ 344 if (((User)obj).getLogin().equals (this.login)){ 345 result = true; 346 } 347 } 348 } 349 catch (Exception e){ 350 result = false; 351 } 352 return result; 353 } 354 355 public boolean getSuperUser() { 356 return superUser; 357 } 358 359 public void setSuperUser(boolean superUser) { 360 this.superUser = superUser; 361 } 362 } 363 | Popular Tags |