Search in sources :

Example 1 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.

the class CheckAccess method apply.

@Override
public AccessCheckInfo apply(ConfigResource unused, AccessCheckInput input) throws OrmException, PermissionBackendException, RestApiException, IOException {
    permissionBackend.user(currentUser.get()).check(GlobalPermission.ADMINISTRATE_SERVER);
    if (input == null) {
        throw new BadRequestException("input is required");
    }
    if (Strings.isNullOrEmpty(input.account)) {
        throw new BadRequestException("input requires 'account'");
    }
    if (Strings.isNullOrEmpty(input.project)) {
        throw new BadRequestException("input requires 'project'");
    }
    Account match = accountResolver.find(db.get(), input.account);
    if (match == null) {
        throw new BadRequestException(String.format("cannot find account %s", input.account));
    }
    AccessCheckInfo info = new AccessCheckInfo();
    Project.NameKey key = new Project.NameKey(input.project);
    if (projectCache.get(key) == null) {
        info.message = String.format("project %s does not exist", key);
        info.status = HttpServletResponse.SC_NOT_FOUND;
        return info;
    }
    IdentifiedUser user = userFactory.create(match.getId());
    try {
        permissionBackend.user(user).project(key).check(ProjectPermission.ACCESS);
    } catch (AuthException | PermissionBackendException e) {
        info.message = String.format("user %s (%s) cannot see project %s", user.getNameEmail(), user.getAccount().getId(), key);
        info.status = HttpServletResponse.SC_FORBIDDEN;
        return info;
    }
    if (!Strings.isNullOrEmpty(input.ref)) {
        try {
            permissionBackend.user(user).ref(new Branch.NameKey(key, input.ref)).check(RefPermission.READ);
        } catch (AuthException | PermissionBackendException e) {
            info.status = HttpServletResponse.SC_FORBIDDEN;
            info.message = String.format("user %s (%s) cannot see ref %s in project %s", user.getNameEmail(), user.getAccount().getId(), input.ref, key);
            return info;
        }
    }
    info.status = HttpServletResponse.SC_OK;
    return info;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Project(com.google.gerrit.reviewdb.client.Project) AccessCheckInfo(com.google.gerrit.extensions.api.config.AccessCheckInfo) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 2 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.

the class CheckConsistency method apply.

@Override
public ConsistencyCheckInfo apply(ConfigResource resource, ConsistencyCheckInput input) throws RestApiException, IOException {
    IdentifiedUser user = userProvider.get();
    if (!user.isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    if (!user.getCapabilities().canAccessDatabase()) {
        throw new AuthException("not allowed to run consistency checks");
    }
    if (input == null || input.checkAccountExternalIds == null) {
        throw new BadRequestException("input required");
    }
    ConsistencyCheckInfo consistencyCheckInfo = new ConsistencyCheckInfo();
    if (input.checkAccountExternalIds != null) {
        consistencyCheckInfo.checkAccountExternalIdsResult = new CheckAccountExternalIdsResultInfo(externalIdsConsistencyChecker.check());
    }
    return consistencyCheckInfo;
}
Also used : ConsistencyCheckInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) CheckAccountExternalIdsResultInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.CheckAccountExternalIdsResultInfo)

Example 3 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.

the class TestSubmitType method apply.

@Override
public SubmitType apply(RevisionResource rsrc, TestSubmitRuleInput input) throws AuthException, BadRequestException, OrmException {
    if (input == null) {
        input = new TestSubmitRuleInput();
    }
    if (input.rule != null && !rules.isProjectRulesEnabled()) {
        throw new AuthException("project rules are disabled");
    }
    input.filters = MoreObjects.firstNonNull(input.filters, filters);
    SubmitRuleEvaluator evaluator = new SubmitRuleEvaluator(changeDataFactory.create(db.get(), rsrc.getControl()));
    SubmitTypeRecord rec = evaluator.setPatchSet(rsrc.getPatchSet()).setLogErrors(false).setSkipSubmitFilters(input.filters == Filters.SKIP).setRule(input.rule).getSubmitType();
    if (rec.status != SubmitTypeRecord.Status.OK) {
        throw new BadRequestException(String.format("rule %s produced invalid result: %s", evaluator.getSubmitRuleName(), rec));
    }
    return rec.type;
}
Also used : SubmitRuleEvaluator(com.google.gerrit.server.project.SubmitRuleEvaluator) TestSubmitRuleInput(com.google.gerrit.extensions.common.TestSubmitRuleInput) SubmitTypeRecord(com.google.gerrit.common.data.SubmitTypeRecord) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Example 4 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.

