|
|
|
@ -1,5 +1,9 @@
|
|
|
|
package com.mfsys.aconnect.security.service;
|
|
|
|
package com.mfsys.aconnect.security.service;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.mfsys.aconnect.security.dto.SignupRequest;
|
|
|
|
|
|
|
|
import com.mfsys.aconnect.security.dto.SignupResponse;
|
|
|
|
|
|
|
|
import com.mfsys.common.configuration.exception.EmailAlreadyUsedException;
|
|
|
|
|
|
|
|
import com.mfsys.common.configuration.exception.UserAlreadyExistsException;
|
|
|
|
import com.mfsys.common.configuration.service.JwtService;
|
|
|
|
import com.mfsys.common.configuration.service.JwtService;
|
|
|
|
import com.mfsys.common.configuration.service.PasswordEncryptionService;
|
|
|
|
import com.mfsys.common.configuration.service.PasswordEncryptionService;
|
|
|
|
import com.mfsys.aconnect.security.dto.LoginRequest;
|
|
|
|
import com.mfsys.aconnect.security.dto.LoginRequest;
|
|
|
|
@ -15,11 +19,13 @@ public class AuthenticationService {
|
|
|
|
|
|
|
|
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
private final UserRepository userRepository;
|
|
|
|
private final JwtService jwtService;
|
|
|
|
private final JwtService jwtService;
|
|
|
|
|
|
|
|
private final PasswordEncryptionService passwordEncryptionService;
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
@Autowired
|
|
|
|
public AuthenticationService (UserRepository userRepository, PasswordEncryptionService passwordEncryptionService,
|
|
|
|
public AuthenticationService (UserRepository userRepository, PasswordEncryptionService passwordEncryptionService,
|
|
|
|
JwtService jwtService) {
|
|
|
|
JwtService jwtService) {
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
this.userRepository = userRepository;
|
|
|
|
|
|
|
|
this.passwordEncryptionService = passwordEncryptionService;
|
|
|
|
this.jwtService = jwtService;
|
|
|
|
this.jwtService = jwtService;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -39,4 +45,33 @@ public class AuthenticationService {
|
|
|
|
return new LoginResponse(user, false, user.getIsFirstLogin(), token);
|
|
|
|
return new LoginResponse(user, false, user.getIsFirstLogin(), token);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean userAlreadyExists(String userId) {
|
|
|
|
|
|
|
|
return userRepository.findByUserIdAndIsActiveTrue(userId).isPresent();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
|
|
|
public SignupResponse signup(SignupRequest request){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String porOrgacode = request.getPorOrgacode();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(userAlreadyExists(request.getUsername())){
|
|
|
|
|
|
|
|
throw new UserAlreadyExistsException(request.getPorOrgacode());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
userRepository.findByEmailAndIsActiveTrue(request.getEmail())
|
|
|
|
|
|
|
|
.ifPresent(existingUser -> {
|
|
|
|
|
|
|
|
throw new EmailAlreadyUsedException(porOrgacode, existingUser.getUserId());
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
User user = new User();
|
|
|
|
|
|
|
|
user.setUserId(request.getUsername());
|
|
|
|
|
|
|
|
user.setPassword(passwordEncryptionService.hashPassword(request.getPassword()));
|
|
|
|
|
|
|
|
user.setEmail(request.getEmail());
|
|
|
|
|
|
|
|
user.setUserFullname(request.getFullName());
|
|
|
|
|
|
|
|
user.setIsActive(true);
|
|
|
|
|
|
|
|
user.setIsFirstLogin(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
userRepository.save(user);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return new SignupResponse(true, "Signup successful");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|