Line data Source code
1 : // Copyright (C) 2017 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.account; 16 : 17 : import com.google.gerrit.entities.Account; 18 : import com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo; 19 : import com.google.inject.Inject; 20 : import com.google.inject.Singleton; 21 : import java.io.IOException; 22 : import java.util.ArrayList; 23 : import java.util.List; 24 : 25 : @Singleton 26 : public class AccountsConsistencyChecker { 27 : private final Accounts accounts; 28 : 29 : @Inject 30 138 : AccountsConsistencyChecker(Accounts accounts) { 31 138 : this.accounts = accounts; 32 138 : } 33 : 34 : public List<ConsistencyProblemInfo> check() throws IOException { 35 1 : List<ConsistencyProblemInfo> problems = new ArrayList<>(); 36 : 37 1 : for (AccountState accountState : accounts.all()) { 38 1 : Account account = accountState.account(); 39 1 : if (account.preferredEmail() != null) { 40 1 : if (accountState.externalIds().stream() 41 1 : .noneMatch(e -> account.preferredEmail().equals(e.email()))) { 42 1 : addError( 43 1 : String.format( 44 : "Account '%s' has no external ID for its preferred email '%s'", 45 1 : account.id().get(), account.preferredEmail()), 46 : problems); 47 : } 48 : } 49 1 : } 50 : 51 1 : return problems; 52 : } 53 : 54 : private static void addError(String error, List<ConsistencyProblemInfo> problems) { 55 1 : problems.add(new ConsistencyProblemInfo(ConsistencyProblemInfo.Status.ERROR, error)); 56 1 : } 57 : }