| 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 "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" | 5 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/prefs/pref_change_registrar.h" | 9 #include "base/prefs/pref_change_registrar.h" |
| 10 #include "base/prefs/pref_registry_simple.h" | 10 #include "base/prefs/pref_registry_simple.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "components/user_manager/user.h" | 21 #include "components/user_manager/user.h" |
| 22 #include "google_apis/gaia/gaia_auth_util.h" | 22 #include "google_apis/gaia/gaia_auth_util.h" |
| 23 | 23 |
| 24 namespace chromeos { | 24 namespace chromeos { |
| 25 | 25 |
| 26 namespace { | 26 namespace { |
| 27 | 27 |
| 28 std::string SanitizeBehaviorValue(const std::string& value) { | 28 std::string SanitizeBehaviorValue(const std::string& value) { |
| 29 if (value == MultiProfileUserController::kBehaviorUnrestricted || | 29 if (value == MultiProfileUserController::kBehaviorUnrestricted || |
| 30 value == MultiProfileUserController::kBehaviorPrimaryOnly || | 30 value == MultiProfileUserController::kBehaviorPrimaryOnly || |
| 31 value == MultiProfileUserController::kBehaviorNotAllowed || | 31 value == MultiProfileUserController::kBehaviorNotAllowed) { |
| 32 value == MultiProfileUserController::kBehaviorOwnerPrimaryOnly) { | |
| 33 return value; | 32 return value; |
| 34 } | 33 } |
| 35 | 34 |
| 36 return std::string(MultiProfileUserController::kBehaviorUnrestricted); | 35 return std::string(MultiProfileUserController::kBehaviorUnrestricted); |
| 37 } | 36 } |
| 38 | 37 |
| 38 bool SetUserAllowedReason( |
| 39 MultiProfileUserController::UserAllowedInSessionReason* reason, |
| 40 MultiProfileUserController::UserAllowedInSessionReason value) { |
| 41 if (reason) |
| 42 *reason = value; |
| 43 return value == MultiProfileUserController::ALLOWED; |
| 44 } |
| 45 |
| 39 } // namespace | 46 } // namespace |
| 40 | 47 |
| 41 // static | 48 // static |
| 42 const char MultiProfileUserController::kBehaviorUnrestricted[] = "unrestricted"; | 49 const char MultiProfileUserController::kBehaviorUnrestricted[] = "unrestricted"; |
| 43 const char MultiProfileUserController::kBehaviorPrimaryOnly[] = "primary-only"; | 50 const char MultiProfileUserController::kBehaviorPrimaryOnly[] = "primary-only"; |
| 44 const char MultiProfileUserController::kBehaviorNotAllowed[] = "not-allowed"; | 51 const char MultiProfileUserController::kBehaviorNotAllowed[] = "not-allowed"; |
| 45 | 52 |
| 46 // Note: this policy value is not a real one an is only returned locally for | 53 // Note: this policy value is not a real one an is only returned locally for |
| 47 // owner users instead of default one kBehaviorUnrestricted. | 54 // owner users instead of default one kBehaviorUnrestricted. |
| 48 const char MultiProfileUserController::kBehaviorOwnerPrimaryOnly[] = | 55 const char MultiProfileUserController::kBehaviorOwnerPrimaryOnly[] = |
| (...skipping 24 matching lines...) Expand all Loading... |
| 73 registry->RegisterBooleanPref( | 80 registry->RegisterBooleanPref( |
| 74 prefs::kMultiProfileNeverShowIntro, | 81 prefs::kMultiProfileNeverShowIntro, |
| 75 false, | 82 false, |
| 76 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 83 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 77 registry->RegisterBooleanPref( | 84 registry->RegisterBooleanPref( |
| 78 prefs::kMultiProfileWarningShowDismissed, | 85 prefs::kMultiProfileWarningShowDismissed, |
| 79 false, | 86 false, |
| 80 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); | 87 user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); |
| 81 } | 88 } |
| 82 | 89 |
| 83 MultiProfileUserController::UserAllowedInSessionResult | 90 bool MultiProfileUserController::IsUserAllowedInSession( |
| 84 MultiProfileUserController::IsUserAllowedInSession( | 91 const std::string& user_email, |
| 85 const std::string& user_email) const { | 92 MultiProfileUserController::UserAllowedInSessionReason* reason) const { |
| 86 UserManager* user_manager = UserManager::Get(); | 93 UserManager* user_manager = UserManager::Get(); |
| 87 CHECK(user_manager); | 94 CHECK(user_manager); |
| 88 | 95 |
| 89 const user_manager::User* primary_user = user_manager->GetPrimaryUser(); | 96 const user_manager::User* primary_user = user_manager->GetPrimaryUser(); |
| 90 std::string primary_user_email; | 97 std::string primary_user_email; |
| 91 if (primary_user) | 98 if (primary_user) |
| 92 primary_user_email = primary_user->email(); | 99 primary_user_email = primary_user->email(); |
| 93 | 100 |
| 94 // Always allow if there is no primary user or user being checked is the | 101 // Always allow if there is no primary user or user being checked is the |
| 95 // primary user. | 102 // primary user. |
| 96 if (primary_user_email.empty() || primary_user_email == user_email) | 103 if (primary_user_email.empty() || primary_user_email == user_email) |
| 97 return ALLOWED; | 104 return SetUserAllowedReason(reason, ALLOWED); |
| 98 | 105 |
| 99 // Owner is not allowed to be secondary user. | 106 // Owner is not allowed to be secondary user. |
| 100 if (user_manager->GetOwnerEmail() == user_email) | 107 if (user_manager->GetOwnerEmail() == user_email) |
| 101 return NOT_ALLOWED_OWNER_AS_SECONDARY; | 108 return SetUserAllowedReason(reason, NOT_ALLOWED_OWNER_AS_SECONDARY); |
| 102 | 109 |
| 103 // Don't allow profiles potentially tainted by data fetched with policy-pushed | 110 // Don't allow profiles potentially tainted by data fetched with policy-pushed |
| 104 // certificates to join a multiprofile session. | 111 // certificates to join a multiprofile session. |
| 105 if (policy::PolicyCertServiceFactory::UsedPolicyCertificates(user_email)) | 112 if (policy::PolicyCertServiceFactory::UsedPolicyCertificates(user_email)) |
| 106 return NOT_ALLOWED_POLICY_CERT_TAINTED; | 113 return SetUserAllowedReason(reason, NOT_ALLOWED_POLICY_CERT_TAINTED); |
| 107 | 114 |
| 108 // Don't allow any secondary profiles if the primary profile is tainted. | 115 // Don't allow any secondary profiles if the primary profile is tainted. |
| 109 if (policy::PolicyCertServiceFactory::UsedPolicyCertificates( | 116 if (policy::PolicyCertServiceFactory::UsedPolicyCertificates( |
| 110 primary_user_email)) { | 117 primary_user_email)) { |
| 111 // Check directly in local_state before checking if the primary user has | 118 // Check directly in local_state before checking if the primary user has |
| 112 // a PolicyCertService. His profile may have been tainted previously though | 119 // a PolicyCertService. His profile may have been tainted previously though |
| 113 // he didn't get a PolicyCertService created for this session. | 120 // he didn't get a PolicyCertService created for this session. |
| 114 return NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED; | 121 return SetUserAllowedReason(reason, |
| 122 NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED); |
| 115 } | 123 } |
| 116 | 124 |
| 117 // If the primary profile already has policy certificates installed but hasn't | 125 // If the primary profile already has policy certificates installed but hasn't |
| 118 // used them yet then it can become tainted at any time during this session; | 126 // used them yet then it can become tainted at any time during this session; |
| 119 // disable secondary profiles in this case too. | 127 // disable secondary profiles in this case too. |
| 120 Profile* primary_user_profile = | 128 Profile* primary_user_profile = |
| 121 primary_user ? ProfileHelper::Get()->GetProfileByUser(primary_user) | 129 primary_user ? ProfileHelper::Get()->GetProfileByUser(primary_user) |
| 122 : NULL; | 130 : NULL; |
| 123 policy::PolicyCertService* service = | 131 policy::PolicyCertService* service = |
| 124 primary_user_profile ? policy::PolicyCertServiceFactory::GetForProfile( | 132 primary_user_profile ? policy::PolicyCertServiceFactory::GetForProfile( |
| 125 primary_user_profile) | 133 primary_user_profile) |
| 126 : NULL; | 134 : NULL; |
| 127 if (service && service->has_policy_certificates()) | 135 if (service && service->has_policy_certificates()) |
| 128 return NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED; | 136 return SetUserAllowedReason(reason, |
| 137 NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED); |
| 129 | 138 |
| 130 // No user is allowed if the primary user policy forbids it. | 139 // No user is allowed if the primary user policy forbids it. |
| 131 const std::string primary_user_behavior = | 140 const std::string primary_user_behavior = |
| 132 primary_user_profile->GetPrefs()->GetString( | 141 primary_user_profile->GetPrefs()->GetString( |
| 133 prefs::kMultiProfileUserBehavior); | 142 prefs::kMultiProfileUserBehavior); |
| 134 if (primary_user_behavior == kBehaviorNotAllowed) | 143 if (primary_user_behavior == kBehaviorNotAllowed) |
| 135 return NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS; | 144 return SetUserAllowedReason(reason, |
| 145 NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS); |
| 136 | 146 |
| 137 // The user must have 'unrestricted' policy to be a secondary user. | 147 // The user must have 'unrestricted' policy to be a secondary user. |
| 138 const std::string behavior = GetCachedValue(user_email); | 148 const std::string behavior = GetCachedValue(user_email); |
| 139 return behavior == kBehaviorUnrestricted ? ALLOWED : | 149 return SetUserAllowedReason( |
| 140 NOT_ALLOWED_POLICY_FORBIDS; | 150 reason, |
| 151 behavior == kBehaviorUnrestricted ? ALLOWED : NOT_ALLOWED_POLICY_FORBIDS); |
| 141 } | 152 } |
| 142 | 153 |
| 143 void MultiProfileUserController::StartObserving(Profile* user_profile) { | 154 void MultiProfileUserController::StartObserving(Profile* user_profile) { |
| 144 // Profile name could be empty during tests. | 155 // Profile name could be empty during tests. |
| 145 if (user_profile->GetProfileName().empty()) | 156 if (user_profile->GetProfileName().empty()) |
| 146 return; | 157 return; |
| 147 | 158 |
| 148 scoped_ptr<PrefChangeRegistrar> registrar(new PrefChangeRegistrar); | 159 scoped_ptr<PrefChangeRegistrar> registrar(new PrefChangeRegistrar); |
| 149 registrar->Init(user_profile->GetPrefs()); | 160 registrar->Init(user_profile->GetPrefs()); |
| 150 registrar->Add( | 161 registrar->Add( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 166 } | 177 } |
| 167 | 178 |
| 168 std::string MultiProfileUserController::GetCachedValue( | 179 std::string MultiProfileUserController::GetCachedValue( |
| 169 const std::string& user_email) const { | 180 const std::string& user_email) const { |
| 170 const base::DictionaryValue* dict = | 181 const base::DictionaryValue* dict = |
| 171 local_state_->GetDictionary(prefs::kCachedMultiProfileUserBehavior); | 182 local_state_->GetDictionary(prefs::kCachedMultiProfileUserBehavior); |
| 172 std::string value; | 183 std::string value; |
| 173 if (dict && dict->GetStringWithoutPathExpansion(user_email, &value)) | 184 if (dict && dict->GetStringWithoutPathExpansion(user_email, &value)) |
| 174 return SanitizeBehaviorValue(value); | 185 return SanitizeBehaviorValue(value); |
| 175 | 186 |
| 176 // Owner is not allowed to be secondary user (see http://crbug.com/385034). | |
| 177 if (UserManager::Get()->GetOwnerEmail() == user_email) | |
| 178 return std::string(kBehaviorOwnerPrimaryOnly); | |
| 179 | |
| 180 return std::string(kBehaviorUnrestricted); | 187 return std::string(kBehaviorUnrestricted); |
| 181 } | 188 } |
| 182 | 189 |
| 183 void MultiProfileUserController::SetCachedValue( | 190 void MultiProfileUserController::SetCachedValue( |
| 184 const std::string& user_email, | 191 const std::string& user_email, |
| 185 const std::string& behavior) { | 192 const std::string& behavior) { |
| 186 DictionaryPrefUpdate update(local_state_, | 193 DictionaryPrefUpdate update(local_state_, |
| 187 prefs::kCachedMultiProfileUserBehavior); | 194 prefs::kCachedMultiProfileUserBehavior); |
| 188 update->SetStringWithoutPathExpansion(user_email, | 195 update->SetStringWithoutPathExpansion(user_email, |
| 189 SanitizeBehaviorValue(behavior)); | 196 SanitizeBehaviorValue(behavior)); |
| 190 } | 197 } |
| 191 | 198 |
| 192 void MultiProfileUserController::CheckSessionUsers() { | 199 void MultiProfileUserController::CheckSessionUsers() { |
| 193 const user_manager::UserList& users = UserManager::Get()->GetLoggedInUsers(); | 200 const user_manager::UserList& users = UserManager::Get()->GetLoggedInUsers(); |
| 194 for (user_manager::UserList::const_iterator it = users.begin(); | 201 for (user_manager::UserList::const_iterator it = users.begin(); |
| 195 it != users.end(); | 202 it != users.end(); |
| 196 ++it) { | 203 ++it) { |
| 197 if (IsUserAllowedInSession((*it)->email()) != ALLOWED) { | 204 if (!IsUserAllowedInSession((*it)->email(), NULL)) { |
| 198 delegate_->OnUserNotAllowed((*it)->email()); | 205 delegate_->OnUserNotAllowed((*it)->email()); |
| 199 return; | 206 return; |
| 200 } | 207 } |
| 201 } | 208 } |
| 202 } | 209 } |
| 203 | 210 |
| 204 void MultiProfileUserController::OnUserPrefChanged( | 211 void MultiProfileUserController::OnUserPrefChanged( |
| 205 Profile* user_profile) { | 212 Profile* user_profile) { |
| 206 std::string user_email = user_profile->GetProfileName(); | 213 std::string user_email = user_profile->GetProfileName(); |
| 207 CHECK(!user_email.empty()); | 214 CHECK(!user_email.empty()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 218 } else { | 225 } else { |
| 219 const std::string behavior = | 226 const std::string behavior = |
| 220 prefs->GetString(prefs::kMultiProfileUserBehavior); | 227 prefs->GetString(prefs::kMultiProfileUserBehavior); |
| 221 SetCachedValue(user_email, behavior); | 228 SetCachedValue(user_email, behavior); |
| 222 } | 229 } |
| 223 | 230 |
| 224 CheckSessionUsers(); | 231 CheckSessionUsers(); |
| 225 } | 232 } |
| 226 | 233 |
| 227 } // namespace chromeos | 234 } // namespace chromeos |
| OLD | NEW |