Skip to main content
summaryrefslogtreecommitdiffstats
blob: 175a503c5575e0e1fcd72b944f41154170d3e4b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.dialogs;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.exception.OseeAuthenticationException;
import org.eclipse.osee.framework.core.exception.OseeAuthenticationException.AuthenticationErrorCode;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.logging.IHealthStatus;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.UserManager;
import org.eclipse.osee.framework.ui.skynet.FrameworkImage;
import org.eclipse.osee.framework.ui.skynet.panels.AuthenticationComposite;
import org.eclipse.osee.framework.ui.swt.ImageManager;
import org.eclipse.osee.framework.ui.swt.OseeMessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

/**
 * @author Roberto E. Escobar
 */
public class AuthenticationDialog extends OseeMessageDialog {

   private Button okButton;
   private Button cancelButton;
   protected AuthenticationComposite authenticationComposite;
   private boolean selectionOk;
   private static final int MAX_RETRIES = 3;

   public AuthenticationDialog(Shell parentShell) {
      super(parentShell, "OSEE Authenticate", null, "Enter your user id (email address), password, and domain.",
            ImageManager.getImage(FrameworkImage.LOCKED_KEY), new String[] {"Enter", "Cancel"}, 0);

      selectionOk = false;
      authenticationComposite = new AuthenticationComposite(parentShell, SWT.NONE, false);
   }

   @Override
   protected Control createCustomArea(Composite parent) {
      authenticationComposite.setParent(parent);
      return authenticationComposite;
   }

   @Override
   protected Control createButtonBar(Composite parent) {
      Control c = super.createButtonBar(parent);
      okButton = getButton(0);
      cancelButton = getButton(1);

      okButton.setEnabled(false);
      okButton.addSelectionListener(authenticationComposite.getAuthenticateListener());
      okButton.addSelectionListener(new SelectionAdapter() {

         @Override
         public void widgetSelected(SelectionEvent e) {
            selectionOk = true;
         }
      });
      authenticationComposite.getShell().setDefaultButton(okButton);

      cancelButton.addSelectionListener(new SelectionAdapter() {
         @Override
         public void widgetSelected(SelectionEvent e) {
            selectionOk = false;
         }
      });
      return c;
   }

   public boolean isValid() {
      return selectionOk;
   }

   private void setUserName(String user) {
      authenticationComposite.setUserName(user);
   }

   private void setPassword(String password) {
      authenticationComposite.setPassword(password);
   }

   private void setDomain(String domain) {
      authenticationComposite.setDomain(domain);
   }

   private void setStorageAllowed(boolean isStorageAllowed) {
      authenticationComposite.setStorageAllowed(isStorageAllowed);
   }

   private void setGuestLogin(boolean isGuestLogin) {
      authenticationComposite.setGuestLogin(isGuestLogin);
   }

   private void setProtocol(String protocol) {
      authenticationComposite.setProtocol(protocol);
   }

   private String getUserName() {
      return authenticationComposite.getUserName();
   }

   private String getDomain() {
      return authenticationComposite.getDomain();
   }

   private String getProtocol() {
      return authenticationComposite.getProtocol();
   }

   private boolean isStorageAllowed() {
      return authenticationComposite.isStorageAllowed();
   }

   private boolean isGuestLogin() {
      return authenticationComposite.isGuestLogin();
   }

   public static void openDialog() {
      Display.getDefault().syncExec(new Runnable() {

         private String getErrorMessage(AuthenticationErrorCode status) {
            String toReturn = "";
            if (status == null) {
               status = AuthenticationErrorCode.Unknown;
            }
            switch (status) {
               case UserNotFound:
                  toReturn = "User Id not found.\n" + "Enter your user id.";
                  break;
               case InvalidPassword:
                  toReturn =
                        "Invalid Password.\n" + "Make sure <CAPS LOCK> is not enabled.\n" + "Enter a valid password.";
                  break;
               case NoResponse:
                  toReturn = "Please enter a valid user id and password.";
                  break;
               default:
                  toReturn = "Unknown authentication error";
                  break;
            }
            return toReturn;
         }

         public void run() {
            String dialogTitle = "Authentication Failed";
            String endMsg = "Shutting down the workbench.";
            String user = "";
            String domain = "";
            String message = "";
            String protocol = "";
            boolean isStorageAllowed = false;
            boolean isGuestLogin = false;
            boolean shutdown = false;
            Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();

            for (int numberOfTries = 0; numberOfTries < MAX_RETRIES; numberOfTries++) {
               AuthenticationDialog dialog = new AuthenticationDialog(shell);
               if (numberOfTries != 0) {
                  dialog.setUserName(user);
                  dialog.setPassword("");
                  dialog.setDomain(domain);
                  dialog.setProtocol(protocol);
                  dialog.setStorageAllowed(isStorageAllowed);
                  dialog.setGuestLogin(isGuestLogin);
               }
               int result = dialog.open();

               user = dialog.getUserName();
               protocol = dialog.getProtocol();
               domain = dialog.getDomain();
               isStorageAllowed = dialog.isStorageAllowed();
               isGuestLogin = dialog.isGuestLogin();

               if (result == Window.CANCEL) {
                  // TODO This was added because ATS requires a user to be logged in
                  // Non-Authentication is not an option --
                  if (numberOfTries > MAX_RETRIES) {
                     message = "Maximum number of Retries reached.\n" + endMsg;
                     shutdown = true;
                  } else {
                     message =
                           "Please log in as Guest or with your credentials.\n" + "A Log-in account is required to continue.";
                  }

                  MessageDialog.openError(shell, "Authentication Cancelled", message);
               }
               // else if (result != Window.OK ) {
               // numberOfTries = MAX_RETRIES;
               // }
               else {
                  if (dialog.isValid()) {
                     if (ClientSessionManager.isSessionValid()) {
                        numberOfTries = MAX_RETRIES;
                        String userText;
                        try {
                           userText = UserManager.getUser().toString();
                        } catch (OseeCoreException ex) {
                           userText = ex.getLocalizedMessage();
                        }
                        MessageDialog.openInformation(shell, "Authenticated", "Logged in as: " + userText);
                     } else {
                        if (numberOfTries >= MAX_RETRIES - 1) {
                           message = "Maximum number of Retries reached.\n" + endMsg;
                           shutdown = true;
                        } else {
                           IHealthStatus status = OseeLog.getStatusByName(ClientSessionManager.getStatusId());
                           if (status != null && status.getException() != null) {
                              Throwable ex = status.getException();
                              if (ex instanceof OseeAuthenticationException) {
                                 message = getErrorMessage(((OseeAuthenticationException) ex).getCode());
                              }
                              message = ex.getLocalizedMessage();
                           } else {
                              message = "Authentication error";
                           }
                        }
                        MessageDialog.openError(shell, dialogTitle, message);
                     }
                  }
               }
            }

            if (shutdown) {
               PlatformUI.getWorkbench().close();
            }
         }
      });
   }
}

Back to the top