summaryrefslogtreecommitdiffstats
path: root/Dozentenmodul/src/GUI/Login_GUI.java
blob: 7bb56c048fe56807d9ae46f272e94c2a6c2b79cc (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package gui;


import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import models.person;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.ini4j.Wini;
import thrift.AuthenticationException;
import thrift.ImageServer;
import thrift.InvalidTokenException;
import thrift.SessionData;
import thrift.MasterThriftConnection;
import thrift.ImageServer.Client;
import thrift.UserInfo;

import config.config_file;


@SuppressWarnings("serial")
public class Login_GUI extends JFrame {

	private JPanel contentPane;
	private JTextField username;
	private JPasswordField pass;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//Prüfe und Erzeuge gegebenfalls Config					
					config_file cf=new config_file();
					boolean conf=cf.createConfig();
					if(conf==true)
					{
					//Aufruf und Anzeige des Login Fensters
					Login_GUI frame = new Login_GUI();
					frame.setVisible(true);
					}
					else
					{
			            JOptionPane.showMessageDialog(null,
			                    "Fehler beim erzeugen der Konfigurationsfile", "Message",
			                    JOptionPane.ERROR_MESSAGE);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login_GUI() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		
		//Fenster darf nicht vergrößert werden
		setResizable(false);
		try {
			//Setzt das Look and Feel auf System
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Titel des Fensters setzen
		setTitle("Dozentenmodul *Prototyp*");
		//Aktion die beim Schließen durchgeführt werden soll
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Größe des Fensters definieren
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int top=(screenSize.height - 400) / 2;
		int left=(screenSize.width - 603) / 2;
		setBounds(left, top, 603, 400);
		//Erzeugen eines Panels
		contentPane = new JPanel();
		//Hintergrund Farbe des Panels setzen
		contentPane.setBackground(SystemColor.menu);
		//Rahmen des Fensters setzen
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		//Layout setzen
		contentPane.setLayout(null);
		//Label für das Logo erzeugen
		JLabel imgLabel = new JLabel();
		//Größe und Position des Logos festelegen
		imgLabel.setBounds(150, 11, 350, 142);
		//Pfadangabe des Logos
		
		ImageIcon icon = new ImageIcon(getClass().getResource("/Logo_bwLehrpool.png"),"Logo");
		//Skalierung des Logos
		Image scaled=icon.getImage().getScaledInstance(270, 64, 0);
		imgLabel.setIcon(new ImageIcon(scaled));
		//Hinzufügen des Logos in das Fenster
		contentPane.add(imgLabel);
		
		//Erzeugen und Hinzufügen des Labels 
		JLabel LabelUser = new JLabel("bwIDM-Benutzername:");
		LabelUser.setBounds(150, 164, 134, 20);
		contentPane.add(LabelUser);
		
		//Erzeugen und Hinzufügen des Textfeldes
		username = new JTextField();
		username.setBounds(343, 164, 125, 20);
		contentPane.add(username);
		username.setColumns(10);
		
		//Erzeugen und Hinzufügen des Labels
		JLabel LabelPass = new JLabel("bwIDM-Passwort:");
		LabelPass.setBounds(150, 195, 134, 20);
		contentPane.add(LabelPass);
		
		//Erzeugen, Hinzufügen und definierung der Aktion des Buttons
		JButton BtnLogin = new JButton("Login");
		BtnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//Aufruf der Ldap Klasse, welche die Ldap überprüfung vornimmt
				//Ldap check=new Ldap();
				boolean login=true;
				//boolean login=check.LdapAuth(username.getText(),  new String(pass.getPassword()));
				MasterThriftConnection thrift=new MasterThriftConnection();
				Client client=thrift.getMasterThriftConnection();
				String token=null;
				final thrift.SessionData result;
				try {
					String passText = new String(pass.getPassword());
					System.out.println(username.getText()+" "+passText);
					result = client.authenticate(username.getText(), passText);
					
				} catch (thrift.AuthenticationException e) {
					System.out.println("Authentifizierung fehlgeschlagen: "
							+ e.message);
					return;
				} catch (TException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return;
				}
				System.out.println("Konnte mich beim MasterServer authentifizieren. Meine Session-ID ist " + result.sessionId + ", mein Token für den Satelliten " + result.authToken);
				token = result.authToken;
				if(login==true)
				{
					
					try {
						Wini ini=new Wini(new File("C:\\Users\\"+System.getProperty("user.name")+"\\AppData\\Roaming\\bwLehrpoolSuite\\config.ini"));
						//Prüfe ob Bills Of Rights schon akzeptiert wurden, wenn nicht zeige diese an
						if(ini.get("main", "BillOfRights",boolean.class)==false){
							//Erstellen einer Instanz der Aktionsauswahl
							BillOfRights_GUI re=new BillOfRights_GUI();
							re.setVisible(true);
							//Schließen des Fensters nach erfolgreichen Login
							setVisible(false);
						}
						else
						{
							//Prüfe ob die Links zu vmware angezeigt werden sollen
							if(ini.get("main", "vmware",boolean.class)==false){
								VmWareLink_GUI vm=new VmWareLink_GUI();
								vm.setVisible(true);
								setVisible(false);
							}
							else
							{
								UserInfo user=client.getUserFromToken(token);
								
								String hochschule=user.eMail.substring(user.eMail.indexOf("@")+1, user.eMail.indexOf(".", user.eMail.indexOf("@")));
								//System.out.println("hochschule="+hochschule);
								person.verantwortlicher.setUsername(username.getText());
								person.verantwortlicher.setName(user.lastName);
								person.verantwortlicher.setVorname(user.firstName);
								person.verantwortlicher.setEMail(user.eMail);
								person.verantwortlicher.setHochschule(hochschule);
								//Öffne das Hauprmenü
								MainMenue_GUI main=new MainMenue_GUI();
								main.setVisible(true);
								setVisible(false);
							}
						}
					} catch (IOException | TException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				else
				{
		            JOptionPane.showMessageDialog(null,
		                    "Login denied!", "Message",
		                    JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		BtnLogin.setBounds(343, 244, 125, 23);
		contentPane.add(BtnLogin);
		
		//Erzeugen und Hinzufügen des Passwortfeldes
		pass = new JPasswordField();
		pass.setBounds(343, 195, 125, 20);
		contentPane.add(pass);
	}
}