Switch to UUID of groups for all public methods of GroupsUpdate

Change-Id: I9a4bb23b69bf50b19b30f29ddbf65e621e42968a
This commit is contained in:
Alice Kober-Sotzek
2017-07-28 11:27:25 +02:00
parent 6f2b3c95d6
commit 01154ca574
7 changed files with 36 additions and 54 deletions

View File

@@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.US_ASCII;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.server.ReviewDb;
@@ -28,6 +29,7 @@ import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.account.VersionedAuthorizedKeys;
import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gerrit.server.group.Groups;
import com.google.gerrit.server.group.GroupsUpdate;
import com.google.gerrit.server.group.ServerInitiated;
import com.google.gerrit.server.ssh.SshKeyCache;
@@ -44,6 +46,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@Singleton
public class AccountCreator {
@@ -53,6 +56,7 @@ public class AccountCreator {
private final Sequences sequences;
private final AccountsUpdate.Server accountsUpdate;
private final VersionedAuthorizedKeys.Accessor authorizedKeys;
private final Groups groups;
private final Provider<GroupsUpdate> groupsUpdateProvider;
private final SshKeyCache sshKeyCache;
private final AccountCache accountCache;
@@ -66,6 +70,7 @@ public class AccountCreator {
Sequences sequences,
AccountsUpdate.Server accountsUpdate,
VersionedAuthorizedKeys.Accessor authorizedKeys,
Groups groups,
@ServerInitiated Provider<GroupsUpdate> groupsUpdateProvider,
SshKeyCache sshKeyCache,
AccountCache accountCache,
@@ -77,6 +82,7 @@ public class AccountCreator {
this.sequences = sequences;
this.accountsUpdate = accountsUpdate;
this.authorizedKeys = authorizedKeys;
this.groups = groups;
this.groupsUpdateProvider = groupsUpdateProvider;
this.sshKeyCache = sshKeyCache;
this.accountCache = accountCache;
@@ -89,7 +95,7 @@ public class AccountCreator {
@Nullable String username,
@Nullable String email,
@Nullable String fullName,
String... groups)
String... groupNames)
throws Exception {
TestAccount account = accounts.get(username);
@@ -121,10 +127,14 @@ public class AccountCreator {
a.setPreferredEmail(email);
});
if (groups != null) {
for (String n : groups) {
if (groupNames != null) {
for (String n : groupNames) {
AccountGroup.NameKey k = new AccountGroup.NameKey(n);
groupsUpdateProvider.get().addGroupMember(db, k, id);
Optional<AccountGroup> group = groups.get(db, k);
if (!group.isPresent()) {
throw new NoSuchGroupException(n);
}
groupsUpdateProvider.get().addGroupMember(db, group.get().getGroupUUID(), id);
}
}

View File

@@ -132,7 +132,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
"Username '" + username + "' must contain only letters, numbers, _, - or .");
}
Set<AccountGroup.Id> groups = parseGroups(input.groups);
Set<AccountGroup.UUID> groups = parseGroups(input.groups);
Account.Id id = new Account.Id(seq.nextAccountId());
@@ -185,8 +185,8 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
a.setPreferredEmail(input.email);
});
for (AccountGroup.Id groupId : groups) {
groupsUpdate.get().addGroupMember(db, groupId, id);
for (AccountGroup.UUID groupUuid : groups) {
groupsUpdate.get().addGroupMember(db, groupUuid, id);
}
if (input.sshKey != null) {
@@ -207,14 +207,15 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
return Response.created(info);
}
private Set<AccountGroup.Id> parseGroups(List<String> groups)
private Set<AccountGroup.UUID> parseGroups(List<String> groups)
throws UnprocessableEntityException {
Set<AccountGroup.Id> groupIds = new HashSet<>();
Set<AccountGroup.UUID> groupUuids = new HashSet<>();
if (groups != null) {
for (String g : groups) {
groupIds.add(GroupDescriptions.toAccountGroup(groupsCollection.parseInternal(g)).getId());
AccountGroup group = GroupDescriptions.toAccountGroup(groupsCollection.parseInternal(g));
groupUuids.add(group.getGroupUUID());
}
}
return groupIds;
return groupUuids;
}
}

View File

@@ -98,7 +98,7 @@ public class PutAgreement implements RestModifyView<AccountResource, AgreementIn
}
Account account = self.get().getAccount();
addMembers.addMembers(group.getId(), ImmutableList.of(account.getId()));
addMembers.addMembers(group.getGroupUUID(), ImmutableList.of(account.getId()));
agreementSignup.fire(account, agreementName);
return Response.ok(agreementName);

View File

