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;
29 private static final long serialVersionUID = 6095471616361606231L;
31 private class Handler implements CallbackHandler {
32 // ------------------------------------------------
33 public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
35 for (int i = 0; i < callbacks.length; i++) {
36 if (callbacks[i] instanceof TextOutputCallback) {
37 // Display a message according to a specified type
39 } else if (callbacks[i] instanceof NameCallback) {
41 NameCallback call = (NameCallback)callbacks[i];
42 call.setName(username);
44 } else if (callbacks[i] instanceof PasswordCallback) {
46 if (password != null) {
47 PasswordCallback call = (PasswordCallback)callbacks[i];
48 call.setPassword(password.toCharArray());
51 throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
57 // ==============================================================================================================================
59 // ==============================================================================================================================
61 @SuppressWarnings("unchecked")
62 public String doLogin () throws Exception {
63 // ------------------------
64 if (username == null || username.length() == 0) return INPUT;
65 if (password != null && password.length() == 0) password = null; // User having no password
67 Session connex = Database.getSession();
68 Transaction transax = connex.beginTransaction();
69 LoginContext context = new LoginContext("Simer", new Handler());
73 Subject identity = context.getSubject();
74 Set<User> table = identity.getPrincipals(User.class);
75 if (table.isEmpty()) throw new Exception();
77 User user = table.iterator().next(); // The user is (apparently...) the 1st principal
78 ApplicationRights logged = new ApplicationRights(user);
79 if (logged.canContributeToStudy() || logged.canValidate()) {
80 //TODO: Set the search filter according to user preferences
81 Map<String,Object> session = getSession();
82 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
83 Map<String,Object> sfilter = (Map<String, Object>)session.get("study.filter");
85 sfilter.put("state", "ANY");
86 sfilter.put("visibility", "PRIVATE");
87 if (logged.canCreateStudy()) sfilter.put("author", String.valueOf(user.getIndex()));
89 this.connect(context, user); // Updates the session context
92 catch (FailedLoginException error) {
93 setErrorCode("message.error.login." + error.getMessage());
96 catch (Exception error) {
97 logger.error("Reason:", error);
102 @SuppressWarnings("unchecked")
103 public String doLogout () {
104 // -------------------------
106 Map<String,Object> session = getSession();
107 // Map<String,Object> kfilter = (Map<String, Object>)session.get("knowledge.filter");
108 Map<String,Object> sfilter = (Map<String, Object>)session.get("study.filter");
109 LoginContext context = (LoginContext)session.get("login.context");
111 logger.info("Deconnection of " + getConnectedUser().toString() + ".");
114 //TODO: ProjectSettings.deleteDownloadDirectory(user);
115 sfilter.put("state", "ANY");
116 sfilter.put("author", "0");
118 this.disconnect(); // Updates the session context
121 catch (Exception error) {
122 logger.error("Reason:", error);
127 // ==============================================================================================================================
128 // Getters and setters
129 // ==============================================================================================================================
131 public String getUsername () {
132 // ----------------------------
135 public String getPassword () {
136 // ----------------------------
140 public void setUsername (String value) {
141 // --------------------------------------
142 this.username = value;
144 public void setPassword (String value) {
145 // --------------------------------------
146 this.password = value;
148 public void setBackMenu (String menu) {
149 // -------------------------------------
150 this.backmenu = menu;