Merge "ls-groups: add option to list groups for a project"
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2011 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import com.google.gerrit.reviewdb.AccountGroup;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class GroupComparator implements Comparator<AccountGroup> {
|
||||
|
||||
@Override
|
||||
public int compare(final AccountGroup group1, final AccountGroup group2) {
|
||||
return group1.getName().compareTo(group2.getName());
|
||||
}
|
||||
}
|
||||
@@ -16,17 +16,22 @@ package com.google.gerrit.server.account;
|
||||
|
||||
import com.google.gerrit.common.data.GroupDetail;
|
||||
import com.google.gerrit.common.data.GroupList;
|
||||
import com.google.gerrit.common.data.GroupReference;
|
||||
import com.google.gerrit.common.errors.NoSuchGroupException;
|
||||
import com.google.gerrit.reviewdb.AccountGroup;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.project.ProjectControl;
|
||||
import com.google.gwtorm.client.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class VisibleGroups {
|
||||
|
||||
@@ -39,6 +44,8 @@ public class VisibleGroups {
|
||||
private final GroupControl.Factory groupControlFactory;
|
||||
private final GroupDetailFactory.Factory groupDetailFactory;
|
||||
|
||||
private Collection<ProjectControl> projects;
|
||||
|
||||
@Inject
|
||||
VisibleGroups(final Provider<IdentifiedUser> currentUser,
|
||||
final GroupCache groupCache,
|
||||
@@ -50,24 +57,59 @@ public class VisibleGroups {
|
||||
this.groupDetailFactory = groupDetailFactory;
|
||||
}
|
||||
|
||||
public void setProjects(final Collection<ProjectControl> projects) {
|
||||
this.projects = projects;
|
||||
}
|
||||
|
||||
public GroupList get() throws OrmException, NoSuchGroupException {
|
||||
final IdentifiedUser user = identifiedUser.get();
|
||||
final List<AccountGroup> list = new ArrayList<AccountGroup>();
|
||||
if (user.getCapabilities().canAdministrateServer()) {
|
||||
CollectionUtils.addAll(list, groupCache.all().iterator());
|
||||
final Iterable<AccountGroup> groups;
|
||||
if (projects != null && !projects.isEmpty()) {
|
||||
groups = getGroupsForProjects();
|
||||
} else {
|
||||
for(final AccountGroup group : groupCache.all()) {
|
||||
final GroupControl c = groupControlFactory.controlFor(group);
|
||||
if (c.isVisible()) {
|
||||
list.add(c.getAccountGroup());
|
||||
groups = groupCache.all();
|
||||
}
|
||||
return createGroupList(filterGroups(groups));
|
||||
}
|
||||
|
||||
private Set<AccountGroup> getGroupsForProjects() throws NoSuchGroupException {
|
||||
final SortedSet<AccountGroup> groups =
|
||||
new TreeSet<AccountGroup>(new GroupComparator());
|
||||
for (final ProjectControl projectControl : projects) {
|
||||
final Set<GroupReference> groupsRefs = projectControl.getAllGroups();
|
||||
for (final GroupReference groupRef : groupsRefs) {
|
||||
final AccountGroup group = groupCache.get(groupRef.getUUID());
|
||||
if (group == null) {
|
||||
throw new NoSuchGroupException(groupRef.getUUID());
|
||||
}
|
||||
groups.add(group);
|
||||
}
|
||||
}
|
||||
return groups;
|
||||
}
|
||||
|
||||
List<GroupDetail> l = new ArrayList<GroupDetail>();
|
||||
for(AccountGroup group : list) {
|
||||
l.add(groupDetailFactory.create(group.getId()).call());
|
||||
private List<AccountGroup> filterGroups(final Iterable<AccountGroup> groups) {
|
||||
final List<AccountGroup> filteredGroups = new LinkedList<AccountGroup>();
|
||||
final boolean isAdmin =
|
||||
identifiedUser.get().getCapabilities().canAdministrateServer();
|
||||
for (final AccountGroup group : groups) {
|
||||
if (!isAdmin) {
|
||||
final GroupControl c = groupControlFactory.controlFor(group);
|
||||
if (!c.isVisible()) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
filteredGroups.add(group);
|
||||
}
|
||||
return new GroupList(l, user.getCapabilities().canCreateGroup());
|
||||
return filteredGroups;
|
||||
}
|
||||
|
||||
private GroupList createGroupList(final List<AccountGroup> groups)
|
||||
throws OrmException, NoSuchGroupException {
|
||||
final List<GroupDetail> groupDetailList = new ArrayList<GroupDetail>();
|
||||
for (final AccountGroup group : groups) {
|
||||
groupDetailList.add(groupDetailFactory.create(group.getId()).call());
|
||||
}
|
||||
return new GroupList(groupDetailList, identifiedUser.get()
|
||||
.getCapabilities().canCreateGroup());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.google.gerrit.server.project;
|
||||
import com.google.gerrit.common.PageLinks;
|
||||
import com.google.gerrit.common.data.AccessSection;
|
||||
import com.google.gerrit.common.data.Capable;
|
||||
import com.google.gerrit.common.data.GroupReference;
|
||||
import com.google.gerrit.common.data.Permission;
|
||||
import com.google.gerrit.common.data.PermissionRule;
|
||||
import com.google.gerrit.reviewdb.AbstractAgreement;
|
||||
@@ -252,6 +253,19 @@ public class ProjectControl {
|
||||
return Capable.OK;
|
||||
}
|
||||
|
||||
public Set<GroupReference> getAllGroups() {
|
||||
final Set<GroupReference> all = new HashSet<GroupReference>();
|
||||
for (final SectionMatcher matcher : access()) {
|
||||
final AccessSection section = matcher.section;
|
||||
for (final Permission permission : section.getPermissions()) {
|
||||
for (final PermissionRule rule : permission.getRules()) {
|
||||
all.add(rule.getGroup());
|
||||
}
|
||||
}
|
||||
}
|
||||
return all;
|
||||
}
|
||||
|
||||
private Capable verifyActiveContributorAgreement() throws OrmException {
|
||||
if (! (user instanceof IdentifiedUser)) {
|
||||
return new Capable("Must be logged in to verify Contributor Agreement");
|
||||
|
||||
Reference in New Issue
Block a user