001: package org.manentia.kasai;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.io.StringReader;
006: import java.sql.ResultSet;
007: import java.sql.SQLException;
008: import java.util.ArrayList;
009: import java.util.Collection;
010: import java.util.HashMap;
011: import java.util.Map;
012: import java.util.ResourceBundle;
013: import java.util.StringTokenizer;
014:
015: import javax.xml.parsers.DocumentBuilder;
016: import javax.xml.parsers.DocumentBuilderFactory;
017: import javax.xml.parsers.FactoryConfigurationError;
018: import javax.xml.parsers.ParserConfigurationException;
019:
020: import org.apache.commons.lang.StringUtils;
021: import org.manentia.kasai.exceptions.InvalidAttributesException;
022: import org.manentia.kasai.exceptions.InvalidPasswordException;
023: import org.manentia.kasai.exceptions.ServiceException;
024: import org.manentia.kasai.exceptions.ServiceNotAvailableException;
025: import org.manentia.kasai.services.AuthService;
026: import org.manentia.kasai.services.AuthServiceFactory;
027: import org.manentia.kasai.user.passwordvalidators.PasswordValidator;
028: import org.manentia.kasai.user.passwordvalidators.PasswordValidatorFactory;
029: import org.manentia.kasai.util.MiscUtils;
030: import org.w3c.dom.Document;
031: import org.w3c.dom.Element;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.SAXException;
034:
035: import com.manentia.commons.log.Log;
036: import com.manentia.commons.xml.XMLException;
037:
038: /**
039: *
040: * @author fpena
041: *
042: */
043: public class User implements Serializable {
044:
045: /**
046: *
047: */
048: private static final long serialVersionUID = -2559503917687585951L;
049:
050: public static final int AUTH_OK = AuthService.AUTH_OK;
051: public static final int AUTH_BAD_USERNAME = AuthService.AUTH_BAD_USERNAME;
052: public static final int AUTH_BAD_PASSWORD = AuthService.AUTH_BAD_PASSWORD;
053: public static final int STATUS_ACTIVE = 1;
054: public static final int STATUS_BLOCKED = 2;
055:
056: private String login;
057:
058: private String firstName;
059:
060: private String lastName;
061:
062: private String email;
063:
064: private boolean blocked;
065:
066: private String description;
067:
068: private Map attributes;
069:
070: private String password;
071:
072: private Collection groups;
073:
074: private Collection objectsUsersRoles;
075:
076: private boolean super User;
077:
078: public User() {
079: groups = new ArrayList();
080: objectsUsersRoles = new ArrayList();
081: super User = false;
082: attributes = new HashMap();
083: }
084:
085: public User(ResultSet rs) throws SQLException, SAXException,
086: IOException, ParserConfigurationException,
087: FactoryConfigurationError {
088: login = rs.getString("id");
089: firstName = StringUtils.defaultString(rs
090: .getString("first_name"));
091: lastName = StringUtils.defaultString(rs.getString("last_name"));
092: email = StringUtils.defaultString(rs.getString("email"));
093: blocked = (rs.getInt("blocked") != 0);
094: description = StringUtils.defaultString(rs
095: .getString("description"));
096: attributes = MiscUtils.parseXMLMap(rs.getString("data"));
097: super User = (rs.getInt("super_user") != 0);
098: groups = new ArrayList();
099: objectsUsersRoles = new ArrayList();
100: }
101:
102: public int checkPassword(String password)
103: throws ServiceNotAvailableException, ServiceException {
104: ResourceBundle res = ResourceBundle
105: .getBundle(Constants.CONFIG_PROPERTY_FILE);
106:
107: AuthService authService = AuthServiceFactory.getAuthService(res
108: .getString("auth.service"));
109:
110: return authService.checkPassword(this .login, StringUtils
111: .defaultString(password));
112: }
113:
114: public void changePassword(String oldPassword, String newPassword,
115: String confirmation) throws ServiceNotAvailableException,
116: ServiceException, InvalidAttributesException,
117: InvalidPasswordException {
118: ResourceBundle res = ResourceBundle
119: .getBundle(Constants.CONFIG_PROPERTY_FILE);
120:
121: AuthService authService = AuthServiceFactory.getAuthService(res
122: .getString("auth.service"));
123: if ((newPassword != null) && (newPassword.equals(confirmation))) {
124:
125: executePasswordValidators(newPassword);
126: authService.changePassword(this .login, oldPassword,
127: newPassword);
128: this .setPassword(newPassword);
129: } else {
130: Log.write("Password and confirmation password don't match",
131: Log.INFO, "changePassword", User.class);
132:
133: throw new InvalidPasswordException(User.class.getName()
134: + ".changePassword.passwordMisMatch");
135: }
136: }
137:
138: private void executePasswordValidators(String password)
139: throws ServiceNotAvailableException,
140: InvalidPasswordException {
141: ResourceBundle res = ResourceBundle
142: .getBundle(Constants.CONFIG_PROPERTY_FILE);
143:
144: StringTokenizer validatorsList = new StringTokenizer(res
145: .getString("passwords.validators"), ",");
146: String validatorClassName = null;
147: PasswordValidator validator = null;
148:
149: while (validatorsList.hasMoreTokens()) {
150: validatorClassName = validatorsList.nextToken();
151:
152: validator = PasswordValidatorFactory
153: .getPasswordValidator(validatorClassName);
154:
155: if (!validator.validate(password)) {
156: throw new InvalidPasswordException(validatorClassName
157: + ".message");
158: }
159: }
160: }
161:
162: public void overridePassword(String newPassword)
163: throws ServiceNotAvailableException, ServiceException,
164: InvalidAttributesException, InvalidPasswordException {
165: ResourceBundle res = ResourceBundle
166: .getBundle(Constants.CONFIG_PROPERTY_FILE);
167:
168: AuthService authService = AuthServiceFactory.getAuthService(res
169: .getString("auth.service"));
170:
171: authService.setPassword(this .login, StringUtils
172: .defaultString(newPassword));
173: this .setPassword(newPassword);
174: }
175:
176: public String getLogin() {
177: return this .login;
178: }
179:
180: public void setLogin(String login) {
181: this .login = login;
182: }
183:
184: public String getFirstName() {
185: return this .firstName;
186: }
187:
188: public void setFirstName(String firstName) {
189: this .firstName = firstName;
190: }
191:
192: public String getLastName() {
193: return this .lastName;
194: }
195:
196: public void setLastName(String lastName) {
197: this .lastName = lastName;
198: }
199:
200: public String getFullName() {
201: String result = "";
202: if (StringUtils.isEmpty(this .lastName)) {
203: if (StringUtils.isNotEmpty(this .firstName)) {
204: result = this .firstName + " ";
205: }
206: } else {
207: if (StringUtils.isEmpty(this .firstName)) {
208: result = this .lastName + " ";
209: } else {
210: result = this .lastName + ", " + this .firstName + " ";
211: }
212: }
213: return result;
214: }
215:
216: public String getFullNameWithLogin() {
217: return getFullName() + "(" + getLogin() + ")";
218: }
219:
220: public String getEmail() {
221: return this .email;
222: }
223:
224: public void setEmail(String email) {
225: this .email = email;
226: }
227:
228: public boolean getBlocked() {
229: return this .blocked;
230: }
231:
232: public void setBlocked(boolean blocked) {
233: this .blocked = blocked;
234: }
235:
236: public String getDescription() {
237: return this .description;
238: }
239:
240: public void setDescription(String description) {
241: this .description = description;
242: }
243:
244: public String getDescriptionPrefix() {
245: String result = StringUtils.defaultString(description);
246:
247: if (result.length() > 60) {
248: result = result.substring(0, 57) + "...";
249: }
250:
251: return result;
252: }
253:
254: public String getPassword() {
255: return this .password;
256: }
257:
258: public void setPassword(String password) {
259: this .password = password;
260: }
261:
262: public void setAttribute(String key, String value) {
263: this .attributes.put(key, value);
264: }
265:
266: public String getAttribute(String key)
267: throws com.manentia.commons.xml.XMLException {
268: return (String) this .attributes.get(key);
269: }
270:
271: public String getAttributesXML()
272: throws ParserConfigurationException,
273: FactoryConfigurationError {
274: return MiscUtils.serializeMapToXML(attributes);
275: }
276:
277: public Collection getGroups() {
278: return groups;
279: }
280:
281: public void setGroups(Collection groups) {
282: this .groups = groups;
283: }
284:
285: public Collection getObjectsUsersRoles() {
286: return objectsUsersRoles;
287: }
288:
289: public void setObjectsUsersRoles(Collection objectUserRole) {
290: this .objectsUsersRoles = objectUserRole;
291: }
292:
293: public void addObjectUserRole(ObjectUserRole objectUserRole) {
294: if (objectUserRole != null) {
295: if (!objectsUsersRoles.contains(objectUserRole)) {
296: this .objectsUsersRoles.add(objectUserRole);
297: }
298: }
299: }
300:
301: public void removeObjectUserRole(ObjectUserRole objectUserRole) {
302: if (objectUserRole != null) {
303: this .objectsUsersRoles.remove(objectUserRole);
304: }
305: }
306:
307: public void addGroup(Group group) {
308: if (group != null) {
309: if (!groups.contains(group)) {
310: this .groups.add(group);
311: }
312: }
313: }
314:
315: public void removeGroup(Group group) {
316: if (group != null) {
317: this .groups.remove(group);
318: }
319: }
320:
321: public String resetPassword() throws ServiceNotAvailableException,
322: ServiceException {
323: ResourceBundle res = ResourceBundle
324: .getBundle(Constants.CONFIG_PROPERTY_FILE);
325: ResourceBundle messages = ResourceBundle
326: .getBundle(Constants.MESSAGES_PROPERTY_FILE);
327:
328: AuthService authService = AuthServiceFactory.getAuthService(res
329: .getString("auth.service"));
330:
331: this .setPassword(authService.resetPassword(this .login));
332:
333: String msg = StringUtils.replace(messages
334: .getString("user.resetPassword.mail.body"),
335: "<NEW_PASSWORD>", this .getPassword());
336: String subject = messages
337: .getString("user.resetPassword.mail.subject");
338:
339: try {
340: org.manentia.kasai.util.MailUtil.send(subject, msg, this
341: .getEmail());
342: } catch (Exception e) {
343: Log.write(
344: "Password was modified successfully (user: "
345: + this .getLogin()
346: + ") but email could not be sent", e,
347: Log.ERROR, "resetPassword", User.class);
348: }
349: return this .getPassword();
350: }
351:
352: public void validate() throws InvalidAttributesException {
353: Log.write("Enter", Log.INFO, "validate", User.class);
354:
355: if ((this .getLogin() == null)
356: || (this .getLogin().length() == 0)) {
357: Log.write("Login was not specified", Log.WARN, "validate",
358: User.class);
359:
360: throw new InvalidAttributesException(User.class.getName()
361: + ".emptyLogin");
362: } else if ((this .getEmail() == null)
363: || (this .getEmail().length() < 5)) {
364: Log.write("Email was not specified", Log.WARN, "validate",
365: User.class);
366:
367: throw new InvalidAttributesException(User.class.getName()
368: + ".emptyEmail");
369: }
370:
371: Log.write("Exit", Log.INFO, "validate", User.class);
372: }
373:
374: public boolean equals(java.lang.Object obj) {
375: boolean result = false;
376:
377: try {
378: if (obj instanceof User) {
379: if (((User) obj).getLogin().equals(this .login)) {
380: result = true;
381: }
382: }
383: } catch (Exception e) {
384: result = false;
385: }
386: return result;
387: }
388:
389: public boolean getSuperUser() {
390: return super User;
391: }
392:
393: public void setSuperUser(boolean super User) {
394: this .super User = super User;
395: }
396:
397: public String getObjectId() {
398: return "/kasai/user/" + this.getLogin();
399: }
400: }
|