the class SetEditPreferences method apply.

@Override
public EditPreferencesInfo apply(AccountResource rsrc, EditPreferencesInfo in) throws AuthException, BadRequestException, RepositoryNotFoundException, IOException, ConfigInvalidException, PermissionBackendException {
    if (self.get() != rsrc.getUser()) {
        permissionBackend.user(self).check(GlobalPermission.MODIFY_ACCOUNT);
    }
    if (in == null) {
        throw new BadRequestException("input must be provided");
    }
    Account.Id accountId = rsrc.getUser().getAccountId();
    VersionedAccountPreferences prefs;
    EditPreferencesInfo out = new EditPreferencesInfo();
    try (MetaDataUpdate md = metaDataUpdateFactory.get().create(allUsersName)) {
        prefs = VersionedAccountPreferences.forUser(accountId);
        prefs.load(md);
        storeSection(prefs.getConfig(), UserConfigSections.EDIT, null, readFromGit(accountId, gitMgr, allUsersName, in), EditPreferencesInfo.defaults());
        prefs.commit(md);
        out = loadSection(prefs.getConfig(), UserConfigSections.EDIT, null, out, EditPreferencesInfo.defaults(), null);
    }
    return out;
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) EditPreferencesInfo(com.google.gerrit.extensions.client.EditPreferencesInfo) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 5 with BadRequestException

use of com.google.gerrit.extensions.restapi.BadRequestException in project gerrit by GerritCodeReview.

the class DeleteExternalIds method apply.

@Override
public Response<?> apply(AccountResource resource, List<String> extIds) throws RestApiException, IOException, OrmException, ConfigInvalidException {
    if (self.get() != resource.getUser() && !self.get().getCapabilities().canAccessDatabase()) {
        throw new AuthException("not allowed to delete external IDs");
    }
    if (extIds == null || extIds.size() == 0) {
        throw new BadRequestException("external IDs are required");
    }
    Map<ExternalId.Key, ExternalId> externalIdMap = externalIds.byAccount(resource.getUser().getAccountId()).stream().collect(toMap(i -> i.key(), i -> i));
    List<ExternalId> toDelete = new ArrayList<>();
    ExternalId.Key last = resource.getUser().getLastLoginExternalIdKey();
    for (String externalIdStr : extIds) {
        ExternalId id = externalIdMap.get(ExternalId.Key.parse(externalIdStr));
        if (id == null) {
            throw new UnprocessableEntityException(String.format("External id %s does not exist", externalIdStr));
        }
        if ((!id.isScheme(SCHEME_USERNAME)) && ((last == null) || (!last.get().equals(id.key().get())))) {
            toDelete.add(id);
        } else {
            throw new ResourceConflictException(String.format("External id %s cannot be deleted", externalIdStr));
        }
    }
    try {
        for (ExternalId extId : toDelete) {
            AuthRequest authRequest = new AuthRequest(extId.key());
            authRequest.setEmailAddress(extId.email());
            accountManager.unlink(extId.accountId(), authRequest);
        }
    } catch (AccountException e) {
        throw new ResourceConflictException(e.getMessage());
    }
    return Response.none();
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Inject(com.google.inject.Inject) IOException(java.io.IOException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Response(com.google.gerrit.extensions.restapi.Response) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) ArrayList(java.util.ArrayList) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Provider(com.google.inject.Provider) List(java.util.List) Collectors.toMap(java.util.stream.Collectors.toMap) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException)

Aggregations

BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)310 Test (org.junit.Test)154 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)146 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)56 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)51 AuthException (com.google.gerrit.extensions.restapi.AuthException)46 Repository (org.eclipse.jgit.lib.Repository)30 IdString (com.google.gerrit.extensions.restapi.IdString)29 UnprocessableEntityException (com.google.gerrit.extensions.restapi.UnprocessableEntityException)29 LabelDefinitionInput (com.google.gerrit.extensions.common.LabelDefinitionInput)28 ArrayList (java.util.ArrayList)28 RevCommit (org.eclipse.jgit.revwalk.RevCommit)28 MethodNotAllowedException (com.google.gerrit.extensions.restapi.MethodNotAllowedException)27 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)27 IOException (java.io.IOException)25 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)24 RevWalk (org.eclipse.jgit.revwalk.RevWalk)22 ObjectId (org.eclipse.jgit.lib.ObjectId)20 Map (java.util.Map)19 Change (com.google.gerrit.entities.Change)18