You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChangePasswordPage.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2011 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.wicket.pages;
  17. import java.text.MessageFormat;
  18. import org.apache.wicket.RestartResponseException;
  19. import org.apache.wicket.markup.html.form.Button;
  20. import org.apache.wicket.markup.html.form.StatelessForm;
  21. import org.apache.wicket.model.IModel;
  22. import org.apache.wicket.model.Model;
  23. import org.apache.wicket.protocol.http.WebRequest;
  24. import org.apache.wicket.protocol.http.WebResponse;
  25. import com.gitblit.GitBlitException;
  26. import com.gitblit.Keys;
  27. import com.gitblit.models.UserModel;
  28. import com.gitblit.utils.PasswordHash;
  29. import com.gitblit.wicket.GitBlitWebSession;
  30. import com.gitblit.wicket.NonTrimmedPasswordTextField;
  31. public class ChangePasswordPage extends RootSubPage {
  32. private IModel<String> password = new Model<String>("");
  33. private IModel<String> confirmPassword = new Model<String>("");
  34. public ChangePasswordPage() {
  35. super();
  36. if (!GitBlitWebSession.get().isLoggedIn()) {
  37. // Change password requires a login
  38. throw new RestartResponseException(getApplication().getHomePage());
  39. }
  40. if (!app().settings().getBoolean(Keys.web.authenticateAdminPages, true)
  41. && !app().settings().getBoolean(Keys.web.authenticateViewPages, false)) {
  42. // no authentication enabled
  43. throw new RestartResponseException(getApplication().getHomePage());
  44. }
  45. UserModel user = GitBlitWebSession.get().getUser();
  46. if (!app().authentication().supportsCredentialChanges(user)) {
  47. error(MessageFormat.format(getString("gb.userServiceDoesNotPermitPasswordChanges"),
  48. app().settings().getString(Keys.realm.userService, "${baseFolder}/users.conf")), true);
  49. }
  50. setupPage(getString("gb.changePassword"), user.username);
  51. StatelessForm<Void> form = new StatelessForm<Void>("passwordForm") {
  52. private static final long serialVersionUID = 1L;
  53. @Override
  54. public void onSubmit() {
  55. String password = ChangePasswordPage.this.password.getObject();
  56. String confirmPassword = ChangePasswordPage.this.confirmPassword.getObject();
  57. // ensure passwords match
  58. if (!password.equals(confirmPassword)) {
  59. error(getString("gb.passwordsDoNotMatch"));
  60. return;
  61. }
  62. // ensure password satisfies minimum length requirement
  63. int minLength = app().settings().getInteger(Keys.realm.minPasswordLength, 5);
  64. if (minLength < 4) {
  65. minLength = 4;
  66. }
  67. if (password.length() < minLength) {
  68. error(MessageFormat.format(getString("gb.passwordTooShort"), minLength));
  69. return;
  70. }
  71. UserModel user = GitBlitWebSession.get().getUser();
  72. // convert to digest, if appropriate
  73. String type = app().settings().getString(Keys.realm.passwordStorage, PasswordHash.getDefaultType().name());
  74. PasswordHash pwdHash = PasswordHash.instanceOf(type);
  75. if (pwdHash != null) {
  76. password = pwdHash.toHashedEntry(password, user.username);
  77. }
  78. user.password = password;
  79. try {
  80. app().gitblit().reviseUser(user.username, user);
  81. if (app().settings().getBoolean(Keys.web.allowCookieAuthentication, false)) {
  82. WebRequest request = (WebRequest) getRequestCycle().getRequest();
  83. WebResponse response = (WebResponse) getRequestCycle().getResponse();
  84. app().authentication().setCookie(request.getHttpServletRequest(),
  85. response.getHttpServletResponse(), user);
  86. }
  87. } catch (GitBlitException e) {
  88. error(e.getMessage());
  89. return;
  90. }
  91. setRedirect(false);
  92. info(getString("gb.passwordChanged"));
  93. setResponsePage(RepositoriesPage.class);
  94. }
  95. };
  96. NonTrimmedPasswordTextField passwordField = new NonTrimmedPasswordTextField("password", password);
  97. passwordField.setResetPassword(false);
  98. form.add(passwordField);
  99. NonTrimmedPasswordTextField confirmPasswordField = new NonTrimmedPasswordTextField("confirmPassword",
  100. confirmPassword);
  101. confirmPasswordField.setResetPassword(false);
  102. form.add(confirmPasswordField);
  103. form.add(new Button("save"));
  104. Button cancel = new Button("cancel") {
  105. private static final long serialVersionUID = 1L;
  106. @Override
  107. public void onSubmit() {
  108. setRedirect(false);
  109. error(getString("gb.passwordChangeAborted"));
  110. setResponsePage(RepositoriesPage.class);
  111. }
  112. };
  113. cancel.setDefaultFormProcessing(false);
  114. form.add(cancel);
  115. add(form);
  116. }
  117. }