Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AuthorizationStrategy.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.Component;
  18. import org.apache.wicket.RestartResponseAtInterceptPageException;
  19. import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;
  20. import org.apache.wicket.authorization.strategies.page.AbstractPageAuthorizationStrategy;
  21. import com.gitblit.GitBlit;
  22. import com.gitblit.Keys;
  23. import com.gitblit.models.UserModel;
  24. import com.gitblit.wicket.pages.BasePage;
  25. import com.gitblit.wicket.pages.RepositoriesPage;
  26. public class AuthorizationStrategy extends AbstractPageAuthorizationStrategy implements
  27. IUnauthorizedComponentInstantiationListener {
  28. public AuthorizationStrategy() {
  29. }
  30. @SuppressWarnings({ "unchecked", "rawtypes" })
  31. @Override
  32. protected boolean isPageAuthorized(Class pageClass) {
  33. if (RepositoriesPage.class.equals(pageClass)) {
  34. // allow all requests to get to the RepositoriesPage with its inline
  35. // authentication form
  36. return true;
  37. }
  38. if (BasePage.class.isAssignableFrom(pageClass)) {
  39. boolean authenticateView = GitBlit.getBoolean(Keys.web.authenticateViewPages, true);
  40. boolean authenticateAdmin = GitBlit.getBoolean(Keys.web.authenticateAdminPages, true);
  41. boolean allowAdmin = GitBlit.getBoolean(Keys.web.allowAdministration, true);
  42. GitBlitWebSession session = GitBlitWebSession.get();
  43. if (authenticateView && !session.isLoggedIn()) {
  44. // authentication required
  45. return false;
  46. }
  47. UserModel user = session.getUser();
  48. if (pageClass.isAnnotationPresent(RequiresAdminRole.class)) {
  49. // admin page
  50. if (allowAdmin) {
  51. if (authenticateAdmin) {
  52. // authenticate admin
  53. if (user != null) {
  54. return user.canAdmin;
  55. }
  56. return false;
  57. } else {
  58. // no admin authentication required
  59. return true;
  60. }
  61. } else {
  62. // admin prohibited
  63. return false;
  64. }
  65. }
  66. }
  67. return true;
  68. }
  69. @Override
  70. public void onUnauthorizedInstantiation(Component component) {
  71. if (component instanceof BasePage) {
  72. throw new RestartResponseAtInterceptPageException(RepositoriesPage.class);
  73. }
  74. }
  75. }