package eu.smartlm.abs.portal.security.login.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import eu.smartlm.abs.portal.security.login.dao.UserDao; import eu.smartlm.abs.portal.security.login.model.User; /** * This is the default implementation of the LoginService interface to access to all the users information * @author David García Pérez - CESGA * */ @Service(value="myLoginService") public class LoginServiceImpl implements LoginService { @Autowired @Qualifier("userDao") private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public LoginServiceImpl() { } /** * If the user has valid credentials it gives back a true value * @param user User credentials * @return true if the User credentials are valid, false otherwise */ public boolean isValidLogin(User user) { User userFromDatabase = userDao.getUser(user.getUsername()); if(userFromDatabase != null ) return true; else return false; } /** * Gets a list of all users that can log in into the server * @return a list of all users */ public List getUsers() { return userDao.getUsers(); } /** * Adds a user to the database * @param user user to be added * @return code if a user was successfully added or the error message */ public int addUser(User user) { if(user.getUsername() == null) { return LoginService.WRONG_USERNAME; } else if(user.getUsername().isEmpty()) { return LoginService.WRONG_USERNAME; } else if(user.getPassword() == null) { return LoginService.WRONG_PASSWORD; } else if(user.getPassword().isEmpty()) { return LoginService.WRONG_PASSWORD; } else if(isValidLogin(user)) { return LoginService.USER_ALREADY_EXISTS; } else { int code = userDao.addUser(user); if(code == UserDao.USER_ADDED) return LoginService.USER_SUCCESSFULLY_ADDED; else return 0; } } /** * Gets user by username * @param username * @return */ public User getUser(String username) { return userDao.getUser(username); } /** * Edits the data of an specific user * @param userXedited username of the user to be edit * @return code if a user was succesfully edited or the error message */ public int editUser(User user) { if(user.getPassword() == null) { return LoginService.WRONG_PASSWORD; } else if(user.getPassword().isEmpty()) { return LoginService.WRONG_PASSWORD; } else { userDao.editUser(user); return LoginService.USER_SUCCESSFULLY_EDITED; } } /** * Removes a user vi its username * @param username */ public void removeUser(String username) { if(userDao.getUser(username) != null) userDao.removeUser(username); } }