use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class InitAdminUser method postRun.
@Override
public void postRun() throws Exception {
if (!accounts.hasAnyAccount()) {
welcome();
}
AuthType authType = flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
if (authType != AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT) {
return;
}
if (!accounts.hasAnyAccount()) {
ui.header("Gerrit Administrator");
if (ui.yesno(true, "Create administrator user")) {
Account.Id id = Account.id(sequencesOnInit.nextAccountId());
String username = ui.readString("admin", "username");
String name = ui.readString("Administrator", "name");
String httpPassword = ui.readString("secret", "HTTP password");
AccountSshKey sshKey = readSshKey(id);
String email = readEmail(sshKey);
List<ExternalId> extIds = new ArrayList<>(2);
extIds.add(externalIdFactory.createUsername(username, id, httpPassword));
if (email != null) {
extIds.add(externalIdFactory.createEmail(id, email));
}
externalIds.insert("Add external IDs for initial admin user", extIds);
Account persistedAccount = accounts.insert(Account.builder(id, TimeUtil.now()).setFullName(name).setPreferredEmail(email));
// Only two groups should exist at this point in time and hence iterating over all of them
// is cheap.
Optional<GroupReference> adminGroupReference = groupsOnInit.getAllGroupReferences().filter(group -> group.getName().equals("Administrators")).findAny();
if (!adminGroupReference.isPresent()) {
throw new NoSuchGroupException("Administrators");
}
GroupReference adminGroup = adminGroupReference.get();
groupsOnInit.addGroupMember(adminGroup.getUUID(), persistedAccount);
if (sshKey != null) {
VersionedAuthorizedKeysOnInit authorizedKeys = authorizedKeysFactory.create(id).load();
authorizedKeys.addKey(sshKey.sshPublicKey());
authorizedKeys.save("Add SSH key for initial admin user\n");
}
AccountState as = AccountState.forAccount(persistedAccount, extIds);
for (AccountIndex accountIndex : accountIndexCollection.getWriteIndexes()) {
accountIndex.replace(as);
}
InternalGroup adminInternalGroup = groupsOnInit.getExistingGroup(adminGroup);
for (GroupIndex groupIndex : groupIndexCollection.getWriteIndexes()) {
groupIndex.replace(adminInternalGroup);
}
}
}
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class BatchAbandon method batchAbandon.
/**
* If an extension has more than one changes to abandon that belong to the same project, they
* should use the batch instead of abandoning one by one.
*
* <p>It's the caller's responsibility to ensure that all jobs inside the same batch have the
* matching project from its ChangeData. Violations will result in a ResourceConflictException.
*/
public void batchAbandon(BatchUpdate.Factory updateFactory, Project.NameKey project, CurrentUser user, Collection<ChangeData> changes, String msgTxt, NotifyResolver.Result notify) throws RestApiException, UpdateException {
if (changes.isEmpty()) {
return;
}
AccountState accountState = user.isIdentifiedUser() ? user.asIdentifiedUser().state() : null;
try (BatchUpdate u = updateFactory.create(project, user, TimeUtil.now())) {
u.setNotify(notify);
for (ChangeData change : changes) {
if (!project.equals(change.project())) {
throw new ResourceConflictException(String.format("Project name \"%s\" doesn't match \"%s\"", change.project().get(), project.get()));
}
u.addOp(change.getId(), abandonOpFactory.create(accountState, msgTxt));
u.addOp(change.getId(), storeSubmitRequirementsOpFactory.create(change.submitRequirements().values(), change));
}
u.execute();
if (cfg.getCleanupAccountPatchReview()) {
cleanupAccountPatchReview(changes);
}
}
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class InternalAuthBackend method authenticate.
// TODO(gerritcodereview-team): This function has no coverage.
@Override
public AuthUser authenticate(AuthRequest req) throws MissingCredentialsException, InvalidCredentialsException, UnknownUserException, UserNotAllowedException, AuthException {
if (!req.getUsername().isPresent() || !req.getPassword().isPresent()) {
throw new MissingCredentialsException();
}
String username;
if (authConfig.isUserNameToLowerCase()) {
username = req.getUsername().map(u -> u.toLowerCase(Locale.US)).get();
} else {
username = req.getUsername().get();
}
AccountState who = accountCache.getByUsername(username).orElseThrow(UnknownUserException::new);
if (!who.account().isActive()) {
throw new UserNotAllowedException("Authentication failed for " + username + ": account inactive or not provisioned in Gerrit");
}
if (!passwordVerifier.checkPassword(who.externalIds(), username, req.getPassword().get())) {
throw new InvalidCredentialsException();
}
return new AuthUser(AuthUser.UUID.create(username), username);
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class PostReview method batchReviewerEvents.
private void batchReviewerEvents(CurrentUser user, ChangeData cd, PatchSet patchSet, List<ReviewerModification> reviewerModifications, Instant when) {
List<AccountState> newlyAddedReviewers = new ArrayList<>();
// There are no events for CCs and reviewers added/deleted by email.
for (ReviewerModification modification : reviewerModifications) {
Result reviewerAdditionResult = modification.op.getResult();
if (modification.state() == ReviewerState.REVIEWER) {
newlyAddedReviewers.addAll(reviewerAdditionResult.addedReviewers().stream().map(psa -> psa.accountId()).map(accountId -> accountCache.get(accountId)).flatMap(Streams::stream).collect(toList()));
} else if (modification.state() == ReviewerState.REMOVED) {
// There is no batch event for reviewer removals, hence fire the event for each
// modification that deleted a reviewer immediately.
modification.op.sendEvent();
}
}
// Fire a batch event for all newly added reviewers.
reviewerAdded.fire(cd, patchSet, newlyAddedReviewers, user.asIdentifiedUser().state(), when);
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AccountTemplateUtil method replaceTemplates.
/**
* Builds user-readable text from text, that might contain {@link #ACCOUNT_TEMPLATE}.
*/
public String replaceTemplates(String messageTemplate) {
Matcher matcher = ACCOUNT_TEMPLATE_PATTERN.matcher(messageTemplate);
StringBuilder out = new StringBuilder();
while (matcher.find()) {
String accountId = matcher.group(1);
String unrecognizedAccount = "Unrecognized Gerrit Account " + accountId;
Optional<Account.Id> parsedAccountId = Account.Id.tryParse(accountId);
if (parsedAccountId.isPresent()) {
Optional<AccountState> account = accountCache.get(parsedAccountId.get());
if (account.isPresent()) {
matcher.appendReplacement(out, account.get().account().getNameEmail(unrecognizedAccount));
continue;
}
}
matcher.appendReplacement(out, unrecognizedAccount);
}
matcher.appendTail(out);
return out.toString();
}
Aggregations