001 /**
002 *
003 */
004 package de.jw.cloud42.webapp;
005
006 import java.sql.SQLException;
007 import java.util.ArrayList;
008 import java.util.List;
009
010 import javax.faces.application.FacesMessage;
011
012 import org.hibernate.FlushMode;
013 import org.hibernate.Session;
014 import org.jboss.seam.ScopeType;
015 import org.jboss.seam.annotations.In;
016 import org.jboss.seam.annotations.Name;
017 import org.jboss.seam.annotations.Scope;
018 import org.jboss.seam.faces.FacesMessages;
019
020 import de.jw.cloud42.core.domain.AwsCredentials;
021 import de.jw.cloud42.core.domain.KeypairMapping;
022 import de.jw.cloud42.core.domain.User;
023
024 /**
025 * Holds current user and credentials. Also contains logic for managing the user's RSA private keys.
026 *
027 * @author fbitzer
028 *
029 */
030 @Name("userManager")
031 @Scope(ScopeType.SESSION)
032 public class UserManager {
033
034 private User user = new User();
035
036 /**
037 * current credentials. These are the credentials that are shown on top of each page.
038 */
039 private AwsCredentials currentCredentials = new AwsCredentials();
040
041 /**
042 * the injected Seam-managed Hibernate session
043 */
044 @In
045 Session session;
046
047 /**
048 * Inject faces messages to trigger error and success messages.
049 */
050 @In
051 FacesMessages facesMessages;
052
053
054 @In(create = true)
055 BaseFunctionsManager baseFunctionsManager;
056
057 /**
058 * @return the user
059 */
060 public User getUser() {
061 return user;
062 }
063
064 /**
065 * @param user the user to set
066 */
067 public void setUser(User user) {
068 this.user = user;
069 }
070
071 /**
072 * @return the currentCredentials
073 */
074 public AwsCredentials getCurrentCredentials() {
075 return currentCredentials;
076 }
077
078 /**
079 * @param currentCredentials the currentCredentials to set
080 */
081 public void setCurrentCredentials(AwsCredentials currentCredentials) {
082 this.currentCredentials = currentCredentials;
083 }
084
085
086 /**
087 * Creates a new User instance.
088 */
089 public void newUser(){
090 user = new User();
091 }
092
093 /**
094 * Saves a new user.
095 */
096 public void createAccount(){
097
098 //check for duplicated usernames etc.
099 try {
100
101 session.setFlushMode(FlushMode.COMMIT);
102
103 session.beginTransaction();
104 session.save(user);
105 session.getTransaction().commit();
106
107 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_accountCreated");
108
109 } catch (Exception ex){
110 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_accountNotCreated");
111 }
112 }
113 /**
114 * Saves current credentials by assigning them to the user.
115 */
116 public void saveCredentials(){
117 try {
118 //reset all lists
119 baseFunctionsManager.resetGroupList();
120 baseFunctionsManager.resetImageList();
121 baseFunctionsManager.resetInstanceList();
122 baseFunctionsManager.resetKeypairList();
123 baseFunctionsManager.resetPrivateKey();
124
125 baseFunctionsManager.resetRegionList();
126
127
128 user.setCredentials(currentCredentials);
129
130 session.setFlushMode(FlushMode.COMMIT);
131
132 session.beginTransaction();
133 session.saveOrUpdate(user);
134 session.getTransaction().commit();
135
136 } catch (Exception ex){
137 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_credentialsNotSaved");
138 }
139
140 }
141
142 /**
143 * Saves current region.
144 */
145 public void saveRegion(){
146
147
148 session.setFlushMode(FlushMode.COMMIT);
149
150 session.beginTransaction();
151 session.saveOrUpdate(user);
152 session.getTransaction().commit();
153
154 //reset all the lists
155 baseFunctionsManager.resetGroupList();
156 baseFunctionsManager.resetImageList();
157 baseFunctionsManager.resetInstanceList();
158 baseFunctionsManager.resetKeypairList();
159 baseFunctionsManager.resetPrivateKey();
160
161 baseFunctionsManager.resetRegionList();
162 }
163
164 /**
165 * Stores a private key for a keypair in the database and maps it to the current user.
166 * If there is already a key for a keypair with same name, it is overridden.
167 * @param keypairName name of the keypair.
168 * @param privateKey the private key to save.
169 */
170 public void savePrivateKey(String keypairName, String privateKey){
171
172 if (!privateKey.equals(BaseFunctionsManager.MSG_KEYPAIR_ERROR)){
173
174 this.deletePrivateKey(keypairName);
175
176 //update user in DB
177 session.setFlushMode(FlushMode.COMMIT);
178 session.beginTransaction();
179
180
181 KeypairMapping newMapping = new KeypairMapping();
182
183 newMapping.setKeypairName(keypairName);
184 newMapping.setRsaPrivateKey(privateKey);
185
186 user.getKeys().add(newMapping);
187
188 session.saveOrUpdate(newMapping);
189
190 session.saveOrUpdate(user);
191 session.getTransaction().commit();
192
193 }
194
195 }
196 /**
197 * Checks whether a private key for the keypair with given name exists.
198 * @param keypairName name of the keypair.
199 * @return true, if a private key for the keypair with given name exists, false else
200 */
201 public boolean privateKeyExists(String keypairName){
202
203 List<KeypairMapping> l = user.getKeys();
204
205 for (KeypairMapping m : l){
206 if (m.getKeypairName().equals(keypairName)){
207 return true;
208 }
209 }
210
211 return false;
212 }
213
214
215 /**
216 * Delete a private key for a keypair.
217 * @param keypairName the name of the keypair.
218 */
219 public void deletePrivateKey(String keypairName){
220
221
222 //delete a key
223 List<KeypairMapping> l = user.getKeys();
224
225 //list of keys to delete (in case there are multiple mappings with the same name)
226 List<KeypairMapping> toDelete = new ArrayList<KeypairMapping>();
227
228 for (KeypairMapping m : l){
229 if (m.getKeypairName().equals(keypairName)){
230 toDelete.add(m);
231 }
232 }
233
234 session.setFlushMode(FlushMode.COMMIT);
235 session.beginTransaction();
236
237 for (KeypairMapping m : toDelete){
238 user.getKeys().remove(m);
239 session.delete(m);
240 }
241
242
243 session.saveOrUpdate(user);
244
245 session.getTransaction().commit();
246
247 }
248
249 /**
250 * Retreive a previously saved private key.
251 * @param keyName the name of the keypair the key belongs to.
252 * @return RSA private key for given keypair or null if no key was found.
253 */
254 public String getKeyForName(String keyName){
255
256 List<KeypairMapping> l = user.getKeys();
257
258 for (KeypairMapping m : l){
259 if (m.getKeypairName().equals(keyName)){
260 return m.getRsaPrivateKey();
261 }
262 }
263
264 return null;
265 }
266 }