]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ConnectionAction.java
Salome HOME
1e90a55ef9ae33d0e19544237620b7b878d850c2
[tools/siman.git] / Workspace / Siman / src / org / splat / simer / ConnectionAction.java
1 package org.splat.simer;
2
3 import java.util.Map;
4 import java.util.Set;
5
6 import javax.security.auth.login.LoginContext;
7 import javax.security.auth.Subject;
8 import javax.security.auth.callback.*;
9
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;
15
16 import java.io.IOException;
17 import javax.security.auth.login.FailedLoginException;
18
19
20 public class ConnectionAction extends Action {
21
22     private String             username = null;
23     private String             password = null;
24     private String             backmenu = null;
25
26         private static final long  serialVersionUID = 6095471616361606231L;
27         
28         private class Handler implements CallbackHandler {
29 //  ------------------------------------------------            
30       public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException
31       {
32         for (int i = 0; i < callbacks.length; i++) {
33           if (callbacks[i] instanceof TextOutputCallback) {
34 //        Display a message according to a specified type
35
36           } else if (callbacks[i] instanceof NameCallback) {
37 //          Get the username            
38             NameCallback call = (NameCallback)callbacks[i];         
39             call.setName(username);
40                 
41           } else if (callbacks[i] instanceof PasswordCallback) {    
42 //          Get the password
43                 if (password != null) {
44               PasswordCallback call = (PasswordCallback)callbacks[i];
45               call.setPassword(password.toCharArray());
46                 }
47           } else {
48             throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
49           }
50         }
51           }
52         }
53
54 //  ==============================================================================================================================
55 //  Action execution
56 //  ==============================================================================================================================
57
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
63       try {
64         Session      connex  = Database.getSession();
65         Transaction  transax = connex.beginTransaction();
66         LoginContext context = new LoginContext("Simer", new Handler());
67         context.login();
68         transax.commit();
69           
70         Subject   identity = context.getSubject();
71         Set<User> table    = identity.getPrincipals(User.class);
72         if (table.isEmpty()) throw new Exception();
73
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");
81           
82           sfilter.put("state", "ANY");
83           sfilter.put("visibility", "PRIVATE");
84           if (logged.canCreateStudy()) sfilter.put("author", String.valueOf(user.getIndex()));
85         }
86         this.connect(context, user);                                    // Updates the session context
87             return backmenu;
88      }
89       catch (FailedLoginException error) {
90         setErrorCode("message.error.login." + error.getMessage());
91         return INPUT;
92       }
93       catch (Exception error) {
94         logger.error("Reason:", error);
95         return ERROR;
96       }
97         }
98
99     @SuppressWarnings("unchecked")
100         public String doLogout () {
101 //  -------------------------
102       try {
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");     
107         
108         logger.info("Deconnection of " + getConnectedUser().toString() + ".");
109         context.logout();
110
111 //TODO: ProjectSettings.deleteDownloadDirectory(user);
112         sfilter.put("state", "ANY");
113         sfilter.put("author", "0");
114
115         this.disconnect();                                              // Updates the session context
116         return backmenu;
117       }
118       catch (Exception error) {
119         logger.error("Reason:", error);
120         return ERROR;
121       }
122     }
123
124 //  ==============================================================================================================================
125 //  Getters and setters
126 //  ==============================================================================================================================
127         
128     public String getUsername () {
129 //  ----------------------------        
130       return username;
131     }
132     public String getPassword () {
133 //  ----------------------------
134       return password;
135     }
136
137     public void setUsername (String value) {
138 //  --------------------------------------
139       this.username = value;
140     }
141     public void setPassword (String value) {
142 //  --------------------------------------
143       this.password = value;
144     }
145     public void setBackMenu (String menu) {
146 //  -------------------------------------
147       this.backmenu = menu;
148     }
149 }