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.

DownloadZipFilter.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.servlet;
  17. import javax.inject.Inject;
  18. import javax.inject.Singleton;
  19. import com.gitblit.Constants.AccessRestrictionType;
  20. import com.gitblit.manager.IRepositoryManager;
  21. import com.gitblit.manager.IRuntimeManager;
  22. import com.gitblit.manager.IAuthenticationManager;
  23. import com.gitblit.models.RepositoryModel;
  24. import com.gitblit.models.UserModel;
  25. /**
  26. * The DownloadZipFilter is an AccessRestrictionFilter which ensures that zip
  27. * requests for view-restricted repositories have proper authentication
  28. * credentials and are authorized.
  29. *
  30. * @author James Moger
  31. *
  32. */
  33. @Singleton
  34. public class DownloadZipFilter extends AccessRestrictionFilter {
  35. @Inject
  36. public DownloadZipFilter(
  37. IRuntimeManager runtimeManager,
  38. IAuthenticationManager authenticationManager,
  39. IRepositoryManager repositoryManager) {
  40. super(runtimeManager, authenticationManager, repositoryManager);
  41. }
  42. /**
  43. * Extract the repository name from the url.
  44. *
  45. * @param url
  46. * @return repository name
  47. */
  48. @Override
  49. protected String extractRepositoryName(String url) {
  50. int a = url.indexOf("r=");
  51. String repository = url.substring(a + 2);
  52. if (repository.indexOf('&') > -1) {
  53. repository = repository.substring(0, repository.indexOf('&'));
  54. }
  55. return repository;
  56. }
  57. /**
  58. * Analyze the url and returns the action of the request.
  59. *
  60. * @param url
  61. * @return action of the request
  62. */
  63. @Override
  64. protected String getUrlRequestAction(String url) {
  65. return "DOWNLOAD";
  66. }
  67. /**
  68. * Determine if a non-existing repository can be created using this filter.
  69. *
  70. * @return true if the filter allows repository creation
  71. */
  72. @Override
  73. protected boolean isCreationAllowed() {
  74. return false;
  75. }
  76. /**
  77. * Determine if the action may be executed on the repository.
  78. *
  79. * @param repository
  80. * @param action
  81. * @return true if the action may be performed
  82. */
  83. @Override
  84. protected boolean isActionAllowed(RepositoryModel repository, String action) {
  85. return true;
  86. }
  87. /**
  88. * Determine if the repository requires authentication.
  89. *
  90. * @param repository
  91. * @param action
  92. * @return true if authentication required
  93. */
  94. @Override
  95. protected boolean requiresAuthentication(RepositoryModel repository, String action) {
  96. return repository.accessRestriction.atLeast(AccessRestrictionType.VIEW);
  97. }
  98. /**
  99. * Determine if the user can access the repository and perform the specified
  100. * action.
  101. *
  102. * @param repository
  103. * @param user
  104. * @param action
  105. * @return true if user may execute the action on the repository
  106. */
  107. @Override
  108. protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {
  109. return user.canView(repository);
  110. }
  111. }