fixes #805 and started to work on #835
[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 /**
46 * @param parentShell
47 */
48 public LoginDialog(Shell parentShell) {
49 super(parentShell);
50 }
51
52
53 /* (non-Javadoc)
54 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
55 */
56 @Override
57 protected Control createDialogArea(Composite parent) {
58 Composite composite = (Composite) super.createDialogArea(parent);
59 //add controls to composite as necessary
60
61
62 // Label for the heading
63 final CLabel titleLabel = new CLabel(composite, SWT.NONE);
64 titleLabel.setText("User Login");
65
66 // Label for the username
67 final CLabel userNameLabel = new CLabel(composite, SWT.NONE);
68 userNameLabel.setText("Username");
69
70 // Textfield for the username
71 text_username = new Text(composite, SWT.BORDER);
72 text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
73
74 // Label for the password
75 final CLabel passwordLabel = new CLabel(composite, SWT.NONE);
76 passwordLabel.setText("Password");
77
78 // Textfield for the password
79 text_password = new Text(composite, SWT.BORDER);
80 text_password.setEchoChar('*');
81 text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
82
83
84 return composite;
85 }
86
87
88 /* (non-Javadoc)
89 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
90 */
91 @Override
92 protected void okPressed() {
93 String username = text_username.getText().trim();
94 String password = text_password.getText().trim();
95
96 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
97
98 try{
99 CdmStore.getLoginManager().authenticate(token);
100 }catch(BadCredentialsException e){
101 logger.error("Bad credentials", e);
102 StoreUtil.warningDialog("Could not authenticate. Reason: Bad Credentials");
103 }catch(LockedException e){
104 logger.error("Account is locked", e);
105 StoreUtil.warningDialog("Could not authenticate. Reason: Account is locked");
106 }catch(IllegalArgumentException e){
107 logger.error("Null argument for either user or password", e);
108 }
109
110 super.okPressed();
111 }
112
113
114 }