Salome HOME
Modifications done to respect PMD rules. Versioning a document is fixed. Validation...
[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.splat.dal.bo.kernel.User;
11 import org.splat.som.ApplicationRights;
12
13 import java.io.IOException;
14 import javax.security.auth.login.FailedLoginException;
15
16 public class ConnectionAction extends Action {
17
18         private String username = null;
19         private String password = null;
20         private String backmenu = null;
21     
22     private String _menuProperty;
23
24         /**
25          * Serial version ID.
26          */
27         private static final long serialVersionUID = 6095471616361606231L;
28
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
36
37                                 } else if (callbacks[i] instanceof NameCallback) {
38                                         // Get the username
39                                         NameCallback call = (NameCallback) callbacks[i];
40                                         call.setName(username);
41
42                                 } else if (callbacks[i] instanceof PasswordCallback) {
43                                         // Get the password
44                                         if (password != null) {
45                                                 PasswordCallback call = (PasswordCallback) callbacks[i];
46                                                 call.setPassword(password.toCharArray());
47                                         }
48                                 } else {
49                                         throw new UnsupportedCallbackException(callbacks[i],
50                                                         "Unrecognized Callback");
51                                 }
52                         }
53                 }
54         }
55
56         // ==============================================================================================================================
57         // Action execution
58         // ==============================================================================================================================
59
60         @SuppressWarnings("unchecked")
61         public String doLogin() throws Exception {
62                 // ------------------------
63                 if (username == null || username.length() == 0)
64                         return INPUT;
65                 if (password != null && password.length() == 0)
66                         password = null; // User having no password
67                 try {
68                         LoginContext context = new LoginContext("Simer", new Handler());
69                         context.login();
70
71                         Subject identity = context.getSubject();
72                         Set<User> table = identity.getPrincipals(User.class);
73                         if (table.isEmpty())
74                                 throw new Exception();
75
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
83                                                 .get("study.filter");
84
85                                 sfilter.put("state", "ANY");
86                                 sfilter.put("visibility", "PRIVATE");
87                                 if (logged.canCreateStudy())
88                                         sfilter.put("author", String.valueOf(user.getIndex()));
89                         }
90                         this.connect(context, user); // Updates the session context
91                         
92                         setMenuProperty("none");
93                         initializationScreenContext(_menuProperty);
94                         
95                         return backmenu;
96                 } catch (FailedLoginException error) {
97                         setErrorCode("message.error.login." + error.getMessage());
98                         return INPUT;
99                 } catch (Exception error) {
100                         logger.error("Reason:", error);
101                         return ERROR;
102                 }
103         }
104
105
106         @SuppressWarnings("unchecked")
107         public String doLogout() {
108                 // -------------------------
109                 try {
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");
115
116                         String connectedUsr = "";
117                         if (getConnectedUser() != null) {
118                                 connectedUsr = getConnectedUser().toString();
119                         }
120                         
121                         logger.info("Deconnection of " + connectedUsr
122                                         + ".");
123                         
124                         if (context != null) {
125                                 context.logout();
126                         }
127
128                         // TODO: ProjectSettings.deleteDownloadDirectory(user);
129                         if (sfilter != null) {
130                                 sfilter.put("state", "ANY");
131                                 sfilter.put("author", "0");
132                         }
133
134                         this.disconnect(); // Updates the session context
135                         
136                         setMenuProperty("none");
137                         initializationScreenContext(_menuProperty);
138                         
139                         return backmenu;
140                 } catch (Exception error) {
141                         logger.error("Reason:", error);
142                         return ERROR;
143                 }
144         }
145
146 //  ==============================================================================================================================
147 //  Getters and setters
148 //  ==============================================================================================================================
149         
150     public String getUsername () {
151 //  ----------------------------        
152       return username;
153     }
154     public String getPassword () {
155 //  ----------------------------
156       return password;
157     }
158
159     public void setUsername (String value) {
160 //  --------------------------------------
161       this.username = value;
162     }
163     public void setPassword (String value) {
164 //  --------------------------------------
165       this.password = value;
166     }
167     public void setBackMenu (String menu) {
168 //  -------------------------------------
169       this.backmenu = menu;
170     }
171     
172     /**
173          * Get the menuProperty.
174          * @return the menuProperty
175          */
176         public String getMenuProperty() {
177                 return _menuProperty;
178         }
179
180         /**
181          * Set the menuProperty.
182          * @param menuProperty the menuProperty to set
183          */
184         public void setMenuProperty(String menuProperty) {
185                 this._menuProperty = menuProperty;
186         }
187 }