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.

LoginPage.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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;
  17. import org.apache.wicket.PageParameters;
  18. import org.apache.wicket.markup.html.WebPage;
  19. import org.apache.wicket.markup.html.basic.Label;
  20. import org.apache.wicket.markup.html.form.Form;
  21. import org.apache.wicket.markup.html.form.PasswordTextField;
  22. import org.apache.wicket.markup.html.form.StatelessForm;
  23. import org.apache.wicket.markup.html.form.TextField;
  24. import org.apache.wicket.markup.html.panel.FeedbackPanel;
  25. import org.apache.wicket.model.IModel;
  26. import org.apache.wicket.model.Model;
  27. import com.gitblit.Constants;
  28. import com.gitblit.GitBlit;
  29. import com.gitblit.Keys;
  30. import com.gitblit.wicket.models.UserModel;
  31. public class LoginPage extends WebPage {
  32. IModel<String> username = new Model<String>("");
  33. IModel<String> password = new Model<String>("");
  34. public LoginPage(PageParameters params) {
  35. super(params);
  36. add(new Label("title", GitBlit.self().settings().getString(Keys.web.siteName, Constants.NAME)));
  37. add(new Label("name", Constants.NAME));
  38. Form<Void> loginForm = new LoginForm("loginForm");
  39. loginForm.add(new TextField<String>("username", username));
  40. loginForm.add(new PasswordTextField("password", password));
  41. loginForm.add(new FeedbackPanel("feedback"));
  42. add(loginForm);
  43. }
  44. class LoginForm extends StatelessForm<Void> {
  45. private static final long serialVersionUID = 1L;
  46. public LoginForm(String id) {
  47. super(id);
  48. // If we are already logged in because user directly accessed
  49. // the login url, redirect to the home page
  50. if (GitBlitWebSession.get().isLoggedIn()) {
  51. setRedirect(true);
  52. setResponsePage(getApplication().getHomePage());
  53. }
  54. }
  55. @Override
  56. public void onSubmit() {
  57. String username = LoginPage.this.username.getObject();
  58. char[] password = LoginPage.this.password.getObject().toCharArray();
  59. UserModel user = GitBlit.self().authenticate(username, password);
  60. if (user == null)
  61. error("Invalid username or password!");
  62. else
  63. loginUser(user);
  64. }
  65. }
  66. private void loginUser(UserModel user) {
  67. if (user != null) {
  68. // Set the user into the session
  69. GitBlitWebSession.get().setUser(user);
  70. if (!continueToOriginalDestination()) {
  71. // Redirect to home page
  72. setResponsePage(getApplication().getHomePage());
  73. }
  74. }
  75. }
  76. }