|
| 1 | +package org.wise.portal.presentation.web.controllers.user; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Locale; |
| 5 | + |
| 6 | +import javax.mail.MessagingException; |
| 7 | + |
| 8 | +import org.springframework.security.access.annotation.Secured; |
| 9 | +import org.springframework.security.core.Authentication; |
| 10 | +import org.springframework.web.bind.annotation.GetMapping; |
| 11 | +import org.springframework.web.bind.annotation.PostMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 13 | +import org.springframework.web.bind.annotation.RequestParam; |
| 14 | +import org.springframework.web.bind.annotation.RestController; |
| 15 | +import org.wise.portal.domain.authentication.impl.PersistentUserDetails; |
| 16 | +import org.wise.portal.domain.authentication.impl.TeacherUserDetails; |
| 17 | +import org.wise.portal.domain.user.User; |
| 18 | +import org.wise.portal.presentation.web.exception.InvalidPasswordExcpetion; |
| 19 | + |
| 20 | +@RestController |
| 21 | +@RequestMapping("/api/google-user") |
| 22 | +public class GoogleUserAPIController extends UserAPIController { |
| 23 | + |
| 24 | + @GetMapping("/check-user-exists") |
| 25 | + boolean isGoogleIdExist(@RequestParam String googleUserId) { |
| 26 | + return userService.retrieveUserByGoogleUserId(googleUserId) != null; |
| 27 | + } |
| 28 | + |
| 29 | + @GetMapping("/check-user-matches") |
| 30 | + boolean isGoogleIdMatches(@RequestParam String googleUserId, @RequestParam String userId) { |
| 31 | + User user = userService.retrieveUserByGoogleUserId(googleUserId); |
| 32 | + return user != null && user.getId().toString().equals(userId); |
| 33 | + } |
| 34 | + |
| 35 | + @GetMapping("/get-user") |
| 36 | + HashMap<String, Object> getUserByGoogleId(@RequestParam String googleUserId) { |
| 37 | + User user = userService.retrieveUserByGoogleUserId(googleUserId); |
| 38 | + HashMap<String, Object> response = new HashMap<String, Object>(); |
| 39 | + if (user == null) { |
| 40 | + response.put("status", "error"); |
| 41 | + } else { |
| 42 | + response.put("status", "success"); |
| 43 | + response.put("userId", user.getId()); |
| 44 | + response.put("username", user.getUserDetails().getUsername()); |
| 45 | + response.put("firstName", user.getUserDetails().getFirstname()); |
| 46 | + response.put("lastName", user.getUserDetails().getLastname()); |
| 47 | + } |
| 48 | + return response; |
| 49 | + } |
| 50 | + |
| 51 | + @Secured("ROLE_USER") |
| 52 | + @PostMapping("/unlink-account") |
| 53 | + HashMap<String, Object> unlinkGoogleAccount(Authentication auth, @RequestParam String newPassword) |
| 54 | + throws InvalidPasswordExcpetion { |
| 55 | + if (newPassword.isEmpty()) { |
| 56 | + throw new InvalidPasswordExcpetion(); |
| 57 | + } |
| 58 | + String username = auth.getName(); |
| 59 | + User user = userService.retrieveUserByUsername(username); |
| 60 | + ((PersistentUserDetails) user.getUserDetails()).setGoogleUserId(null); |
| 61 | + userService.updateUserPassword(user, newPassword); |
| 62 | + boolean isSendEmail = Boolean.parseBoolean(appProperties.getProperty("send_email_enabled", "false")); |
| 63 | + if (isSendEmail && user.isTeacher()) { |
| 64 | + this.sendUnlinkGoogleEmail((TeacherUserDetails) user.getUserDetails()); |
| 65 | + } |
| 66 | + return this.getUserInfo(auth, username); |
| 67 | + } |
| 68 | + |
| 69 | + private void sendUnlinkGoogleEmail(TeacherUserDetails userDetails) { |
| 70 | + String[] recipients = { userDetails.getEmailAddress() }; |
| 71 | + String subject = messageSource.getMessage("unlink_google_account_success_email_subject", null, |
| 72 | + "Successfully Unlinked Google Account", new Locale(userDetails.getLanguage())); |
| 73 | + String username = userDetails.getUsername(); |
| 74 | + String message = messageSource.getMessage("unlink_google_account_success_email_body", |
| 75 | + new Object[]{username}, |
| 76 | + "You have unlinked your Google account from WISE. To sign in to WISE in the future, please use your username and the password you just created. Your username is: " + username, |
| 77 | + new Locale(userDetails.getLanguage())); |
| 78 | + try { |
| 79 | + mailService.postMail(recipients, subject, message, appProperties.getProperty("portalemailaddress")); |
| 80 | + } catch (MessagingException e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments