a804b94a4d0df014acd4b30326826a1233f916fe
[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.authentication.BadCredentialsException;
23 import org.springframework.security.authentication.LockedException;
24 import org.springframework.security.authentication.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 * <p>Constructor for LoginDialog.</p>
49 *
50 * @param parentShell a {@link org.eclipse.swt.widgets.Shell} object.
51 */
52 public LoginDialog(Shell parentShell) {
53 super(parentShell);
54 title = "Login";
55 }
56
57
58 /* (non-Javadoc)
59 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
60 */
61 /** {@inheritDoc} */
62 @Override
63 protected Control createDialogArea(Composite parent) {
64 Composite composite = (Composite) super.createDialogArea(parent);
65 //add controls to composite as necessary
66
67
68 // Label for the heading
69 final CLabel titleLabel = new CLabel(composite, SWT.NONE);
70 titleLabel.setText("User Login");
71
72 // Label for the username
73 final CLabel userNameLabel = new CLabel(composite, SWT.NONE);
74 userNameLabel.setText("Username");
75
76 // Textfield for the username
77 text_username = new Text(composite, SWT.BORDER);
78 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
79
80 // Label for the password
81 final CLabel passwordLabel = new CLabel(composite, SWT.NONE);
82 passwordLabel.setText("Password");
83
84 // Textfield for the password
85 text_password = new Text(composite, SWT.PASSWORD | SWT.BORDER);
86 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
87
88
89 return composite;
90 }
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
96 */
97 /** {@inheritDoc} */
98 protected void configureShell(Shell shell) {
99 super.configureShell(shell);
100 if (title != null) {
101 shell.setText(title);
102 }
103 }
104
105 /* (non-Javadoc)
106 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
107 */
108 /** {@inheritDoc} */
109 @Override
110 protected void okPressed() {
111 String username = text_username.getText().trim();
112 String password = text_password.getText().trim();
113
114 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
115
116 try{
117 CdmStore.getLoginManager().authenticate(token);
118 }catch(BadCredentialsException e){
119 logger.error("Bad credentials", e);
120 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
121 }catch(LockedException e){
122 logger.error("Account is locked", e);
123 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
124 }catch(IllegalArgumentException e){
125 logger.error("Null argument for either user or password", e);
126 StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
127 }
128
129 super.okPressed();
130 }
131
132
133 }