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.

PagesFilter.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2012 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.servlet;
  17. import javax.inject.Inject;
  18. import javax.inject.Singleton;
  19. import org.eclipse.jgit.lib.Repository;
  20. import com.gitblit.Constants.AccessRestrictionType;
  21. import com.gitblit.manager.IRepositoryManager;
  22. import com.gitblit.manager.IRuntimeManager;
  23. import com.gitblit.manager.IAuthenticationManager;
  24. import com.gitblit.models.RepositoryModel;
  25. import com.gitblit.models.UserModel;
  26. /**
  27. * The PagesFilter is an AccessRestrictionFilter which ensures the gh-pages
  28. * requests for a view-restricted repository are authenticated and authorized.
  29. *
  30. * @author James Moger
  31. *
  32. */
  33. @Singleton
  34. public class PagesFilter extends AccessRestrictionFilter {
  35. @Inject
  36. public PagesFilter(IRuntimeManager runtimeManager,
  37. IAuthenticationManager authenticationManager,
  38. IRepositoryManager repositoryManager) {
  39. super(runtimeManager, authenticationManager, repositoryManager);
  40. }
  41. /**
  42. * Extract the repository name from the url.
  43. *
  44. * @param url
  45. * @return repository name
  46. */
  47. @Override
  48. protected String extractRepositoryName(String url) {
  49. // get the repository name from the url by finding a known url suffix
  50. String repository = "";
  51. Repository r = null;
  52. int offset = 0;
  53. while (r == null) {
  54. int slash = url.indexOf('/', offset);
  55. if (slash == -1) {
  56. repository = url;
  57. } else {
  58. repository = url.substring(0, slash);
  59. }
  60. r = repositoryManager.getRepository(repository, false);
  61. if (r == null) {
  62. // try again
  63. offset = slash + 1;
  64. } else {
  65. // close the repo
  66. r.close();
  67. }
  68. if (repository.equals(url)) {
  69. // either only repository in url or no repository found
  70. break;
  71. }
  72. }
  73. return repository;
  74. }
  75. /**
  76. * Analyze the url and returns the action of the request.
  77. *
  78. * @param cloneUrl
  79. * @return action of the request
  80. */
  81. @Override
  82. protected String getUrlRequestAction(String suffix) {
  83. return "VIEW";
  84. }
  85. /**
  86. * Determine if a non-existing repository can be created using this filter.
  87. *
  88. * @return true if the filter allows repository creation
  89. */
  90. @Override
  91. protected boolean isCreationAllowed() {
  92. return false;
  93. }
  94. /**
  95. * Determine if the action may be executed on the repository.
  96. *
  97. * @param repository
  98. * @param action
  99. * @return true if the action may be performed
  100. */
  101. @Override
  102. protected boolean isActionAllowed(RepositoryModel repository, String action) {
  103. return true;
  104. }
  105. /**
  106. * Determine if the repository requires authentication.
  107. *
  108. * @param repository
  109. * @param action
  110. * @return true if authentication required
  111. */
  112. @Override
  113. protected boolean requiresAuthentication(RepositoryModel repository, String action) {
  114. return repository.accessRestriction.atLeast(AccessRestrictionType.VIEW);
  115. }
  116. /**
  117. * Determine if the user can access the repository and perform the specified
  118. * action.
  119. *
  120. * @param repository
  121. * @param user
  122. * @param action
  123. * @return true if user may execute the action on the repository
  124. */
  125. @Override
  126. protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {
  127. return user.canView(repository);
  128. }
  129. }