]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ConnectionAction.java
Salome HOME
"Remove document" operation is fixed.
[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.FailedLoginException;
15 import javax.security.auth.login.LoginContext;
16
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.som.ApplicationRights;
19
20 /**
21  * User login action.
22  */
23 public class ConnectionAction extends Action {
24
25         /**
26          * User name.
27          */
28         private String _username = null;
29         /**
30          * User password.
31          */
32         private String _password = null;
33         private transient String _backmenu = null;
34
35         private String _menuProperty;
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 Exception();
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                                         sfilter.put("state", "ANY");
116                                         sfilter.put("visibility", "PRIVATE");
117                                         if (logged.canCreateStudy()) {
118                                                 sfilter.put("author", String.valueOf(user.getIndex()));
119                                         }
120                                 }
121                                 this.connect(context, user); // Updates the session context
122
123                                 setMenuProperty("none");
124                                 initializationScreenContext(_menuProperty);
125
126                                 res = _backmenu;
127                         } catch (FailedLoginException error) {
128                                 setErrorCode("message.error.login." + error.getMessage());
129                                 res = INPUT;
130                         } catch (Exception error) {
131                                 LOG.error("Reason:", error);
132                                 res = ERROR;
133                         }
134                 }
135                 return res;
136         }
137
138         /**
139          * Disconnect the current user from SIMAN application.
140          * 
141          * @return SUCCESS if disconnected, ERROR - if exception is caught
142          */
143         @SuppressWarnings("unchecked")
144         public String doLogout() {
145                 String res;
146                 try {
147                         Map<String, Object> session = getSession();
148                         // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
149                         Map<String, Object> sfilter = (Map<String, Object>) session
150                                         .get("study.filter");
151                         LoginContext context = (LoginContext) session.get("login.context");
152
153                         String connectedUsr = "";
154                         if (getConnectedUser() != null) {
155                                 connectedUsr = getConnectedUser().toString();
156                         }
157
158                         LOG.info("Deconnection of " + connectedUsr + ".");
159
160                         if (context != null) {
161                                 context.logout();
162                         }
163
164                         // TODO: ProjectSettings.deleteDownloadDirectory(user);
165                         if (sfilter != null) {
166                                 sfilter.put("state", "ANY");
167                                 sfilter.put("author", "0");
168                         }
169
170                         this.disconnect(); // Updates the session context
171
172                         setMenuProperty("none");
173                         initializationScreenContext(_menuProperty);
174
175                         res = _backmenu;
176                 } catch (Exception error) {
177                         LOG.error("Reason:", error);
178                         res = ERROR;
179                 }
180                 return res;
181         }
182
183         // ==============================================================================================================================
184         // Getters and setters
185         // ==============================================================================================================================
186
187         /**
188          * Get user name.
189          * 
190          * @return user name
191          */
192         public String getUsername() {
193                 return _username;
194         }
195
196         /**
197          * Get user password.
198          * 
199          * @return user password
200          */
201         public String getPassword() {
202                 return _password;
203         }
204
205         /**
206          * Set user name.
207          * 
208          * @param value
209          *            user name
210          */
211         public void setUsername(final String value) {
212                 this._username = value;
213         }
214
215         /**
216          * Set user password.
217          * 
218          * @param value
219          *            the password
220          */
221         public void setPassword(final String value) {
222                 this._password = value;
223         }
224
225         /**
226          * Set menu for the user.
227          * 
228          * @param menu
229          *            menu key string
230          */
231         public void setBackMenu(final String menu) {
232                 this._backmenu = menu;
233         }
234
235         /**
236          * Get the menuProperty.
237          * 
238          * @return the menuProperty
239          */
240         public String getMenuProperty() {
241                 return _menuProperty;
242         }
243
244         /**
245          * Set the menuProperty.
246          * 
247          * @param menuProperty
248          *            the menuProperty to set
249          */
250         public void setMenuProperty(final String menuProperty) {
251                 this._menuProperty = menuProperty;
252         }
253 }