Guest User

Untitled

a guest
Apr 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. function changePassword(email, oldPassword, newPassword) {
  2. var user = getUser(email);
  3. var oldPasswordDecrypted = decrypt(oldPassword)
  4. var newPasswordDecrypted = decrypt(newPassword)
  5. var existingPasswordDecrypted = decrypt(user.password)
  6.  
  7. if (user === null || existingPasswordDecrypted !== oldPasswordDecrypted) {
  8. throw new EmailPasswordMissMatchException();
  9. }
  10. if (oldPasswordDecrypted === newPasswordDecrypted) {
  11. throw new PasswordAlreadyInUseException();
  12. }
  13. if (user.isAdmin) {
  14. var isError = false;
  15. try {
  16. user.password = newPassword;
  17. save(user);
  18. // clear the session when the user changes the password
  19. // so that he/she has to log in again
  20. clearSession(user);
  21. } catch (e) {
  22. isError = true;
  23. // throw something went wrong, try again
  24. } finally {
  25. if (!isError) {
  26. var emails = user.getEmails();
  27. var token = generatePasswordRevertToken(oldPassword, newPassword);
  28. for (i = 0; i < emails.length; i++) {
  29. // Confirm with admin that he as in fact changed the password,
  30. // if he has not, give him option to reset the password using token
  31. sendConfirmationEmail(email[i], token);
  32. }
  33. }
  34. }
  35. } else {
  36. if (user.isEnabled) {
  37. try {
  38. user.password = newPassword;
  39. save(user);
  40. // clear the session when the user changes the password
  41. // so that he/she has to log in again
  42. clearSession(user);
  43. } catch (e) {
  44. throw new SomethingWentWrongException();
  45. }
  46. } else {
  47. throw new UnsupporedOperationException();
  48. }
  49. }
  50.  
  51. }
Add Comment
Please, Sign In to add comment