Write account.config for initial admin user

When an initial admin user is created by the InitAdminUser init step we
only created a user branch in the All-Users repository, but we forgot to
write the account.config file to it.

Change-Id: I09cfc058ab3aaeaa2fe2adf38c779f6f672eef18
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-07-07 13:27:37 +02:00
parent e86e4d370c
commit 530cefa02c
4 changed files with 84 additions and 78 deletions

View File

@@ -177,12 +177,16 @@ public class AccountConfig extends VersionedMetaData implements ValidationError.
}
Config cfg = readConfig(ACCOUNT_CONFIG);
writeToConfig(account, cfg);
saveConfig(ACCOUNT_CONFIG, cfg);
return true;
}
public static void writeToConfig(Account account, Config cfg) {
setActive(cfg, account.isActive());
set(cfg, KEY_FULL_NAME, account.getFullName());
set(cfg, KEY_PREFERRED_EMAIL, account.getPreferredEmail());
set(cfg, KEY_STATUS, account.getStatus());
saveConfig(ACCOUNT_CONFIG, cfg);
return true;
}
/**

View File

@@ -37,14 +37,10 @@ import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
@@ -333,52 +329,6 @@ public class AccountsUpdate {
deleteUserBranch(accountId);
}
public static void createUserBranch(
Repository repo,
Project.NameKey project,
GitReferenceUpdated gitRefUpdated,
@Nullable IdentifiedUser user,
ObjectInserter oi,
PersonIdent committerIdent,
PersonIdent authorIdent,
Account.Id accountId,
Timestamp registeredOn)
throws IOException {
ObjectId id = createInitialEmptyCommit(oi, committerIdent, authorIdent, registeredOn);
String refName = RefNames.refsUsers(accountId);
RefUpdate ru = repo.updateRef(refName);
ru.setExpectedOldObjectId(ObjectId.zeroId());
ru.setNewObjectId(id);
ru.setRefLogIdent(committerIdent);
ru.setRefLogMessage("Create Account", false);
Result result = ru.update();
if (result != Result.NEW) {
throw new IOException(String.format("Failed to update ref %s: %s", refName, result.name()));
}
gitRefUpdated.fire(project, ru, user != null ? user.getAccount() : null);
}
private static ObjectId createInitialEmptyCommit(
ObjectInserter oi,
PersonIdent committerIdent,
PersonIdent authorIdent,
Timestamp registrationDate)
throws IOException {
CommitBuilder cb = new CommitBuilder();
cb.setTreeId(emptyTree(oi));
cb.setCommitter(new PersonIdent(committerIdent, registrationDate));
cb.setAuthor(new PersonIdent(authorIdent, registrationDate));
cb.setMessage("Create Account");
ObjectId id = oi.insert(cb);
oi.flush();
return id;
}
private static ObjectId emptyTree(ObjectInserter oi) throws IOException {
return oi.insert(Constants.OBJ_TREE, new byte[] {});
}
private void deleteUserBranch(Account.Id accountId) throws IOException {
try (Repository repo = repoManager.openRepository(allUsersName)) {
deleteUserBranch(repo, allUsersName, gitRefUpdated, currentUser, committerIdent, accountId);