001 package de.jw.cloud42.webapp;
002
003 import java.util.List;
004
005 import org.hibernate.Session;
006 import org.jboss.seam.annotations.In;
007 import org.jboss.seam.annotations.Name;
008 import org.jboss.seam.annotations.Out;
009 import org.jboss.seam.security.Identity;
010
011 import de.jw.cloud42.core.domain.AwsCredentials;
012 import de.jw.cloud42.core.domain.User;
013
014
015
016 /**
017 * Seam Authenticator component.
018 *
019 */
020 @Name("authenticator")
021 //@Scope(ScopeType.SESSION)
022 public class Authenticator {
023
024 /**
025 * injected Hibernate session
026 */
027 @In
028 Session session;
029
030 /**
031 * userManager is initialized by login method and outjected again.
032 */
033 @In(create=true)
034 @Out
035 UserManager userManager;
036
037 /**
038 * Seam's authentication method.
039 * Login a user and set credentials by initializing userManager.
040 * @return
041 */
042 public boolean authenticate() {
043
044 List users = session.createQuery(
045 "from User where name = :name and password = :password")
046 .setParameter("name", Identity.instance().getUsername())
047 .setParameter("password", Identity.instance().getPassword())
048 .list();
049
050
051 if (users.size() == 1){
052 User user = (User) users.get(0);
053 userManager.setUser(user);
054
055 if (user.getCredentials()==null){
056 //no AWS credentials saved for this user -> set new ones
057 userManager.setCurrentCredentials(new AwsCredentials());
058 }else {
059 //user has already AWS credentials stored in DB -> set current credentials.
060 userManager.setCurrentCredentials(user.getCredentials());
061 }
062 return true;
063 } else {//no unique user found
064 //userManager.setUser(null);
065 //userManager.setCurrentCredentials(null);
066 return false;
067 }
068
069
070 }
071
072 }