James Moger
2013-03-29 ffe9bd0ea959cf768983ff1a3d2de897390016d7
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     /**
72cb19 80      * Determine if a non-existing repository can be created using this filter.
JM 81      *  
82      * @return true if the filter allows repository creation
83      */
84     @Override
85     protected boolean isCreationAllowed() {
86         return false;
87     }
88
89     /**
b74031 90      * Determine if the action may be executed on the repository.
JM 91      * 
92      * @param repository
93      * @param action
94      * @return true if the action may be performed
95      */
96     @Override
97     protected boolean isActionAllowed(RepositoryModel repository, String action) {
98         return true;
99     }
100     
101     /**
11924d 102      * Determine if the repository requires authentication.
JM 103      * 
104      * @param repository
3f8cd4 105      * @param action
11924d 106      * @return true if authentication required
JM 107      */
108     @Override
3f8cd4 109     protected boolean requiresAuthentication(RepositoryModel repository, String action) {
11924d 110         return repository.accessRestriction.atLeast(AccessRestrictionType.VIEW);
JM 111     }
112
113     /**
114      * Determine if the user can access the repository and perform the specified
115      * action.
116      * 
117      * @param repository
118      * @param user
119      * @param action
120      * @return true if user may execute the action on the repository
121      */
122     @Override
123     protected boolean canAccess(RepositoryModel repository, UserModel user, String action) {        
20714a 124         return user.canView(repository);
11924d 125     }
JM 126 }