Guest User

Untitled

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