1 package org.splat.simer;
6 import javax.security.auth.login.LoginContext;
7 import javax.security.auth.Subject;
8 import javax.security.auth.callback.*;
10 import org.splat.dal.bo.kernel.User;
11 import org.splat.som.ApplicationRights;
13 import java.io.IOException;
14 import javax.security.auth.login.FailedLoginException;
16 public class ConnectionAction extends Action {
18 private String username = null;
19 private String password = null;
20 private String backmenu = null;
22 private String _menuProperty;
27 private static final long serialVersionUID = 6095471616361606231L;
29 private class Handler implements CallbackHandler {
30 // ------------------------------------------------
31 public void handle(Callback[] callbacks) throws IOException,
32 UnsupportedCallbackException {
33 for (int i = 0; i < callbacks.length; i++) {
34 if (callbacks[i] instanceof TextOutputCallback) {
35 // Display a message according to a specified type
37 } else if (callbacks[i] instanceof NameCallback) {
39 NameCallback call = (NameCallback) callbacks[i];
40 call.setName(username);
42 } else if (callbacks[i] instanceof PasswordCallback) {
44 if (password != null) {
45 PasswordCallback call = (PasswordCallback) callbacks[i];
46 call.setPassword(password.toCharArray());
49 throw new UnsupportedCallbackException(callbacks[i],
50 "Unrecognized Callback");
56 // ==============================================================================================================================
58 // ==============================================================================================================================
60 @SuppressWarnings("unchecked")
61 public String doLogin() throws Exception {
62 // ------------------------
63 if (username == null || username.length() == 0)
65 if (password != null && password.length() == 0)
66 password = null; // User having no password
68 LoginContext context = new LoginContext("Simer", new Handler());
71 Subject identity = context.getSubject();
72 Set<User> table = identity.getPrincipals(User.class);
74 throw new Exception();
76 User user = table.iterator().next(); // The user is (apparently...) the 1st principal
77 ApplicationRights logged = new ApplicationRights(user);
78 if (logged.canContributeToStudy() || logged.canValidate()) {
79 // TODO: Set the search filter according to user preferences
80 Map<String, Object> session = getSession();
81 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
82 Map<String, Object> sfilter = (Map<String, Object>) session
85 sfilter.put("state", "ANY");
86 sfilter.put("visibility", "PRIVATE");
87 if (logged.canCreateStudy())
88 sfilter.put("author", String.valueOf(user.getIndex()));
90 this.connect(context, user); // Updates the session context
92 setMenuProperty("none");
93 initializationScreenContext(_menuProperty);
96 } catch (FailedLoginException error) {
97 setErrorCode("message.error.login." + error.getMessage());
99 } catch (Exception error) {
100 logger.error("Reason:", error);
106 @SuppressWarnings("unchecked")
107 public String doLogout() {
108 // -------------------------
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 LoginContext context = (LoginContext) session.get("login.context");
116 String connectedUsr = "";
117 if (getConnectedUser() != null) {
118 connectedUsr = getConnectedUser().toString();
121 logger.info("Deconnection of " + connectedUsr
124 if (context != null) {
128 // TODO: ProjectSettings.deleteDownloadDirectory(user);
129 if (sfilter != null) {
130 sfilter.put("state", "ANY");
131 sfilter.put("author", "0");
134 this.disconnect(); // Updates the session context
136 setMenuProperty("none");
137 initializationScreenContext(_menuProperty);
140 } catch (Exception error) {
141 logger.error("Reason:", error);
146 // ==============================================================================================================================
147 // Getters and setters
148 // ==============================================================================================================================
150 public String getUsername () {
151 // ----------------------------
154 public String getPassword () {
155 // ----------------------------
159 public void setUsername (String value) {
160 // --------------------------------------
161 this.username = value;
163 public void setPassword (String value) {
164 // --------------------------------------
165 this.password = value;
167 public void setBackMenu (String menu) {
168 // -------------------------------------
169 this.backmenu = menu;
173 * Get the menuProperty.
174 * @return the menuProperty
176 public String getMenuProperty() {
177 return _menuProperty;
181 * Set the menuProperty.
182 * @param menuProperty the menuProperty to set
184 public void setMenuProperty(String menuProperty) {
185 this._menuProperty = menuProperty;