James Moger
2012-02-02 d394d950100a97b7d73f0e162b64b0b8f3cef988
commit | author | age
11924d 1 /*
JM 2  * Copyright 2012 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 org.eclipse.jgit.lib.Repository;
19
20 import com.gitblit.Constants.AccessRestrictionType;
21 import com.gitblit.models.RepositoryModel;
22 import com.gitblit.models.UserModel;
23
24 /**
25  * The PagesFilter is an AccessRestrictionFilter which ensures the gh-pages
26  * requests for a view-restricted repository are authenticated and authorized.
27  * 
28  * @author James Moger
29  * 
30  */
31 public class PagesFilter extends AccessRestrictionFilter {
32
33     /**
34      * Extract the repository name from the url.
35      * 
36      * @param url
37      * @return repository name
38      */
39     @Override
40     protected String extractRepositoryName(String url) {        
41         // get the repository name from the url by finding a known url suffix
42         String repository = "";        
43         Repository r = null;
44         int offset = 0;
45         while (r == null) {
46             int slash = url.indexOf('/', offset);
47             if (slash == -1) {
48                 repository = url;
49             } else {
50                 repository = url.substring(0, slash);
51             }
52             r = GitBlit.self().getRepository(repository, false);
53             if (r == null) {
54                 // try again
55                 offset = slash + 1;    
56             } else {
57                 // close the repo
58                 r.close();
59             }            
60             if (repository.equals(url)) {
61                 // either only repository in url or no repository found
62                 break;
63             }
64         }
65         return repository;
66     }
67
68     /**
69      * Analyze the url and returns the action of the request.
70      * 
71      * @param url
72      * @return action of the request
73      */
74     @Override
75     protected String getUrlRequestAction(String suffix) {
76         return "VIEW";
77     }
78
79     /**
80      * Determine if the repository requires authentication.
81      * 
82      * @param repository
83      * @return true if authentication required
84      */
85     @Override
86     protected boolean requiresAuthentication(RepositoryModel repository) {
87         return repository.accessRestriction.atLeast(AccessRestrictionType.VIEW);
88     }
89
90     /**
91      * Determine if the user can access the repository and perform the specified
92      * action.
93      * 
94      * @param repository
95      * @param user
96      * @param action
97      * @return true if user may execute the action on the repository
98      */
99     @Override
100     protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {        
101         return user.canAccessRepository(repository);
102     }
103 }