Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
}

@Override
public List<User> userAuthenticate(String userName, String password) throws Exception {

Check failure on line 232 in src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 20 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AZ7uxBYe7oTRF9lfYxwh&open=AZ7uxBYe7oTRF9lfYxwh&pullRequest=431
List<User> users = iEMRUserRepositoryCustom.findByUserNameNew(userName);
if (users.size() != 1) {
throw new IEMRException("Invalid username or password");
Expand Down Expand Up @@ -265,27 +265,43 @@
checkUserAccountStatus(user);
iEMRUserRepositoryCustom.save(user);
} else if (validatePassword == 0) {
if (user.getFailedAttempt() + 1 < failedAttempt) {
user.setFailedAttempt(user.getFailedAttempt() + 1);
int currentFailedAttempt =
user.getFailedAttempt() != null ? user.getFailedAttempt() : 0;

int newFailedAttempt = currentFailedAttempt + 1;
int remainingAttempts = failedAttempt - newFailedAttempt;
if (newFailedAttempt < failedAttempt) {

user.setFailedAttempt(newFailedAttempt);
user = iEMRUserRepositoryCustom.save(user);

logger.warn("User Password Wrong");
throw new IEMRException("Invalid username or password");
} else if (user.getFailedAttempt() + 1 >= failedAttempt) {

if (remainingAttempts == 1) {
throw new IEMRException(
"Invalid username or password. Remaining attempts: 1. "
+ "If you enter wrong username or password again, your account will be locked.");
}

throw new IEMRException(
"Invalid username or password. Remaining attempts: "
+ remainingAttempts);
}else if (user.getFailedAttempt() + 1 >= failedAttempt) {
user.setFailedAttempt(user.getFailedAttempt() + 1);
user.setDeleted(true);
user = iEMRUserRepositoryCustom.save(user);
logger.warn("User Account has been locked after reaching the limit of {} failed login attempts.",
ConfigProperties.getInteger("failedLoginAttempt"));

throw new IEMRException(
"Invalid username or password. Please contact administrator.");
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
} else {
user.setFailedAttempt(user.getFailedAttempt() + 1);
user = iEMRUserRepositoryCustom.save(user);
logger.warn("Failed login attempt {} of {} for a user account.",
user.getFailedAttempt(), ConfigProperties.getInteger("failedLoginAttempt"));
throw new IEMRException(
"Invalid username or password. Please contact administrator.");
"Your account has been locked due to multiple failed login attempts. Please contact administrator.");
}
} else {
checkUserAccountStatus(user);
Expand Down
Loading