James Moger
2012-09-10 fabe060d3a435f116128851f828e35c2af5fde67
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     /**
b74031 80      * Determine if the action may be executed on the repository.
JM 81      * 
82      * @param repository
83      * @param action
84      * @return true if the action may be performed
85      */
86     @Override
87     protected boolean isActionAllowed(RepositoryModel repository, String action) {
88         return true;
89     }
90     
91     /**
11924d 92      * Determine if the repository requires authentication.
JM 93      * 
94      * @param repository
3f8cd4 95      * @param action
11924d 96      * @return true if authentication required
JM 97      */
98     @Override
3f8cd4 99     protected boolean requiresAuthentication(RepositoryModel repository, String action) {
11924d 100         return repository.accessRestriction.atLeast(AccessRestrictionType.VIEW);
JM 101     }
102
103     /**
104      * Determine if the user can access the repository and perform the specified
105      * action.
106      * 
107      * @param repository
108      * @param user
109      * @param action
110      * @return true if user may execute the action on the repository
111      */
112     @Override
113     protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {        
114         return user.canAccessRepository(repository);
115     }
116 }