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;
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[])
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) {
60 NameCallback call = (NameCallback) callbacks[i];
61 call.setName(_username);
63 } else if (callbacks[i] instanceof PasswordCallback) {
65 if (_password != null) {
66 PasswordCallback call = (PasswordCallback) callbacks[i];
67 call.setPassword(_password.toCharArray());
70 throw new UnsupportedCallbackException(callbacks[i],
71 "Unrecognized Callback");
77 // ==============================================================================================================================
79 // ==============================================================================================================================
82 * Login the user with the given name and password into SIMAN.
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
87 * @see org.splat.kernel.RealmLoginModule
89 @SuppressWarnings("unchecked")
90 public String doLogin() {
92 if (_username != null && _username.length() > 0) {
93 if (_password != null && _password.length() == 0) {
94 _password = null; // User having no password
97 LoginContext context = new LoginContext("Siman", new Handler());
100 Subject identity = context.getSubject();
101 Set<User> table = identity.getPrincipals(User.class);
102 if (table.isEmpty()) {
103 throw new AccountNotFoundException();
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 session.remove(AbstractSearchBaseAction.RESULT_KEY); // The current result is obsolete
112 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
113 Map<String, Object> sfilter = (Map<String, Object>) session
114 .get("study.filter");
116 if (sfilter != null) {
117 sfilter.put("state", "ANY");
118 sfilter.put("visibility", "PRIVATE");
119 if (logged.canCreateStudy()) {
120 sfilter.put("author", String.valueOf(user
125 this.connect(context, user); // Updates the session context
127 initializationScreenContext(Constants.NONE);
130 if (res == null || "null".equals(res) || res.isEmpty()
131 || Constants.NONE.equals(res)) {
132 res = Constants.OPEN;
134 } catch (FailedLoginException error) {
135 setErrorCode("message.error.login." + error.getMessage());
137 } catch (Exception error) {
138 LOG.error("Reason:", error);
146 * Disconnect the current user from SIMAN application.
148 * @return SUCCESS if disconnected, ERROR - if exception is caught
150 @SuppressWarnings("unchecked")
151 public String doLogout() {
154 Map<String, Object> session = getSession();
155 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
156 session.remove(AbstractSearchBaseAction.RESULT_KEY); // The current result is obsolete
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
181 initializationScreenContext(Constants.NONE);
184 if (res == null || "null".equals(res) || res.isEmpty()
185 || Constants.NONE.equals(res)) {
186 res = Constants.OPEN;
188 } catch (Exception error) {
189 LOG.error("Reason:", error);
195 // ==============================================================================================================================
196 // Getters and setters
197 // ==============================================================================================================================
204 public String getUsername() {
211 * @return user password
213 public String getPassword() {
223 public void setUsername(final String value) {
224 this._username = value;
233 public void setPassword(final String value) {
234 this._password = value;
238 * Set menu for the user.
243 public void setBackMenu(final String menu) {
244 this._backmenu = menu;