summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/gui/core/LoginGUI.java
blob: 5c329d5d48fb5cc6afb47894576de367c058392e (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
package gui.core;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Shell;
import org.openslx.bwlp.thrift.iface.Organization;
import org.openslx.bwlp.thrift.iface.UserInfo;
import org.openslx.thrifthelper.ThriftManager;

import auth.BWIDMAuthenticator;
import auth.BaseAuthenticator.AuthenticatorCallback;
import edu.kit.scc.dei.ecplean.ECPAuthenticationException;
import gui.GuiManager;

public class LoginGUI extends LoginComposite {

	private final static Logger LOGGER = Logger.getLogger(LoginGUI.class);

	private final String NO_USERNAME = "Kein Benutzername angegeben!";
	private final String NO_PASSWORD = "Kein Passwort angegeben!";

	private String username = null;
	private String password = null;

	/**
	 * @param mainShell
	 */
	public LoginGUI(final Shell mainShell) {
		// call the constructor of the superclass
		super(mainShell);

		// entries in the combo
		List<Organization> orgs = null;
		try {
			orgs = ThriftManager.getMasterClient().getOrganizations();
		} catch (TException e) {
			LOGGER.error(
					"Could not fetch the IdP list from the masterserver! See trace:", e);
			// in this case, we can just call the default fillIdPCombo method of the
			// superclass
			idpCombo.add("No entries");
		}
		// all fine, lets sort it
		Collections.sort(orgs, new Comparator<Organization>() {
			public int compare(Organization o1, Organization o2) {
				return o1.getDisplayName().compareTo(o2.getDisplayName());
			}
		});
		for (Organization o : orgs) {
			idpCombo.add(o.displayName);
			idpCombo.setData(o.displayName, o);
		}
		idpCombo.select(0);

		// actions of the login button
		loginButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				try {
					doLogin(loginType);
				} catch (ECPAuthenticationException ae) {
					LOGGER.error("Authentication error, see trace: ", ae);
					GuiManager.showMessage(ae.getMessage());
				}
			}
		});

		// for save username checkbox.
		saveUsernameCheck.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// clickedSaveUsernameCheck();
			}
		});

		// selecting the "Authentifizierung über bwIDM" radio button
		authButtons[0].addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				idpText.setVisible(true);
				idpCombo.setVisible(true);
				loginType = LOGIN_TYPE.BWIDM;
			}
		});

		// selecting the "Test-Zugang mit festem Benutzer" radio button
		authButtons[1].addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				idpText.setVisible(false);
				idpCombo.setVisible(false);
				loginType = LOGIN_TYPE.BWLP;
			}
		});

		authButtons[2].setEnabled(false);
		// selecting the "Direkte Verbindung zum Satteliten" radio button
		authButtons[2].addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				idpText.setVisible(false);
				idpCombo.setVisible(false);
				loginType = LOGIN_TYPE.SAT;
			}
		});
	}

	/**
	 * Actually do the login using the username/password in the field using the
	 * authentication mechanism corresponding to the selected authButton
	 * @throws ECPAuthenticationException 
	 */
	private void doLogin(LOGIN_TYPE loginType) throws ECPAuthenticationException {
		// here we only check for the fields
		username = usernameText.getText();
		password = passwordText.getText();
		// login clicked, lets first read the fields
		if (username == null) {
			// tell the GUIManager to show an error to the user
			// TODO either popup or in the bottom status bar
			GuiManager.showMessage(NO_USERNAME);
			return;
		}
		if (password == null) {
			// tell the GUIManager to show an error to the user
			// TODO either popup or in the bottom status bar
			GuiManager.showMessage(NO_PASSWORD);
			return;
		}
		// get the index of the selected item in the combobox
		int selectionIndex = idpCombo.getSelectionIndex();
		if (selectionIndex == -1) {
			LOGGER
					.error("No IdP is selected in the combobox. There should be a default value!");
			return;
		}
		Organization selectedOrg = 
				(Organization) idpCombo.getData(idpCombo.getItem(selectionIndex));

		// now switch over the login types.
		switch (loginType) {
		case BWIDM:
			BWIDMAuthenticator bwidmAuth = new BWIDMAuthenticator(
					selectedOrg.getEcpUrl());
			bwidmAuth.login(username, password, new AuthenticatorCallback() {
				@Override
				public void postLogin(UserInfo user) {
					LOGGER.info(user.firstName + " " + user.lastName);
					if (user != null)
						GuiManager.addContent(new DisclaimerComposite(getShell()));
				}
			});
			break;
		case BWLP:
			LOGGER.info("bwlp");
			break;
		case SAT:
			LOGGER.info("sat");
			break;
		default:
			LOGGER.error("derp");
			break;
		}
		return;

	}
}