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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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;
  17. import com.gitblit.Constants.AccessRestrictionType;
  18. import com.gitblit.models.RepositoryModel;
  19. import com.gitblit.models.UserModel;
  20. /**
  21. * The DownloadZipFilter is an AccessRestrictionFilter which ensures that zip
  22. * requests for view-restricted repositories have proper authentication
  23. * credentials and are authorized.
  24. *
  25. * @author James Moger
  26. *
  27. */
  28. public class DownloadZipFilter 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. int a = url.indexOf("r=");
  38. String repository = url.substring(a + 2);
  39. if (repository.indexOf('&') > -1) {
  40. repository = repository.substring(0, repository.indexOf('&'));
  41. }
  42. return repository;
  43. }
  44. /**
  45. * Analyze the url and returns the action of the request.
  46. *
  47. * @param url
  48. * @return action of the request
  49. */
  50. @Override
  51. protected String getUrlRequestAction(String url) {
  52. return "DOWNLOAD";
  53. }
  54. /**
  55. * Determine if the action may be executed on the repository.
  56. *
  57. * @param repository
  58. * @param action
  59. * @return true if the action may be performed
  60. */
  61. @Override
  62. protected boolean isActionAllowed(RepositoryModel repository, String action) {
  63. return true;
  64. }
  65. /**
  66. * Determine if the repository requires authentication.
  67. *
  68. * @param repository
  69. * @param action
  70. * @return true if authentication required
  71. */
  72. @Override
  73. protected boolean requiresAuthentication(RepositoryModel repository, String action) {
  74. return repository.accessRestriction.atLeast(AccessRestrictionType.VIEW);
  75. }
  76. /**
  77. * Determine if the user can access the repository and perform the specified
  78. * action.
  79. *
  80. * @param repository
  81. * @param user
  82. * @param action
  83. * @return true if user may execute the action on the repository
  84. */
  85. @Override
  86. protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {
  87. return user.canAccessRepository(repository);
  88. }
  89. }