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