Line data Source code
1 : // Copyright (C) 2016 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.query.account; 16 : 17 : import com.google.common.collect.Lists; 18 : import com.google.common.primitives.Ints; 19 : import com.google.gerrit.entities.Account; 20 : import com.google.gerrit.entities.Project; 21 : import com.google.gerrit.index.Schema; 22 : import com.google.gerrit.index.SchemaFieldDefs.SchemaField; 23 : import com.google.gerrit.index.query.IndexPredicate; 24 : import com.google.gerrit.index.query.Predicate; 25 : import com.google.gerrit.index.query.QueryBuilder; 26 : import com.google.gerrit.server.account.AccountState; 27 : import com.google.gerrit.server.index.account.AccountField; 28 : import com.google.gerrit.server.notedb.ChangeNotes; 29 : import java.util.List; 30 : 31 : /** Utility class to create predicates for account index queries. */ 32 : public class AccountPredicates { 33 : public static boolean hasActive(Predicate<AccountState> p) { 34 6 : return QueryBuilder.find(p, AccountPredicate.class, AccountField.ACTIVE_FIELD_SPEC.getName()) 35 : != null; 36 : } 37 : 38 : public static Predicate<AccountState> andActive(Predicate<AccountState> p) { 39 5 : return Predicate.and(p, isActive()); 40 : } 41 : 42 : public static Predicate<AccountState> defaultPredicate( 43 : Schema<AccountState> schema, boolean canSeeSecondaryEmails, String query) { 44 : // Adapt the capacity of this list when adding more default predicates. 45 33 : List<Predicate<AccountState>> preds = Lists.newArrayListWithCapacity(3); 46 33 : Integer id = Ints.tryParse(query); 47 33 : if (id != null) { 48 3 : preds.add(id(schema, Account.id(id))); 49 : } 50 33 : if (canSeeSecondaryEmails) { 51 32 : preds.add(equalsNameIncludingSecondaryEmails(query)); 52 : } else { 53 12 : if (schema.hasField(AccountField.NAME_PART_NO_SECONDARY_EMAIL_SPEC)) { 54 12 : preds.add(equalsName(query)); 55 : } else { 56 0 : preds.add(AccountPredicates.fullName(query)); 57 0 : if (schema.hasField(AccountField.PREFERRED_EMAIL_LOWER_CASE_SPEC)) { 58 0 : preds.add(AccountPredicates.preferredEmail(query)); 59 : } 60 : } 61 : } 62 33 : preds.add(username(query)); 63 : // Adapt the capacity of the "predicates" list when adding more default 64 : // predicates. 65 33 : return Predicate.or(preds); 66 : } 67 : 68 : public static Predicate<AccountState> id(Schema<AccountState> schema, Account.Id accountId) { 69 3 : return new AccountPredicate( 70 3 : schema.hasField(AccountField.ID_FIELD_SPEC) 71 0 : ? AccountField.ID_FIELD_SPEC 72 3 : : AccountField.ID_STR_FIELD_SPEC, 73 : AccountQueryBuilder.FIELD_ACCOUNT, 74 3 : accountId.toString()); 75 : } 76 : 77 : public static Predicate<AccountState> emailIncludingSecondaryEmails(String email) { 78 2 : return new AccountPredicate( 79 2 : AccountField.EMAIL_SPEC, AccountQueryBuilder.FIELD_EMAIL, email.toLowerCase()); 80 : } 81 : 82 : public static Predicate<AccountState> preferredEmail(String email) { 83 2 : return new AccountPredicate( 84 : AccountField.PREFERRED_EMAIL_LOWER_CASE_SPEC, 85 : AccountQueryBuilder.FIELD_PREFERRED_EMAIL, 86 2 : email.toLowerCase()); 87 : } 88 : 89 : public static Predicate<AccountState> preferredEmailExact(String email) { 90 36 : return new AccountPredicate( 91 : AccountField.PREFERRED_EMAIL_EXACT_SPEC, 92 : AccountQueryBuilder.FIELD_PREFERRED_EMAIL_EXACT, 93 : email); 94 : } 95 : 96 : public static Predicate<AccountState> equalsNameIncludingSecondaryEmails(String name) { 97 32 : return new AccountPredicate( 98 32 : AccountField.NAME_PART_SPEC, AccountQueryBuilder.FIELD_NAME, name.toLowerCase()); 99 : } 100 : 101 : public static Predicate<AccountState> equalsName(String name) { 102 12 : return new AccountPredicate( 103 : AccountField.NAME_PART_NO_SECONDARY_EMAIL_SPEC, 104 : AccountQueryBuilder.FIELD_NAME, 105 12 : name.toLowerCase()); 106 : } 107 : 108 : public static Predicate<AccountState> externalIdIncludingSecondaryEmails(String externalId) { 109 3 : return new AccountPredicate(AccountField.EXTERNAL_ID_FIELD_SPEC, externalId); 110 : } 111 : 112 : public static Predicate<AccountState> fullName(String fullName) { 113 33 : return new AccountPredicate(AccountField.FULL_NAME_SPEC, fullName); 114 : } 115 : 116 : public static Predicate<AccountState> isActive() { 117 7 : return new AccountPredicate(AccountField.ACTIVE_FIELD_SPEC, "1"); 118 : } 119 : 120 : public static Predicate<AccountState> isNotActive() { 121 2 : return new AccountPredicate(AccountField.ACTIVE_FIELD_SPEC, "0"); 122 : } 123 : 124 : public static Predicate<AccountState> username(String username) { 125 33 : return new AccountPredicate( 126 33 : AccountField.USERNAME_SPEC, AccountQueryBuilder.FIELD_USERNAME, username.toLowerCase()); 127 : } 128 : 129 : public static Predicate<AccountState> watchedProject(Project.NameKey project) { 130 103 : return new AccountPredicate(AccountField.WATCHED_PROJECT_SPEC, project.get()); 131 : } 132 : 133 : public static Predicate<AccountState> cansee( 134 : AccountQueryBuilder.Arguments args, ChangeNotes changeNotes) { 135 2 : return new CanSeeChangePredicate(args.permissionBackend, changeNotes); 136 : } 137 : 138 : /** Predicate that is mapped to a field in the account index. */ 139 : static class AccountPredicate extends IndexPredicate<AccountState> { 140 : AccountPredicate(SchemaField<AccountState, ?> def, String value) { 141 109 : super(def, value); 142 109 : } 143 : 144 : AccountPredicate(SchemaField<AccountState, ?> def, String name, String value) { 145 53 : super(def, name, value); 146 53 : } 147 : } 148 : 149 : private AccountPredicates() {} 150 : }