1 package org.splat.simer;
3 import java.io.IOException;
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;
17 import org.splat.dal.bo.kernel.User;
18 import org.splat.som.ApplicationRights;
23 public class ConnectionAction extends Action {
28 private String _username = null;
32 private String _password = null;
33 private transient String _backmenu = null;
35 private String _menuProperty;
40 private static final long serialVersionUID = 6095471616361606231L;
43 * Handler for login into SIMAN.
45 private class Handler implements CallbackHandler {
49 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
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) {
59 NameCallback call = (NameCallback) callbacks[i];
60 call.setName(_username);
62 } else if (callbacks[i] instanceof PasswordCallback) {
64 if (_password != null) {
65 PasswordCallback call = (PasswordCallback) callbacks[i];
66 call.setPassword(_password.toCharArray());
69 throw new UnsupportedCallbackException(callbacks[i],
70 "Unrecognized Callback");
76 // ==============================================================================================================================
78 // ==============================================================================================================================
81 * Login the user with the given name and password into SIMAN.
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
86 * @see org.splat.kernel.RealmLoginModule
88 @SuppressWarnings("unchecked")
89 public String doLogin() {
91 if (_username != null && _username.length() > 0) {
92 if (_password != null && _password.length() == 0) {
93 _password = null; // User having no password
96 LoginContext context = new LoginContext("Siman", new Handler());
99 Subject identity = context.getSubject();
100 Set<User> table = identity.getPrincipals(User.class);
101 if (table.isEmpty()) {
102 throw new Exception();
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");
114 sfilter.put("state", "ANY");
115 sfilter.put("visibility", "PRIVATE");
116 if (logged.canCreateStudy()) {
117 sfilter.put("author", String.valueOf(user.getIndex()));
120 this.connect(context, user); // Updates the session context
122 setMenuProperty("none");
123 initializationScreenContext(_menuProperty);
126 } catch (FailedLoginException error) {
127 setErrorCode("message.error.login." + error.getMessage());
129 } catch (Exception error) {
130 LOG.error("Reason:", error);
138 * Disconnect the current user from SIMAN application.
140 * @return SUCCESS if disconnected, ERROR - if exception is caught
142 @SuppressWarnings("unchecked")
143 public String doLogout() {
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");
152 String connectedUsr = "";
153 if (getConnectedUser() != null) {
154 connectedUsr = getConnectedUser().toString();
157 LOG.info("Deconnection of " + connectedUsr + ".");
159 if (context != null) {
163 // TODO: ProjectSettings.deleteDownloadDirectory(user);
164 if (sfilter != null) {
165 sfilter.put("state", "ANY");
166 sfilter.put("author", "0");
169 this.disconnect(); // Updates the session context
171 setMenuProperty("none");
172 initializationScreenContext(_menuProperty);
175 } catch (Exception error) {
176 LOG.error("Reason:", error);
182 // ==============================================================================================================================
183 // Getters and setters
184 // ==============================================================================================================================
191 public String getUsername() {
198 * @return user password
200 public String getPassword() {
210 public void setUsername(final String value) {
211 this._username = value;
220 public void setPassword(final String value) {
221 this._password = value;
225 * Set menu for the user.
230 public void setBackMenu(final String menu) {
231 this._backmenu = menu;
235 * Get the menuProperty.
237 * @return the menuProperty
239 public String getMenuProperty() {
240 return _menuProperty;
244 * Set the menuProperty.
246 * @param menuProperty
247 * the menuProperty to set
249 public void setMenuProperty(final String menuProperty) {
250 this._menuProperty = menuProperty;