Salome HOME
Refactoring continues: UserService is created instead of UserDirectory. Database...
[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         /**
23          * Serial version ID.
24          */
25         private static final long serialVersionUID = 6095471616361606231L;
26
27         private class Handler implements CallbackHandler {
28                 // ------------------------------------------------
29                 public void handle(Callback[] callbacks) throws IOException,
30                                 UnsupportedCallbackException {
31                         for (int i = 0; i < callbacks.length; i++) {
32                                 if (callbacks[i] instanceof TextOutputCallback) {
33                                         // Display a message according to a specified type
34
35                                 } else if (callbacks[i] instanceof NameCallback) {
36                                         // Get the username
37                                         NameCallback call = (NameCallback) callbacks[i];
38                                         call.setName(username);
39
40                                 } else if (callbacks[i] instanceof PasswordCallback) {
41                                         // Get the password
42                                         if (password != null) {
43                                                 PasswordCallback call = (PasswordCallback) callbacks[i];
44                                                 call.setPassword(password.toCharArray());
45                                         }
46                                 } else {
47                                         throw new UnsupportedCallbackException(callbacks[i],
48                                                         "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)
62                         return INPUT;
63                 if (password != null && password.length() == 0)
64                         password = null; // User having no password
65                 try {
66                         LoginContext context = new LoginContext("Simer", new Handler());
67                         context.login();
68
69                         Subject identity = context.getSubject();
70                         Set<User> table = identity.getPrincipals(User.class);
71                         if (table.isEmpty())
72                                 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
81                                                 .get("study.filter");
82
83                                 sfilter.put("state", "ANY");
84                                 sfilter.put("visibility", "PRIVATE");
85                                 if (logged.canCreateStudy())
86                                         sfilter.put("author", String.valueOf(user.getIndex()));
87                         }
88                         this.connect(context, user); // Updates the session context
89                         return backmenu;
90                 } catch (FailedLoginException error) {
91                         setErrorCode("message.error.login." + error.getMessage());
92                         return INPUT;
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
106                                         .get("study.filter");
107                         LoginContext context = (LoginContext) session.get("login.context");
108
109                         logger.info("Deconnection of " + getConnectedUser().toString()
110                                         + ".");
111                         context.logout();
112
113                         // TODO: ProjectSettings.deleteDownloadDirectory(user);
114                         sfilter.put("state", "ANY");
115                         sfilter.put("author", "0");
116
117                         this.disconnect(); // Updates the session context
118                         return backmenu;
119                 } catch (Exception error) {
120                         logger.error("Reason:", error);
121                         return ERROR;
122                 }
123         }
124
125         // ==============================================================================================================================
126         // Getters and setters
127         // ==============================================================================================================================
128
129         public String getUsername() {
130                 // ----------------------------
131                 return username;
132         }
133
134         public String getPassword() {
135                 // ----------------------------
136                 return password;
137         }
138
139         public void setUsername(String value) {
140                 // --------------------------------------
141                 this.username = value;
142         }
143
144         public void setPassword(String value) {
145                 // --------------------------------------
146                 this.password = value;
147         }
148
149         public void setBackMenu(String menu) {
150                 // -------------------------------------
151                 this.backmenu = menu;
152         }
153 }