use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.
the class PasswordChangeController method changePassword.
@GetMapping
public String changePassword(Model model) {
User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
if (!user.isEmailVerified()) {
log.warn("User not verified and tried to change the password!");
model.addAttribute("error", "The user is not verified, and hence can't change the password");
} else {
if (!model.containsAttribute("password")) {
PasswordChange passwordChange = new PasswordChange();
model.addAttribute("password", passwordChange);
}
model.addAttribute("blacklist", Arrays.asList(user.getName(), user.getEmail(), user.getUserId()));
}
return "user/change_password";
}
use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.
the class TwoFAService method generate2FASecret.
/**
* Generates a random secret to be be seed of TOTP secret and QR Code URL
* @return {@link TwoFASecret} enclosing the user ID and secret
*/
TwoFASecret generate2FASecret() {
User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
if (user.getTotpSecret() != null) {
log.warn("User {} is overwriting TOTP with new one", user.getUserId());
}
String secret = Base32.random();
log.debug("Adding secret {} to user {}", secret, user.getUserId());
TwoFASecret twoFASecret = new TwoFASecret(user.getUserId(), secret);
log.debug("QR code URL: {}", twoFASecret.getQrUrl());
return twoFASecret;
}
use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.
the class TwoFAService method disable2FA.
void disable2FA() {
User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
user.setUsing2fa(false);
user.setTotpSecret(null);
userService.save(user);
}
use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.
the class PasswordChangeController method savePassword.
@PostMapping
public String savePassword(@Valid PasswordChange passwordChange, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
if (bindingResult.hasErrors()) {
redirectAttributes.addFlashAttribute("password", passwordChange);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.password", bindingResult);
} else {
try {
passwordChangeService.changePassword(user, passwordChange);
redirectAttributes.addFlashAttribute("flash_messages", Flash.success("Password was changed successfully"));
return "redirect:/profile/settings#account";
} catch (PasswordValidationException pve) {
redirectAttributes.addFlashAttribute("pass_errors", pve.getMessage());
}
}
return "redirect:/profile/password/change";
}
use of amu.zhcet.data.user.UserNotFoundException in project zhcet-web by zhcet-amu.
the class TwoFAService method enable2FA.
/**
* Takes in the secret and OTP from frontend and enables 2 factor authentication if they are verified
* @param secret String secret for the user
* @param code String OTP code
*/
void enable2FA(String secret, String code) {
User user = userService.getLoggedInUser().orElseThrow(UserNotFoundException::new);
if (!isValidOtp(secret, code)) {
throw new RuntimeException("Could not verify code, please try again");
}
user.setUsing2fa(true);
user.setTotpSecret(secret);
userService.save(user);
}
Aggregations