PageRenderTime 35ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/jira-project/jira-components/jira-core/src/main/java/com/atlassian/jira/web/action/user/ForgotLoginDetails.java

https://bitbucket.org/ahmed_bilal_360factors/jira7-core
Java | 172 lines | 128 code | 29 blank | 15 comment | 22 complexity | ef46996548697f037b4f70d2a96c8c6c MD5 | raw file
Possible License(s): Apache-2.0
  1. package com.atlassian.jira.web.action.user;
  2. import com.atlassian.jira.config.properties.APKeys;
  3. import com.atlassian.jira.config.properties.ApplicationProperties;
  4. import com.atlassian.jira.event.user.UserEventDispatcher;
  5. import com.atlassian.jira.event.user.UserEventType;
  6. import com.atlassian.jira.user.ApplicationUser;
  7. import com.atlassian.jira.user.UserUtils;
  8. import com.atlassian.jira.user.util.UserManager;
  9. import com.atlassian.jira.user.util.UserUtil;
  10. import com.atlassian.jira.util.collect.MapBuilder;
  11. import com.atlassian.jira.web.action.JiraWebActionSupport;
  12. import com.google.common.collect.Lists;
  13. import java.util.List;
  14. import java.util.Map;
  15. public class ForgotLoginDetails extends JiraWebActionSupport {
  16. private final UserUtil userUtil;
  17. private final ApplicationProperties applicationProperties;
  18. private final UserManager userManager;
  19. private String username;
  20. private String email;
  21. private boolean forgotPassword = false;
  22. private boolean forgotUserName = false;
  23. private static final String FORGOT_PASSWORD = "forgotPassword";
  24. private static final String FORGOT_USER_NAME = "forgotUserName";
  25. public ForgotLoginDetails(final UserUtil userUtil, final ApplicationProperties applicationProperties, final UserManager userManager) {
  26. this.userUtil = userUtil;
  27. this.applicationProperties = applicationProperties;
  28. this.userManager = userManager;
  29. }
  30. @Override
  31. protected String doExecute() throws Exception {
  32. if (isExternalUserManagement() || !userManager.hasPasswordWritableDirectory()) {
  33. throw new IllegalStateException("User login details can not be reset for this JIRA site.");
  34. }
  35. if (!forgotPassword && !forgotUserName) {
  36. forgotPassword = true;
  37. return INPUT;
  38. }
  39. if (forgotPassword) {
  40. return doPassword();
  41. } else {
  42. return doUserNames();
  43. }
  44. }
  45. /**
  46. * Processes the request when the user has indicated that he has forgotten his password.
  47. *
  48. * @return The view to be rendered.
  49. */
  50. private String doPassword() {
  51. if (!isSubmittedUserNameValid()) {
  52. // return the success page so no one can tell the difference between an user name that exists on this JIRA instance and one that doesn't
  53. return passwordSuccessPage();
  54. }
  55. final ApplicationUser user = userUtil.getUser(username);
  56. final int userEventType;
  57. final Map<String, Object> eventParams;
  58. // Check if we are able to reset the password
  59. if (userManager.canUpdateUserPassword(user)) {
  60. userEventType = UserEventType.USER_FORGOTPASSWORD;
  61. final UserUtil.PasswordResetToken passwordResetToken = userUtil.generatePasswordResetToken(user);
  62. eventParams = MapBuilder.<String, Object>build("username", username, "password.token", passwordResetToken.getToken(),
  63. "password.hours", passwordResetToken.getExpiryHours());
  64. } else {
  65. // For security reasons, send the user an email rather than giving UI feedback
  66. userEventType = UserEventType.USER_CANNOTCHANGEPASSWORD;
  67. eventParams = MapBuilder.<String, Object>build("username", username);
  68. }
  69. UserEventDispatcher.dispatchEvent(userEventType, user, eventParams);
  70. return passwordSuccessPage();
  71. }
  72. private String passwordSuccessPage() {
  73. return "password_success";
  74. }
  75. private boolean isSubmittedUserNameValid() {
  76. return username != null && userUtil.getUser(username) != null;
  77. }
  78. /**
  79. * Processes the request when the user has indicated that he has forgotten his user-name.
  80. *
  81. * @return The view to be rendered.
  82. */
  83. private String doUserNames() {
  84. if (!isSubmittedEmailValid()) {
  85. // return the success page so no one can tell the difference between an email that exists on this JIRA instance and one that doesn't
  86. return userNameSuccessPage();
  87. }
  88. List<ApplicationUser> users = UserUtils.getUsersByEmail(email);
  89. // Users may be internally or externally managed so we build 2 lists
  90. List<ApplicationUser> managedUsers = Lists.newArrayListWithCapacity(users.size());
  91. List<ApplicationUser> unManagedUsers = Lists.newArrayListWithCapacity(users.size());
  92. for (ApplicationUser user : users) {
  93. if (userManager.canUpdateUserPassword(user)) {
  94. managedUsers.add(user);
  95. } else {
  96. unManagedUsers.add(user);
  97. }
  98. }
  99. UserEventDispatcher.dispatchEvent(UserEventType.USER_FORGOTUSERNAME, users.get(0), MapBuilder.<String, Object>build("users", users, "managedUsers", managedUsers, "unmanagedUsers", unManagedUsers));
  100. return userNameSuccessPage();
  101. }
  102. private String userNameSuccessPage() {
  103. return "username_success";
  104. }
  105. private boolean isSubmittedEmailValid() {
  106. return !UserUtils.getUsersByEmail(email).isEmpty();
  107. }
  108. private boolean isExternalUserManagement() {
  109. return applicationProperties.getOption(APKeys.JIRA_OPTION_USER_EXTERNALMGT);
  110. }
  111. public boolean checked(String id) {
  112. if (FORGOT_PASSWORD.equals(id)) {
  113. return forgotPassword;
  114. } else if (FORGOT_USER_NAME.equals(id)) {
  115. return forgotUserName;
  116. }
  117. return false;
  118. }
  119. public String displayStyle(String id) {
  120. if (FORGOT_PASSWORD.equals(id)) {
  121. return forgotPassword ? "" : "display:none";
  122. } else if (FORGOT_USER_NAME.equals(id)) {
  123. return forgotUserName ? "" : "display:none";
  124. }
  125. return "";
  126. }
  127. public String getEmail() {
  128. return email;
  129. }
  130. public void setEmail(String email) {
  131. this.email = email;
  132. }
  133. public String getUsername() {
  134. return username;
  135. }
  136. public void setUsername(String username) {
  137. this.username = username;
  138. }
  139. public void setForgotten(String forgotten) {
  140. forgotPassword = FORGOT_PASSWORD.equals(forgotten);
  141. forgotUserName = FORGOT_USER_NAME.equals(forgotten);
  142. }
  143. }