use of com.google.gerrit.server.account.AccountResource in project gerrit by GerritCodeReview.
the class RestApiQuotaEnforcer method enforce.
/**
* Enforce quota on a request for a given resource.
*/
void enforce(RestResource rsrc, HttpServletRequest req) throws QuotaException {
String pathForQuotaReporting = RequestUtil.getRestPathWithoutIds(req);
// Enrich the quota request we are operating on an interesting collection
QuotaBackend.WithResource report = quotaBackend.currentUser();
if (rsrc instanceof ChangeResource) {
ChangeResource changeResource = (ChangeResource) rsrc;
report = quotaBackend.currentUser().change(changeResource.getId(), changeResource.getProject());
} else if (rsrc instanceof AccountResource) {
AccountResource accountResource = (AccountResource) rsrc;
report = quotaBackend.currentUser().account(accountResource.getUser().getAccountId());
} else if (rsrc instanceof ProjectResource) {
ProjectResource projectResource = (ProjectResource) rsrc;
report = quotaBackend.currentUser().project(projectResource.getNameKey());
}
report.requestToken(quotaGroup(pathForQuotaReporting, req.getMethod())).throwOnError();
}
use of com.google.gerrit.server.account.AccountResource in project gerrit by GerritCodeReview.
the class SetAccountCommand method setAccount.
private void setAccount() throws Failure {
user = genericUserFactory.create(id);
rsrc = new AccountResource(user.asIdentifiedUser());
try {
for (String email : addEmails) {
addEmail(email);
}
for (String email : deleteEmails) {
deleteEmail(email);
}
if (preferredEmail != null) {
putPreferred(preferredEmail);
}
if (fullName != null) {
NameInput in = new NameInput();
in.name = fullName;
putName.apply(rsrc, in);
}
if (httpPassword != null || clearHttpPassword || generateHttpPassword) {
HttpPasswordInput in = new HttpPasswordInput();
in.httpPassword = httpPassword;
if (generateHttpPassword) {
in.generate = true;
}
Response<String> resp = putHttpPassword.apply(rsrc, in);
if (generateHttpPassword) {
stdout.print("New password: " + resp.value() + "\n");
}
}
if (active) {
putActive.apply(rsrc, null);
} else if (inactive) {
try {
deleteActive.apply(rsrc, null);
} catch (ResourceNotFoundException e) {
// user is already inactive
}
}
addSshKeys = readSshKey(addSshKeys);
if (!addSshKeys.isEmpty()) {
addSshKeys(addSshKeys);
}
deleteSshKeys = readSshKey(deleteSshKeys);
if (!deleteSshKeys.isEmpty()) {
deleteSshKeys(deleteSshKeys);
}
for (String externalId : externalIdsToDelete) {
deleteExternalId(externalId);
}
} catch (RestApiException e) {
throw die(e.getMessage());
} catch (Exception e) {
throw new Failure(1, "unavailable", e);
}
}
use of com.google.gerrit.server.account.AccountResource in project gerrit by GerritCodeReview.
the class PostGpgKeys method apply.
@Override
public Map<String, GpgKeyInfo> apply(AccountResource rsrc, Input input) throws ResourceNotFoundException, BadRequestException, ResourceConflictException, PGPException, OrmException, IOException, ConfigInvalidException {
GpgKeys.checkVisible(self, rsrc);
Collection<ExternalId> existingExtIds = externalIds.byAccount(rsrc.getUser().getAccountId(), SCHEME_GPGKEY);
try (PublicKeyStore store = storeProvider.get()) {
Set<Fingerprint> toRemove = readKeysToRemove(input, existingExtIds);
List<PGPPublicKeyRing> newKeys = readKeysToAdd(input, toRemove);
List<ExternalId> newExtIds = new ArrayList<>(existingExtIds.size());
for (PGPPublicKeyRing keyRing : newKeys) {
PGPPublicKey key = keyRing.getPublicKey();
ExternalId.Key extIdKey = toExtIdKey(key.getFingerprint());
Account account = getAccountByExternalId(extIdKey);
if (account != null) {
if (!account.getId().equals(rsrc.getUser().getAccountId())) {
throw new ResourceConflictException("GPG key already associated with another account");
}
} else {
newExtIds.add(ExternalId.create(extIdKey, rsrc.getUser().getAccountId()));
}
}
storeKeys(rsrc, newKeys, toRemove);
List<ExternalId.Key> extIdKeysToRemove = toRemove.stream().map(fp -> toExtIdKey(fp.get())).collect(toList());
externalIdsUpdateFactory.create().replace(rsrc.getUser().getAccountId(), extIdKeysToRemove, newExtIds);
accountCache.evict(rsrc.getUser().getAccountId());
return toJson(newKeys, toRemove, store, rsrc.getUser());
}
}
use of com.google.gerrit.server.account.AccountResource 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.AccountResource 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()));
}
Aggregations