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.

GitblitUploadPackFactory.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.git;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Map;
  20. import javax.servlet.http.HttpServletRequest;
  21. import org.eclipse.jgit.lib.Ref;
  22. import org.eclipse.jgit.lib.Repository;
  23. import org.eclipse.jgit.transport.RefFilter;
  24. import org.eclipse.jgit.transport.UploadPack;
  25. import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
  26. import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
  27. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  28. import com.gitblit.Constants;
  29. import com.gitblit.GitBlit;
  30. import com.gitblit.models.UserModel;
  31. /**
  32. * The upload pack factory creates an upload pack which controls what refs are
  33. * advertised to cloning/pulling clients.
  34. *
  35. * @author James Moger
  36. *
  37. * @param <X> the connection type
  38. */
  39. public class GitblitUploadPackFactory<X> implements UploadPackFactory<X> {
  40. @Override
  41. public UploadPack create(X req, Repository db)
  42. throws ServiceNotEnabledException, ServiceNotAuthorizedException {
  43. UserModel user = UserModel.ANONYMOUS;
  44. int timeout = 0;
  45. if (req instanceof HttpServletRequest) {
  46. // http/https request may or may not be authenticated
  47. user = GitBlit.self().authenticate((HttpServletRequest) req);
  48. if (user == null) {
  49. user = UserModel.ANONYMOUS;
  50. }
  51. } else if (req instanceof GitDaemonClient) {
  52. // git daemon request is always anonymous
  53. GitDaemonClient client = (GitDaemonClient) req;
  54. // set timeout from Git daemon
  55. timeout = client.getDaemon().getTimeout();
  56. }
  57. RefFilter refFilter = new UserRefFilter(user);
  58. UploadPack up = new UploadPack(db);
  59. up.setRefFilter(refFilter);
  60. up.setTimeout(timeout);
  61. return up;
  62. }
  63. /**
  64. * Restricts advertisement of certain refs based on the permission of the
  65. * requesting user.
  66. */
  67. public static class UserRefFilter implements RefFilter {
  68. final UserModel user;
  69. public UserRefFilter(UserModel user) {
  70. this.user = user;
  71. }
  72. @Override
  73. public Map<String, Ref> filter(Map<String, Ref> refs) {
  74. if (user.canAdmin()) {
  75. // admins can see all refs
  76. return refs;
  77. }
  78. // normal users can not clone any gitblit refs
  79. // JGit's RefMap is custom and does not support iterator removal :(
  80. List<String> toRemove = new ArrayList<String>();
  81. for (String ref : refs.keySet()) {
  82. if (ref.startsWith(Constants.R_GITBLIT)) {
  83. toRemove.add(ref);
  84. }
  85. }
  86. for (String ref : toRemove) {
  87. refs.remove(ref);
  88. }
  89. return refs;
  90. }
  91. }
  92. }