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.hibernate.Session;
11 import org.hibernate.Transaction;
12 import org.splat.dal.bo.kernel.User;
13 import org.splat.som.ApplicationRights;
14 import org.splat.dal.dao.som.Database;
16 import java.io.IOException;
17 import javax.security.auth.login.FailedLoginException;
20 public class ConnectionAction extends Action {
22 private String username = null;
23 private String password = null;
24 private String backmenu = null;
26 private static final long serialVersionUID = 6095471616361606231L;
28 private class Handler implements CallbackHandler {
29 // ------------------------------------------------
30 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
32 for (int i = 0; i < callbacks.length; i++) {
33 if (callbacks[i] instanceof TextOutputCallback) {
34 // Display a message according to a specified type
36 } else if (callbacks[i] instanceof NameCallback) {
38 NameCallback call = (NameCallback)callbacks[i];
39 call.setName(username);
41 } else if (callbacks[i] instanceof PasswordCallback) {
43 if (password != null) {
44 PasswordCallback call = (PasswordCallback)callbacks[i];
45 call.setPassword(password.toCharArray());
48 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
54 // ==============================================================================================================================
56 // ==============================================================================================================================
58 @SuppressWarnings("unchecked")
59 public String doLogin () throws Exception {
60 // ------------------------
61 if (username == null || username.length() == 0) return INPUT;
62 if (password != null && password.length() == 0) password = null; // User having no password
64 Session connex = Database.getSession();
65 Transaction transax = connex.beginTransaction();
66 LoginContext context = new LoginContext("Simer", new Handler());
70 Subject identity = context.getSubject();
71 Set<User> table = identity.getPrincipals(User.class);
72 if (table.isEmpty()) 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.get("study.filter");
82 sfilter.put("state", "ANY");
83 sfilter.put("visibility", "PRIVATE");
84 if (logged.canCreateStudy()) sfilter.put("author", String.valueOf(user.getIndex()));
86 this.connect(context, user); // Updates the session context
89 catch (FailedLoginException error) {
90 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.get("study.filter");
106 LoginContext context = (LoginContext)session.get("login.context");
108 logger.info("Deconnection of " + getConnectedUser().toString() + ".");
111 //TODO: ProjectSettings.deleteDownloadDirectory(user);
112 sfilter.put("state", "ANY");
113 sfilter.put("author", "0");
115 this.disconnect(); // Updates the session context
118 catch (Exception error) {
119 logger.error("Reason:", error);
124 // ==============================================================================================================================
125 // Getters and setters
126 // ==============================================================================================================================
128 public String getUsername () {
129 // ----------------------------
132 public String getPassword () {
133 // ----------------------------
137 public void setUsername (String value) {
138 // --------------------------------------
139 this.username = value;
141 public void setPassword (String value) {
142 // --------------------------------------
143 this.password = value;
145 public void setBackMenu (String menu) {
146 // -------------------------------------
147 this.backmenu = menu;