This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Access.java
334 lines (292 loc) · 8.26 KB
/
Access.java
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
/**
* {@code Access} class provides the authentication page and all associated
* utilities for the same. It is highly dependent on the {@code Register} and
* {@code Login} classes. The class provides for a pluggable panel {@code p}.
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions.
* </p>
*
* @author Emmanuel Jojy
*
*/
public class Access {
/**
* Pluggable panel which is plugged in to the frame generated by the {@code GUI}
* class.
*/
JPanel p;
/**
* An instance of the {@code Register} class.
*/
JPanel register;
/**
* An instance of the {@code Login} class. It is provided a {@code static}
* modifier for purpose of being accessed by the {@code Register} class.
*/
static Login login;
/**
* Displays status message. Default message is
* '{@code HangMan's Authentication Service}'.<br>
* Inspired by the CS50's authentication service page.
*
* <p>
* Changes itself when a new user generates a credential
* </p>
*/
static JLabel status;
/**
* Default constrctor provided for {@code Access} class. Initializes the
* pluggable JPanel p, creates instances of {@code Login} and {@code Register}.
*
* To be invoked only from {@code GUI} class.
*/
public Access() {
p = new JPanel();
p.setLayout(null);
JLabel title = new JLabel("Authentication", JLabel.CENTER);
title.setFont(new Font("Calibri", Font.PLAIN, 38));
title.setBounds(533, 20, 300, 35);
p.add(title);
JPanel min = new JPanel();
min.setLayout(null);
register = new Register().p;
min.add(register);
login = new Login();
min.add(login.p);
status = new JLabel("Hangman's Authentication Service");
status.setBounds(20, 550, 450, 50);
status.setFont(new Font("Consolas", Font.PLAIN, 14));
min.add(status);
min.setBounds(0, 45, 1366, 720);
p.add(min);
}
}
/**
* {@code Register} class provides the registration panel and all associated
* utilities for the same. The class provides for a pluggable panel {@code p}
* for purpose of plugging into {@code Access.p}.
*
* <p>
* Requires {@code ActionListener} for purpose of incorporating a button.
* </p>
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions.
* </p>
*
* @author Emmanuel Jojy
*
*/
class Register implements ActionListener {
/**
* Pluggable {@code JPanel}. Plug dimension 300 X 460.
*/
JPanel p;
private JLabel head, l1, l2, l2_re, error;
private JTextField name;
private JPasswordField pass, pass_re;
private JButton b;
/**
* Iniatializes plug {@code p} and other relevant fields.
*/
public Register() {
p = new JPanel();
p.setLayout(null);
p.setBounds(301 - 40, 60, 300, 460);
p.setBorder(BorderFactory.createLineBorder(Color.black));
head = new JLabel("New Users");
head.setFont(new Font("Calibri", Font.BOLD, 32));
head.setBounds(15, 5, 300, 45);
l1 = new JLabel("User Name");
l1.setFont(new Font("Calibri", Font.PLAIN, 28));
l1.setBounds(15, 65, 200, 50);
name = new JTextField(15);
name.setBounds(15, 115, 250, 35);
l2 = new JLabel("Password");
l2.setFont(new Font("Calibri", Font.PLAIN, 28));
l2.setBounds(15, 160, 200, 50);
pass = new JPasswordField(15);
pass.setBounds(15, 215, 250, 35);
l2_re = new JLabel("Re-enter Password");
l2_re.setFont(new Font("Calibri", Font.PLAIN, 28));
l2_re.setBounds(15, 260, 300, 50);
pass_re = new JPasswordField(15);
pass_re.setBounds(15, 315, 250, 35);
b = new JButton("REGISTER");
b.setFont(new Font("Calibri", Font.PLAIN, 26));
b.setBounds(15, 380, 140, 35);
b.addActionListener(this);
error = new JLabel("", JLabel.CENTER);
error.setFont(new Font("Consolas", Font.PLAIN, 18));
error.setBounds(15, 415, 280, 50);
error.setForeground(Color.RED);
p.add(head);
p.add(l1);
p.add(name);
p.add(l2);
p.add(pass);
p.add(l2_re);
p.add(pass_re);
p.add(b);
p.add(error);
}
/**
* Overriden method of {@code ActionListener}. Refer original documentation of
* the same for more details.
*/
@Override
public void actionPerformed(ActionEvent ev) {
Statement st = Main.st;
ResultSet res;
String nam, pas, par, query;
nam = name.getText().toUpperCase();
pas = new String(pass.getPassword());
par = new String(pass_re.getPassword());
if (nam.equals("") || pas.equals("") || par.equals("")) {
showError("FIELDS CANNOT BE EMPTY");
return;
}
if (!par.equals(pas)) {
showError("PASSWORDS DO NOT MATCH");
return;
}
try {
query = "SELECT * FROM users WHERE NAME = '" + nam + "';";
res = st.executeQuery(query);
if (res.next()) {
showError("USER NAME NOT AVAILABLE");
return;
}
query = "INSERT INTO users(NAME, PASS) VALUES('" + nam + "', '" + pas + "');";
st.executeUpdate(query);
} catch (SQLException e) {
System.out.println("#DB Error - " + e);
System.exit(0);
}
p.setVisible(false);
Access.login.error.setText("#$@ Login with Creadential");
Access.login.p.setBorder(BorderFactory.createLineBorder(Color.GREEN, 3));
Access.status.setText("Registration Successful. User ID generated: " + nam);
Access.login.p.setBounds(683 - 150, 60, 300, 350);
}
private void showError(String msg) {
error.setText("#$! " + msg);
p.setBorder(BorderFactory.createLineBorder(Color.RED, 5));
}
}
/**
* {@code Login} class provides the login panel and all associated utilities for
* the same. The class provides for a pluggable panel {@code p} for purpose of
* plugging into {@code Access.p}.
*
* <p>
* Requires {@code ActionListener} for purpose of incorporating a button.
* </p>
*
* <p>
* Copyright (c) 2021. All rights reserved to Emmanuel Jojy. Use is subject to
* the above conditions.
* </p>
*
* @author Emmanuel Jojy
*
*/
class Login implements ActionListener {
/**
* Pluggable {@code JPanel}. Plug dimension 300 X 350.
*/
JPanel p;
/**
* Provides for a error meesage or current status. For use internally only.
*/
JLabel error;
private JLabel head, l1, l2;
private JTextField name;
private JPasswordField pass;
private JButton b;
/**
* Iniatializes plug {@code p} and other relevant fields.
*/
public Login() {
p = new JPanel();
p.setLayout(null);
p.setBounds(864 - 40, 60, 300, 350);
p.setBorder(BorderFactory.createLineBorder(Color.black));
head = new JLabel("Sign-in");
head.setFont(new Font("Calibri", Font.BOLD, 32));
head.setBounds(15, 5, 300, 45);
l1 = new JLabel("User Name");
l1.setFont(new Font("Calibri", Font.PLAIN, 28));
l1.setBounds(15, 65, 200, 50);
name = new JTextField(15);
name.setBounds(15, 115, 250, 35);
l2 = new JLabel("Password");
l2.setFont(new Font("Calibri", Font.PLAIN, 28));
l2.setBounds(15, 160, 200, 50);
pass = new JPasswordField(15);
pass.setBounds(15, 225, 250, 35);
b = new JButton("LOGIN");
b.setFont(new Font("Calibri", Font.PLAIN, 26));
b.setBounds(15, 280, 120, 35);
b.addActionListener(this);
error = new JLabel("");
error.setFont(new Font("Consolas", Font.PLAIN, 18));
error.setBounds(15, 315, 280, 50);
error.setForeground(Color.GREEN);
p.add(head);
p.add(l1);
p.add(name);
p.add(l2);
p.add(pass);
p.add(b);
p.add(error);
}
/**
* Overriden method of {@code ActionListener}. Refer original documentation of
* the same for more details.
*/
@Override
public void actionPerformed(ActionEvent ev) {
Statement st = Main.st;
ResultSet res;
String nam, pas, dpass, query;
nam = name.getText().toUpperCase();
pas = new String(pass.getPassword());
if (nam.equals("") || pas.equals("")) {
showError("FIELDS CANNOT BE EMPTY");
return;
}
try {
query = "SELECT * FROM users WHERE NAME = '" + nam + "';";
res = st.executeQuery(query);
if (!res.next()) {
showError("USER NOT REGISTERED");
return;
}
dpass = res.getString("PASS");
if (!dpass.equals(pas)) {
showError("INCORRECT CREDENTIALS");
return;
} else {
System.out.println("\n[Login as @" + nam + "]");
GUI.name = nam;
GUI.dashboard();
}
} catch (SQLException e) {
System.out.println("#DB Error - " + e);
System.exit(0);
}
}
private void showError(String msg) {
error.setText("#$! " + msg);
error.setForeground(Color.RED);
p.setBorder(BorderFactory.createLineBorder(Color.RED, 5));
}
}