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.

AuthorizationStrategy.java 2.7KB

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