Add an AccountsUpdate class to update accounts from a single class

As a first step AccountsUpdate is only responsible for creating new
accounts. Additional functionality to update accounts will be added
later.

This is another step towards migrating accounts from ReviewDb to NoteDb.
In the end all code that needs to update accounts should use
AccountsUpdate. AccountsUpdate can then take care to update the accounts
in ReviewDb and NoteDb.

Change-Id: I77e3a664e23bfa579da7da7c1e86823e7ad905eb
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-04-06 16:13:20 +02:00
parent 6702433385
commit 74ddf290a3
4 changed files with 49 additions and 3 deletions

View File

@@ -26,6 +26,7 @@ import com.google.gerrit.reviewdb.client.AccountGroupMember;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.AccountByEmailCache;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.account.VersionedAuthorizedKeys;
import com.google.gerrit.server.account.externalids.ExternalId;
@@ -52,6 +53,7 @@ public class AccountCreator {
private final Map<String, TestAccount> accounts;
private final SchemaFactory<ReviewDb> reviewDbProvider;
private final AccountsUpdate accountsUpdate;
private final VersionedAuthorizedKeys.Accessor authorizedKeys;
private final GroupCache groupCache;
private final SshKeyCache sshKeyCache;
@@ -63,6 +65,7 @@ public class AccountCreator {
@Inject
AccountCreator(
SchemaFactory<ReviewDb> schema,
AccountsUpdate accountsUpdate,
VersionedAuthorizedKeys.Accessor authorizedKeys,
GroupCache groupCache,
SshKeyCache sshKeyCache,
@@ -72,6 +75,7 @@ public class AccountCreator {
ExternalIdsUpdate.Server externalIdsUpdate) {
accounts = new HashMap<>();
reviewDbProvider = schema;
this.accountsUpdate = accountsUpdate;
this.authorizedKeys = authorizedKeys;
this.groupCache = groupCache;
this.sshKeyCache = sshKeyCache;
@@ -110,7 +114,7 @@ public class AccountCreator {
Account a = new Account(id, TimeUtil.nowTs());
a.setFullName(fullName);
a.setPreferredEmail(email);
db.accounts().insert(Collections.singleton(a));
accountsUpdate.insert(db, a);
if (groups != null) {
for (String n : groups) {

View File

@@ -29,6 +29,7 @@ import com.google.gerrit.reviewdb.client.AccountGroupName;
import com.google.gerrit.reviewdb.client.AccountSshKey;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.index.account.AccountIndex;
import com.google.gerrit.server.index.account.AccountIndexCollection;
@@ -47,6 +48,7 @@ import org.apache.commons.validator.routines.EmailValidator;
public class InitAdminUser implements InitStep {
private final ConsoleUI ui;
private final InitFlags flags;
private final AccountsUpdate accountsUpdate;
private final VersionedAuthorizedKeysOnInit.Factory authorizedKeysFactory;
private final ExternalIdsOnInit externalIds;
private SchemaFactory<ReviewDb> dbFactory;
@@ -56,10 +58,12 @@ public class InitAdminUser implements InitStep {
InitAdminUser(
InitFlags flags,
ConsoleUI ui,
AccountsUpdate accountsUpdate,
VersionedAuthorizedKeysOnInit.Factory authorizedKeysFactory,
ExternalIdsOnInit externalIds) {
this.flags = flags;
this.ui = ui;
this.accountsUpdate = accountsUpdate;
this.authorizedKeysFactory = authorizedKeysFactory;
this.externalIds = externalIds;
}
@@ -106,7 +110,7 @@ public class InitAdminUser implements InitStep {
Account a = new Account(id, TimeUtil.nowTs());
a.setFullName(name);
a.setPreferredEmail(email);
db.accounts().insert(Collections.singleton(a));
accountsUpdate.insert(db, a);
AccountGroupName adminGroupName =
db.accountGroupNames().get(new AccountGroup.NameKey("Administrators"));

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2017 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.common.collect.ImmutableSet;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gwtorm.server.OrmDuplicateKeyException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Singleton;
/** Updates accounts. */
@Singleton
public class AccountsUpdate {
/**
* Inserts a new account.
*
* @throws OrmDuplicateKeyException if the account already exists
*/
public void insert(ReviewDb db, Account account) throws OrmException {
db.accounts().insert(ImmutableSet.of(account));
}
}

View File

@@ -69,6 +69,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
private final VersionedAuthorizedKeys.Accessor authorizedKeys;
private final SshKeyCache sshKeyCache;
private final AccountCache accountCache;
private final AccountsUpdate accountsUpdate;
private final AccountIndexer indexer;
private final AccountByEmailCache byEmailCache;
private final AccountLoader.Factory infoLoader;
@@ -86,6 +87,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
VersionedAuthorizedKeys.Accessor authorizedKeys,
SshKeyCache sshKeyCache,
AccountCache accountCache,
AccountsUpdate accountsUpdate,
AccountIndexer indexer,
AccountByEmailCache byEmailCache,
AccountLoader.Factory infoLoader,
@@ -100,6 +102,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
this.authorizedKeys = authorizedKeys;
this.sshKeyCache = sshKeyCache;
this.accountCache = accountCache;
this.accountsUpdate = accountsUpdate;
this.indexer = indexer;
this.byEmailCache = byEmailCache;
this.infoLoader = infoLoader;
@@ -172,7 +175,7 @@ public class CreateAccount implements RestModifyView<TopLevelResource, AccountIn
Account a = new Account(id, TimeUtil.nowTs());
a.setFullName(input.name);
a.setPreferredEmail(input.email);
db.accounts().insert(Collections.singleton(a));
accountsUpdate.insert(db, a);
for (AccountGroup.Id groupId : groups) {
AccountGroupMember m = new AccountGroupMember(new AccountGroupMember.Key(id, groupId));