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.AccountNotFoundException;
15 import javax.security.auth.login.FailedLoginException;
16 import javax.security.auth.login.LoginContext;
18 import org.splat.dal.bo.kernel.User;
19 import org.splat.som.ApplicationRights;
20 import org.splat.wapp.Constants;
25 public class ConnectionAction extends Action {
30 private String _username = null;
34 private String _password = null;
35 private transient String _backmenu = null;
37 private String _menuProperty;
42 private static final long serialVersionUID = 6095471616361606231L;
45 * Handler for login into SIMAN.
47 private class Handler implements CallbackHandler {
51 * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
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) {
62 NameCallback call = (NameCallback) callbacks[i];
63 call.setName(_username);
65 } else if (callbacks[i] instanceof PasswordCallback) {
67 if (_password != null) {
68 PasswordCallback call = (PasswordCallback) callbacks[i];
69 call.setPassword(_password.toCharArray());
72 throw new UnsupportedCallbackException(callbacks[i],
73 "Unrecognized Callback");
79 // ==============================================================================================================================
81 // ==============================================================================================================================
84 * Login the user with the given name and password into SIMAN.
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
89 * @see org.splat.kernel.RealmLoginModule
91 @SuppressWarnings("unchecked")
92 public String doLogin() {
94 if (_username != null && _username.length() > 0) {
95 if (_password != null && _password.length() == 0) {
96 _password = null; // User having no password
99 LoginContext context = new LoginContext("Siman", new Handler());
102 Subject identity = context.getSubject();
103 Set<User> table = identity.getPrincipals(User.class);
104 if (table.isEmpty()) {
105 throw new AccountNotFoundException();
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");
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
126 this.connect(context, user); // Updates the session context
128 setMenuProperty(Constants.NONE);
129 initializationScreenContext(_menuProperty);
132 if (res == null || "null".equals(res)) {
133 res = Constants.NONE;
135 } catch (FailedLoginException error) {
136 setErrorCode("message.error.login." + error.getMessage());
138 } catch (Exception error) {
139 LOG.error("Reason:", error);
147 * Disconnect the current user from SIMAN application.
149 * @return SUCCESS if disconnected, ERROR - if exception is caught
151 @SuppressWarnings("unchecked")
152 public String doLogout() {
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");
161 String connectedUsr = "";
162 if (getConnectedUser() != null) {
163 connectedUsr = getConnectedUser().toString();
166 LOG.info("Deconnection of " + connectedUsr + ".");
168 if (context != null) {
172 // TODO: ProjectSettings.deleteDownloadDirectory(user);
173 if (sfilter != null) {
174 sfilter.put("state", "ANY");
175 sfilter.put("author", "0");
178 this.disconnect(); // Updates the session context
180 setMenuProperty(Constants.NONE);
181 initializationScreenContext(_menuProperty);
184 if (res == null || "null".equals(res)) {
185 res = Constants.NONE;
187 } catch (Exception error) {
188 LOG.error("Reason:", error);
194 // ==============================================================================================================================
195 // Getters and setters
196 // ==============================================================================================================================
203 public String getUsername() {
210 * @return user password
212 public String getPassword() {
222 public void setUsername(final String value) {
223 this._username = value;
232 public void setPassword(final String value) {
233 this._password = value;
237 * Set menu for the user.
242 public void setBackMenu(final String menu) {
243 this._backmenu = menu;
247 * Get the menuProperty.
249 * @return the menuProperty
251 public String getMenuProperty() {
252 return _menuProperty;
256 * Set the menuProperty.
258 * @param menuProperty
259 * the menuProperty to set
261 public void setMenuProperty(final String menuProperty) {
262 this._menuProperty = menuProperty;