package us.jyg.freshet.service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import us.jyg.freshet.dao.model.User;
import us.jyg.freshet.dao.model.Guest;
import us.jyg.freshet.util.UserManager;
import us.jyg.freshet.Constants;
import javax.security.auth.login.LoginException;
import java.util.List;
import java.util.ArrayList;
import java.net.InetAddress;
public class UserService {
static final Log log = LogFactory.getLog(UserService.class);
List<User> users;
List<Guest> guests;
public List<User> getLoggedInUsers() {
return users;
}
public List<Guest> getLoggedInGuests() {
return guests;
}
public List<User> getAllLoggedIn() {
List everyone = new ArrayList<User>();
everyone.add(users);
everyone.add(guests);
return everyone;
}
public boolean login(String name, String password, String host) throws LoginException {
boolean status = false;
try {
host = InetAddress.getByName(host).getHostAddress();
} catch (Exception e) {
log.error("cannot get local host name, weird.");
}
if (name == null || host == null || password == null)
throw new LoginException("Empty name, host or password");
User user = userManager.getUser(name);
if (user == null)
throw new LoginException("No such user \""+name+"\"");
if (canAuthenticate(name, password)) {
status = true;
users.add(user);
}
return status;
}
public boolean loginGuest(String hostname) throws LoginException {
boolean status = false;
String host = null;
try {
host = InetAddress.getByName(hostname).getHostAddress();
} catch (Exception e) {
log.error("cannot get local host name, weird.");
}
if (host != null) {
Guest guest = new Guest();
guest.setUsername(Constants.USERNAME_GUEST);
guest.setHost(host);
guests.add(guest);
log.info( guest.getHost() + "added to guest list");
status = true;
log.info("Guest logged in from: " + hostname+"/"+host);
guests.add(guest);
} else {
throw new LoginException("Cannot have a null host for a guest");
}
return status;
}
public void logout(User user) throws LoginException {
boolean isLoggedIn = false;
if (user instanceof Guest)
guests.remove(user);
else
users.remove(user);
}
public boolean hostIsAuthenticated(String host) {
return true;
//
// try {
// host = InetAddress.getByName(host).getHostAddress();
// } catch (Exception e) {
// log.warn("cannot get local host name, weird.");
// }
//
// if (localhostOk && getServerHost().equals(host))
// return true;
// if (userMap.containsKey(host) || guestList.contains(host))
// return true;
// else
// return false;
}
public boolean userIsAuthenticated(String user, String host) {
return true;
// for(Iterator i = userMap.values().iterator();i.hasNext();) {
// if (((UserBean)i.next()).getName().equals(user)) {
// // log.debug(user + "fully authenticated");
// return true;
// }
// }
// // if localhostOk is true, but the user doesn't exist and the client is
// // on the localhost, authenticate anyway
// if (localhostOk && getServerHost().equals(host)) {
// // log.debug(user + " authenticated via host");
// return true;
// }
//
// return false;
}
// TODO make canAuthenticate() do some real authentication
private boolean canAuthenticate(String name, String password) {
return true;
}
UserManager userManager;
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
}