Line data Source code
1 : // Copyright (C) 2013 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.restapi.group; 16 : 17 : import static com.google.gerrit.extensions.client.ListGroupsOption.INCLUDES; 18 : import static com.google.gerrit.extensions.client.ListGroupsOption.MEMBERS; 19 : 20 : import com.google.common.base.Strings; 21 : import com.google.common.base.Suppliers; 22 : import com.google.gerrit.entities.AccountGroup; 23 : import com.google.gerrit.entities.GroupDescription; 24 : import com.google.gerrit.extensions.client.ListGroupsOption; 25 : import com.google.gerrit.extensions.common.GroupInfo; 26 : import com.google.gerrit.extensions.common.GroupOptionsInfo; 27 : import com.google.gerrit.extensions.restapi.Url; 28 : import com.google.gerrit.server.account.GroupBackend; 29 : import com.google.gerrit.server.account.GroupControl; 30 : import com.google.gerrit.server.group.GroupResource; 31 : import com.google.gerrit.server.permissions.PermissionBackendException; 32 : import com.google.inject.Inject; 33 : import com.google.inject.Provider; 34 : import java.util.Collection; 35 : import java.util.EnumSet; 36 : import java.util.function.Supplier; 37 : 38 : public class GroupJson { 39 : public static GroupOptionsInfo createOptions(GroupDescription.Basic group) { 40 36 : GroupOptionsInfo options = new GroupOptionsInfo(); 41 36 : if (group instanceof GroupDescription.Internal 42 36 : && ((GroupDescription.Internal) group).isVisibleToAll()) { 43 7 : options.visibleToAll = true; 44 : } 45 36 : return options; 46 : } 47 : 48 : private final GroupBackend groupBackend; 49 : private final GroupControl.Factory groupControlFactory; 50 : private final Provider<ListMembers> listMembers; 51 : private final Provider<ListSubgroups> listSubgroups; 52 : private EnumSet<ListGroupsOption> options; 53 : 54 : @Inject 55 : GroupJson( 56 : GroupBackend groupBackend, 57 : GroupControl.Factory groupControlFactory, 58 : Provider<ListMembers> listMembers, 59 149 : Provider<ListSubgroups> listSubgroups) { 60 149 : this.groupBackend = groupBackend; 61 149 : this.groupControlFactory = groupControlFactory; 62 149 : this.listMembers = listMembers; 63 149 : this.listSubgroups = listSubgroups; 64 : 65 149 : options = EnumSet.noneOf(ListGroupsOption.class); 66 149 : } 67 : 68 : public GroupJson addOption(ListGroupsOption o) { 69 146 : options.add(o); 70 146 : return this; 71 : } 72 : 73 : public GroupJson addOptions(Collection<ListGroupsOption> o) { 74 15 : options.addAll(o); 75 15 : return this; 76 : } 77 : 78 : public GroupInfo format(GroupResource rsrc) throws PermissionBackendException { 79 4 : return createGroupInfo(rsrc.getGroup(), rsrc::getControl); 80 : } 81 : 82 : public GroupInfo format(GroupDescription.Basic group) throws PermissionBackendException { 83 36 : return createGroupInfo(group, Suppliers.memoize(() -> groupControlFactory.controlFor(group))); 84 : } 85 : 86 : private GroupInfo createGroupInfo( 87 : GroupDescription.Basic group, Supplier<GroupControl> groupControlSupplier) 88 : throws PermissionBackendException { 89 36 : GroupInfo info = createBasicGroupInfo(group); 90 : 91 36 : if (group instanceof GroupDescription.Internal) { 92 36 : addInternalDetails(info, (GroupDescription.Internal) group, groupControlSupplier); 93 : } 94 : 95 36 : return info; 96 : } 97 : 98 : private static GroupInfo createBasicGroupInfo(GroupDescription.Basic group) { 99 36 : GroupInfo info = new GroupInfo(); 100 36 : info.id = Url.encode(group.getGroupUUID().get()); 101 36 : info.name = Strings.emptyToNull(group.getName()); 102 36 : info.url = Strings.emptyToNull(group.getUrl()); 103 36 : info.options = createOptions(group); 104 36 : return info; 105 : } 106 : 107 : private void addInternalDetails( 108 : GroupInfo info, 109 : GroupDescription.Internal internalGroup, 110 : Supplier<GroupControl> groupControlSupplier) 111 : throws PermissionBackendException { 112 36 : info.description = Strings.emptyToNull(internalGroup.getDescription()); 113 36 : info.groupId = internalGroup.getId().get(); 114 : 115 36 : AccountGroup.UUID ownerGroupUUID = internalGroup.getOwnerGroupUUID(); 116 36 : if (ownerGroupUUID != null) { 117 36 : info.ownerId = Url.encode(ownerGroupUUID.get()); 118 36 : GroupDescription.Basic o = groupBackend.get(ownerGroupUUID); 119 36 : if (o != null) { 120 36 : info.owner = o.getName(); 121 : } 122 : } 123 : 124 36 : info.setCreatedOn(internalGroup.getCreatedOn()); 125 : 126 36 : if (options.contains(MEMBERS)) { 127 5 : info.members = listMembers.get().getDirectMembers(internalGroup, groupControlSupplier.get()); 128 : } 129 : 130 36 : if (options.contains(INCLUDES)) { 131 4 : info.includes = 132 4 : listSubgroups.get().getDirectSubgroups(internalGroup, groupControlSupplier.get()); 133 : } 134 36 : } 135 : }