Skip to content

Commit

Permalink
Merge pull request #2903 from WISE-Community/issue-2870-unlink-google…
Browse files Browse the repository at this point in the history
…-account

Unlinking Google account for teachers and students
  • Loading branch information
breity authored Jan 29, 2021
2 parents 75367e8 + ffd530c commit ca5d33d
Show file tree
Hide file tree
Showing 38 changed files with 1,084 additions and 371 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.acls.model.Permission;
import org.springframework.security.core.Authentication;
Expand All @@ -38,7 +37,6 @@
import org.wise.portal.presentation.web.response.SimpleResponse;
import org.wise.portal.service.authentication.DuplicateUsernameException;
import org.wise.portal.service.authentication.UserDetailsService;
import org.wise.portal.service.mail.IMailFacade;

/**
* Teacher REST API
Expand All @@ -55,12 +53,6 @@ public class TeacherAPIController extends UserAPIController {
@Autowired
private UserDetailsService userDetailsService;

@Autowired
protected IMailFacade mailService;

@Autowired
protected MessageSource messageSource;

@Value("${google.clientId:}")
private String googleClientId;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.wise.portal.presentation.web.controllers.user;

import java.util.HashMap;
import java.util.Locale;

import javax.mail.MessagingException;

import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wise.portal.domain.authentication.impl.PersistentUserDetails;
import org.wise.portal.domain.authentication.impl.TeacherUserDetails;
import org.wise.portal.domain.user.User;
import org.wise.portal.presentation.web.exception.InvalidPasswordExcpetion;

@RestController
@RequestMapping("/api/google-user")
public class GoogleUserAPIController extends UserAPIController {

@GetMapping("/check-user-exists")
boolean isGoogleIdExist(@RequestParam String googleUserId) {
return userService.retrieveUserByGoogleUserId(googleUserId) != null;
}

@GetMapping("/check-user-matches")
boolean isGoogleIdMatches(@RequestParam String googleUserId, @RequestParam String userId) {
User user = userService.retrieveUserByGoogleUserId(googleUserId);
return user != null && user.getId().toString().equals(userId);
}

@GetMapping("/get-user")
HashMap<String, Object> getUserByGoogleId(@RequestParam String googleUserId) {
User user = userService.retrieveUserByGoogleUserId(googleUserId);
HashMap<String, Object> response = new HashMap<String, Object>();
if (user == null) {
response.put("status", "error");
} else {
response.put("status", "success");
response.put("userId", user.getId());
response.put("username", user.getUserDetails().getUsername());
response.put("firstName", user.getUserDetails().getFirstname());
response.put("lastName", user.getUserDetails().getLastname());
}
return response;
}

@Secured("ROLE_USER")
@PostMapping("/unlink-account")
HashMap<String, Object> unlinkGoogleAccount(Authentication auth, @RequestParam String newPassword)
throws InvalidPasswordExcpetion {
if (newPassword.isEmpty()) {
throw new InvalidPasswordExcpetion();
}
String username = auth.getName();
User user = userService.retrieveUserByUsername(username);
((PersistentUserDetails) user.getUserDetails()).setGoogleUserId(null);
userService.updateUserPassword(user, newPassword);
boolean isSendEmail = Boolean.parseBoolean(appProperties.getProperty("send_email_enabled", "false"));
if (isSendEmail && user.isTeacher()) {
this.sendUnlinkGoogleEmail((TeacherUserDetails) user.getUserDetails());
}
return this.getUserInfo(auth, username);
}

private void sendUnlinkGoogleEmail(TeacherUserDetails userDetails) {
String[] recipients = { userDetails.getEmailAddress() };
String subject = messageSource.getMessage("unlink_google_account_success_email_subject", null,
"Successfully Unlinked Google Account", new Locale(userDetails.getLanguage()));
String username = userDetails.getUsername();
String message = messageSource.getMessage("unlink_google_account_success_email_body",
new Object[]{username},
"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,
new Locale(userDetails.getLanguage()));
try {
mailService.postMail(recipients, subject, message, appProperties.getProperty("portalemailaddress"));
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.MessageSource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.switchuser.SwitchUserFilter;
Expand Down Expand Up @@ -63,6 +64,9 @@ public class UserAPIController {
@Autowired
protected IMailFacade mailService;

@Autowired
protected MessageSource messageSource;

@Value("${google.clientId:}")
protected String googleClientId = "";

Expand Down Expand Up @@ -189,33 +193,6 @@ List<HashMap<String, String>> getSupportedLanguages() {
return langs;
}

@GetMapping("/check-google-user-exists")
boolean isGoogleIdExist(@RequestParam String googleUserId) {
return userService.retrieveUserByGoogleUserId(googleUserId) != null;
}

@GetMapping("/check-google-user-matches")
boolean isGoogleIdMatches(@RequestParam String googleUserId, @RequestParam String userId) {
User user = userService.retrieveUserByGoogleUserId(googleUserId);
return user != null && user.getId().toString().equals(userId);
}

@GetMapping("/google-user")
HashMap<String, Object> getUserByGoogleId(@RequestParam String googleUserId) {
User user = userService.retrieveUserByGoogleUserId(googleUserId);
HashMap<String, Object> response = new HashMap<String, Object>();
if (user == null) {
response.put("status", "error");
} else {
response.put("status", "success");
response.put("userId", user.getId());
response.put("username", user.getUserDetails().getUsername());
response.put("firstName", user.getUserDetails().getFirstname());
response.put("lastName", user.getUserDetails().getLastname());
}
return response;
}

private String getLanguageName(String localeString) {
if (localeString.toLowerCase().equals("zh_tw")) {
return "Chinese (Traditional)";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.wise.portal.presentation.web.exception;

public class InvalidPasswordExcpetion extends Exception {

private static final long serialVersionUID = 1L;
}
6 changes: 6 additions & 0 deletions src/main/resources/i18n/i18n.properties
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ teacher_cap.description=Text for the word "Teacher"
team_cap=Team
team_cap.description=Text for the word "Team"

unlink_google_account_success_email_subject=Successfully Unlinked Google Account
unlink_google_account_success_email_subject.description=Subject text in email to notify user about successfuly unlinking google account

unlink_google_account_success_email_body=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.\n\nYour username is: {0}\n\nThank you for using WISE,\nWISE Team
unlink_google_account_success_email_body.description=Body text in email to notify user about successfully unlinking google account

# Root (/) Pages #

accountmenu.forgot=Forgot Username or Password?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { finalize } from 'rxjs/operators';
import { LibraryProject } from '../libraryProject';
import { LibraryService } from '../../../services/library.service';
import { MatSnackBar } from '@angular/material/snack-bar';
import { Subscription } from 'rxjs';

@Component({
selector: 'app-copy-project-dialog',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,17 @@
</div>
</form>
<ng-container *ngIf="isGoogleUser">
<p class="notice" i18n>This account was created using Google and doesn't use a WISE password. If you would like to unlink your Google account, please <a routerLink="/contact">contact us</a>.</p>
<p fxLayoutAlign="start center" fxLayoutGap="8px" i18n>
<img class="google-icon" src="assets/img/icons/g-logo.png" i18n-alt alt="Google logo" />
<span>This account was created using Google and doesn't use a WISE password.</span>
</p>
<p>
<button id="unlinkGoogleAccount"
class="unlink"
type="button"
mat-raised-button
(click)="unlinkGoogleAccount()">
<span class="warn" i18n>Unlink Google Account</span>
</button>
</p>
</ng-container>
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ form {
}
}

.notice {
margin: 0 auto;
.google-icon {
height: 1.8em;
width: auto;
}

.unlink {
margin: 8px 0;
}
Loading

0 comments on commit ca5d33d

Please sign in to comment.