@@ -131,7 +131,7 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
newMemberIds.add(a.getId());
}
addMembers(internalGroup.getId(), newMemberIds);
addMembers(internalGroup.getGroupUUID(), newMemberIds);
return toAccountInfoList(newMemberIds);
}
@@ -168,11 +168,11 @@ public class AddMembers implements RestModifyView<GroupResource, Input> {
}
}
public void addMembers(AccountGroup.Id groupId, Collection<? extends Account.Id> newMemberIds)
public void addMembers(AccountGroup.UUID groupUuid, Collection<? extends Account.Id> newMemberIds)
throws OrmException, IOException {
groupsUpdateProvider
.get()
.addGroupMembers(db.get(), groupId, ImmutableSet.copyOf(newMemberIds));
.addGroupMembers(db.get(), groupUuid, ImmutableSet.copyOf(newMemberIds));
}
private Account createAccountByLdap(String user) throws IOException {

View File

@@ -211,7 +211,7 @@ public class CreateGroup implements RestModifyView<TopLevelResource, GroupInput>
"group '" + createGroupArgs.getGroupName() + "' already exists");
}
addMembers.addMembers(groupId, createGroupArgs.initialMembers);
addMembers.addMembers(uuid, createGroupArgs.initialMembers);
groupCache.onCreateGroup(createGroupArgs.getGroup());

View File

@@ -147,9 +147,9 @@ public class GroupsUpdate {
}
public Optional<AccountGroup> renameGroup(
ReviewDb db, AccountGroup.Id groupId, AccountGroup.NameKey newName)
ReviewDb db, AccountGroup.UUID groupUuid, AccountGroup.NameKey newName)
throws OrmException, IOException, NameAlreadyUsedException {
Optional<AccountGroup> foundGroup = groups.get(db, groupId);
Optional<AccountGroup> foundGroup = groups.get(db, groupUuid);
if (!foundGroup.isPresent()) {
return Optional.empty();
}
@@ -158,13 +158,13 @@ public class GroupsUpdate {
AccountGroup.NameKey oldName = group.getNameKey();
try {
AccountGroupName id = new AccountGroupName(newName, groupId);
AccountGroupName id = new AccountGroupName(newName, group.getId());
db.accountGroupNames().insert(ImmutableList.of(id));
} catch (OrmException e) {
AccountGroupName other = db.accountGroupNames().get(newName);
if (other != null) {
// If we are using this identity, don't report the exception.
if (other.getId().equals(groupId)) {
if (other.getId().equals(group.getId())) {
return Optional.of(group);
}
@@ -185,25 +185,18 @@ public class GroupsUpdate {
@SuppressWarnings("unused")
Future<?> possiblyIgnoredError =
renameGroupOpFactory
.create(committerIdent, group.getGroupUUID(), oldName.get(), newName.get())
.create(committerIdent, groupUuid, oldName.get(), newName.get())
.start(0, TimeUnit.MILLISECONDS);
return Optional.of(group);
}
public void addGroupMember(ReviewDb db, AccountGroup.NameKey groupName, Account.Id accountId)
throws OrmException, IOException {
Optional<AccountGroup> foundGroup = groups.get(db, groupName);
if (!foundGroup.isPresent()) {
// TODO(aliceks): Throw an exception?
return;
}
AccountGroup group = foundGroup.get();
addGroupMembers(db, group, ImmutableSet.of(accountId));
}
public void addGroupMember(ReviewDb db, AccountGroup.UUID groupUuid, Account.Id accountId)
throws OrmException, IOException {
addGroupMembers(db, groupUuid, ImmutableSet.of(accountId));
}
public void addGroupMembers(ReviewDb db, AccountGroup.UUID groupUuid, Set<Account.Id> accountIds)
throws OrmException, IOException {
Optional<AccountGroup> foundGroup = groups.get(db, groupUuid);
if (!foundGroup.isPresent()) {
// TODO(aliceks): Throw an exception?
@@ -211,28 +204,6 @@ public class GroupsUpdate {
}
AccountGroup group = foundGroup.get();
addGroupMembers(db, group, ImmutableSet.of(accountId));
}
public void addGroupMember(ReviewDb db, AccountGroup.Id groupId, Account.Id accountId)
throws OrmException, IOException {
addGroupMembers(db, groupId, ImmutableSet.of(accountId));
}
public void addGroupMembers(ReviewDb db, AccountGroup.Id groupId, Set<Account.Id> accountIds)
throws OrmException, IOException {
Optional<AccountGroup> foundGroup = groups.get(db, groupId);
if (!foundGroup.isPresent()) {
// TODO(aliceks): Throw an exception?
return;
}
AccountGroup group = foundGroup.get();
addGroupMembers(db, group, accountIds);
}
private void addGroupMembers(ReviewDb db, AccountGroup group, Set<Account.Id> accountIds)
throws OrmException, IOException {
AccountGroup.Id groupId = group.getId();
Set<AccountGroupMember> newMembers = new HashSet<>();
for (Account.Id accountId : accountIds) {

View File

@@ -77,7 +77,7 @@ public class PutName implements RestModifyView<GroupResource, Input> {
Optional<AccountGroup> renamedGroup =
groupsUpdateProvider
.get()
.renameGroup(db.get(), group.getId(), new AccountGroup.NameKey(newName));
.renameGroup(db.get(), group.getGroupUUID(), new AccountGroup.NameKey(newName));
return renamedGroup
.map(AccountGroup::getName)
.orElseThrow(() -> new ResourceNotFoundException("Group not found"));