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