Project

General

Profile

Download (3.95 KB) Statistics
| Branch: | Tag: | Revision:
1 5dd980ce n.hoffmann
// $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 eca18c8b n.hoffmann
import org.springframework.security.authentication.BadCredentialsException;
23
import org.springframework.security.authentication.LockedException;
24
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
25 5dd980ce n.hoffmann
26
import eu.etaxonomy.taxeditor.store.CdmStore;
27 df60b579 n.hoffmann
import eu.etaxonomy.taxeditor.store.StoreUtil;
28 5dd980ce n.hoffmann
29
/**
30 3be6ef3e n.hoffmann
 * TODO wrap in a LoginModule
31 df60b579 n.hoffmann
 * see: http://java.sun.com/j2se/1.5.0/docs/api/javax/security/auth/spi/LoginModule.html
32 3be6ef3e n.hoffmann
 *
33 5dd980ce n.hoffmann
 * @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 df60b579 n.hoffmann
	private static Text text_password;
43
	private static Text text_username;
44 5dd980ce n.hoffmann
45 c4b87786 n.hoffmann
	private String title;
46
47 5dd980ce n.hoffmann
	/**
48 3be6ef3e n.hoffmann
	 * <p>Constructor for LoginDialog.</p>
49
	 *
50
	 * @param parentShell a {@link org.eclipse.swt.widgets.Shell} object.
51 5dd980ce n.hoffmann
	 */
52
	public LoginDialog(Shell parentShell) {
53
		super(parentShell);
54 c4b87786 n.hoffmann
		title = "Login";
55 5dd980ce n.hoffmann
	}
56
		
57
58
	/* (non-Javadoc)
59
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
60
	 */
61 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
62 5dd980ce n.hoffmann
	@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 df60b579 n.hoffmann
		text_username = new Text(composite, SWT.BORDER);
78
		text_username.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
79 5dd980ce n.hoffmann
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 c4b87786 n.hoffmann
		text_password = new Text(composite, SWT.PASSWORD | SWT.BORDER);
86 df60b579 n.hoffmann
		text_password.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
87 5dd980ce n.hoffmann
		
88
		
89
		return composite;
90
	}
91
92 c4b87786 n.hoffmann
    /*
93
     * (non-Javadoc)
94
     * 
95
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
96
     */
97 3be6ef3e n.hoffmann
    /** {@inheritDoc} */
98 c4b87786 n.hoffmann
    protected void configureShell(Shell shell) {
99
        super.configureShell(shell);
100
        if (title != null) {
101
			shell.setText(title);
102
		}
103
    }
104 5dd980ce n.hoffmann
105
	/* (non-Javadoc)
106
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
107
	 */
108 3be6ef3e n.hoffmann
	/** {@inheritDoc} */
109 5dd980ce n.hoffmann
	@Override
110
	protected void okPressed() {
111 df60b579 n.hoffmann
		String username = text_username.getText().trim();
112
		String password = text_password.getText().trim();
113 5dd980ce n.hoffmann
		
114 df60b579 n.hoffmann
		UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password); 
115 5dd980ce n.hoffmann
		
116
		try{
117 329facb4 n.hoffmann
			CdmStore.getLoginManager().authenticate(token);			
118 5dd980ce n.hoffmann
		}catch(BadCredentialsException e){
119 ab1f8155 n.hoffmann
			logger.error("Bad credentials", e);
120 915cda99 n.hoffmann
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Bad Credentials.");
121 ab1f8155 n.hoffmann
		}catch(LockedException e){
122
			logger.error("Account is locked", e);
123 915cda99 n.hoffmann
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Account is locked.");
124 5dd980ce n.hoffmann
		}catch(IllegalArgumentException e){
125 ab1f8155 n.hoffmann
			logger.error("Null argument for either user or password", e);
126 915cda99 n.hoffmann
			StoreUtil.warningDialog("Could not authenticate", this, "Could not authenticate. Reason: Username and/or Password empty.");
127 5dd980ce n.hoffmann
		}
128
		
129
		super.okPressed();
130
	}	
131
	
132
	
133
}