fixes #888 and #889
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / dialogs / LoginDialog.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.dialogs;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.jface.dialogs.Dialog;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.CLabel;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.swt.widgets.Text;
22 import org.springframework.security.BadCredentialsException;
23 import org.springframework.security.LockedException;
24 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
25
26 import eu.etaxonomy.taxeditor.store.CdmStore;
27 import eu.etaxonomy.taxeditor.store.StoreUtil;
28
29 /**
30 * TODO wrap in a LoginModule
31 * see: http://java.sun.com/j2se/1.5.0/docs/api/javax/security/auth/spi/LoginModule.html
32 *
33 * @author n.hoffmann
34 * @created 16.06.2009
35 * @version 1.0
36 */
37 public class LoginDialog extends Dialog {
38
39
40 private static final Logger logger = Logger.getLogger(LoginDialog.class);
41
42 private static Text text_password;
43 private static Text text_username;
44
45 private String title;
46
47 /**
48 * @param parentShell
49 */
50 public LoginDialog(Shell parentShell) {
51 super(parentShell);
52 title = "Login";
53 }
54
55
56 /* (non-Javadoc)
57 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
58 */
59 @Override
60 protected Control createDialogArea(Composite parent) {
61 Composite composite = (Composite) super.createDialogArea(parent);
62 //add controls to composite as necessary
63
64
65 // Label for the heading
66 final CLabel titleLabel = new CLabel(composite, SWT.NONE);
67 titleLabel.setText("User Login");
68
69 // Label for the username
70 final CLabel userNameLabel = new CLabel(composite, SWT.NONE);
71 userNameLabel.setText("Username");
72
73 // Textfield for the username
74 text_username = new Text(composite, SWT.BORDER);
75 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
76
77 // Label for the password
78 final CLabel passwordLabel = new CLabel(composite, SWT.NONE);
79 passwordLabel.setText("Password");
80
81 // Textfield for the password
82 text_password = new Text(composite, SWT.PASSWORD | SWT.BORDER);
83 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
84
85
86 return composite;
87 }
88
89 /*
90 * (non-Javadoc)
91 *
92 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
93 */
94 protected void configureShell(Shell shell) {
95 super.configureShell(shell);
96 if (title != null) {
97 shell.setText(title);
98 }
99 }
100
101 /* (non-Javadoc)
102 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
103 */
104 @Override
105 protected void okPressed() {
106 String username = text_username.getText().trim();
107 String password = text_password.getText().trim();
108
109 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
110
111 try{
112 CdmStore.getLoginManager().authenticate(token);
113 }catch(BadCredentialsException e){
114 logger.error("Bad credentials", e);
115 StoreUtil.warningDialog("Could not authenticate. Reason: Bad Credentials.");
116 }catch(LockedException e){
117 logger.error("Account is locked", e);
118 StoreUtil.warningDialog("Could not authenticate. Reason: Account is locked.");
119 }catch(IllegalArgumentException e){
120 logger.error("Null argument for either user or password", e);
121 StoreUtil.warningDialog("Could not authenticate. Reason: Username and/or Password empty.");
122 }
123
124 super.okPressed();
125 }
126
127
128 }