Search in sources :

Example 1 with AccountResource

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();
}
Also used : QuotaBackend(com.google.gerrit.server.quota.QuotaBackend) AccountResource(com.google.gerrit.server.account.AccountResource) ChangeResource(com.google.gerrit.server.change.ChangeResource) ProjectResource(com.google.gerrit.server.project.ProjectResource)

Example 2 with AccountResource

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);
    }
}
Also used : AccountResource(com.google.gerrit.server.account.AccountResource) HttpPasswordInput(com.google.gerrit.extensions.common.HttpPasswordInput) IdString(com.google.gerrit.extensions.restapi.IdString) NameInput(com.google.gerrit.extensions.common.NameInput) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) EmailException(com.google.gerrit.exceptions.EmailException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) AuthException(com.google.gerrit.extensions.restapi.AuthException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) IOException(java.io.IOException)

Example 3 with AccountResource

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());
    }
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) OrmException(com.google.gwtorm.server.OrmException) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) ByteArrayInputStream(java.io.ByteArrayInputStream) GpgKeyInfo(com.google.gerrit.extensions.common.GpgKeyInfo) Map(java.util.Map) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) PGPException(org.bouncycastle.openpgp.PGPException) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) RefUpdate(org.eclipse.jgit.lib.RefUpdate) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) SCHEME_GPGKEY(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GPGKEY) Sets(com.google.common.collect.Sets) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) ExternalIdsUpdate(com.google.gerrit.server.account.externalids.ExternalIdsUpdate) Joiner(com.google.common.base.Joiner) Singleton(com.google.inject.Singleton) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) InternalAccountQuery(com.google.gerrit.server.query.account.InternalAccountQuery) GerritPublicKeyChecker(com.google.gerrit.gpg.GerritPublicKeyChecker) Fingerprint(com.google.gerrit.gpg.Fingerprint) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Account(com.google.gerrit.reviewdb.client.Account) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ArmoredInputStream(org.bouncycastle.bcpg.ArmoredInputStream) CheckResult(com.google.gerrit.gpg.CheckResult) Input(com.google.gerrit.gpg.server.PostGpgKeys.Input) CurrentUser(com.google.gerrit.server.CurrentUser) Logger(org.slf4j.Logger) BaseEncoding(com.google.common.io.BaseEncoding) UTF_8(java.nio.charset.StandardCharsets.UTF_8) AccountResource(com.google.gerrit.server.account.AccountResource) EmailException(com.google.gerrit.common.errors.EmailException) PublicKeyChecker(com.google.gerrit.gpg.PublicKeyChecker) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) PublicKeyStore.keyIdToString(com.google.gerrit.gpg.PublicKeyStore.keyIdToString) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) AddKeySender(com.google.gerrit.server.mail.send.AddKeySender) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) AccountState(com.google.gerrit.server.account.AccountState) BcPGPObjectFactory(org.bouncycastle.openpgp.bc.BcPGPObjectFactory) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) InputStream(java.io.InputStream) PGPPublicKeyRing(org.bouncycastle.openpgp.PGPPublicKeyRing) Account(com.google.gerrit.reviewdb.client.Account) Fingerprint(com.google.gerrit.gpg.Fingerprint) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) PublicKeyStore(com.google.gerrit.gpg.PublicKeyStore) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey)

Example 4 with AccountResource

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());
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) CurrentUser(com.google.gerrit.server.CurrentUser) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Inject(com.google.inject.Inject) AccountResource(com.google.gerrit.server.account.AccountResource) IOException(java.io.IOException) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Strings(com.google.common.base.Strings) Provider(com.google.inject.Provider) Nullable(com.google.gerrit.common.Nullable) DisplayNameInput(com.google.gerrit.extensions.api.accounts.DisplayNameInput) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ServerInitiated(com.google.gerrit.server.ServerInitiated) AuthException(com.google.gerrit.extensions.restapi.AuthException) AccountState(com.google.gerrit.server.account.AccountState) AccountsUpdate(com.google.gerrit.server.account.AccountsUpdate) Singleton(com.google.inject.Singleton) DisplayNameInput(com.google.gerrit.extensions.api.accounts.DisplayNameInput) AccountState(com.google.gerrit.server.account.AccountState) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 5 with AccountResource

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()));
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Inject(com.google.inject.Inject) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) Accounts(com.google.gerrit.server.account.Accounts) Strings(com.google.common.base.Strings) AuthException(com.google.gerrit.extensions.restapi.AuthException) Comparator.comparing(java.util.Comparator.comparing) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) ImmutableSet(com.google.common.collect.ImmutableSet) RestReadView(com.google.gerrit.extensions.restapi.RestReadView) AccountResource(com.google.gerrit.server.account.AccountResource) Account(com.google.gerrit.entities.Account) IOException(java.io.IOException) ProjectWatchKey(com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey) Collectors.toList(java.util.stream.Collectors.toList) Provider(com.google.inject.Provider) List(java.util.List) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) NotifyType(com.google.gerrit.entities.NotifyConfig.NotifyType) ProjectWatchInfo(com.google.gerrit.extensions.client.ProjectWatchInfo) AccountState(com.google.gerrit.server.account.AccountState) Singleton(com.google.inject.Singleton) Account(com.google.gerrit.entities.Account) ProjectWatchInfo(com.google.gerrit.extensions.client.ProjectWatchInfo) AccountState(com.google.gerrit.server.account.AccountState) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Aggregations

AccountResource (com.google.gerrit.server.account.AccountResource)5 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)4 IOException (java.io.IOException)4 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)4 AuthException (com.google.gerrit.extensions.restapi.AuthException)3 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)3 AccountState (com.google.gerrit.server.account.AccountState)3 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)3 Inject (com.google.inject.Inject)3 Provider (com.google.inject.Provider)3 Singleton (com.google.inject.Singleton)3 Strings (com.google.common.base.Strings)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Response (com.google.gerrit.extensions.restapi.Response)2 RestModifyView (com.google.gerrit.extensions.restapi.RestModifyView)2 CurrentUser (com.google.gerrit.server.CurrentUser)2 GlobalPermission (com.google.gerrit.server.permissions.GlobalPermission)2 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2