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