commit | author | age
|
fe24a0
|
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.models;
|
|
17 |
|
|
18 |
import java.io.Serializable;
|
d7905a
|
19 |
import java.util.ArrayList;
|
fe24a0
|
20 |
import java.util.Collection;
|
20714a
|
21 |
import java.util.HashMap;
|
fe24a0
|
22 |
import java.util.HashSet;
|
0b9119
|
23 |
import java.util.List;
|
20714a
|
24 |
import java.util.Map;
|
fe24a0
|
25 |
import java.util.Set;
|
20714a
|
26 |
|
JM |
27 |
import com.gitblit.Constants.AccessPermission;
|
|
28 |
import com.gitblit.Constants.AccessRestrictionType;
|
|
29 |
import com.gitblit.Constants.Unused;
|
fe24a0
|
30 |
|
JM |
31 |
/**
|
|
32 |
* TeamModel is a serializable model class that represents a group of users and
|
|
33 |
* a list of accessible repositories.
|
|
34 |
*
|
|
35 |
* @author James Moger
|
|
36 |
*
|
|
37 |
*/
|
|
38 |
public class TeamModel implements Serializable, Comparable<TeamModel> {
|
|
39 |
|
|
40 |
private static final long serialVersionUID = 1L;
|
|
41 |
|
|
42 |
// field names are reflectively mapped in EditTeam page
|
|
43 |
public String name;
|
|
44 |
public final Set<String> users = new HashSet<String>();
|
20714a
|
45 |
// retained for backwards-compatibility with RPC clients
|
JM |
46 |
@Deprecated
|
fe24a0
|
47 |
public final Set<String> repositories = new HashSet<String>();
|
20714a
|
48 |
public final Map<String, AccessPermission> permissions = new HashMap<String, AccessPermission>();
|
0b9119
|
49 |
public final Set<String> mailingLists = new HashSet<String>();
|
d7905a
|
50 |
public final List<String> preReceiveScripts = new ArrayList<String>();
|
JM |
51 |
public final List<String> postReceiveScripts = new ArrayList<String>();
|
fe24a0
|
52 |
|
JM |
53 |
public TeamModel(String name) {
|
|
54 |
this.name = name;
|
|
55 |
}
|
|
56 |
|
20714a
|
57 |
/**
|
JM |
58 |
* @use hasRepositoryPermission
|
|
59 |
* @param name
|
|
60 |
* @return
|
|
61 |
*/
|
|
62 |
@Deprecated
|
|
63 |
@Unused
|
fe24a0
|
64 |
public boolean hasRepository(String name) {
|
20714a
|
65 |
return hasRepositoryPermission(name);
|
fe24a0
|
66 |
}
|
JM |
67 |
|
20714a
|
68 |
@Deprecated
|
JM |
69 |
@Unused
|
fe24a0
|
70 |
public void addRepository(String name) {
|
20714a
|
71 |
addRepositoryPermission(name);
|
fe24a0
|
72 |
}
|
JM |
73 |
|
20714a
|
74 |
@Deprecated
|
JM |
75 |
@Unused
|
fe24a0
|
76 |
public void addRepositories(Collection<String> names) {
|
20714a
|
77 |
addRepositoryPermissions(names);
|
JM |
78 |
}
|
fe24a0
|
79 |
|
20714a
|
80 |
@Deprecated
|
JM |
81 |
@Unused
|
fe24a0
|
82 |
public void removeRepository(String name) {
|
20714a
|
83 |
removeRepositoryPermission(name);
|
fe24a0
|
84 |
}
|
JM |
85 |
|
20714a
|
86 |
/**
|
JM |
87 |
* Returns true if the team has any type of specified access permission for
|
|
88 |
* this repository.
|
|
89 |
*
|
|
90 |
* @param name
|
|
91 |
* @return true if team has a specified access permission for the repository
|
|
92 |
*/
|
|
93 |
public boolean hasRepositoryPermission(String name) {
|
|
94 |
String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
|
|
95 |
return permissions.containsKey(repository) || repositories.contains(repository);
|
|
96 |
}
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Adds a repository permission to the team.
|
|
100 |
* <p>
|
|
101 |
* Role may be formatted as:
|
|
102 |
* <ul>
|
|
103 |
* <li> myrepo.git <i>(this is implicitly RW+)</i>
|
|
104 |
* <li> RW+:myrepo.git
|
|
105 |
* </ul>
|
|
106 |
* @param role
|
|
107 |
*/
|
|
108 |
public void addRepositoryPermission(String role) {
|
|
109 |
AccessPermission permission = AccessPermission.permissionFromRole(role);
|
|
110 |
String repository = AccessPermission.repositoryFromRole(role).toLowerCase();
|
|
111 |
repositories.add(repository);
|
|
112 |
permissions.put(repository, permission);
|
|
113 |
}
|
|
114 |
|
|
115 |
public void addRepositoryPermissions(Collection<String> roles) {
|
|
116 |
for (String role:roles) {
|
|
117 |
addRepositoryPermission(role);
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
public AccessPermission removeRepositoryPermission(String name) {
|
|
122 |
String repository = AccessPermission.repositoryFromRole(name).toLowerCase();
|
|
123 |
repositories.remove(repository);
|
|
124 |
return permissions.remove(repository);
|
|
125 |
}
|
|
126 |
|
|
127 |
public void setRepositoryPermission(String repository, AccessPermission permission) {
|
|
128 |
permissions.put(repository.toLowerCase(), permission);
|
|
129 |
repositories.add(repository.toLowerCase());
|
|
130 |
}
|
|
131 |
|
|
132 |
public AccessPermission getRepositoryPermission(RepositoryModel repository) {
|
|
133 |
AccessPermission permission = AccessPermission.NONE;
|
|
134 |
if (permissions.containsKey(repository.name.toLowerCase())) {
|
5d7545
|
135 |
// exact repository permission specified
|
20714a
|
136 |
AccessPermission p = permissions.get(repository.name.toLowerCase());
|
JM |
137 |
if (p != null) {
|
|
138 |
permission = p;
|
|
139 |
}
|
5d7545
|
140 |
} else {
|
JM |
141 |
// search for regex permission match
|
|
142 |
for (String key : permissions.keySet()) {
|
|
143 |
if (repository.name.matches(key)) {
|
|
144 |
AccessPermission p = permissions.get(key);
|
|
145 |
if (p != null) {
|
|
146 |
permission = p;
|
|
147 |
}
|
|
148 |
}
|
|
149 |
}
|
20714a
|
150 |
}
|
JM |
151 |
return permission;
|
|
152 |
}
|
|
153 |
|
|
154 |
private boolean canAccess(RepositoryModel repository, AccessRestrictionType ifRestriction, AccessPermission requirePermission) {
|
|
155 |
if (repository.accessRestriction.atLeast(ifRestriction)) {
|
|
156 |
AccessPermission permission = getRepositoryPermission(repository);
|
|
157 |
return permission.atLeast(requirePermission);
|
|
158 |
}
|
|
159 |
return true;
|
|
160 |
}
|
|
161 |
|
|
162 |
public boolean canView(RepositoryModel repository) {
|
|
163 |
return canAccess(repository, AccessRestrictionType.VIEW, AccessPermission.VIEW);
|
|
164 |
}
|
|
165 |
|
|
166 |
public boolean canClone(RepositoryModel repository) {
|
|
167 |
return canAccess(repository, AccessRestrictionType.CLONE, AccessPermission.CLONE);
|
|
168 |
}
|
|
169 |
|
|
170 |
public boolean canPush(RepositoryModel repository) {
|
|
171 |
if (repository.isFrozen) {
|
|
172 |
return false;
|
|
173 |
}
|
|
174 |
return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.PUSH);
|
|
175 |
}
|
|
176 |
|
|
177 |
public boolean canCreateRef(RepositoryModel repository) {
|
|
178 |
if (repository.isFrozen) {
|
|
179 |
return false;
|
|
180 |
}
|
|
181 |
return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.CREATE);
|
|
182 |
}
|
|
183 |
|
|
184 |
public boolean canDeleteRef(RepositoryModel repository) {
|
|
185 |
if (repository.isFrozen) {
|
|
186 |
return false;
|
|
187 |
}
|
|
188 |
return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.DELETE);
|
|
189 |
}
|
|
190 |
|
|
191 |
public boolean canRewindRef(RepositoryModel repository) {
|
|
192 |
if (repository.isFrozen) {
|
|
193 |
return false;
|
|
194 |
}
|
|
195 |
return canAccess(repository, AccessRestrictionType.PUSH, AccessPermission.REWIND);
|
|
196 |
}
|
|
197 |
|
fe24a0
|
198 |
public boolean hasUser(String name) {
|
JM |
199 |
return users.contains(name.toLowerCase());
|
|
200 |
}
|
|
201 |
|
|
202 |
public void addUser(String name) {
|
|
203 |
users.add(name.toLowerCase());
|
|
204 |
}
|
|
205 |
|
|
206 |
public void addUsers(Collection<String> names) {
|
|
207 |
for (String name:names) {
|
|
208 |
users.add(name.toLowerCase());
|
|
209 |
}
|
|
210 |
}
|
|
211 |
|
|
212 |
public void removeUser(String name) {
|
|
213 |
users.remove(name.toLowerCase());
|
|
214 |
}
|
|
215 |
|
0b9119
|
216 |
public void addMailingLists(Collection<String> addresses) {
|
JM |
217 |
for (String address:addresses) {
|
|
218 |
mailingLists.add(address.toLowerCase());
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|
fe24a0
|
222 |
@Override
|
JM |
223 |
public String toString() {
|
|
224 |
return name;
|
|
225 |
}
|
|
226 |
|
|
227 |
@Override
|
|
228 |
public int compareTo(TeamModel o) {
|
|
229 |
return name.compareTo(o.name);
|
|
230 |
}
|
|
231 |
}
|