package org.bigk.invoices.services;
import java.security.NoSuchAlgorithmException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bigk.invoices.dao.UsersDAO;
import org.bigk.invoices.exceptions.DBAccessException;
import org.bigk.invoices.exceptions.ServiceException;
import org.bigk.invoices.model.User;
import org.bigk.invoices.utils.DigestUtils;
public class LoginServiceImpl implements LoginService {
/**
* Logger for this class
*/
private static final Log logger = LogFactory.getLog(LoginServiceImpl.class);
private UsersDAO usersDAO;
public LoginServiceImpl() {
}
public User processLogin(String login, String password)
throws ServiceException {
if (logger.isDebugEnabled()) {
logger.debug("processLogin(String login=" + login + ", String password=xxx) - start");
}
User user = null;
try {
user = usersDAO.getUser4Login(login);
} catch (DBAccessException ex) {
logger.error("processLogin(String, String)", ex);
throw new ServiceException(ex);
}
if (user == null) {
throw new ServiceException("No user found for username",
"services.LoginServiceImpl.processLogin.no_user",
new Object[] {login}
);
}
String passSHA1 = null;
try {
passSHA1 = DigestUtils.digestSHA1(password);
} catch (NoSuchAlgorithmException e) {
logger.error("processLogin(String, String)", e);
throw new ServiceException("NoSuchAlgorithmException", e,
"services.LoginServiceImpl.processLogin.algoritm_problem",
new Object[] {e.getMessage()}
);
}
if (logger.isDebugEnabled()) {
logger.debug("processLogin(String, String) - passSHA1=" + passSHA1);
logger.debug("processLogin(String, String) - user.getPassword()=" + user.getPassword());
}
if (!passSHA1.equalsIgnoreCase(user.getPassword())) {
throw new ServiceException("Wrong password",
"services.LoginServiceImpl.processLogin.wrong_password",
null
);
}
// all ok - user logged in corretly
if (logger.isDebugEnabled()) {
logger.debug("processLogin(String, String) - end - return value=" + user);
}
return user;
}
public int processLogout() throws ServiceException {
return 0;
}
public UsersDAO getUsersDAO() {
return usersDAO;
}
public void setUsersDAO(UsersDAO usersDAO) {
this.usersDAO = usersDAO;
}
}