OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/user_manager/user_manager_base.h" | 5 #include "components/user_manager/user_manager_base.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 577 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 std::set<AccountId>* users_set) { | 588 std::set<AccountId>* users_set) { |
589 users_vector->clear(); | 589 users_vector->clear(); |
590 users_set->clear(); | 590 users_set->clear(); |
591 for (size_t i = 0; i < users_list.GetSize(); ++i) { | 591 for (size_t i = 0; i < users_list.GetSize(); ++i) { |
592 std::string email; | 592 std::string email; |
593 if (!users_list.GetString(i, &email) || email.empty()) { | 593 if (!users_list.GetString(i, &email) || email.empty()) { |
594 LOG(ERROR) << "Corrupt entry in user list at index " << i << "."; | 594 LOG(ERROR) << "Corrupt entry in user list at index " << i << "."; |
595 continue; | 595 continue; |
596 } | 596 } |
597 | 597 |
598 const AccountId partial_account_id = AccountId::FromUserEmail(email); | 598 const AccountId account_id = GetKnownUserAccountId(email, std::string()); |
599 AccountId account_id = EmptyAccountId(); | |
600 | |
601 const bool lookup_result = | |
602 GetKnownUserAccountId(partial_account_id, &account_id); | |
603 // TODO(alemate): | |
604 // DCHECK(lookup_result) << "KnownUser lookup falied for '" << email << "'"; | |
605 // (tests do not initialize KnownUserData) | |
606 | |
607 if (!lookup_result) { | |
608 account_id = partial_account_id; | |
609 LOG(WARNING) << "KnownUser lookup falied for '" << email << "'"; | |
610 } | |
611 | 599 |
612 if (existing_users.find(account_id) != existing_users.end() || | 600 if (existing_users.find(account_id) != existing_users.end() || |
613 !users_set->insert(account_id).second) { | 601 !users_set->insert(account_id).second) { |
614 LOG(ERROR) << "Duplicate user: " << email; | 602 LOG(ERROR) << "Duplicate user: " << email; |
615 continue; | 603 continue; |
616 } | 604 } |
617 users_vector->push_back(account_id); | 605 users_vector->push_back(account_id); |
618 } | 606 } |
619 } | 607 } |
620 | 608 |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1035 } | 1023 } |
1036 | 1024 |
1037 bool UserManagerBase::FindKnownUserPrefs( | 1025 bool UserManagerBase::FindKnownUserPrefs( |
1038 const AccountId& account_id, | 1026 const AccountId& account_id, |
1039 const base::DictionaryValue** out_value) { | 1027 const base::DictionaryValue** out_value) { |
1040 PrefService* local_state = GetLocalState(); | 1028 PrefService* local_state = GetLocalState(); |
1041 | 1029 |
1042 // Local State may not be initialized in tests. | 1030 // Local State may not be initialized in tests. |
1043 if (!local_state) | 1031 if (!local_state) |
1044 return false; | 1032 return false; |
| 1033 |
1045 if (IsUserNonCryptohomeDataEphemeral(account_id)) | 1034 if (IsUserNonCryptohomeDataEphemeral(account_id)) |
1046 return false; | 1035 return false; |
1047 | 1036 |
1048 const base::ListValue* known_users = local_state->GetList(kKnownUsers); | 1037 const base::ListValue* known_users = local_state->GetList(kKnownUsers); |
1049 for (size_t i = 0; i < known_users->GetSize(); ++i) { | 1038 for (size_t i = 0; i < known_users->GetSize(); ++i) { |
1050 const base::DictionaryValue* element = nullptr; | 1039 const base::DictionaryValue* element = nullptr; |
1051 if (known_users->GetDictionary(i, &element)) { | 1040 if (known_users->GetDictionary(i, &element)) { |
1052 if (UserMatches(account_id, *element)) { | 1041 if (UserMatches(account_id, *element)) { |
1053 known_users->GetDictionary(i, out_value); | 1042 known_users->GetDictionary(i, out_value); |
1054 return true; | 1043 return true; |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 // Local State may not be initialized in tests. | 1145 // Local State may not be initialized in tests. |
1157 if (!local_state) | 1146 if (!local_state) |
1158 return; | 1147 return; |
1159 | 1148 |
1160 ListPrefUpdate update(local_state, kKnownUsers); | 1149 ListPrefUpdate update(local_state, kKnownUsers); |
1161 base::DictionaryValue dict; | 1150 base::DictionaryValue dict; |
1162 dict.SetInteger(path, in_value); | 1151 dict.SetInteger(path, in_value); |
1163 UpdateKnownUserPrefs(account_id, dict, false); | 1152 UpdateKnownUserPrefs(account_id, dict, false); |
1164 } | 1153 } |
1165 | 1154 |
1166 bool UserManagerBase::GetKnownUserAccountId( | 1155 AccountId UserManagerBase::GetKnownUserAccountIdImpl( |
1167 const AccountId& authenticated_account_id, | 1156 const std::string& user_email, |
1168 AccountId* out_account_id) { | 1157 const std::string& gaia_id) { |
1169 if (!authenticated_account_id.GetGaiaId().empty()) { | 1158 DCHECK(!(user_email.empty() && gaia_id.empty())); |
1170 std::string canonical_email; | |
1171 if (!GetKnownUserStringPref( | |
1172 AccountId::FromGaiaId(authenticated_account_id.GetGaiaId()), | |
1173 kCanonicalEmail, &canonical_email)) { | |
1174 return false; | |
1175 } | |
1176 | 1159 |
1177 *out_account_id = AccountId::FromUserEmailGaiaId( | 1160 // We can have several users with the same gaia_id but different e-mails. |
1178 canonical_email, authenticated_account_id.GetGaiaId()); | 1161 // The opposite case is not possible. |
1179 return true; | 1162 std::string stored_gaia_id; |
| 1163 const std::string sanitized_email = |
| 1164 user_email.empty() |
| 1165 ? std::string() |
| 1166 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); |
| 1167 if (!sanitized_email.empty() && |
| 1168 GetKnownUserStringPref(AccountId::FromUserEmail(sanitized_email), |
| 1169 kGAIAIdKey, &stored_gaia_id)) { |
| 1170 if (!gaia_id.empty() && gaia_id != stored_gaia_id) |
| 1171 LOG(ERROR) << "User gaia id has changed. Sync will not work."; |
| 1172 |
| 1173 // gaia_id is associated with cryptohome. |
| 1174 return AccountId::FromUserEmailGaiaId(sanitized_email, stored_gaia_id); |
1180 } | 1175 } |
1181 DCHECK(!authenticated_account_id.GetUserEmail().empty()); | |
1182 std::string gaia_id; | |
1183 if (!GetKnownUserStringPref(authenticated_account_id, kGAIAIdKey, &gaia_id)) | |
1184 return false; | |
1185 | 1176 |
1186 *out_account_id = AccountId::FromUserEmailGaiaId( | 1177 std::string stored_email; |
1187 authenticated_account_id.GetUserEmail(), gaia_id); | 1178 // GetKnownUserStringPref() returns the first user record that matches |
1188 return true; | 1179 // given ID. So we will get the first one if there are multiples. |
| 1180 if (!gaia_id.empty() && |
| 1181 GetKnownUserStringPref(AccountId::FromGaiaId(gaia_id), kCanonicalEmail, |
| 1182 &stored_email)) { |
| 1183 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); |
| 1184 } |
| 1185 |
| 1186 return AccountId::FromUserEmailGaiaId(sanitized_email, gaia_id); |
1189 } | 1187 } |
1190 | 1188 |
1191 void UserManagerBase::UpdateGaiaID(const AccountId& account_id, | 1189 void UserManagerBase::UpdateGaiaID(const AccountId& account_id, |
1192 const std::string& gaia_id) { | 1190 const std::string& gaia_id) { |
1193 SetKnownUserStringPref(account_id, kGAIAIdKey, gaia_id); | 1191 SetKnownUserStringPref(account_id, kGAIAIdKey, gaia_id); |
1194 } | 1192 } |
1195 | 1193 |
1196 bool UserManagerBase::FindGaiaID(const AccountId& account_id, | 1194 bool UserManagerBase::FindGaiaID(const AccountId& account_id, |
1197 std::string* out_value) { | 1195 std::string* out_value) { |
1198 return GetKnownUserStringPref(account_id, kGAIAIdKey, out_value); | 1196 return GetKnownUserStringPref(account_id, kGAIAIdKey, out_value); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1391 } | 1389 } |
1392 | 1390 |
1393 void UserManagerBase::DeleteUser(User* user) { | 1391 void UserManagerBase::DeleteUser(User* user) { |
1394 const bool is_active_user = (user == active_user_); | 1392 const bool is_active_user = (user == active_user_); |
1395 delete user; | 1393 delete user; |
1396 if (is_active_user) | 1394 if (is_active_user) |
1397 active_user_ = nullptr; | 1395 active_user_ = nullptr; |
1398 } | 1396 } |
1399 | 1397 |
1400 } // namespace user_manager | 1398 } // namespace user_manager |
OLD | NEW |