Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: components/user_manager/known_user.cc

Issue 2529103002: Add account_type into AccountId (Closed)
Patch Set: Fix MultiUserWindowManagerChromeOSTest.* Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/known_user.h" 5 #include "components/user_manager/known_user.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 14 matching lines...) Expand all
25 const char kKnownUsers[] = "KnownUsers"; 25 const char kKnownUsers[] = "KnownUsers";
26 26
27 // Known user preferences keys (stored in Local State). 27 // Known user preferences keys (stored in Local State).
28 28
29 // Key of canonical e-mail value. 29 // Key of canonical e-mail value.
30 const char kCanonicalEmail[] = "email"; 30 const char kCanonicalEmail[] = "email";
31 31
32 // Key of obfuscated GAIA id value. 32 // Key of obfuscated GAIA id value.
33 const char kGAIAIdKey[] = "gaia_id"; 33 const char kGAIAIdKey[] = "gaia_id";
34 34
35 // Key of account type.
36 const char kAccountTypeKey[] = "account_type";
37
35 // Key of whether this user ID refers to a SAML user. 38 // Key of whether this user ID refers to a SAML user.
36 const char kUsingSAMLKey[] = "using_saml"; 39 const char kUsingSAMLKey[] = "using_saml";
37 40
38 // Key of Device Id. 41 // Key of Device Id.
39 const char kDeviceId[] = "device_id"; 42 const char kDeviceId[] = "device_id";
40 43
41 // Key of GAPS cookie. 44 // Key of GAPS cookie.
42 const char kGAPSCookie[] = "gaps_cookie"; 45 const char kGAPSCookie[] = "gaps_cookie";
43 46
44 // Key of the reason for re-auth. 47 // Key of the reason for re-auth.
45 const char kReauthReasonKey[] = "reauth_reason"; 48 const char kReauthReasonKey[] = "reauth_reason";
46 49
47 // Key for the GaiaId migration status. 50 // Key for the GaiaId migration status.
48 const char kGaiaIdMigration[] = "gaia_id_migration"; 51 const char kGaiaIdMigration[] = "gaia_id_migration";
49 52
50 PrefService* GetLocalState() { 53 PrefService* GetLocalState() {
51 if (!UserManager::IsInitialized()) 54 if (!UserManager::IsInitialized())
52 return nullptr; 55 return nullptr;
53 56
54 return UserManager::Get()->GetLocalState(); 57 return UserManager::Get()->GetLocalState();
55 } 58 }
56 59
57 // Checks if values in |dict| correspond with |account_id| identity. 60 // Checks if values in |dict| correspond with |account_id| identity.
58 bool UserMatches(const AccountId& account_id, 61 bool UserMatches(const AccountId& account_id,
59 const base::DictionaryValue& dict) { 62 const base::DictionaryValue& dict) {
60 std::string value; 63 std::string value;
64 AccountType account_type(AccountType::GOOGLE);
65 {
66 std::string account_type_string;
67 if (dict.GetString(kAccountTypeKey, &account_type_string))
68 account_type = AccountId::StringToAccountType(account_type_string);
69 }
61 70
62 // TODO(alemate): update code once user id is really a struct. 71 // TODO(alemate): update code once user id is really a struct.
63 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value); 72 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value);
64 if (has_gaia_id && account_id.GetGaiaId() == value) 73 if (has_gaia_id && account_id.GetGaiaId() == value) {
74 DCHECK(account_id.GetAccountType() == account_type);
65 return true; 75 return true;
76 }
66 77
67 bool has_email = dict.GetString(kCanonicalEmail, &value); 78 bool has_email = dict.GetString(kCanonicalEmail, &value);
68 if (has_email && account_id.GetUserEmail() == value) 79 if (has_email && account_id.GetUserEmail() == value)
69 return true; 80 return true;
70 81
71 return false; 82 return false;
72 } 83 }
73 84
74 // Fills relevant |dict| values based on |account_id|. 85 // Fills relevant |dict| values based on |account_id|.
75 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) { 86 void UpdateIdentity(const AccountId& account_id, base::DictionaryValue& dict) {
76 if (!account_id.GetUserEmail().empty()) 87 if (!account_id.GetUserEmail().empty())
77 dict.SetString(kCanonicalEmail, account_id.GetUserEmail()); 88 dict.SetString(kCanonicalEmail, account_id.GetUserEmail());
78 89
79 if (!account_id.GetGaiaId().empty()) 90 if (!account_id.GetGaiaId().empty()) {
80 dict.SetString(kGAIAIdKey, account_id.GetGaiaId()); 91 dict.SetString(kGAIAIdKey, account_id.GetGaiaId());
92 dict.SetString(kAccountTypeKey,
93 AccountId::AccountTypeToString(account_id.GetAccountType()));
94 }
81 } 95 }
82 96
83 } // namespace 97 } // namespace
84 98
85 bool FindPrefs(const AccountId& account_id, 99 bool FindPrefs(const AccountId& account_id,
86 const base::DictionaryValue** out_value) { 100 const base::DictionaryValue** out_value) {
87 PrefService* local_state = GetLocalState(); 101 PrefService* local_state = GetLocalState();
88 102
89 // Local State may not be initialized in tests. 103 // Local State may not be initialized in tests.
90 if (!local_state) 104 if (!local_state)
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 if (!local_state) 223 if (!local_state)
210 return; 224 return;
211 225
212 ListPrefUpdate update(local_state, kKnownUsers); 226 ListPrefUpdate update(local_state, kKnownUsers);
213 base::DictionaryValue dict; 227 base::DictionaryValue dict;
214 dict.SetInteger(path, in_value); 228 dict.SetInteger(path, in_value);
215 UpdatePrefs(account_id, dict, false); 229 UpdatePrefs(account_id, dict, false);
216 } 230 }
217 231
218 AccountId GetAccountId(const std::string& user_email, 232 AccountId GetAccountId(const std::string& user_email,
219 const std::string& gaia_id) { 233 const std::string& gaia_id,
234 const AccountType& account_type) {
235 // Account type shouldn't be passed w/o gaia_id.
236 DCHECK(account_type == AccountType::UNKNOWN || !gaia_id.empty());
220 // In tests empty accounts are possible. 237 // In tests empty accounts are possible.
221 if (user_email.empty() && gaia_id.empty()) 238 if (user_email.empty() && gaia_id.empty())
222 return EmptyAccountId(); 239 return EmptyAccountId();
223 240
224 AccountId result(EmptyAccountId()); 241 AccountId result(EmptyAccountId());
225 // UserManager is usually NULL in unit tests. 242 // UserManager is usually NULL in unit tests.
226 if (UserManager::IsInitialized() && 243 if (UserManager::IsInitialized() &&
227 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id, 244 UserManager::Get()->GetPlatformKnownUserId(user_email, gaia_id,
228 &result)) { 245 &result)) {
229 return result; 246 return result;
230 } 247 }
231 248
232 // We can have several users with the same gaia_id but different e-mails. 249 // We can have several users with the same gaia_id but different e-mails.
233 // The opposite case is not possible. 250 // The opposite case is not possible.
234 std::string stored_gaia_id; 251 std::string stored_gaia_id;
235 const std::string sanitized_email = 252 const std::string sanitized_email =
236 user_email.empty() 253 user_email.empty()
237 ? std::string() 254 ? std::string()
238 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email)); 255 : gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_email));
256 std::string stored_account_type_string;
257 const bool has_stored_account_type =
258 GetStringPref(AccountId::FromUserEmail(sanitized_email), kAccountTypeKey,
259 &stored_account_type_string);
260 const AccountType stored_account_type =
261 has_stored_account_type
262 ? AccountId::StringToAccountType(stored_account_type_string)
263 : AccountType::UNKNOWN;
264
265 if (has_stored_account_type && account_type != AccountType::UNKNOWN &&
266 account_type != stored_account_type) {
267 LOG(FATAL) << "Account type has changed";
268 }
239 269
240 if (!sanitized_email.empty() && 270 if (!sanitized_email.empty() &&
241 GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey, 271 GetStringPref(AccountId::FromUserEmail(sanitized_email), kGAIAIdKey,
242 &stored_gaia_id)) { 272 &stored_gaia_id)) {
243 if (!gaia_id.empty() && gaia_id != stored_gaia_id) 273 if (!gaia_id.empty() && gaia_id != stored_gaia_id)
244 LOG(ERROR) << "User gaia id has changed. Sync will not work."; 274 LOG(ERROR) << "User gaia id has changed. Sync will not work.";
245 275
246 // gaia_id is associated with cryptohome. 276 return has_stored_account_type
247 return AccountId::FromUserEmailGaiaId(sanitized_email, stored_gaia_id); 277 ? AccountId::FromUserEmailGaiaIdAccountType(
278 sanitized_email, stored_gaia_id, stored_account_type)
279 : AccountId::FromUserEmailGaiaId(sanitized_email,
280 stored_gaia_id);
248 } 281 }
282 DCHECK(!has_stored_account_type);
249 283
250 std::string stored_email; 284 std::string stored_email;
251 // GetStringPref() returns the first user record that matches 285 // GetStringPref() returns the first user record that matches
252 // given ID. So we will get the first one if there are multiples. 286 // given ID. So we will get the first one if there are multiples.
253 if (!gaia_id.empty() && GetStringPref(AccountId::FromGaiaId(gaia_id), 287 if (!gaia_id.empty()) {
254 kCanonicalEmail, &stored_email)) { 288 const AccountId account_id(
255 return AccountId::FromUserEmailGaiaId(stored_email, gaia_id); 289 account_type == AccountType::UNKNOWN
290 ? AccountId::FromGaiaId(gaia_id)
291 : AccountId::FromGaiaIdAccountType(gaia_id, account_type));
292 if (GetStringPref(account_id, kCanonicalEmail, &stored_email)) {
293 return AccountId::FromUserEmailGaiaIdAccountType(
294 stored_email, gaia_id, account_id.GetAccountType());
295 }
256 } 296 }
257 297
258 return (gaia_id.empty() 298 if (account_type == AccountType::UNKNOWN) {
259 ? AccountId::FromUserEmail(user_email) 299 return gaia_id.empty()
260 : AccountId::FromUserEmailGaiaId(user_email, gaia_id)); 300 ? AccountId::FromUserEmail(user_email)
301 : AccountId::FromUserEmailGaiaId(user_email, gaia_id);
302 }
303 return AccountId::FromUserEmailGaiaIdAccountType(user_email, gaia_id,
304 account_type);
261 } 305 }
262 306
263 std::vector<AccountId> GetKnownAccountIds() { 307 std::vector<AccountId> GetKnownAccountIds() {
264 std::vector<AccountId> result; 308 std::vector<AccountId> result;
265 PrefService* local_state = GetLocalState(); 309 PrefService* local_state = GetLocalState();
266 310
267 // Local State may not be initialized in tests. 311 // Local State may not be initialized in tests.
268 if (!local_state) 312 if (!local_state)
269 return result; 313 return result;
270 314
271 const base::ListValue* known_users = local_state->GetList(kKnownUsers); 315 const base::ListValue* known_users = local_state->GetList(kKnownUsers);
272 for (size_t i = 0; i < known_users->GetSize(); ++i) { 316 for (size_t i = 0; i < known_users->GetSize(); ++i) {
273 const base::DictionaryValue* element = nullptr; 317 const base::DictionaryValue* element = nullptr;
274 if (known_users->GetDictionary(i, &element)) { 318 if (known_users->GetDictionary(i, &element)) {
275 std::string email; 319 std::string email;
276 std::string gaia_id; 320 std::string gaia_id;
321 AccountType account_type = AccountType::GOOGLE;
277 const bool has_email = element->GetString(kCanonicalEmail, &email); 322 const bool has_email = element->GetString(kCanonicalEmail, &email);
278 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id); 323 const bool has_gaia_id = element->GetString(kGAIAIdKey, &gaia_id);
279 if (has_email || has_gaia_id) 324 std::string account_type_string;
280 result.push_back(AccountId::FromUserEmailGaiaId(email, gaia_id)); 325 if (element->GetString(kAccountTypeKey, &account_type_string)) {
326 account_type = AccountId::StringToAccountType(account_type_string);
327 }
328 if (has_email || has_gaia_id) {
329 result.push_back(AccountId::FromUserEmailGaiaIdAccountType(
330 email, gaia_id, account_type));
331 }
281 } 332 }
282 } 333 }
283 return result; 334 return result;
284 } 335 }
285 336
286 bool GetGaiaIdMigrationStatus(const AccountId& account_id, 337 bool GetGaiaIdMigrationStatus(const AccountId& account_id,
287 const std::string& subsystem) { 338 const std::string& subsystem) {
288 bool migrated = false; 339 bool migrated = false;
289 340
290 if (GetBooleanPref(account_id, 341 if (GetBooleanPref(account_id,
291 std::string(kGaiaIdMigration) + "." + subsystem, 342 std::string(kGaiaIdMigration) + "." + subsystem,
292 &migrated)) { 343 &migrated)) {
293 return migrated; 344 return migrated;
294 } 345 }
295 346
296 return false; 347 return false;
297 } 348 }
298 349
299 void SetGaiaIdMigrationStatusDone(const AccountId& account_id, 350 void SetGaiaIdMigrationStatusDone(const AccountId& account_id,
300 const std::string& subsystem) { 351 const std::string& subsystem) {
301 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem, 352 SetBooleanPref(account_id, std::string(kGaiaIdMigration) + "." + subsystem,
302 true); 353 true);
303 } 354 }
304 355
305 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) { 356 void UpdateGaiaID(const AccountId& account_id, const std::string& gaia_id) {
306 SetStringPref(account_id, kGAIAIdKey, gaia_id); 357 SetStringPref(account_id, kGAIAIdKey, gaia_id);
307 } 358 }
308 359
360 void UpdateAccountType(const AccountId& account_id) {
361 SetStringPref(account_id, kAccountTypeKey,
362 AccountId::AccountTypeToString(account_id.GetAccountType()));
363 }
364
309 bool FindGaiaID(const AccountId& account_id, std::string* out_value) { 365 bool FindGaiaID(const AccountId& account_id, std::string* out_value) {
310 return GetStringPref(account_id, kGAIAIdKey, out_value); 366 return GetStringPref(account_id, kGAIAIdKey, out_value);
311 } 367 }
312 368
313 void SetDeviceId(const AccountId& account_id, const std::string& device_id) { 369 void SetDeviceId(const AccountId& account_id, const std::string& device_id) {
314 const std::string known_device_id = GetDeviceId(account_id); 370 const std::string known_device_id = GetDeviceId(account_id);
315 if (!known_device_id.empty() && device_id != known_device_id) { 371 if (!known_device_id.empty() && device_id != known_device_id) {
316 NOTREACHED() << "Trying to change device ID for known user."; 372 NOTREACHED() << "Trying to change device ID for known user.";
317 } 373 }
318 SetStringPref(account_id, kDeviceId, device_id); 374 SetStringPref(account_id, kDeviceId, device_id);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 } 432 }
377 } 433 }
378 } 434 }
379 435
380 void RegisterPrefs(PrefRegistrySimple* registry) { 436 void RegisterPrefs(PrefRegistrySimple* registry) {
381 registry->RegisterListPref(kKnownUsers); 437 registry->RegisterListPref(kKnownUsers);
382 } 438 }
383 439
384 } // namespace known_user 440 } // namespace known_user
385 } // namespace user_manager 441 } // namespace user_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698