All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gerrit.server.query.account.AccountQueryBuilder Maven / Gradle / Ivy

There is a newer version: 3.10.0-rc4
Show newest version
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.query.account;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.gerrit.common.errors.NotSignedInException;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.ChangeFinder;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.query.LimitPredicate;
import com.google.gerrit.server.query.Predicate;
import com.google.gerrit.server.query.QueryBuilder;
import com.google.gerrit.server.query.QueryParseException;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;

/** Parses a query string meant to be applied to account objects. */
public class AccountQueryBuilder extends QueryBuilder {
  public static final String FIELD_ACCOUNT = "account";
  public static final String FIELD_EMAIL = "email";
  public static final String FIELD_LIMIT = "limit";
  public static final String FIELD_NAME = "name";
  public static final String FIELD_USERNAME = "username";
  public static final String FIELD_VISIBLETO = "visibleto";

  private static final QueryBuilder.Definition mydef =
      new QueryBuilder.Definition<>(AccountQueryBuilder.class);

  public static class Arguments {
    final Provider db;
    final ChangeFinder changeFinder;
    final ChangeControl.GenericFactory changeControlFactory;
    final IdentifiedUser.GenericFactory userFactory;

    private final Provider self;

    @Inject
    public Arguments(
        Provider self,
        Provider db,
        ChangeFinder changeFinder,
        ChangeControl.GenericFactory changeControlFactory,
        IdentifiedUser.GenericFactory userFactory) {
      this.self = self;
      this.db = db;
      this.changeFinder = changeFinder;
      this.changeControlFactory = changeControlFactory;
      this.userFactory = userFactory;
    }

    IdentifiedUser getIdentifiedUser() throws QueryParseException {
      try {
        CurrentUser u = getUser();
        if (u.isIdentifiedUser()) {
          return u.asIdentifiedUser();
        }
        throw new QueryParseException(NotSignedInException.MESSAGE);
      } catch (ProvisionException e) {
        throw new QueryParseException(NotSignedInException.MESSAGE, e);
      }
    }

    CurrentUser getUser() throws QueryParseException {
      try {
        return self.get();
      } catch (ProvisionException e) {
        throw new QueryParseException(NotSignedInException.MESSAGE, e);
      }
    }
  }

  private final Arguments args;

  @Inject
  AccountQueryBuilder(Arguments args) {
    super(mydef);
    this.args = args;
  }

  @Operator
  public Predicate cansee(String change) throws QueryParseException, OrmException {
    ChangeControl changeControl = args.changeFinder.findOne(change, args.getUser());
    if (changeControl == null || !changeControl.isVisible(args.db.get())) {
      throw error(String.format("change %s not found", change));
    }

    return AccountPredicates.cansee(args, changeControl.getNotes());
  }

  @Operator
  public Predicate email(String email) {
    return AccountPredicates.email(email);
  }

  @Operator
  public Predicate is(String value) throws QueryParseException {
    if ("active".equalsIgnoreCase(value)) {
      return AccountPredicates.isActive();
    }
    if ("inactive".equalsIgnoreCase(value)) {
      return AccountPredicates.isInactive();
    }
    throw error("Invalid query");
  }

  @Operator
  public Predicate limit(String query) throws QueryParseException {
    Integer limit = Ints.tryParse(query);
    if (limit == null) {
      throw error("Invalid limit: " + query);
    }
    return new LimitPredicate<>(FIELD_LIMIT, limit);
  }

  @Operator
  public Predicate name(String name) {
    return AccountPredicates.equalsName(name);
  }

  @Operator
  public Predicate username(String username) {
    return AccountPredicates.username(username);
  }

  public Predicate defaultQuery(String query) {
    return Predicate.and(
        Lists.transform(
            Splitter.on(' ').omitEmptyStrings().splitToList(query), this::defaultField));
  }

  @Override
  protected Predicate defaultField(String query) {
    if (query.startsWith("cansee:")) {
      try {
        return cansee(query.substring(7));
      } catch (OrmException | QueryParseException e) {
        // Ignore, fall back to default query
      }
    }
    Predicate defaultPredicate = AccountPredicates.defaultPredicate(query);
    if ("self".equalsIgnoreCase(query)) {
      try {
        return Predicate.or(defaultPredicate, AccountPredicates.id(self()));
      } catch (QueryParseException e) {
        // Skip.
      }
    }
    return defaultPredicate;
  }

  private Account.Id self() throws QueryParseException {
    return args.getIdentifiedUser().getAccountId();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy