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