-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
324 lines (261 loc) · 8.7 KB
/
Copy pathUser.java
File metadata and controls
324 lines (261 loc) · 8.7 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
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
package com.securefileshare.models;
import java.sql.Timestamp;
public class User {
private int userId;
private String username;
private String password;
private String email;
private String fullName;
private String phone;
private String role;
private Timestamp createdAt;
private Timestamp lastLogin;
private String lastLoginIP;
private boolean isActive;
private String firstName;
private String lastName;
// Additional profile settings
private boolean emailNotifications;
private boolean twoFactorEnabled;
private boolean autoEncrypt;
private int itemsPerPage;
// Security settings
private String trustedDevices;
private String trustedIPs;
// Constructors
public User() {
this.emailNotifications = true;
this.autoEncrypt = true;
this.itemsPerPage = 25;
this.isActive = true;
this.role = "USER";
}
public User(String username, String email, String password) {
this();
this.username = username;
this.email = email;
this.password = password;
}
// ==================== BASIC GETTERS AND SETTERS ====================
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Timestamp getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Timestamp createdAt) {
this.createdAt = createdAt;
}
public Timestamp getLastLogin() {
return lastLogin;
}
public void setLastLogin(Timestamp lastLogin) {
this.lastLogin = lastLogin;
}
public String getLastLoginIP() {
return lastLoginIP;
}
public void setLastLoginIP(String lastLoginIP) {
this.lastLoginIP = lastLoginIP;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
// ==================== SETTINGS GETTERS AND SETTERS ====================
public boolean isEmailNotifications() {
return emailNotifications;
}
public void setEmailNotifications(boolean emailNotifications) {
this.emailNotifications = emailNotifications;
}
public boolean isTwoFactorEnabled() {
return twoFactorEnabled;
}
public void setTwoFactorEnabled(boolean twoFactorEnabled) {
this.twoFactorEnabled = twoFactorEnabled;
}
public boolean isAutoEncrypt() {
return autoEncrypt;
}
public void setAutoEncrypt(boolean autoEncrypt) {
this.autoEncrypt = autoEncrypt;
}
public int getItemsPerPage() {
return itemsPerPage;
}
public void setItemsPerPage(int itemsPerPage) {
this.itemsPerPage = itemsPerPage > 0 ? itemsPerPage : 25;
}
// ==================== SECURITY GETTERS AND SETTERS ====================
public String getTrustedDevices() {
return trustedDevices;
}
public void setTrustedDevices(String trustedDevices) {
this.trustedDevices = trustedDevices;
}
public String getTrustedIPs() {
return trustedIPs;
}
public void setTrustedIPs(String trustedIPs) {
this.trustedIPs = trustedIPs;
}
// ==================== HELPER METHODS ====================
public String getDisplayName() {
return (fullName != null && !fullName.trim().isEmpty()) ? fullName : username;
}
public boolean isAdmin() {
return "ADMIN".equalsIgnoreCase(role);
}
public void addTrustedDevice(String deviceId) {
if (trustedDevices == null || trustedDevices.isEmpty()) {
trustedDevices = deviceId;
} else {
trustedDevices += "," + deviceId;
}
}
public void addTrustedIP(String ipAddress) {
if (trustedIPs == null || trustedIPs.isEmpty()) {
trustedIPs = ipAddress;
} else {
trustedIPs += "," + ipAddress;
}
}
public boolean isDeviceTrusted(String deviceId) {
if (trustedDevices == null || trustedDevices.isEmpty()) return false;
String[] devices = trustedDevices.split(",");
for (String device : devices) {
if (device.equals(deviceId)) return true;
}
return false;
}
public boolean isIPTrusted(String ipAddress) {
if (trustedIPs == null || trustedIPs.isEmpty()) return false;
String[] ips = trustedIPs.split(",");
for (String ip : ips) {
if (ip.equals(ipAddress)) return true;
}
return false;
}
public void removeTrustedDevice(String deviceId) {
if (trustedDevices == null || trustedDevices.isEmpty()) return;
String[] devices = trustedDevices.split(",");
StringBuilder newDevices = new StringBuilder();
for (String device : devices) {
if (!device.equals(deviceId)) {
if (newDevices.length() > 0) newDevices.append(",");
newDevices.append(device);
}
}
trustedDevices = newDevices.toString();
}
public void removeTrustedIP(String ipAddress) {
if (trustedIPs == null || trustedIPs.isEmpty()) return;
String[] ips = trustedIPs.split(",");
StringBuilder newIPs = new StringBuilder();
for (String ip : ips) {
if (!ip.equals(ipAddress)) {
if (newIPs.length() > 0) newIPs.append(",");
newIPs.append(ip);
}
}
trustedIPs = newIPs.toString();
}
public void clearTrustedDevices() {
trustedDevices = null;
}
public void clearTrustedIPs() {
trustedIPs = null;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", username='" + username + '\'' +
", email='" + email + '\'' +
", fullName='" + fullName + '\'' +
", role='" + role + '\'' +
", emailNotifications=" + emailNotifications +
", twoFactorEnabled=" + twoFactorEnabled +
", autoEncrypt=" + autoEncrypt +
'}';
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
User user = (User) obj;
return userId == user.userId;
}
@Override
public int hashCode() {
return Integer.hashCode(userId);
}
public void setPasswordHash(String passwordHash) {
this.password = passwordHash;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
updateFullName();
}
public void setLastName(String lastName) {
this.lastName = lastName;
updateFullName();
}
public String getFirstName() {
return firstName != null ? firstName : "";
}
public String getLastName() {
return lastName != null ? lastName : "";
}
private void updateFullName() {
if (firstName != null && !firstName.isEmpty() && lastName != null && !lastName.isEmpty()) {
this.fullName = firstName + " " + lastName;
} else if (firstName != null && !firstName.isEmpty()) {
this.fullName = firstName;
} else if (lastName != null && !lastName.isEmpty()) {
this.fullName = lastName;
}
}
}