您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ChangePasswordPage.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.PasswordTextField;
  21. import org.apache.wicket.markup.html.form.StatelessForm;
  22. import org.apache.wicket.model.IModel;
  23. import org.apache.wicket.model.Model;
  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.StringUtils;
  29. import com.gitblit.wicket.GitBlitWebSession;
  30. public class ChangePasswordPage extends RootSubPage {
  31. IModel<String> password = new Model<String>("");
  32. IModel<String> confirmPassword = new Model<String>("");
  33. public ChangePasswordPage() {
  34. super();
  35. if (!GitBlitWebSession.get().isLoggedIn()) {
  36. // Change password requires a login
  37. throw new RestartResponseException(getApplication().getHomePage());
  38. }
  39. if (!app().settings().getBoolean(Keys.web.authenticateAdminPages, true)
  40. && !app().settings().getBoolean(Keys.web.authenticateViewPages, false)) {
  41. // no authentication enabled
  42. throw new RestartResponseException(getApplication().getHomePage());
  43. }
  44. UserModel user = GitBlitWebSession.get().getUser();
  45. if (!app().users().supportsCredentialChanges(user)) {
  46. error(MessageFormat.format(getString("gb.userServiceDoesNotPermitPasswordChanges"),
  47. app().settings().getString(Keys.realm.userService, "${baseFolder}/users.conf")), true);
  48. }
  49. setupPage(getString("gb.changePassword"), user.username);
  50. StatelessForm<Void> form = new StatelessForm<Void>("passwordForm") {
  51. private static final long serialVersionUID = 1L;
  52. @Override
  53. public void onSubmit() {
  54. String password = ChangePasswordPage.this.password.getObject();
  55. String confirmPassword = ChangePasswordPage.this.confirmPassword.getObject();
  56. // ensure passwords match
  57. if (!password.equals(confirmPassword)) {
  58. error(getString("gb.passwordsDoNotMatch"));
  59. return;
  60. }
  61. // ensure password satisfies minimum length requirement
  62. int minLength = app().settings().getInteger(Keys.realm.minPasswordLength, 5);
  63. if (minLength < 4) {
  64. minLength = 4;
  65. }
  66. if (password.length() < minLength) {
  67. error(MessageFormat.format(getString("gb.passwordTooShort"), minLength));
  68. return;
  69. }
  70. UserModel user = GitBlitWebSession.get().getUser();
  71. // convert to MD5 digest, if appropriate
  72. String type = app().settings().getString(Keys.realm.passwordStorage, "md5");
  73. if (type.equalsIgnoreCase("md5")) {
  74. // store MD5 digest of password
  75. password = StringUtils.MD5_TYPE + StringUtils.getMD5(password);
  76. } else if (type.equalsIgnoreCase("combined-md5")) {
  77. // store MD5 digest of username+password
  78. password = StringUtils.COMBINED_MD5_TYPE
  79. + StringUtils.getMD5(user.username.toLowerCase() + password);
  80. }
  81. user.password = password;
  82. try {
  83. app().gitblit().updateUserModel(user.username, user, false);
  84. if (app().settings().getBoolean(Keys.web.allowCookieAuthentication, false)) {
  85. WebResponse response = (WebResponse) getRequestCycle().getResponse();
  86. app().session().setCookie(response.getHttpServletResponse(), user);
  87. }
  88. } catch (GitBlitException e) {
  89. error(e.getMessage());
  90. return;
  91. }
  92. setRedirect(false);
  93. info(getString("gb.passwordChanged"));
  94. setResponsePage(RepositoriesPage.class);
  95. }
  96. };
  97. PasswordTextField passwordField = new PasswordTextField("password", password);
  98. passwordField.setResetPassword(false);
  99. form.add(passwordField);
  100. PasswordTextField confirmPasswordField = new PasswordTextField("confirmPassword",
  101. confirmPassword);
  102. confirmPasswordField.setResetPassword(false);
  103. form.add(confirmPasswordField);
  104. form.add(new Button("save"));
  105. Button cancel = new Button("cancel") {
  106. private static final long serialVersionUID = 1L;
  107. @Override
  108. public void onSubmit() {
  109. setRedirect(false);
  110. error(getString("gb.passwordChangeAborted"));
  111. setResponsePage(RepositoriesPage.class);
  112. }
  113. };
  114. cancel.setDefaultFormProcessing(false);
  115. form.add(cancel);
  116. add(form);
  117. }
  118. }