mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
Implement registration whitelist flow
This commit is contained in:
@@ -6,6 +6,11 @@ import com.openisle.service.JwtService;
|
||||
import com.openisle.service.UserService;
|
||||
import com.openisle.service.CaptchaService;
|
||||
import com.openisle.service.GoogleAuthService;
|
||||
import com.openisle.service.RegisterModeService;
|
||||
import com.openisle.service.NotificationService;
|
||||
import com.openisle.model.RegisterMode;
|
||||
import com.openisle.model.NotificationType;
|
||||
import com.openisle.repository.UserRepository;
|
||||
import lombok.Data;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@@ -23,6 +28,9 @@ public class AuthController {
|
||||
private final EmailSender emailService;
|
||||
private final CaptchaService captchaService;
|
||||
private final GoogleAuthService googleAuthService;
|
||||
private final RegisterModeService registerModeService;
|
||||
private final NotificationService notificationService;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Value("${app.captcha.enabled:false}")
|
||||
private boolean captchaEnabled;
|
||||
@@ -38,8 +46,15 @@ public class AuthController {
|
||||
if (captchaEnabled && registerCaptchaEnabled && !captchaService.verify(req.getCaptcha())) {
|
||||
return ResponseEntity.badRequest().body(Map.of("error", "Invalid captcha"));
|
||||
}
|
||||
User user = userService.register(req.getUsername(), req.getEmail(), req.getPassword());
|
||||
User user = userService.register(
|
||||
req.getUsername(), req.getEmail(), req.getPassword(), req.getReason(), registerModeService.getRegisterMode());
|
||||
emailService.sendEmail(user.getEmail(), "Verification Code", "Your verification code is " + user.getVerificationCode());
|
||||
if (!user.isApproved()) {
|
||||
for (User admin : userRepository.findByRole(com.openisle.model.Role.ADMIN)) {
|
||||
notificationService.createNotification(admin, NotificationType.REGISTER_REQUEST, null, null,
|
||||
null, user, null, user.getRegisterReason());
|
||||
}
|
||||
}
|
||||
return ResponseEntity.ok(Map.of("message", "Verification code sent"));
|
||||
}
|
||||
|
||||
@@ -67,8 +82,11 @@ public class AuthController {
|
||||
|
||||
@PostMapping("/google")
|
||||
public ResponseEntity<?> loginWithGoogle(@RequestBody GoogleLoginRequest req) {
|
||||
Optional<User> user = googleAuthService.authenticate(req.getIdToken());
|
||||
Optional<User> user = googleAuthService.authenticate(req.getIdToken(), req.getReason(), registerModeService.getRegisterMode());
|
||||
if (user.isPresent()) {
|
||||
if (!user.get().isApproved()) {
|
||||
return ResponseEntity.badRequest().body(Map.of("error", "Account awaiting approval"));
|
||||
}
|
||||
return ResponseEntity.ok(Map.of("token", jwtService.generateToken(user.get().getUsername())));
|
||||
}
|
||||
return ResponseEntity.badRequest().body(Map.of("error", "Invalid google token"));
|
||||
@@ -85,6 +103,7 @@ public class AuthController {
|
||||
private String email;
|
||||
private String password;
|
||||
private String captcha;
|
||||
private String reason;
|
||||
}
|
||||
|
||||
@Data
|
||||
@@ -97,6 +116,7 @@ public class AuthController {
|
||||
@Data
|
||||
private static class GoogleLoginRequest {
|
||||
private String idToken;
|
||||
private String reason;
|
||||
}
|
||||
|
||||
@Data
|
||||
|
||||
Reference in New Issue
Block a user