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