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.3KB

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