]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ConnectionAction.java
Salome HOME
Refactoring of Database, replacing SQL by DAOs calls. Methods for search by criteria...
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ConnectionAction.java
1 package org.splat.simer;
2
3 import java.util.Map;
4 import java.util.Set;
5
6 import javax.security.auth.login.LoginContext;
7 import javax.security.auth.Subject;
8 import javax.security.auth.callback.*;
9
10 import org.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.som.ApplicationRights;
14 import org.splat.dal.dao.som.Database;
15
16 import java.io.IOException;
17 import javax.security.auth.login.FailedLoginException;
18
19
20 public class ConnectionAction extends Action {
21
22     private String             username = null;
23     private String             password = null;
24     private String             backmenu = null;
25
26         /**
27          * Serial version ID.
28          */
29         private static final long  serialVersionUID = 6095471616361606231L;
30         
31         private class Handler implements CallbackHandler {
32 //  ------------------------------------------------            
33       public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
34       {
35         for (int i = 0; i < callbacks.length; i++) {
36           if (callbacks[i] instanceof TextOutputCallback) {
37 //        Display a message according to a specified type
38
39           } else if (callbacks[i] instanceof NameCallback) {
40 //          Get the username            
41             NameCallback call = (NameCallback)callbacks[i];         
42             call.setName(username);
43                 
44           } else if (callbacks[i] instanceof PasswordCallback) {    
45 //          Get the password
46                 if (password != null) {
47               PasswordCallback call = (PasswordCallback)callbacks[i];
48               call.setPassword(password.toCharArray());
49                 }
50           } else {
51             throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
52           }
53         }
54           }
55         }
56
57 //  ==============================================================================================================================
58 //  Action execution
59 //  ==============================================================================================================================
60
61         @SuppressWarnings("unchecked")
62         public String doLogin () throws Exception {
63 //  ------------------------
64       if (username == null || username.length() == 0) return INPUT;
65       if (password != null && password.length() == 0) password = null;  // User having no password
66       try {
67         Session      connex  = Database.getSession();
68         Transaction  transax = connex.beginTransaction();
69         LoginContext context = new LoginContext("Simer", new Handler());
70         context.login();
71         transax.commit();
72           
73         Subject   identity = context.getSubject();
74         Set<User> table    = identity.getPrincipals(User.class);
75         if (table.isEmpty()) throw new Exception();
76
77         User              user   = table.iterator().next();             // The user is (apparently...) the 1st principal
78         ApplicationRights logged = new ApplicationRights(user);
79         if (logged.canContributeToStudy() || logged.canValidate()) {
80 //TODO: Set the search filter according to user preferences
81           Map<String,Object> session = getSession();
82 //        Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
83           Map<String,Object> sfilter = (Map<String, Object>)session.get("study.filter");
84           
85           sfilter.put("state", "ANY");
86           sfilter.put("visibility", "PRIVATE");
87           if (logged.canCreateStudy()) sfilter.put("author", String.valueOf(user.getIndex()));
88         }
89         this.connect(context, user);                                    // Updates the session context
90             return backmenu;
91      }
92       catch (FailedLoginException error) {
93         setErrorCode("message.error.login." + error.getMessage());
94         return INPUT;
95       }
96       catch (Exception error) {
97         logger.error("Reason:", error);
98         return ERROR;
99       }
100         }
101
102     @SuppressWarnings("unchecked")
103         public String doLogout () {
104 //  -------------------------
105       try {
106         Map<String,Object>  session = getSession();
107 //      Map<String,Object>  kfilter = (Map<String, Object>)session.get("knowledge.filter");
108         Map<String,Object>  sfilter = (Map<String, Object>)session.get("study.filter");
109         LoginContext        context = (LoginContext)session.get("login.context");     
110         
111         logger.info("Deconnection of " + getConnectedUser().toString() + ".");
112         context.logout();
113
114 //TODO: ProjectSettings.deleteDownloadDirectory(user);
115         sfilter.put("state", "ANY");
116         sfilter.put("author", "0");
117
118         this.disconnect();                                              // Updates the session context
119         return backmenu;
120       }
121       catch (Exception error) {
122         logger.error("Reason:", error);
123         return ERROR;
124       }
125     }
126
127 //  ==============================================================================================================================
128 //  Getters and setters
129 //  ==============================================================================================================================
130         
131     public String getUsername () {
132 //  ----------------------------        
133       return username;
134     }
135     public String getPassword () {
136 //  ----------------------------
137       return password;
138     }
139
140     public void setUsername (String value) {
141 //  --------------------------------------
142       this.username = value;
143     }
144     public void setPassword (String value) {
145 //  --------------------------------------
146       this.password = value;
147     }
148     public void setBackMenu (String menu) {
149 //  -------------------------------------
150       this.backmenu = menu;
151     }
152 }