commit | author | age
|
15a51d
|
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.client;
|
|
17 |
|
|
18 |
import java.io.IOException;
|
|
19 |
import java.io.Serializable;
|
|
20 |
import java.util.ArrayList;
|
|
21 |
import java.util.Collections;
|
17820f
|
22 |
import java.util.Date;
|
4cac0d
|
23 |
import java.util.HashSet;
|
97d3af
|
24 |
import java.util.LinkedHashSet;
|
15a51d
|
25 |
import java.util.List;
|
JM |
26 |
import java.util.Map;
|
4cac0d
|
27 |
import java.util.Set;
|
97d3af
|
28 |
import java.util.TreeSet;
|
15a51d
|
29 |
|
9119cf
|
30 |
import com.gitblit.Constants;
|
87f6c3
|
31 |
import com.gitblit.Constants.AccessPermission;
|
94dcbd
|
32 |
import com.gitblit.Constants.AccessRestrictionType;
|
6adf56
|
33 |
import com.gitblit.Constants.AuthorizationControl;
|
ba6150
|
34 |
import com.gitblit.Constants.PermissionType;
|
JM |
35 |
import com.gitblit.Constants.RegistrantType;
|
15a51d
|
36 |
import com.gitblit.Keys;
|
JM |
37 |
import com.gitblit.models.FederationModel;
|
e493cf
|
38 |
import com.gitblit.models.FeedEntryModel;
|
17820f
|
39 |
import com.gitblit.models.FeedModel;
|
87f6c3
|
40 |
import com.gitblit.models.RegistrantAccessPermission;
|
15a51d
|
41 |
import com.gitblit.models.RepositoryModel;
|
84c1d5
|
42 |
import com.gitblit.models.ServerSettings;
|
b75734
|
43 |
import com.gitblit.models.ServerStatus;
|
f08aab
|
44 |
import com.gitblit.models.TeamModel;
|
15a51d
|
45 |
import com.gitblit.models.UserModel;
|
0db5c4
|
46 |
import com.gitblit.utils.ArrayUtils;
|
15a51d
|
47 |
import com.gitblit.utils.RpcUtils;
|
ec5a88
|
48 |
import com.gitblit.utils.StringUtils;
|
4cac0d
|
49 |
import com.gitblit.utils.SyndicationUtils;
|
15a51d
|
50 |
|
84c1d5
|
51 |
/**
|
JM |
52 |
* GitblitClient is a object that retrieves data from a Gitblit server, caches
|
|
53 |
* it for local operations, and allows updating or creating Gitblit objects.
|
699e71
|
54 |
*
|
84c1d5
|
55 |
* @author James Moger
|
699e71
|
56 |
*
|
84c1d5
|
57 |
*/
|
JM |
58 |
public class GitblitClient implements Serializable {
|
15a51d
|
59 |
|
JM |
60 |
private static final long serialVersionUID = 1L;
|
4cac0d
|
61 |
|
bab9c9
|
62 |
private static final Date NEVER = new Date(0);
|
JM |
63 |
|
4cac0d
|
64 |
protected final GitblitRegistration reg;
|
15a51d
|
65 |
|
JM |
66 |
public final String url;
|
|
67 |
|
|
68 |
public final String account;
|
|
69 |
|
|
70 |
private final char[] password;
|
2a99c3
|
71 |
|
f08aab
|
72 |
private volatile int protocolVersion;
|
JM |
73 |
|
d03aff
|
74 |
private volatile boolean allowManagement;
|
15a51d
|
75 |
|
d03aff
|
76 |
private volatile boolean allowAdministration;
|
15a51d
|
77 |
|
84c1d5
|
78 |
private volatile ServerSettings settings;
|
15a51d
|
79 |
|
JM |
80 |
private final List<RepositoryModel> allRepositories;
|
|
81 |
|
|
82 |
private final List<UserModel> allUsers;
|
f08aab
|
83 |
|
JM |
84 |
private final List<TeamModel> allTeams;
|
15a51d
|
85 |
|
JM |
86 |
private final List<FederationModel> federationRegistrations;
|
b75734
|
87 |
|
17820f
|
88 |
private final List<FeedModel> availableFeeds;
|
JM |
89 |
|
ee458f
|
90 |
private final List<FeedEntryModel> syndicatedEntries;
|
17820f
|
91 |
|
JM |
92 |
private final Set<String> subscribedRepositories;
|
4cac0d
|
93 |
|
b75734
|
94 |
private ServerStatus status;
|
15a51d
|
95 |
|
4cac0d
|
96 |
public GitblitClient(GitblitRegistration reg) {
|
JM |
97 |
this.reg = reg;
|
|
98 |
this.url = reg.url;
|
|
99 |
this.account = reg.account;
|
|
100 |
this.password = reg.password;
|
15a51d
|
101 |
|
JM |
102 |
this.allUsers = new ArrayList<UserModel>();
|
f08aab
|
103 |
this.allTeams = new ArrayList<TeamModel>();
|
15a51d
|
104 |
this.allRepositories = new ArrayList<RepositoryModel>();
|
JM |
105 |
this.federationRegistrations = new ArrayList<FederationModel>();
|
17820f
|
106 |
this.availableFeeds = new ArrayList<FeedModel>();
|
ee458f
|
107 |
this.syndicatedEntries = new ArrayList<FeedEntryModel>();
|
17820f
|
108 |
this.subscribedRepositories = new HashSet<String>();
|
15a51d
|
109 |
}
|
JM |
110 |
|
|
111 |
public void login() throws IOException {
|
f08aab
|
112 |
protocolVersion = RpcUtils.getProtocolVersion(url, account, password);
|
ec5a88
|
113 |
refreshSettings();
|
17820f
|
114 |
refreshAvailableFeeds();
|
15a51d
|
115 |
refreshRepositories();
|
e33b91
|
116 |
refreshSubscribedFeeds(0);
|
17820f
|
117 |
|
bb8d1a
|
118 |
// credentials may not have administrator access
|
MS |
119 |
// or server may have disabled rpc management
|
|
120 |
refreshUsers();
|
|
121 |
if (protocolVersion > 1) {
|
|
122 |
refreshTeams();
|
15a51d
|
123 |
}
|
bb8d1a
|
124 |
allowManagement = true;
|
d03aff
|
125 |
|
bb8d1a
|
126 |
// credentials may not have administrator access
|
MS |
127 |
// or server may have disabled rpc administration
|
|
128 |
refreshStatus();
|
|
129 |
allowAdministration = true;
|
|
130 |
|
f08aab
|
131 |
}
|
JM |
132 |
|
|
133 |
public int getProtocolVersion() {
|
|
134 |
return protocolVersion;
|
15a51d
|
135 |
}
|
JM |
136 |
|
d03aff
|
137 |
public boolean allowManagement() {
|
JM |
138 |
return allowManagement;
|
|
139 |
}
|
2a99c3
|
140 |
|
d03aff
|
141 |
public boolean allowAdministration() {
|
JM |
142 |
return allowAdministration;
|
15a51d
|
143 |
}
|
JM |
144 |
|
|
145 |
public boolean isOwner(RepositoryModel model) {
|
661db6
|
146 |
return model.isOwner(account);
|
15a51d
|
147 |
}
|
JM |
148 |
|
ec5a88
|
149 |
public String getURL(String action, String repository, String objectId) {
|
JM |
150 |
boolean mounted = settings.get(Keys.web.mountParameters).getBoolean(true);
|
|
151 |
StringBuilder sb = new StringBuilder();
|
|
152 |
sb.append(url);
|
|
153 |
sb.append('/');
|
|
154 |
sb.append(action);
|
|
155 |
sb.append('/');
|
|
156 |
if (mounted) {
|
|
157 |
// mounted url/action/repository/objectId
|
|
158 |
sb.append(StringUtils.encodeURL(repository));
|
|
159 |
if (!StringUtils.isEmpty(objectId)) {
|
|
160 |
sb.append('/');
|
|
161 |
sb.append(objectId);
|
|
162 |
}
|
|
163 |
return sb.toString();
|
|
164 |
} else {
|
|
165 |
// parameterized url/action/&r=repository&h=objectId
|
|
166 |
sb.append("?r=");
|
|
167 |
sb.append(repository);
|
|
168 |
if (!StringUtils.isEmpty(objectId)) {
|
|
169 |
sb.append("&h=");
|
|
170 |
sb.append(objectId);
|
|
171 |
}
|
|
172 |
return sb.toString();
|
|
173 |
}
|
|
174 |
}
|
699e71
|
175 |
|
94dcbd
|
176 |
public AccessRestrictionType getDefaultAccessRestriction() {
|
b32502
|
177 |
String restriction = "PUSH";
|
94dcbd
|
178 |
if (settings.hasKey(Keys.git.defaultAccessRestriction)) {
|
JM |
179 |
restriction = settings.get(Keys.git.defaultAccessRestriction).currentValue;
|
|
180 |
}
|
|
181 |
return AccessRestrictionType.fromName(restriction);
|
|
182 |
}
|
ec5a88
|
183 |
|
6adf56
|
184 |
public AuthorizationControl getDefaultAuthorizationControl() {
|
JM |
185 |
String authorization = null;
|
|
186 |
if (settings.hasKey(Keys.git.defaultAuthorizationControl)) {
|
|
187 |
authorization = settings.get(Keys.git.defaultAuthorizationControl).currentValue;
|
|
188 |
}
|
|
189 |
return AuthorizationControl.fromName(authorization);
|
|
190 |
}
|
|
191 |
|
97d3af
|
192 |
/**
|
JM |
193 |
* Returns the list of pre-receive scripts the repository inherited from the
|
|
194 |
* global settings and team affiliations.
|
699e71
|
195 |
*
|
97d3af
|
196 |
* @param repository
|
JM |
197 |
* if null only the globally specified scripts are returned
|
|
198 |
* @return a list of scripts
|
|
199 |
*/
|
|
200 |
public List<String> getPreReceiveScriptsInherited(RepositoryModel repository) {
|
|
201 |
Set<String> scripts = new LinkedHashSet<String>();
|
|
202 |
// Globals
|
|
203 |
for (String script : settings.get(Keys.groovy.preReceiveScripts).getStrings()) {
|
|
204 |
if (script.endsWith(".groovy")) {
|
|
205 |
scripts.add(script.substring(0, script.lastIndexOf('.')));
|
|
206 |
} else {
|
|
207 |
scripts.add(script);
|
|
208 |
}
|
4bd203
|
209 |
}
|
97d3af
|
210 |
|
JM |
211 |
// Team Scripts
|
|
212 |
if (repository != null) {
|
|
213 |
for (String teamname : getPermittedTeamnames(repository)) {
|
|
214 |
TeamModel team = getTeamModel(teamname);
|
0db5c4
|
215 |
if (!ArrayUtils.isEmpty(team.preReceiveScripts)) {
|
JM |
216 |
scripts.addAll(team.preReceiveScripts);
|
|
217 |
}
|
97d3af
|
218 |
}
|
JM |
219 |
}
|
|
220 |
return new ArrayList<String>(scripts);
|
|
221 |
}
|
|
222 |
|
|
223 |
/**
|
|
224 |
* Returns the list of all available Groovy pre-receive push hook scripts
|
|
225 |
* that are not already inherited by the repository. Script files must have
|
|
226 |
* .groovy extension
|
699e71
|
227 |
*
|
97d3af
|
228 |
* @param repository
|
JM |
229 |
* optional parameter
|
|
230 |
* @return list of available hook scripts
|
|
231 |
*/
|
|
232 |
public List<String> getPreReceiveScriptsUnused(RepositoryModel repository) {
|
|
233 |
Set<String> inherited = new TreeSet<String>(getPreReceiveScriptsInherited(repository));
|
|
234 |
|
|
235 |
// create list of available scripts by excluding inherited scripts
|
|
236 |
List<String> scripts = new ArrayList<String>();
|
979385
|
237 |
if (!ArrayUtils.isEmpty(settings.pushScripts)) {
|
JM |
238 |
for (String script : settings.pushScripts) {
|
|
239 |
if (!inherited.contains(script)) {
|
|
240 |
scripts.add(script);
|
|
241 |
}
|
97d3af
|
242 |
}
|
JM |
243 |
}
|
|
244 |
return scripts;
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Returns the list of post-receive scripts the repository inherited from
|
|
249 |
* the global settings and team affiliations.
|
699e71
|
250 |
*
|
97d3af
|
251 |
* @param repository
|
JM |
252 |
* if null only the globally specified scripts are returned
|
|
253 |
* @return a list of scripts
|
|
254 |
*/
|
|
255 |
public List<String> getPostReceiveScriptsInherited(RepositoryModel repository) {
|
|
256 |
Set<String> scripts = new LinkedHashSet<String>();
|
|
257 |
// Global Scripts
|
|
258 |
for (String script : settings.get(Keys.groovy.postReceiveScripts).getStrings()) {
|
|
259 |
if (script.endsWith(".groovy")) {
|
|
260 |
scripts.add(script.substring(0, script.lastIndexOf('.')));
|
|
261 |
} else {
|
|
262 |
scripts.add(script);
|
|
263 |
}
|
|
264 |
}
|
|
265 |
// Team Scripts
|
|
266 |
if (repository != null) {
|
|
267 |
for (String teamname : getPermittedTeamnames(repository)) {
|
|
268 |
TeamModel team = getTeamModel(teamname);
|
0db5c4
|
269 |
if (!ArrayUtils.isEmpty(team.postReceiveScripts)) {
|
JM |
270 |
scripts.addAll(team.postReceiveScripts);
|
|
271 |
}
|
97d3af
|
272 |
}
|
JM |
273 |
}
|
|
274 |
return new ArrayList<String>(scripts);
|
|
275 |
}
|
|
276 |
|
|
277 |
/**
|
|
278 |
* Returns the list of unused Groovy post-receive push hook scripts that are
|
|
279 |
* not already inherited by the repository. Script files must have .groovy
|
|
280 |
* extension
|
699e71
|
281 |
*
|
97d3af
|
282 |
* @param repository
|
JM |
283 |
* optional parameter
|
|
284 |
* @return list of available hook scripts
|
|
285 |
*/
|
|
286 |
public List<String> getPostReceiveScriptsUnused(RepositoryModel repository) {
|
|
287 |
Set<String> inherited = new TreeSet<String>(getPostReceiveScriptsInherited(repository));
|
|
288 |
|
|
289 |
// create list of available scripts by excluding inherited scripts
|
|
290 |
List<String> scripts = new ArrayList<String>();
|
699e71
|
291 |
if (!ArrayUtils.isEmpty(settings.pushScripts)) {
|
0db5c4
|
292 |
for (String script : settings.pushScripts) {
|
JM |
293 |
if (!inherited.contains(script)) {
|
|
294 |
scripts.add(script);
|
|
295 |
}
|
97d3af
|
296 |
}
|
JM |
297 |
}
|
|
298 |
return scripts;
|
4bd203
|
299 |
}
|
JM |
300 |
|
84c1d5
|
301 |
public ServerSettings getSettings() {
|
15a51d
|
302 |
return settings;
|
b75734
|
303 |
}
|
JM |
304 |
|
84c1d5
|
305 |
public ServerStatus getStatus() {
|
JM |
306 |
return status;
|
|
307 |
}
|
|
308 |
|
b75734
|
309 |
public String getSettingDescription(String key) {
|
JM |
310 |
return settings.get(key).description;
|
15a51d
|
311 |
}
|
JM |
312 |
|
|
313 |
public List<RepositoryModel> refreshRepositories() throws IOException {
|
|
314 |
Map<String, RepositoryModel> repositories = RpcUtils
|
|
315 |
.getRepositories(url, account, password);
|
|
316 |
allRepositories.clear();
|
|
317 |
allRepositories.addAll(repositories.values());
|
|
318 |
Collections.sort(allRepositories);
|
17820f
|
319 |
markSubscribedFeeds();
|
15a51d
|
320 |
return allRepositories;
|
JM |
321 |
}
|
|
322 |
|
|
323 |
public List<UserModel> refreshUsers() throws IOException {
|
|
324 |
List<UserModel> users = RpcUtils.getUsers(url, account, password);
|
|
325 |
allUsers.clear();
|
|
326 |
allUsers.addAll(users);
|
092f0a
|
327 |
Collections.sort(users);
|
15a51d
|
328 |
return allUsers;
|
JM |
329 |
}
|
2a99c3
|
330 |
|
f08aab
|
331 |
public List<TeamModel> refreshTeams() throws IOException {
|
JM |
332 |
List<TeamModel> teams = RpcUtils.getTeams(url, account, password);
|
|
333 |
allTeams.clear();
|
|
334 |
allTeams.addAll(teams);
|
092f0a
|
335 |
Collections.sort(teams);
|
f08aab
|
336 |
return allTeams;
|
JM |
337 |
}
|
|
338 |
|
f306ef
|
339 |
public ServerSettings refreshSettings() throws IOException {
|
JM |
340 |
settings = RpcUtils.getSettings(url, account, password);
|
|
341 |
return settings;
|
|
342 |
}
|
8b7636
|
343 |
|
486ee1
|
344 |
public ServerStatus refreshStatus() throws IOException {
|
JM |
345 |
status = RpcUtils.getStatus(url, account, password);
|
|
346 |
return status;
|
|
347 |
}
|
15a51d
|
348 |
|
9119cf
|
349 |
public List<String> getBranches(String repository) {
|
JM |
350 |
List<FeedModel> feeds = getAvailableFeeds(repository);
|
|
351 |
List<String> branches = new ArrayList<String>();
|
|
352 |
for (FeedModel feed : feeds) {
|
|
353 |
branches.add(feed.branch);
|
|
354 |
}
|
|
355 |
Collections.sort(branches);
|
|
356 |
return branches;
|
|
357 |
}
|
|
358 |
|
17820f
|
359 |
public List<FeedModel> getAvailableFeeds() {
|
JM |
360 |
return availableFeeds;
|
|
361 |
}
|
|
362 |
|
|
363 |
public List<FeedModel> getAvailableFeeds(RepositoryModel repository) {
|
9119cf
|
364 |
return getAvailableFeeds(repository.name);
|
JM |
365 |
}
|
|
366 |
|
|
367 |
public List<FeedModel> getAvailableFeeds(String repository) {
|
17820f
|
368 |
List<FeedModel> repositoryFeeds = new ArrayList<FeedModel>();
|
JM |
369 |
if (repository == null) {
|
|
370 |
return repositoryFeeds;
|
|
371 |
}
|
|
372 |
for (FeedModel feed : availableFeeds) {
|
9119cf
|
373 |
if (feed.repository.equalsIgnoreCase(repository)) {
|
17820f
|
374 |
repositoryFeeds.add(feed);
|
JM |
375 |
}
|
|
376 |
}
|
|
377 |
return repositoryFeeds;
|
|
378 |
}
|
|
379 |
|
|
380 |
public List<FeedModel> refreshAvailableFeeds() throws IOException {
|
|
381 |
List<FeedModel> feeds = RpcUtils.getBranchFeeds(url, account, password);
|
|
382 |
availableFeeds.clear();
|
|
383 |
availableFeeds.addAll(feeds);
|
|
384 |
markSubscribedFeeds();
|
|
385 |
return availableFeeds;
|
|
386 |
}
|
|
387 |
|
ee458f
|
388 |
public List<FeedEntryModel> refreshSubscribedFeeds(int page) throws IOException {
|
JM |
389 |
Set<FeedEntryModel> allEntries = new HashSet<FeedEntryModel>();
|
17820f
|
390 |
if (reg.feeds.size() > 0) {
|
JM |
391 |
for (FeedModel feed : reg.feeds) {
|
bab9c9
|
392 |
feed.lastRefreshDate = feed.currentRefreshDate;
|
JM |
393 |
feed.currentRefreshDate = new Date();
|
f08aab
|
394 |
List<FeedEntryModel> entries = SyndicationUtils.readFeed(url, feed.repository,
|
JM |
395 |
feed.branch, -1, page, account, password);
|
17820f
|
396 |
allEntries.addAll(entries);
|
4cac0d
|
397 |
}
|
JM |
398 |
}
|
bab9c9
|
399 |
reg.cacheFeeds();
|
4cac0d
|
400 |
syndicatedEntries.clear();
|
17820f
|
401 |
syndicatedEntries.addAll(allEntries);
|
4cac0d
|
402 |
Collections.sort(syndicatedEntries);
|
JM |
403 |
return syndicatedEntries;
|
|
404 |
}
|
|
405 |
|
17820f
|
406 |
public void updateSubscribedFeeds(List<FeedModel> list) {
|
JM |
407 |
reg.updateSubscribedFeeds(list);
|
|
408 |
markSubscribedFeeds();
|
|
409 |
}
|
|
410 |
|
|
411 |
private void markSubscribedFeeds() {
|
|
412 |
subscribedRepositories.clear();
|
|
413 |
for (FeedModel feed : availableFeeds) {
|
|
414 |
// mark feed in the available list as subscribed
|
|
415 |
feed.subscribed = reg.feeds.contains(feed);
|
|
416 |
if (feed.subscribed) {
|
|
417 |
subscribedRepositories.add(feed.repository.toLowerCase());
|
4cac0d
|
418 |
}
|
JM |
419 |
}
|
17820f
|
420 |
}
|
JM |
421 |
|
bab9c9
|
422 |
public Date getLastFeedRefresh(String repository, String branch) {
|
JM |
423 |
FeedModel feed = new FeedModel();
|
|
424 |
feed.repository = repository;
|
|
425 |
feed.branch = branch;
|
|
426 |
if (reg.feeds.contains(feed)) {
|
|
427 |
int idx = reg.feeds.indexOf(feed);
|
|
428 |
feed = reg.feeds.get(idx);
|
|
429 |
return feed.lastRefreshDate;
|
|
430 |
}
|
|
431 |
return NEVER;
|
|
432 |
}
|
|
433 |
|
17820f
|
434 |
public boolean isSubscribed(RepositoryModel repository) {
|
JM |
435 |
return subscribedRepositories.contains(repository.name.toLowerCase());
|
4cac0d
|
436 |
}
|
JM |
437 |
|
ee458f
|
438 |
public List<FeedEntryModel> getSyndicatedEntries() {
|
4cac0d
|
439 |
return syndicatedEntries;
|
JM |
440 |
}
|
|
441 |
|
f08aab
|
442 |
public List<FeedEntryModel> log(String repository, String branch, int numberOfEntries, int page)
|
JM |
443 |
throws IOException {
|
ee458f
|
444 |
return SyndicationUtils.readFeed(url, repository, branch, numberOfEntries, page, account,
|
JM |
445 |
password);
|
|
446 |
}
|
|
447 |
|
|
448 |
public List<FeedEntryModel> search(String repository, String branch, String fragment,
|
e33b91
|
449 |
Constants.SearchType type, int numberOfEntries, int page) throws IOException {
|
9119cf
|
450 |
return SyndicationUtils.readSearchFeed(url, repository, branch, fragment, type,
|
e33b91
|
451 |
numberOfEntries, page, account, password);
|
9119cf
|
452 |
}
|
JM |
453 |
|
15a51d
|
454 |
public List<FederationModel> refreshFederationRegistrations() throws IOException {
|
JM |
455 |
List<FederationModel> list = RpcUtils.getFederationRegistrations(url, account, password);
|
|
456 |
federationRegistrations.clear();
|
|
457 |
federationRegistrations.addAll(list);
|
|
458 |
return federationRegistrations;
|
|
459 |
}
|
|
460 |
|
|
461 |
public List<UserModel> getUsers() {
|
|
462 |
return allUsers;
|
|
463 |
}
|
699e71
|
464 |
|
092f0a
|
465 |
public UserModel getUser(String username) {
|
JM |
466 |
for (UserModel user : getUsers()) {
|
|
467 |
if (user.username.equalsIgnoreCase(username)) {
|
|
468 |
return user;
|
|
469 |
}
|
|
470 |
}
|
|
471 |
return null;
|
|
472 |
}
|
15a51d
|
473 |
|
JM |
474 |
public List<String> getUsernames() {
|
|
475 |
List<String> usernames = new ArrayList<String>();
|
|
476 |
for (UserModel user : this.allUsers) {
|
|
477 |
usernames.add(user.username);
|
|
478 |
}
|
|
479 |
Collections.sort(usernames);
|
|
480 |
return usernames;
|
|
481 |
}
|
|
482 |
|
|
483 |
public List<String> getPermittedUsernames(RepositoryModel repository) {
|
|
484 |
List<String> usernames = new ArrayList<String>();
|
|
485 |
for (UserModel user : this.allUsers) {
|
822dfe
|
486 |
if (user.hasRepositoryPermission(repository.name)) {
|
15a51d
|
487 |
usernames.add(user.username);
|
JM |
488 |
}
|
|
489 |
}
|
|
490 |
return usernames;
|
822dfe
|
491 |
}
|
699e71
|
492 |
|
ba6150
|
493 |
/**
|
JM |
494 |
* Returns the effective list of permissions for this user, taking into account
|
|
495 |
* team memberships, ownerships.
|
699e71
|
496 |
*
|
ba6150
|
497 |
* @param user
|
JM |
498 |
* @return the effective list of permissions for the user
|
|
499 |
*/
|
|
500 |
public List<RegistrantAccessPermission> getUserAccessPermissions(UserModel user) {
|
|
501 |
Set<RegistrantAccessPermission> set = new LinkedHashSet<RegistrantAccessPermission>();
|
|
502 |
set.addAll(user.getRepositoryPermissions());
|
|
503 |
// Flag missing repositories
|
|
504 |
for (RegistrantAccessPermission permission : set) {
|
|
505 |
if (permission.mutable && PermissionType.EXPLICIT.equals(permission.permissionType)) {
|
|
506 |
RepositoryModel rm = getRepository(permission.registrant);
|
|
507 |
if (rm == null) {
|
|
508 |
permission.permissionType = PermissionType.MISSING;
|
|
509 |
permission.mutable = false;
|
|
510 |
continue;
|
|
511 |
}
|
|
512 |
}
|
|
513 |
}
|
|
514 |
|
|
515 |
// TODO reconsider ownership as a user property
|
|
516 |
// manually specify personal repository ownerships
|
|
517 |
for (RepositoryModel rm : allRepositories) {
|
661db6
|
518 |
if (rm.isUsersPersonalRepository(user.username) || rm.isOwner(user.username)) {
|
ba6150
|
519 |
RegistrantAccessPermission rp = new RegistrantAccessPermission(rm.name, AccessPermission.REWIND,
|
JM |
520 |
PermissionType.OWNER, RegistrantType.REPOSITORY, null, false);
|
|
521 |
// user may be owner of a repository to which they've inherited
|
|
522 |
// a team permission, replace any existing perm with owner perm
|
|
523 |
set.remove(rp);
|
|
524 |
set.add(rp);
|
|
525 |
}
|
|
526 |
}
|
699e71
|
527 |
|
ba6150
|
528 |
List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>(set);
|
JM |
529 |
Collections.sort(list);
|
|
530 |
return list;
|
|
531 |
}
|
699e71
|
532 |
|
5e8826
|
533 |
public List<RegistrantAccessPermission> getUserAccessPermissions(RepositoryModel repository) {
|
ba6150
|
534 |
List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
|
JM |
535 |
if (AccessRestrictionType.NONE.equals(repository.accessRestriction)) {
|
|
536 |
// no permissions needed, REWIND for everyone!
|
|
537 |
return list;
|
|
538 |
}
|
|
539 |
if (AuthorizationControl.AUTHENTICATED.equals(repository.authorizationControl)) {
|
|
540 |
// no permissions needed, REWIND for authenticated!
|
|
541 |
return list;
|
|
542 |
}
|
|
543 |
// NAMED users and teams
|
|
544 |
for (UserModel user : allUsers) {
|
644bdd
|
545 |
RegistrantAccessPermission ap = user.getRepositoryPermission(repository);
|
JM |
546 |
if (ap.permission.exceeds(AccessPermission.NONE)) {
|
|
547 |
list.add(ap);
|
092f0a
|
548 |
}
|
JM |
549 |
}
|
644bdd
|
550 |
return list;
|
822dfe
|
551 |
}
|
JM |
552 |
|
|
553 |
public boolean setUserAccessPermissions(RepositoryModel repository, List<RegistrantAccessPermission> permissions) throws IOException {
|
|
554 |
return RpcUtils.setRepositoryMemberPermissions(repository, permissions, url, account, password);
|
15a51d
|
555 |
}
|
JM |
556 |
|
f08aab
|
557 |
public List<TeamModel> getTeams() {
|
JM |
558 |
return allTeams;
|
|
559 |
}
|
|
560 |
|
|
561 |
public List<String> getTeamnames() {
|
|
562 |
List<String> teamnames = new ArrayList<String>();
|
|
563 |
for (TeamModel team : this.allTeams) {
|
|
564 |
teamnames.add(team.name);
|
|
565 |
}
|
|
566 |
Collections.sort(teamnames);
|
|
567 |
return teamnames;
|
|
568 |
}
|
|
569 |
|
|
570 |
public List<String> getPermittedTeamnames(RepositoryModel repository) {
|
|
571 |
List<String> teamnames = new ArrayList<String>();
|
|
572 |
for (TeamModel team : this.allTeams) {
|
822dfe
|
573 |
if (team.hasRepositoryPermission(repository.name)) {
|
f08aab
|
574 |
teamnames.add(team.name);
|
JM |
575 |
}
|
|
576 |
}
|
|
577 |
return teamnames;
|
822dfe
|
578 |
}
|
699e71
|
579 |
|
5e8826
|
580 |
public List<RegistrantAccessPermission> getTeamAccessPermissions(RepositoryModel repository) {
|
JM |
581 |
List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
|
|
582 |
for (TeamModel team : allTeams) {
|
644bdd
|
583 |
RegistrantAccessPermission ap = team.getRepositoryPermission(repository);
|
JM |
584 |
if (ap.permission.exceeds(AccessPermission.NONE)) {
|
|
585 |
list.add(ap);
|
5e8826
|
586 |
}
|
JM |
587 |
}
|
092f0a
|
588 |
Collections.sort(list);
|
5e8826
|
589 |
return list;
|
822dfe
|
590 |
}
|
JM |
591 |
|
|
592 |
public boolean setTeamAccessPermissions(RepositoryModel repository, List<RegistrantAccessPermission> permissions) throws IOException {
|
|
593 |
return RpcUtils.setRepositoryTeamPermissions(repository, permissions, url, account, password);
|
f08aab
|
594 |
}
|
0db5c4
|
595 |
|
97d3af
|
596 |
public TeamModel getTeamModel(String name) {
|
JM |
597 |
for (TeamModel team : allTeams) {
|
|
598 |
if (team.name.equalsIgnoreCase(name)) {
|
|
599 |
return team;
|
|
600 |
}
|
|
601 |
}
|
|
602 |
return null;
|
|
603 |
}
|
f08aab
|
604 |
|
15a51d
|
605 |
public List<String> getFederationSets() {
|
b75734
|
606 |
return settings.get(Keys.federation.sets).getStrings();
|
15a51d
|
607 |
}
|
JM |
608 |
|
|
609 |
public List<RepositoryModel> getRepositories() {
|
|
610 |
return allRepositories;
|
|
611 |
}
|
699e71
|
612 |
|
092f0a
|
613 |
public RepositoryModel getRepository(String name) {
|
JM |
614 |
for (RepositoryModel repository : allRepositories) {
|
|
615 |
if (repository.name.equalsIgnoreCase(name)) {
|
|
616 |
return repository;
|
|
617 |
}
|
|
618 |
}
|
|
619 |
return null;
|
|
620 |
}
|
15a51d
|
621 |
|
822dfe
|
622 |
public boolean createRepository(RepositoryModel repository, List<RegistrantAccessPermission> userPermissions)
|
15a51d
|
623 |
throws IOException {
|
822dfe
|
624 |
return createRepository(repository, userPermissions, null);
|
f08aab
|
625 |
}
|
JM |
626 |
|
822dfe
|
627 |
public boolean createRepository(RepositoryModel repository, List<RegistrantAccessPermission> userPermissions,
|
JM |
628 |
List<RegistrantAccessPermission> teamPermissions) throws IOException {
|
15a51d
|
629 |
boolean success = true;
|
c25a1d
|
630 |
success &= RpcUtils.createRepository(repository, url, account, password);
|
822dfe
|
631 |
if (userPermissions != null && userPermissions.size() > 0) {
|
15a51d
|
632 |
// if new repository has named members, set them
|
822dfe
|
633 |
success &= RpcUtils.setRepositoryMemberPermissions(repository, userPermissions, url, account,
|
f08aab
|
634 |
password);
|
JM |
635 |
}
|
822dfe
|
636 |
if (teamPermissions != null && teamPermissions.size() > 0) {
|
f08aab
|
637 |
// if new repository has named teams, set them
|
822dfe
|
638 |
success &= RpcUtils.setRepositoryTeamPermissions(repository, teamPermissions, url, account,
|
15a51d
|
639 |
password);
|
JM |
640 |
}
|
|
641 |
return success;
|
|
642 |
}
|
|
643 |
|
|
644 |
public boolean updateRepository(String name, RepositoryModel repository,
|
822dfe
|
645 |
List<RegistrantAccessPermission> userPermissions) throws IOException {
|
JM |
646 |
return updateRepository(name, repository, userPermissions, null);
|
f08aab
|
647 |
}
|
JM |
648 |
|
|
649 |
public boolean updateRepository(String name, RepositoryModel repository,
|
822dfe
|
650 |
List<RegistrantAccessPermission> userPermissions, List<RegistrantAccessPermission> teamPermissions) throws IOException {
|
15a51d
|
651 |
boolean success = true;
|
JM |
652 |
success &= RpcUtils.updateRepository(name, repository, url, account, password);
|
f08aab
|
653 |
// set the repository members
|
822dfe
|
654 |
if (userPermissions != null) {
|
JM |
655 |
success &= RpcUtils.setRepositoryMemberPermissions(repository, userPermissions, url, account,
|
f08aab
|
656 |
password);
|
JM |
657 |
}
|
822dfe
|
658 |
if (teamPermissions != null) {
|
JM |
659 |
success &= RpcUtils.setRepositoryTeamPermissions(repository, teamPermissions, url, account,
|
f08aab
|
660 |
password);
|
JM |
661 |
}
|
15a51d
|
662 |
return success;
|
JM |
663 |
}
|
|
664 |
|
|
665 |
public boolean deleteRepository(RepositoryModel repository) throws IOException {
|
|
666 |
return RpcUtils.deleteRepository(repository, url, account, password);
|
|
667 |
}
|
699e71
|
668 |
|
fee060
|
669 |
public boolean clearRepositoryCache() throws IOException {
|
JM |
670 |
return RpcUtils.clearRepositoryCache(url, account, password);
|
|
671 |
}
|
15a51d
|
672 |
|
JM |
673 |
public boolean createUser(UserModel user) throws IOException {
|
|
674 |
return RpcUtils.createUser(user, url, account, password);
|
|
675 |
}
|
|
676 |
|
|
677 |
public boolean updateUser(String name, UserModel user) throws IOException {
|
|
678 |
return RpcUtils.updateUser(name, user, url, account, password);
|
|
679 |
}
|
|
680 |
|
|
681 |
public boolean deleteUser(UserModel user) throws IOException {
|
|
682 |
return RpcUtils.deleteUser(user, url, account, password);
|
|
683 |
}
|
2a99c3
|
684 |
|
f08aab
|
685 |
public boolean createTeam(TeamModel team) throws IOException {
|
JM |
686 |
return RpcUtils.createTeam(team, url, account, password);
|
|
687 |
}
|
|
688 |
|
|
689 |
public boolean updateTeam(String name, TeamModel team) throws IOException {
|
|
690 |
return RpcUtils.updateTeam(name, team, url, account, password);
|
|
691 |
}
|
|
692 |
|
|
693 |
public boolean deleteTeam(TeamModel team) throws IOException {
|
|
694 |
return RpcUtils.deleteTeam(team, url, account, password);
|
|
695 |
}
|
|
696 |
|
2a99c3
|
697 |
public boolean updateSettings(Map<String, String> newSettings) throws IOException {
|
JM |
698 |
return RpcUtils.updateSettings(newSettings, url, account, password);
|
|
699 |
}
|
15a51d
|
700 |
}
|