Delete user branch when account is deleted
Change-Id: I4fde3560c5d5f138a19f69b695d881fc4b3ce337 Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
@@ -41,6 +41,7 @@ import com.google.gerrit.reviewdb.client.RefNames;
|
|||||||
import com.google.gerrit.server.ChangeUtil;
|
import com.google.gerrit.server.ChangeUtil;
|
||||||
import com.google.gerrit.server.IdentifiedUser;
|
import com.google.gerrit.server.IdentifiedUser;
|
||||||
import com.google.gerrit.server.Sequences;
|
import com.google.gerrit.server.Sequences;
|
||||||
|
import com.google.gerrit.server.account.AccountsUpdate;
|
||||||
import com.google.gerrit.server.change.ChangeInserter;
|
import com.google.gerrit.server.change.ChangeInserter;
|
||||||
import com.google.gerrit.server.change.ConsistencyChecker;
|
import com.google.gerrit.server.change.ConsistencyChecker;
|
||||||
import com.google.gerrit.server.change.PatchSetInserter;
|
import com.google.gerrit.server.change.PatchSetInserter;
|
||||||
@@ -90,6 +91,8 @@ public class ConsistencyCheckerIT extends AbstractDaemonTest {
|
|||||||
|
|
||||||
@Inject private Sequences sequences;
|
@Inject private Sequences sequences;
|
||||||
|
|
||||||
|
@Inject private AccountsUpdate.Server accountsUpdate;
|
||||||
|
|
||||||
private RevCommit tip;
|
private RevCommit tip;
|
||||||
private Account.Id adminId;
|
private Account.Id adminId;
|
||||||
private ConsistencyChecker checker;
|
private ConsistencyChecker checker;
|
||||||
@@ -119,7 +122,7 @@ public class ConsistencyCheckerIT extends AbstractDaemonTest {
|
|||||||
public void missingOwner() throws Exception {
|
public void missingOwner() throws Exception {
|
||||||
TestAccount owner = accounts.create("missing");
|
TestAccount owner = accounts.create("missing");
|
||||||
ChangeControl ctl = insertChange(owner);
|
ChangeControl ctl = insertChange(owner);
|
||||||
db.accounts().deleteKeys(singleton(owner.getId()));
|
accountsUpdate.create().deleteByKey(db, owner.getId());
|
||||||
|
|
||||||
assertProblems(ctl, null, problem("Missing change owner: " + owner.getId()));
|
assertProblems(ctl, null, problem("Missing change owner: " + owner.getId()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -232,12 +232,13 @@ public class AccountManager {
|
|||||||
awaitsFirstAccountCheck.getAndSet(false) && db.accounts().anyAccounts().toList().isEmpty();
|
awaitsFirstAccountCheck.getAndSet(false) && db.accounts().anyAccounts().toList().isEmpty();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
accountsUpdateFactory.create().upsert(db, account);
|
AccountsUpdate accountsUpdate = accountsUpdateFactory.create();
|
||||||
|
accountsUpdate.upsert(db, account);
|
||||||
|
|
||||||
ExternalId existingExtId = externalIds.get(db, extId.key());
|
ExternalId existingExtId = externalIds.get(db, extId.key());
|
||||||
if (existingExtId != null && !existingExtId.accountId().equals(extId.accountId())) {
|
if (existingExtId != null && !existingExtId.accountId().equals(extId.accountId())) {
|
||||||
// external ID is assigned to another account, do not overwrite
|
// external ID is assigned to another account, do not overwrite
|
||||||
db.accounts().delete(Collections.singleton(account));
|
accountsUpdate.delete(db, account);
|
||||||
throw new AccountException(
|
throw new AccountException(
|
||||||
"Cannot assign external ID \""
|
"Cannot assign external ID \""
|
||||||
+ extId.key().get()
|
+ extId.key().get()
|
||||||
@@ -345,7 +346,7 @@ public class AccountManager {
|
|||||||
// such an account cannot be used for uploading changes,
|
// such an account cannot be used for uploading changes,
|
||||||
// this is why the best we can do here is to fail early and cleanup
|
// this is why the best we can do here is to fail early and cleanup
|
||||||
// the database
|
// the database
|
||||||
db.accounts().delete(Collections.singleton(account));
|
accountsUpdateFactory.create().delete(db, account);
|
||||||
externalIdsUpdateFactory.create().delete(db, extId);
|
externalIdsUpdateFactory.create().delete(db, extId);
|
||||||
throw new AccountUserNameException(errorMessage, e);
|
throw new AccountUserNameException(errorMessage, e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import org.eclipse.jgit.lib.Constants;
|
|||||||
import org.eclipse.jgit.lib.ObjectId;
|
import org.eclipse.jgit.lib.ObjectId;
|
||||||
import org.eclipse.jgit.lib.ObjectInserter;
|
import org.eclipse.jgit.lib.ObjectInserter;
|
||||||
import org.eclipse.jgit.lib.PersonIdent;
|
import org.eclipse.jgit.lib.PersonIdent;
|
||||||
|
import org.eclipse.jgit.lib.Ref;
|
||||||
import org.eclipse.jgit.lib.RefUpdate;
|
import org.eclipse.jgit.lib.RefUpdate;
|
||||||
import org.eclipse.jgit.lib.RefUpdate.Result;
|
import org.eclipse.jgit.lib.RefUpdate.Result;
|
||||||
import org.eclipse.jgit.lib.Repository;
|
import org.eclipse.jgit.lib.Repository;
|
||||||
@@ -144,6 +145,18 @@ public class AccountsUpdate {
|
|||||||
createUserBranchIfNeeded(account);
|
createUserBranchIfNeeded(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Deletes the account. */
|
||||||
|
public void delete(ReviewDb db, Account account) throws OrmException, IOException {
|
||||||
|
db.accounts().delete(ImmutableSet.of(account));
|
||||||
|
deleteUserBranch(account.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Deletes the account. */
|
||||||
|
public void deleteByKey(ReviewDb db, Account.Id accountId) throws OrmException, IOException {
|
||||||
|
db.accounts().deleteKeys(ImmutableSet.of(accountId));
|
||||||
|
deleteUserBranch(accountId);
|
||||||
|
}
|
||||||
|
|
||||||
private void createUserBranch(Account account) throws IOException {
|
private void createUserBranch(Account account) throws IOException {
|
||||||
try (Repository repo = repoManager.openRepository(allUsersName);
|
try (Repository repo = repoManager.openRepository(allUsersName);
|
||||||
ObjectInserter oi = repo.newObjectInserter()) {
|
ObjectInserter oi = repo.newObjectInserter()) {
|
||||||
@@ -209,4 +222,25 @@ public class AccountsUpdate {
|
|||||||
private static ObjectId emptyTree(ObjectInserter oi) throws IOException {
|
private static ObjectId emptyTree(ObjectInserter oi) throws IOException {
|
||||||
return oi.insert(Constants.OBJ_TREE, new byte[] {});
|
return oi.insert(Constants.OBJ_TREE, new byte[] {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void deleteUserBranch(Account.Id accountId) throws IOException {
|
||||||
|
try (Repository repo = repoManager.openRepository(allUsersName)) {
|
||||||
|
String refName = RefNames.refsUsers(accountId);
|
||||||
|
Ref ref = repo.exactRef(refName);
|
||||||
|
if (ref == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
RefUpdate ru = repo.updateRef(refName);
|
||||||
|
ru.setExpectedOldObjectId(ref.getObjectId());
|
||||||
|
ru.setNewObjectId(ObjectId.zeroId());
|
||||||
|
ru.setForceUpdate(true);
|
||||||
|
ru.setRefLogIdent(committerIdent);
|
||||||
|
ru.setRefLogMessage("Delete Account", true);
|
||||||
|
Result result = ru.delete();
|
||||||
|
if (result != Result.FORCED) {
|
||||||
|
throw new IOException(String.format("Failed to delete ref %s: %s", refName, result.name()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user