-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForgetPassword.java
More file actions
185 lines (156 loc) · 6.22 KB
/
ForgetPassword.java
File metadata and controls
185 lines (156 loc) · 6.22 KB
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
public class ForgetPassword extends Functions{
private String userID;
private String email;
private String username;
private String OTP;
private String userRecord = "Data/UserRecordTest.txt";
private String studentsRecord = "Data/student_information.txt";
private String Filename;
private final Logger logger = Logger.getLogger(ForgetPassword.class.getName());
public ForgetPassword(String userID, String email) {
this.userID = userID;
this.email = email;
}
public boolean checkIDExists() {
boolean IDexists = false;
if (userID.startsWith("AO") || userID.startsWith("CA")) {
Filename = userRecord;
} else {
Filename = studentsRecord;
}
try (BufferedReader reader = new BufferedReader(new FileReader(Filename))) {
String line;
reader.readLine();
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
if (userID.equals(parts[0])) {
IDexists = true;
break;
}
}
} catch (IOException e) {
logger.severe("An error occurred: " + e.getMessage());
}
getUsername();
return IDexists;
}
public boolean MatchEmail() {
boolean MatchEmail = false;
if (userID.startsWith("AO") || userID.startsWith("CA")) {
Filename = userRecord;
} else {
Filename = studentsRecord;
}
try (BufferedReader reader = new BufferedReader(new FileReader(Filename))) {
String line;
reader.readLine();
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
if (userID.equals(parts[0]) && email.equals(parts[5])) {
MatchEmail = true;
break;
}
}
} catch (IOException e) {
logger.severe("An error occurred: " + e.getMessage());
}
return MatchEmail;
}
public String getUsername() {
if (userID.startsWith("AO") || userID.startsWith("CA")) {
Filename = userRecord;
} else {
Filename = studentsRecord;
}
try (BufferedReader reader = new BufferedReader(new FileReader(Filename))) {
String line;
reader.readLine();
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
if (userID.equals(parts[0])) {
username = parts[1];
break;
}
}
} catch (IOException e) {
logger.severe("An error occurred: " + e.getMessage());
}
logger.info("Username retrieved: " + username);
return username;
}
public void generateOTP (String userID, String email) {
Random rand = new Random();
int otpInt = 100000 + rand.nextInt(900000);
OTP = String.valueOf(otpInt);
String AOsenderEmail = "apuacademicofficer@gmail.com";
String AOsenderPassword = "lrsy tzjy awsp pyeh";
String CAsenderEmail = "apucourseofficer@gmail.com";
String CAsenderPassword = "ibwt iswy stvo wrbm";
try {
sendEmail(
userID.startsWith("AO") ? AOsenderEmail : CAsenderEmail,
userID.startsWith("AO") ? AOsenderPassword : CAsenderPassword,
email,
"OTP Code for Reset Password - APU Course Recovery System",
"Dear " + username + ",\n\n"
+ "We received a request to reset your password for the APU Course Recovery System account associated with this email.\n\n"
+ "Your One-Time Password (OTP) is: " + OTP + "\n\n"
+ "Please enter this code on the verification page to continue.\n"
+ "Notes: This code will expire in 5 minutes for security reasons.\n\n"
+ "If you did not request this, please ignore this email.\n\n"
+ "Best regards,\n"
+ (userID.startsWith("AO") ? "APU Academic Officer Team" : "APU Course Admin Team")
);
} catch (FileNotFoundException e) {
logger.severe("Email template file not found: " + e.getMessage());
e.printStackTrace();
} catch (Exception e) {
logger.severe("Error sending OTP email: " + e.getMessage());
}
logger.info("OTP sent to email: " + email);
}
public boolean verifyOTP(String inputOTP) {
return OTP != null && OTP.equals(inputOTP);
}
public void ClearOTP() {
OTP = null;
logger.info("OTP cleared.");
}
public void resetPassword(String newPassword) {
StringBuilder fileContent = new StringBuilder();
if (userID.startsWith("AO") || userID.startsWith("CA")) {
Filename = userRecord;
} else {
Filename = studentsRecord;
}
try (BufferedReader reader = new BufferedReader(new FileReader(Filename))) {
String line;
fileContent.append(reader.readLine()).append("\n");
while ((line = reader.readLine()) != null) {
String[] parts = line.split(";");
if (userID.equals(parts[0])) {
parts[2] = newPassword;
line = String.join(";", parts);
}
fileContent.append(line).append("\n");
}
} catch (IOException e) {
logger.severe("An error occurred while reading the file: " + e.getMessage());
return;
}
try (BufferedWriter writer = new BufferedWriter(new FileWriter(Filename))) {
writer.write(fileContent.toString());
} catch (IOException e) {
logger.severe("An error occurred while writing to the file: " + e.getMessage());
}
logger.info("Password reset successfully for user ID: " + userID);
}
}