use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class PutName method apply.
public Response<String> apply(IdentifiedUser user, NameInput input) throws MethodNotAllowedException, ResourceNotFoundException, IOException, ConfigInvalidException {
if (input == null) {
input = new NameInput();
}
Account.Id accountId = user.getAccountId();
if (realm.accountBelongsToRealm(externalIds.byAccount(accountId)) && !realm.allowsEdit(AccountFieldName.FULL_NAME)) {
throw new MethodNotAllowedException("realm does not allow editing name");
}
String newName = input.name;
AccountState accountState = accountsUpdateProvider.get().update("Set Full Name via API", accountId, u -> u.setFullName(newName)).orElseThrow(() -> new ResourceNotFoundException("account not found"));
return Strings.isNullOrEmpty(accountState.account().fullName()) ? Response.none() : Response.ok(accountState.account().fullName());
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class PutDisplayName method apply.
@Override
public Response<String> apply(AccountResource rsrc, @Nullable DisplayNameInput input) throws AuthException, ResourceNotFoundException, IOException, PermissionBackendException, ConfigInvalidException {
IdentifiedUser user = rsrc.getUser();
if (!self.get().hasSameAccountId(user)) {
permissionBackend.currentUser().check(GlobalPermission.MODIFY_ACCOUNT);
}
if (input == null) {
input = new DisplayNameInput();
}
String newDisplayName = input.displayName;
AccountState accountState = accountsUpdateProvider.get().update("Set Display Name via API", user.getAccountId(), u -> u.setDisplayName(newDisplayName)).orElseThrow(() -> new ResourceNotFoundException("account not found"));
return Strings.isNullOrEmpty(accountState.account().displayName()) ? Response.none() : Response.ok(accountState.account().displayName());
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class GetWatchedProjects method apply.
@Override
public Response<List<ProjectWatchInfo>> apply(AccountResource rsrc) throws AuthException, IOException, ConfigInvalidException, PermissionBackendException, ResourceNotFoundException {
if (!self.get().hasSameAccountId(rsrc.getUser())) {
permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
}
Account.Id accountId = rsrc.getUser().getAccountId();
AccountState account = accounts.get(accountId).orElseThrow(ResourceNotFoundException::new);
return Response.ok(account.projectWatches().entrySet().stream().map(e -> toProjectWatchInfo(e.getKey(), e.getValue())).sorted(comparing((ProjectWatchInfo pwi) -> pwi.project).thenComparing(pwi -> Strings.nullToEmpty(pwi.filter))).collect(toList()));
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class Abandon method abandon.
public Change abandon(BatchUpdate.Factory updateFactory, ChangeNotes notes, CurrentUser user, String msgTxt, NotifyResolver.Result notify) throws RestApiException, UpdateException {
AccountState accountState = user.isIdentifiedUser() ? user.asIdentifiedUser().state() : null;
AbandonOp op = abandonOpFactory.create(accountState, msgTxt);
ChangeData changeData = changeDataFactory.create(notes.getProjectName(), notes.getChangeId());
try (BatchUpdate u = updateFactory.create(notes.getProjectName(), user, TimeUtil.now())) {
u.setNotify(notify);
u.addOp(notes.getChangeId(), op);
u.addOp(notes.getChangeId(), storeSubmitRequirementsOpFactory.create(changeData.submitRequirements().values(), changeData));
u.execute();
}
return op.getChange();
}
use of com.google.gerrit.server.account.AccountState in project gerrit by GerritCodeReview.
the class AccountIT method atomicReadMofifyWrite.
@Test
public void atomicReadMofifyWrite() throws Exception {
gApi.accounts().id(admin.id().get()).setStatus("A-1");
AtomicInteger bgCounterA1 = new AtomicInteger(0);
AtomicInteger bgCounterA2 = new AtomicInteger(0);
PersonIdent ident = serverIdent.get();
AccountsUpdate update = new AccountsUpdate(repoManager, gitReferenceUpdated, Optional.empty(), allUsers, externalIds, metaDataUpdateInternalFactory, new RetryHelper(cfg, retryMetrics, null, null, null, exceptionHooks, r -> r.withBlockStrategy(noSleepBlockStrategy)), extIdNotesFactory, ident, ident, Runnables.doNothing(), () -> {
try {
accountsUpdateProvider.get().update("Set Status", admin.id(), u -> u.setStatus("A-2"));
} catch (IOException | ConfigInvalidException | StorageException e) {
// Ignore, the expected exception is asserted later
}
});
assertThat(bgCounterA1.get()).isEqualTo(0);
assertThat(bgCounterA2.get()).isEqualTo(0);
assertThat(gApi.accounts().id(admin.id().get()).get().status).isEqualTo("A-1");
Optional<AccountState> updatedAccountState = update.update("Set Status", admin.id(), (a, u) -> {
if ("A-1".equals(a.account().status())) {
bgCounterA1.getAndIncrement();
u.setStatus("B-1");
}
if ("A-2".equals(a.account().status())) {
bgCounterA2.getAndIncrement();
u.setStatus("B-2");
}
});
assertThat(bgCounterA1.get()).isEqualTo(1);
assertThat(bgCounterA2.get()).isEqualTo(1);
assertThat(updatedAccountState).isPresent();
assertThat(updatedAccountState.get().account().status()).isEqualTo("B-2");
assertThat(accounts.get(admin.id()).get().account().status()).isEqualTo("B-2");
assertThat(gApi.accounts().id(admin.id().get()).get().status).isEqualTo("B-2");
}
Aggregations