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;
25 private static final long serialVersionUID = 6095471616361606231L;
27 private class Handler implements CallbackHandler {
28 // ------------------------------------------------
29 public void handle(Callback[] callbacks) throws IOException,
30 UnsupportedCallbackException {
31 for (int i = 0; i < callbacks.length; i++) {
32 if (callbacks[i] instanceof TextOutputCallback) {
33 // Display a message according to a specified type
35 } else if (callbacks[i] instanceof NameCallback) {
37 NameCallback call = (NameCallback) callbacks[i];
38 call.setName(username);
40 } else if (callbacks[i] instanceof PasswordCallback) {
42 if (password != null) {
43 PasswordCallback call = (PasswordCallback) callbacks[i];
44 call.setPassword(password.toCharArray());
47 throw new UnsupportedCallbackException(callbacks[i],
48 "Unrecognized Callback");
54 // ==============================================================================================================================
56 // ==============================================================================================================================
58 @SuppressWarnings("unchecked")
59 public String doLogin() throws Exception {
60 // ------------------------
61 if (username == null || username.length() == 0)
63 if (password != null && password.length() == 0)
64 password = null; // User having no password
66 LoginContext context = new LoginContext("Simer", new Handler());
69 Subject identity = context.getSubject();
70 Set<User> table = identity.getPrincipals(User.class);
72 throw new Exception();
74 User user = table.iterator().next(); // The user is (apparently...) the 1st principal
75 ApplicationRights logged = new ApplicationRights(user);
76 if (logged.canContributeToStudy() || logged.canValidate()) {
77 // TODO: Set the search filter according to user preferences
78 Map<String, Object> session = getSession();
79 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
80 Map<String, Object> sfilter = (Map<String, Object>) session
83 sfilter.put("state", "ANY");
84 sfilter.put("visibility", "PRIVATE");
85 if (logged.canCreateStudy())
86 sfilter.put("author", String.valueOf(user.getIndex()));
88 this.connect(context, user); // Updates the session context
90 } catch (FailedLoginException error) {
91 setErrorCode("message.error.login." + error.getMessage());
93 } catch (Exception error) {
94 logger.error("Reason:", error);
99 @SuppressWarnings("unchecked")
100 public String doLogout() {
101 // -------------------------
103 Map<String, Object> session = getSession();
104 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
105 Map<String, Object> sfilter = (Map<String, Object>) session
106 .get("study.filter");
107 LoginContext context = (LoginContext) session.get("login.context");
109 logger.info("Deconnection of " + getConnectedUser().toString()
113 // TODO: ProjectSettings.deleteDownloadDirectory(user);
114 sfilter.put("state", "ANY");
115 sfilter.put("author", "0");
117 this.disconnect(); // Updates the session context
119 } catch (Exception error) {
120 logger.error("Reason:", error);
125 // ==============================================================================================================================
126 // Getters and setters
127 // ==============================================================================================================================
129 public String getUsername() {
130 // ----------------------------
134 public String getPassword() {
135 // ----------------------------
139 public void setUsername(String value) {
140 // --------------------------------------
141 this.username = value;
144 public void setPassword(String value) {
145 // --------------------------------------
146 this.password = value;
149 public void setBackMenu(String menu) {
150 // -------------------------------------
151 this.backmenu = menu;