Done ticket GEPAFINBE-52
This commit is contained in:
@@ -6,9 +6,13 @@ import net.gepafin.tendermanagement.config.Translator;
|
||||
import net.gepafin.tendermanagement.config.jwt.TokenProvider;
|
||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||
import net.gepafin.tendermanagement.dao.CompanyDao;
|
||||
import net.gepafin.tendermanagement.dao.LoginAttemptDao;
|
||||
import net.gepafin.tendermanagement.dao.RoleDao;
|
||||
import net.gepafin.tendermanagement.entities.LoginAttemptEntity;
|
||||
import net.gepafin.tendermanagement.entities.SamlResponseEntity;
|
||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||
import net.gepafin.tendermanagement.enums.LoginAttemptResultEnum;
|
||||
import net.gepafin.tendermanagement.enums.LoginAttemptTypeEnum;
|
||||
import net.gepafin.tendermanagement.enums.UserStatusEnum;
|
||||
import net.gepafin.tendermanagement.model.request.LoginReq;
|
||||
import net.gepafin.tendermanagement.model.response.CompanyResponse;
|
||||
@@ -57,29 +61,61 @@ public class AuthenticationService {
|
||||
@Autowired
|
||||
private SamlResponseRepository samlResponseLogRepository;
|
||||
|
||||
@Autowired
|
||||
private LoginAttemptDao loginAttemptDao;
|
||||
|
||||
@Autowired
|
||||
public AuthenticationService(TokenProvider tokenProvider, AuthenticationManager authenticationManager) {
|
||||
this.tokenProvider = tokenProvider;
|
||||
this.authenticationManager = authenticationManager;
|
||||
}
|
||||
|
||||
public JWTToken login(LoginReq loginReq) {
|
||||
log.info("Attempting login for email: {}", loginReq.getEmail());
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
|
||||
loginReq.getEmail(), loginReq.getPassword());
|
||||
Authentication authentication = this.authenticationManager.authenticate(authenticationToken);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
log.info("Authentication successful for email: {}", loginReq.getEmail());
|
||||
UserEntity user = userRepository.findByEmailIgnoreCase(loginReq.getEmail())
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
|
||||
if (Boolean.FALSE.equals(UserStatusEnum.ACTIVE.getValue().equals(user.getStatus()))) {
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG));
|
||||
public JWTToken login(LoginReq loginReq,HttpServletRequest request) {
|
||||
UserEntity user=null;
|
||||
try {
|
||||
LoginAttemptEntity loginAttemptEntity = prepareLoginAttemptEntity(loginReq, request);
|
||||
log.info("Attempting login for email: {}", loginReq.getEmail());
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
|
||||
loginReq.getEmail(), loginReq.getPassword());
|
||||
Authentication authentication = this.authenticationManager.authenticate(authenticationToken);
|
||||
SecurityContextHolder.getContext().setAuthentication(authentication);
|
||||
log.info("Authentication successful for email: {}", loginReq.getEmail());
|
||||
user = userRepository.findByEmailIgnoreCase(loginReq.getEmail())
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG)));
|
||||
loginAttemptEntity.setUserId(user.getId());
|
||||
if (Boolean.FALSE.equals(UserStatusEnum.ACTIVE.getValue().equals(user.getStatus()))) {
|
||||
throw new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.USER_NOT_FOUND_MSG));
|
||||
}
|
||||
createSuccessLoginAttempt(loginAttemptEntity);
|
||||
} catch (Exception e) {
|
||||
|
||||
|
||||
}
|
||||
return getJWTTokenBean(user, loginReq.getRememberMe());
|
||||
}
|
||||
|
||||
|
||||
private LoginAttemptEntity prepareLoginAttemptEntity(LoginReq loginUserReq, HttpServletRequest request) {
|
||||
String ipAddress = Utils.getClientIpAddress(request);
|
||||
String userAgent = request.getHeader("user-agent");
|
||||
LoginAttemptEntity loginAttemptEntity = new LoginAttemptEntity();
|
||||
loginAttemptEntity.setType(LoginAttemptTypeEnum.LOGIN.getValue());
|
||||
loginAttemptEntity.setUsername(loginUserReq.getEmail());
|
||||
loginAttemptEntity.setIpAddress(ipAddress);
|
||||
loginAttemptEntity.setUserAgent(userAgent);
|
||||
return loginAttemptEntity;
|
||||
}
|
||||
|
||||
private void createSuccessLoginAttempt(LoginAttemptEntity loginAttemptEntity) {
|
||||
loginAttemptEntity.setResult(LoginAttemptResultEnum.SUCCESS.getValue());
|
||||
loginAttemptDao.createLoginAttempt(loginAttemptEntity);
|
||||
}
|
||||
private void createFailedLoginAttempt(LoginAttemptEntity loginAttemptEntity, String errorMsg) {
|
||||
loginAttemptEntity.setResult(LoginAttemptResultEnum.FAILED.getValue());
|
||||
loginAttemptEntity.setErrorMsg(errorMsg);
|
||||
loginAttemptDao.createLoginAttempt(loginAttemptEntity);
|
||||
}
|
||||
public JWTToken getJWTTokenBean(UserEntity user, Boolean rememberMe) {
|
||||
user.setLastLogin(DateTimeUtil.DateServerToUTC(LocalDateTime.now()));
|
||||
userRepository.save(user);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.gepafin.tendermanagement.service.impl;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import net.gepafin.tendermanagement.dao.LoginAttemptDao;
|
||||
import net.gepafin.tendermanagement.entities.LoginAttemptEntity;
|
||||
import net.gepafin.tendermanagement.enums.LoginAttemptResultEnum;
|
||||
import net.gepafin.tendermanagement.enums.LoginAttemptTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.request.LoginAttemptReq;
|
||||
import net.gepafin.tendermanagement.model.response.LoginAttemptPageableResponseBean;
|
||||
import net.gepafin.tendermanagement.service.LoginAttemptService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class LoginAttemptServiceImpl implements LoginAttemptService {
|
||||
|
||||
@Autowired
|
||||
LoginAttemptDao loginAttemptDao;
|
||||
|
||||
@Override
|
||||
public LoginAttemptPageableResponseBean<List<LoginAttemptEntity>> getLoginAttemptsList(Integer pageNo, Integer pageLimit) {
|
||||
return loginAttemptDao.getLoginAttemptsList(pageNo, pageLimit);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createLoginAttempt(LoginAttemptReq loginAttemptReq, HttpServletRequest request) {
|
||||
String ipAddress = Utils.getClientIpAddress(request);
|
||||
String userAgent = request.getHeader("user-agent");
|
||||
LoginAttemptEntity loginAttemptEntity = new LoginAttemptEntity();
|
||||
loginAttemptEntity.setType(LoginAttemptTypeEnum.SWITCH.getValue());
|
||||
loginAttemptEntity.setIpAddress(ipAddress);
|
||||
loginAttemptEntity.setUserAgent(userAgent);
|
||||
loginAttemptEntity.setUsername(loginAttemptReq.getUserName());
|
||||
loginAttemptEntity.setResult(LoginAttemptResultEnum.SUCCESS.getValue());
|
||||
loginAttemptDao.createLoginAttempt(loginAttemptEntity);
|
||||
}
|
||||
}
|
||||
@@ -66,8 +66,8 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public JWTToken login(LoginReq loginReq) {
|
||||
return userDao.login(loginReq);
|
||||
public JWTToken login(LoginReq loginReq,HttpServletRequest request) {
|
||||
return userDao.login(loginReq,request);
|
||||
|
||||
}
|
||||
|
||||
@@ -119,4 +119,9 @@ public class UserServiceImpl implements UserService {
|
||||
public UserEntity getUserByBeneficiaryId(Long beneficiaryId) {
|
||||
return userDao.getUserByBeneficiaryId(beneficiaryId);
|
||||
}
|
||||
@Override
|
||||
public UserEntity getUserEntityById(Long userId) {
|
||||
// Calling DAO Function
|
||||
return userDao.validateUser(userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user