Skip to content

Commit a27db85

Browse files
fix issue of feature multiple login attempt
1 parent 818257c commit a27db85

2 files changed

Lines changed: 22 additions & 77 deletions

File tree

src/main/java/com/iemr/common/controller/users/IEMRAdminController.java

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -171,83 +171,12 @@ public String userAuthenticate(
171171
}
172172

173173
String decryptPassword = aesUtil.decrypt("Piramal12Piramal", m_User.getPassword());
174-
// Fetch user
175-
List<User> existingUser = iemrAdminUserServiceImpl.userExitsCheck(m_User.getUserName());
176-
177-
/*
178-
* =========================================
179-
* ACCOUNT LOCK CHECK
180-
* =========================================
181-
*/
182-
if(!existingUser.isEmpty()){
183-
if (existingUser.get(0) != null
184-
&& existingUser.get(0).getFailedAttempt() != null
185-
&& existingUser.get(0).getFailedAttempt() >= 5) {
186-
187-
throw new IEMRException(
188-
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
189-
}
190-
}
191174

192175

193176
List<User> mUser = iemrAdminUserServiceImpl
194177
.userAuthenticate(m_User.getUserName(), decryptPassword);
195178

196-
/*
197-
* =========================================
198-
* FAILED LOGIN ATTEMPT LOGIC
199-
* =========================================
200-
*/
201-
if(!existingUser.isEmpty()){
202-
if (existingUser != null) {
203-
204-
Integer failedAttempt = existingUser.get(0).getFailedAttempt() != null
205-
? existingUser.get(0).getFailedAttempt()
206-
: 0;
207-
208-
failedAttempt++;
209-
210-
existingUser.get(0).setFailedAttempt(failedAttempt);
211-
212-
iemrAdminUserServiceImpl.save(existingUser.get(0));
213-
214-
int remainingAttempts = 5 - failedAttempt;
215-
216-
// Lock account on 5th attempt
217-
if (failedAttempt >= 5) {
218-
219-
220-
221-
response.setError(new IEMRException(
222-
"Your account has been locked due to multiple failed login attempts."));
223-
return response.toString();
224-
}
225-
226-
// Warning on 3rd attempt
227-
if (failedAttempt == 4) {
228-
229-
230-
response.setError(new IEMRException(
231-
"Invalid username or password. Remaining attempts: "
232-
+ remainingAttempts
233-
+ ". If you enter wrong username or password again, your account will be locked."));
234-
return response.toString();
235-
}
236-
237-
238-
response.setError(new IEMRException(
239-
"Invalid username or password. Remaining attempts: "
240-
+ remainingAttempts));
241-
return response.toString();
242-
243-
}
244-
}
245179

246-
/*
247-
* =========================================
248-
* RESET FAILED ATTEMPTS ON SUCCESS LOGIN
249-
* =========================================
250-
*/
251180
User loggedInUser = mUser.get(0);
252181

253182
loggedInUser.setFailedAttempt(0);

src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,27 +265,43 @@ public List<User> userAuthenticate(String userName, String password) throws Exce
265265
checkUserAccountStatus(user);
266266
iEMRUserRepositoryCustom.save(user);
267267
} else if (validatePassword == 0) {
268-
if (user.getFailedAttempt() + 1 < failedAttempt) {
269-
user.setFailedAttempt(user.getFailedAttempt() + 1);
268+
int currentFailedAttempt =
269+
user.getFailedAttempt() != null ? user.getFailedAttempt() : 0;
270+
271+
int newFailedAttempt = currentFailedAttempt + 1;
272+
int remainingAttempts = failedAttempt - newFailedAttempt;
273+
if (newFailedAttempt < failedAttempt) {
274+
275+
user.setFailedAttempt(newFailedAttempt);
270276
user = iEMRUserRepositoryCustom.save(user);
277+
271278
logger.warn("User Password Wrong");
272-
throw new IEMRException("Invalid username or password");
273-
} else if (user.getFailedAttempt() + 1 >= failedAttempt) {
279+
280+
if (remainingAttempts == 1) {
281+
throw new IEMRException(
282+
"Invalid username or password. Remaining attempts: 1. "
283+
+ "If you enter wrong username or password again, your account will be locked.");
284+
}
285+
286+
throw new IEMRException(
287+
"Invalid username or password. Remaining attempts: "
288+
+ remainingAttempts);
289+
}else if (user.getFailedAttempt() + 1 >= failedAttempt) {
274290
user.setFailedAttempt(user.getFailedAttempt() + 1);
275291
user.setDeleted(true);
276292
user = iEMRUserRepositoryCustom.save(user);
277293
logger.warn("User Account has been locked after reaching the limit of {} failed login attempts.",
278294
ConfigProperties.getInteger("failedLoginAttempt"));
279295

280296
throw new IEMRException(
281-
"Invalid username or password. Please contact administrator.");
297+
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
282298
} else {
283299
user.setFailedAttempt(user.getFailedAttempt() + 1);
284300
user = iEMRUserRepositoryCustom.save(user);
285301
logger.warn("Failed login attempt {} of {} for a user account.",
286302
user.getFailedAttempt(), ConfigProperties.getInteger("failedLoginAttempt"));
287303
throw new IEMRException(
288-
"Invalid username or password. Please contact administrator.");
304+
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
289305
}
290306
} else {
291307
checkUserAccountStatus(user);

0 commit comments

Comments
 (0)