Salome HOME
484e3548c86d11c6848c6741210be31c8a6a9bdb
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ConnectionAction.java
1 package org.splat.simer;
2
3 import java.io.IOException;
4 import java.util.Map;
5 import java.util.Set;
6
7 import javax.security.auth.Subject;
8 import javax.security.auth.callback.Callback;
9 import javax.security.auth.callback.CallbackHandler;
10 import javax.security.auth.callback.NameCallback;
11 import javax.security.auth.callback.PasswordCallback;
12 import javax.security.auth.callback.TextOutputCallback;
13 import javax.security.auth.callback.UnsupportedCallbackException;
14 import javax.security.auth.login.AccountNotFoundException;
15 import javax.security.auth.login.FailedLoginException;
16 import javax.security.auth.login.LoginContext;
17
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.som.ApplicationRights;
20 import org.splat.wapp.Constants;
21
22 /**
23  * User login action.
24  */
25 public class ConnectionAction extends Action {
26
27         /**
28          * User name.
29          */
30         private String _username = null;
31         /**
32          * User password.
33          */
34         private String _password = null;
35         private transient String _backmenu = null;
36
37         /**
38          * Serial version ID.
39          */
40         private static final long serialVersionUID = 6095471616361606231L;
41
42         /**
43          * Handler for login into SIMAN.
44          */
45         private class Handler implements CallbackHandler {
46                 /**
47                  * {@inheritDoc}
48                  * 
49                  * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
50                  */
51                 @Override
52                 public void handle(final Callback[] callbacks) throws IOException,
53                                 UnsupportedCallbackException {
54                         for (int i = 0; i < callbacks.length; i++) {
55                                 if (callbacks[i] instanceof TextOutputCallback) {
56                                         // Display a message according to a specified type
57                                         LOG.info(((TextOutputCallback) callbacks[i]).getMessage());
58                                 } else if (callbacks[i] instanceof NameCallback) {
59                                         // Get the username
60                                         NameCallback call = (NameCallback) callbacks[i];
61                                         call.setName(_username);
62
63                                 } else if (callbacks[i] instanceof PasswordCallback) {
64                                         // Get the password
65                                         if (_password != null) {
66                                                 PasswordCallback call = (PasswordCallback) callbacks[i];
67                                                 call.setPassword(_password.toCharArray());
68                                         }
69                                 } else {
70                                         throw new UnsupportedCallbackException(callbacks[i],
71                                                         "Unrecognized Callback");
72                                 }
73                         }
74                 }
75         }
76
77         // ==============================================================================================================================
78         // Action execution
79         // ==============================================================================================================================
80
81         /**
82          * Login the user with the given name and password into SIMAN.
83          * 
84          * @return SUCCESS if succeeded, INPUT - if some mandatory parameter is absent or FailedLoginException is caught during login, ERROR -
85          *         if other exception is caught
86          * 
87          * @see org.splat.kernel.RealmLoginModule
88          */
89         @SuppressWarnings("unchecked")
90         public String doLogin() {
91                 String res = INPUT;
92                 if (_username != null && _username.length() > 0) {
93                         if (_password != null && _password.length() == 0) {
94                                 _password = null; // User having no password
95                         }
96                         try {
97                                 LoginContext context = new LoginContext("Siman", new Handler());
98                                 context.login();
99
100                                 Subject identity = context.getSubject();
101                                 Set<User> table = identity.getPrincipals(User.class);
102                                 if (table.isEmpty()) {
103                                         throw new AccountNotFoundException();
104                                 }
105
106                                 User user = table.iterator().next(); // The user is (apparently...) the 1st principal
107                                 ApplicationRights logged = new ApplicationRights(user);
108                                 if (logged.canContributeToStudy() || logged.canValidate()) {
109                                         // TODO: Set the search filter according to user preferences
110                                         Map<String, Object> session = getSession();
111                                         // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
112                                         Map<String, Object> sfilter = (Map<String, Object>) session
113                                                         .get("study.filter");
114
115                                         if (sfilter != null) {
116                                                 sfilter.put("state", "ANY");
117                                                 sfilter.put("visibility", "PRIVATE");
118                                                 if (logged.canCreateStudy()) {
119                                                         sfilter.put("author", String.valueOf(user
120                                                                         .getIndex()));
121                                                 }
122                                         }
123                                 }
124                                 this.connect(context, user); // Updates the session context
125
126                                 initializationScreenContext(Constants.NONE);
127
128                                 res = _backmenu;
129                                 if (res == null || "null".equals(res) || res.isEmpty()
130                                                 || Constants.NONE.equals(res)) {
131                                         res = Constants.OPEN;
132                                 }
133                         } catch (FailedLoginException error) {
134                                 setErrorCode("message.error.login." + error.getMessage());
135                                 res = INPUT;
136                         } catch (Exception error) {
137                                 LOG.error("Reason:", error);
138                                 res = ERROR;
139                         }
140                 }
141                 return res;
142         }
143
144         /**
145          * Disconnect the current user from SIMAN application.
146          * 
147          * @return SUCCESS if disconnected, ERROR - if exception is caught
148          */
149         @SuppressWarnings("unchecked")
150         public String doLogout() {
151                 String res;
152                 try {
153                         Map<String, Object> session = getSession();
154                         // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
155                         Map<String, Object> sfilter = (Map<String, Object>) session
156                                         .get("study.filter");
157                         LoginContext context = (LoginContext) session.get("login.context");
158
159                         String connectedUsr = "";
160                         if (getConnectedUser() != null) {
161                                 connectedUsr = getConnectedUser().toString();
162                         }
163
164                         LOG.info("Deconnection of " + connectedUsr + ".");
165
166                         if (context != null) {
167                                 context.logout();
168                         }
169
170                         // TODO: ProjectSettings.deleteDownloadDirectory(user);
171                         if (sfilter != null) {
172                                 sfilter.put("state", "ANY");
173                                 sfilter.put("author", "0");
174                         }
175
176                         this.disconnect(); // Updates the session context
177
178                         closeStudy();
179                         initializationScreenContext(Constants.NONE);
180
181                         res = _backmenu;
182                         if (res == null || "null".equals(res) || res.isEmpty()
183                                         || Constants.NONE.equals(res)) {
184                                 res = Constants.OPEN;
185                         }
186                 } catch (Exception error) {
187                         LOG.error("Reason:", error);
188                         res = ERROR;
189                 }
190                 return res;
191         }
192
193         // ==============================================================================================================================
194         // Getters and setters
195         // ==============================================================================================================================
196
197         /**
198          * Get user name.
199          * 
200          * @return user name
201          */
202         public String getUsername() {
203                 return _username;
204         }
205
206         /**
207          * Get user password.
208          * 
209          * @return user password
210          */
211         public String getPassword() {
212                 return _password;
213         }
214
215         /**
216          * Set user name.
217          * 
218          * @param value
219          *            user name
220          */
221         public void setUsername(final String value) {
222                 this._username = value;
223         }
224
225         /**
226          * Set user password.
227          * 
228          * @param value
229          *            the password
230          */
231         public void setPassword(final String value) {
232                 this._password = value;
233         }
234
235         /**
236          * Set menu for the user.
237          * 
238          * @param menu
239          *            menu key string
240          */
241         public void setBackMenu(final String menu) {
242                 this._backmenu = menu;
243         }
244 }