]> SALOME platform Git repositories - tools/siman.git/blob - Workspace/Siman/src/org/splat/simer/ConnectionAction.java
Salome HOME
Menus are improved
[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                         logger.info("Deconnection of " + getConnectedUser().toString()
117                                         + ".");
118                         context.logout();
119
120                         // TODO: ProjectSettings.deleteDownloadDirectory(user);
121                         sfilter.put("state", "ANY");
122                         sfilter.put("author", "0");
123
124                         this.disconnect(); // Updates the session context
125                         
126                         setMenuProperty("none");
127                         initializationScreenContext(_menuProperty);
128                         
129                         return backmenu;
130                 } catch (Exception error) {
131                         logger.error("Reason:", error);
132                         return ERROR;
133                 }
134         }
135
136 //  ==============================================================================================================================
137 //  Getters and setters
138 //  ==============================================================================================================================
139         
140     public String getUsername () {
141 //  ----------------------------        
142       return username;
143     }
144     public String getPassword () {
145 //  ----------------------------
146       return password;
147     }
148
149     public void setUsername (String value) {
150 //  --------------------------------------
151       this.username = value;
152     }
153     public void setPassword (String value) {
154 //  --------------------------------------
155       this.password = value;
156     }
157     public void setBackMenu (String menu) {
158 //  -------------------------------------
159       this.backmenu = menu;
160     }
161     
162     /**
163          * Get the menuProperty.
164          * @return the menuProperty
165          */
166         public String getMenuProperty() {
167                 return _menuProperty;
168         }
169
170         /**
171          * Set the menuProperty.
172          * @param menuProperty the menuProperty to set
173          */
174         public void setMenuProperty(String menuProperty) {
175                 this._menuProperty = menuProperty;
176         }
177 }