Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.PageParameters;
  19. import org.apache.wicket.markup.html.form.PasswordTextField;
  20. import org.apache.wicket.markup.html.form.StatelessForm;
  21. import org.apache.wicket.markup.html.form.TextField;
  22. import org.apache.wicket.markup.html.link.BookmarkablePageLink;
  23. import org.apache.wicket.model.IModel;
  24. import org.apache.wicket.model.Model;
  25. import org.apache.wicket.protocol.http.WebResponse;
  26. import com.gitblit.Constants;
  27. import com.gitblit.GitBlit;
  28. import com.gitblit.Keys;
  29. import com.gitblit.models.UserModel;
  30. import com.gitblit.utils.StringUtils;
  31. import com.gitblit.wicket.GitBlitWebSession;
  32. import com.gitblit.wicket.WicketUtils;
  33. /**
  34. * Root page is a topbar, navigable page like Repositories, Users, or
  35. * Federation.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public abstract class RootPage extends BasePage {
  41. boolean showAdmin;
  42. IModel<String> username = new Model<String>("");
  43. IModel<String> password = new Model<String>("");
  44. public RootPage() {
  45. super();
  46. }
  47. public RootPage(PageParameters params) {
  48. super(params);
  49. }
  50. @Override
  51. protected void setupPage(String repositoryName, String pageName) {
  52. if (GitBlit.getBoolean(Keys.web.authenticateAdminPages, true)) {
  53. boolean allowAdmin = GitBlit.getBoolean(Keys.web.allowAdministration, false);
  54. showAdmin = allowAdmin && GitBlitWebSession.get().canAdmin();
  55. // authentication requires state and session
  56. setStatelessHint(false);
  57. } else {
  58. showAdmin = GitBlit.getBoolean(Keys.web.allowAdministration, false);
  59. if (GitBlit.getBoolean(Keys.web.authenticateViewPages, false)) {
  60. // authentication requires state and session
  61. setStatelessHint(false);
  62. } else {
  63. // no authentication required, no state and no session required
  64. setStatelessHint(true);
  65. }
  66. }
  67. boolean showRegistrations = GitBlit.canFederate()
  68. && GitBlit.getBoolean(Keys.web.showFederationRegistrations, false);
  69. // navigation links
  70. add(new BookmarkablePageLink<Void>("repositories", RepositoriesPage.class));
  71. add(new BookmarkablePageLink<Void>("users", UsersPage.class).setVisible(showAdmin));
  72. add(new BookmarkablePageLink<Void>("federation", FederationPage.class).setVisible(showAdmin
  73. || showRegistrations));
  74. // login form
  75. StatelessForm<Void> loginForm = new StatelessForm<Void>("loginForm") {
  76. private static final long serialVersionUID = 1L;
  77. @Override
  78. public void onSubmit() {
  79. String username = RootPage.this.username.getObject();
  80. char[] password = RootPage.this.password.getObject().toCharArray();
  81. UserModel user = GitBlit.self().authenticate(username, password);
  82. if (user == null) {
  83. error("Invalid username or password!");
  84. } else if (user.username.equals(Constants.FEDERATION_USER)) {
  85. // disallow the federation user from logging in via the
  86. // web ui
  87. error("Invalid username or password!");
  88. user = null;
  89. } else {
  90. loginUser(user);
  91. }
  92. }
  93. };
  94. TextField<String> unameField = new TextField<String>("username", username);
  95. WicketUtils.setInputPlaceholder(unameField, getString("gb.username"));
  96. loginForm.add(unameField);
  97. PasswordTextField pwField = new PasswordTextField("password", password);
  98. WicketUtils.setInputPlaceholder(pwField, getString("gb.password"));
  99. loginForm.add(pwField);
  100. add(loginForm);
  101. if (GitBlit.getBoolean(Keys.web.authenticateViewPages, true)
  102. || GitBlit.getBoolean(Keys.web.authenticateAdminPages, true)) {
  103. loginForm.setVisible(!GitBlitWebSession.get().isLoggedIn());
  104. } else {
  105. loginForm.setVisible(false);
  106. }
  107. // display an error message cached from a redirect
  108. String cachedMessage = GitBlitWebSession.get().clearErrorMessage();
  109. if (!StringUtils.isEmpty(cachedMessage)) {
  110. error(cachedMessage);
  111. } else if (showAdmin) {
  112. int pendingProposals = GitBlit.self().getPendingFederationProposals().size();
  113. if (pendingProposals == 1) {
  114. info("There is 1 federation proposal awaiting review.");
  115. } else if (pendingProposals > 1) {
  116. info(MessageFormat.format("There are {0} federation proposals awaiting review.",
  117. pendingProposals));
  118. }
  119. }
  120. super.setupPage(repositoryName, pageName);
  121. }
  122. private void loginUser(UserModel user) {
  123. if (user != null) {
  124. // Set the user into the session
  125. GitBlitWebSession.get().setUser(user);
  126. // Set Cookie
  127. if (GitBlit.getBoolean(Keys.web.allowCookieAuthentication, false)) {
  128. WebResponse response = (WebResponse) getRequestCycle().getResponse();
  129. GitBlit.self().setCookie(response, user);
  130. }
  131. if (!continueToOriginalDestination()) {
  132. // Redirect to home page
  133. setResponsePage(getApplication().getHomePage());
  134. }
  135. }
  136. }
  137. }