com.google.gerrit.server.query.account.AccountPredicates Maven / Gradle / Ivy
// 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.collect.Lists;
import com.google.common.primitives.Ints;
import com.google.gerrit.entities.Account;
import com.google.gerrit.entities.Project;
import com.google.gerrit.index.Schema;
import com.google.gerrit.index.SchemaFieldDefs.SchemaField;
import com.google.gerrit.index.query.IndexPredicate;
import com.google.gerrit.index.query.Predicate;
import com.google.gerrit.index.query.QueryBuilder;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.index.account.AccountField;
import com.google.gerrit.server.notedb.ChangeNotes;
import java.util.List;
/** Utility class to create predicates for account index queries. */
public class AccountPredicates {
public static boolean hasActive(Predicate p) {
return QueryBuilder.find(p, AccountPredicate.class, AccountField.ACTIVE_FIELD_SPEC.getName())
!= null;
}
public static Predicate andActive(Predicate p) {
return Predicate.and(p, isActive());
}
public static Predicate defaultPredicate(
Schema schema, boolean canSeeSecondaryEmails, String query) {
// Adapt the capacity of this list when adding more default predicates.
List> preds = Lists.newArrayListWithCapacity(3);
Integer id = Ints.tryParse(query);
if (id != null) {
preds.add(id(schema, Account.id(id)));
}
if (canSeeSecondaryEmails) {
preds.add(equalsNameIncludingSecondaryEmails(query));
} else {
if (schema.hasField(AccountField.NAME_PART_NO_SECONDARY_EMAIL_SPEC)) {
preds.add(equalsName(query));
} else {
preds.add(AccountPredicates.fullName(query));
if (schema.hasField(AccountField.PREFERRED_EMAIL_LOWER_CASE_SPEC)) {
preds.add(AccountPredicates.preferredEmail(query));
}
}
}
preds.add(username(query));
// Adapt the capacity of the "predicates" list when adding more default
// predicates.
return Predicate.or(preds);
}
public static Predicate id(Schema schema, Account.Id accountId) {
return new AccountPredicate(
schema.hasField(AccountField.ID_FIELD_SPEC)
? AccountField.ID_FIELD_SPEC
: AccountField.ID_STR_FIELD_SPEC,
AccountQueryBuilder.FIELD_ACCOUNT,
accountId.toString());
}
public static Predicate emailIncludingSecondaryEmails(String email) {
return new AccountPredicate(
AccountField.EMAIL_SPEC, AccountQueryBuilder.FIELD_EMAIL, email.toLowerCase());
}
public static Predicate preferredEmail(String email) {
return new AccountPredicate(
AccountField.PREFERRED_EMAIL_LOWER_CASE_SPEC,
AccountQueryBuilder.FIELD_PREFERRED_EMAIL,
email.toLowerCase());
}
public static Predicate preferredEmailExact(String email) {
return new AccountPredicate(
AccountField.PREFERRED_EMAIL_EXACT_SPEC,
AccountQueryBuilder.FIELD_PREFERRED_EMAIL_EXACT,
email);
}
public static Predicate equalsNameIncludingSecondaryEmails(String name) {
return new AccountPredicate(
AccountField.NAME_PART_SPEC, AccountQueryBuilder.FIELD_NAME, name.toLowerCase());
}
public static Predicate equalsName(String name) {
return new AccountPredicate(
AccountField.NAME_PART_NO_SECONDARY_EMAIL_SPEC,
AccountQueryBuilder.FIELD_NAME,
name.toLowerCase());
}
public static Predicate externalIdIncludingSecondaryEmails(String externalId) {
return new AccountPredicate(AccountField.EXTERNAL_ID_FIELD_SPEC, externalId);
}
public static Predicate fullName(String fullName) {
return new AccountPredicate(AccountField.FULL_NAME_SPEC, fullName);
}
public static Predicate isActive() {
return new AccountPredicate(AccountField.ACTIVE_FIELD_SPEC, "1");
}
public static Predicate isNotActive() {
return new AccountPredicate(AccountField.ACTIVE_FIELD_SPEC, "0");
}
public static Predicate username(String username) {
return new AccountPredicate(
AccountField.USERNAME_SPEC, AccountQueryBuilder.FIELD_USERNAME, username.toLowerCase());
}
public static Predicate watchedProject(Project.NameKey project) {
return new AccountPredicate(AccountField.WATCHED_PROJECT_SPEC, project.get());
}
public static Predicate cansee(
AccountQueryBuilder.Arguments args, ChangeNotes changeNotes) {
return new CanSeeChangePredicate(args.permissionBackend, changeNotes);
}
/** Predicate that is mapped to a field in the account index. */
static class AccountPredicate extends IndexPredicate {
AccountPredicate(SchemaField def, String value) {
super(def, value);
}
AccountPredicate(SchemaField def, String name, String value) {
super(def, name, value);
}
}
private AccountPredicates() {}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy