001: /*
002: * UserHandler.java
003: *
004: * Created on June 22, 2004, 5:44 PM
005: */
006:
007: package org.manentia.kasai.user;
008:
009: import java.util.List;
010:
011: import org.apache.commons.lang.StringUtils;
012: import org.manentia.kasai.KasaiFacade;
013: import org.manentia.kasai.User;
014: import org.manentia.kasai.exceptions.AlreadyExistsException;
015: import org.manentia.kasai.exceptions.DataAccessException;
016: import org.manentia.kasai.exceptions.InvalidAttributesException;
017: import org.manentia.kasai.exceptions.InvalidPasswordException;
018: import org.manentia.kasai.exceptions.NotFoundException;
019: import org.manentia.kasai.exceptions.ServiceException;
020: import org.manentia.kasai.exceptions.ServiceNotAvailableException;
021: import org.manentia.kasai.exceptions.UserBlockedException;
022:
023: import com.manentia.commons.log.Log;
024: import com.manentia.commons.xml.XMLException;
025:
026: /**
027: *
028: * @author rzuasti
029: */
030: public class UserHandler {
031: private static UserHandler instance;
032:
033: private UserHandler() {
034: }
035:
036: public static synchronized UserHandler getInstance() {
037: if (instance == null) {
038: instance = new UserHandler();
039: }
040:
041: return instance;
042: }
043:
044: public void checkPassword(String login, String password)
045: throws DataAccessException, NotFoundException,
046: UserBlockedException, ServiceNotAvailableException,
047: ServiceException, InvalidPasswordException, XMLException {
048:
049: User user = null;
050: int result = User.AUTH_BAD_USERNAME;
051:
052: Log.write("Enter (login="
053: + StringUtils.defaultString(login, "<null>")
054: + ", password="
055: + ((password == null) ? "<null>" : "******") + ")",
056: Log.INFO, "checkPassword", UserHandler.class);
057:
058: user = this .read(login, true);
059:
060: if (user == null) {
061: Log.write("User doesn't exist", Log.WARN, "checkPassword",
062: UserHandler.class);
063:
064: throw new NotFoundException(KasaiFacade.class.getName()
065: + ".userPasswordInvalid");
066: }
067:
068: if (user.getBlocked()) {
069: Log.write("User " + login + " is blocked", Log.WARN,
070: "checkPassword", UserHandler.class);
071:
072: throw new UserBlockedException(KasaiFacade.class.getName()
073: + ".userLocked");
074: }
075:
076: result = user.checkPassword(password);
077:
078: if (result != User.AUTH_OK) {
079: Log.write("Wrong password", Log.WARN, "checkPassword",
080: UserHandler.class);
081:
082: throw new InvalidPasswordException(KasaiFacade.class
083: .getName()
084: + ".userPasswordInvalid");
085: }
086:
087: Log.write("Exit", Log.INFO, "checkPassword", UserHandler.class);
088: }
089:
090: public boolean checkOperative(String login, String operative,
091: String object) {
092: return UserDAOFactory.getInstance().createDAO().checkOperative(
093: login, operative, object);
094: }
095:
096: public void create(User user, String password)
097: throws InvalidAttributesException, AlreadyExistsException,
098: DataAccessException, ServiceNotAvailableException,
099: ServiceException, InvalidPasswordException, XMLException {
100:
101: UserDAOFactory.getInstance().createDAO().create(user);
102:
103: user.overridePassword(password);
104: }
105:
106: public void create(User user) throws InvalidAttributesException,
107: AlreadyExistsException, DataAccessException,
108: ServiceNotAvailableException, ServiceException,
109: XMLException {
110:
111: UserDAOFactory.getInstance().createDAO().create(user);
112:
113: // Assign the user a new password
114: user.resetPassword();
115: }
116:
117: public void delete(String login) throws DataAccessException {
118: UserDAOFactory.getInstance().createDAO().delete(login);
119: }
120:
121: public List list(String login, String firstName, String lastName,
122: String email, int blocked, String description, String group)
123: throws DataAccessException, XMLException {
124:
125: return UserDAOFactory.getInstance().createDAO()
126: .list(login, firstName, lastName, email, blocked,
127: description, group);
128: }
129:
130: public String[] listUsernames() throws DataAccessException {
131: return UserDAOFactory.getInstance().createDAO().listUsernames();
132: }
133:
134: public String[] listUsernames(String groupId)
135: throws DataAccessException {
136: return UserDAOFactory.getInstance().createDAO().listUsernames(
137: groupId);
138: }
139:
140: public User read(String login, boolean cache)
141: throws DataAccessException, XMLException {
142: return UserDAOFactory.getInstance().createDAO().read(login,
143: cache);
144: }
145:
146: public void update(User user) throws InvalidAttributesException,
147: DataAccessException, XMLException {
148:
149: UserDAOFactory.getInstance().createDAO().update(user);
150: }
151:
152: }
|