Commit 967fcdac authored by rsorokin@chromium.org's avatar rsorokin@chromium.org

Refactoring IsUserAllowedInSession and GetCachedValue

BUG=388279

Review URL: https://codereview.chromium.org/420243002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@287522 0039d316-1c4b-4281-b951-d872f2087c98
parent c6273c83
...@@ -88,11 +88,19 @@ void UserSelectionScreen::FillUserDictionary( ...@@ -88,11 +88,19 @@ void UserSelectionScreen::FillUserDictionary(
if (is_signin_to_add) { if (is_signin_to_add) {
MultiProfileUserController* multi_profile_user_controller = MultiProfileUserController* multi_profile_user_controller =
UserManager::Get()->GetMultiProfileUserController(); UserManager::Get()->GetMultiProfileUserController();
std::string behavior = MultiProfileUserController::UserAllowedInSessionReason isUserAllowedReason;
multi_profile_user_controller->GetCachedValue(user_id); bool isUserAllowed = multi_profile_user_controller->IsUserAllowedInSession(
user_dict->SetBoolean(kKeyMultiProfilesAllowed, user_id, &isUserAllowedReason);
multi_profile_user_controller->IsUserAllowedInSession( user_dict->SetBoolean(kKeyMultiProfilesAllowed, isUserAllowed);
user_id) == MultiProfileUserController::ALLOWED);
std::string behavior;
switch (isUserAllowedReason) {
case MultiProfileUserController::NOT_ALLOWED_OWNER_AS_SECONDARY:
behavior = MultiProfileUserController::kBehaviorOwnerPrimaryOnly;
break;
default:
behavior = multi_profile_user_controller->GetCachedValue(user_id);
}
user_dict->SetString(kKeyMultiProfilesPolicy, behavior); user_dict->SetString(kKeyMultiProfilesPolicy, behavior);
} else { } else {
user_dict->SetBoolean(kKeyMultiProfilesAllowed, true); user_dict->SetBoolean(kKeyMultiProfilesAllowed, true);
......
...@@ -198,9 +198,9 @@ user_manager::UserList ChromeUserManager::GetUsersAdmittedForMultiProfile() ...@@ -198,9 +198,9 @@ user_manager::UserList ChromeUserManager::GetUsersAdmittedForMultiProfile()
++it) { ++it) {
if ((*it)->GetType() == user_manager::USER_TYPE_REGULAR && if ((*it)->GetType() == user_manager::USER_TYPE_REGULAR &&
!(*it)->is_logged_in()) { !(*it)->is_logged_in()) {
MultiProfileUserController::UserAllowedInSessionResult check = MultiProfileUserController::UserAllowedInSessionReason check;
multi_profile_user_controller_->IsUserAllowedInSession( multi_profile_user_controller_->IsUserAllowedInSession((*it)->email(),
(*it)->email()); &check);
if (check == if (check ==
MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS) { MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS) {
return user_manager::UserList(); return user_manager::UserList();
......
...@@ -28,14 +28,21 @@ namespace { ...@@ -28,14 +28,21 @@ namespace {
std::string SanitizeBehaviorValue(const std::string& value) { std::string SanitizeBehaviorValue(const std::string& value) {
if (value == MultiProfileUserController::kBehaviorUnrestricted || if (value == MultiProfileUserController::kBehaviorUnrestricted ||
value == MultiProfileUserController::kBehaviorPrimaryOnly || value == MultiProfileUserController::kBehaviorPrimaryOnly ||
value == MultiProfileUserController::kBehaviorNotAllowed || value == MultiProfileUserController::kBehaviorNotAllowed) {
value == MultiProfileUserController::kBehaviorOwnerPrimaryOnly) {
return value; return value;
} }
return std::string(MultiProfileUserController::kBehaviorUnrestricted); return std::string(MultiProfileUserController::kBehaviorUnrestricted);
} }
bool SetUserAllowedReason(
MultiProfileUserController::UserAllowedInSessionReason* reason,
MultiProfileUserController::UserAllowedInSessionReason value) {
if (reason)
*reason = value;
return value == MultiProfileUserController::ALLOWED;
}
} // namespace } // namespace
// static // static
...@@ -80,9 +87,9 @@ void MultiProfileUserController::RegisterProfilePrefs( ...@@ -80,9 +87,9 @@ void MultiProfileUserController::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF); user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
} }
MultiProfileUserController::UserAllowedInSessionResult bool MultiProfileUserController::IsUserAllowedInSession(
MultiProfileUserController::IsUserAllowedInSession( const std::string& user_email,
const std::string& user_email) const { MultiProfileUserController::UserAllowedInSessionReason* reason) const {
UserManager* user_manager = UserManager::Get(); UserManager* user_manager = UserManager::Get();
CHECK(user_manager); CHECK(user_manager);
...@@ -94,16 +101,16 @@ MultiProfileUserController::IsUserAllowedInSession( ...@@ -94,16 +101,16 @@ MultiProfileUserController::IsUserAllowedInSession(
// Always allow if there is no primary user or user being checked is the // Always allow if there is no primary user or user being checked is the
// primary user. // primary user.
if (primary_user_email.empty() || primary_user_email == user_email) if (primary_user_email.empty() || primary_user_email == user_email)
return ALLOWED; return SetUserAllowedReason(reason, ALLOWED);
// Owner is not allowed to be secondary user. // Owner is not allowed to be secondary user.
if (user_manager->GetOwnerEmail() == user_email) if (user_manager->GetOwnerEmail() == user_email)
return NOT_ALLOWED_OWNER_AS_SECONDARY; return SetUserAllowedReason(reason, NOT_ALLOWED_OWNER_AS_SECONDARY);
// Don't allow profiles potentially tainted by data fetched with policy-pushed // Don't allow profiles potentially tainted by data fetched with policy-pushed
// certificates to join a multiprofile session. // certificates to join a multiprofile session.
if (policy::PolicyCertServiceFactory::UsedPolicyCertificates(user_email)) if (policy::PolicyCertServiceFactory::UsedPolicyCertificates(user_email))
return NOT_ALLOWED_POLICY_CERT_TAINTED; return SetUserAllowedReason(reason, NOT_ALLOWED_POLICY_CERT_TAINTED);
// Don't allow any secondary profiles if the primary profile is tainted. // Don't allow any secondary profiles if the primary profile is tainted.
if (policy::PolicyCertServiceFactory::UsedPolicyCertificates( if (policy::PolicyCertServiceFactory::UsedPolicyCertificates(
...@@ -111,7 +118,8 @@ MultiProfileUserController::IsUserAllowedInSession( ...@@ -111,7 +118,8 @@ MultiProfileUserController::IsUserAllowedInSession(
// Check directly in local_state before checking if the primary user has // Check directly in local_state before checking if the primary user has
// a PolicyCertService. His profile may have been tainted previously though // a PolicyCertService. His profile may have been tainted previously though
// he didn't get a PolicyCertService created for this session. // he didn't get a PolicyCertService created for this session.
return NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED; return SetUserAllowedReason(reason,
NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED);
} }
// If the primary profile already has policy certificates installed but hasn't // If the primary profile already has policy certificates installed but hasn't
...@@ -125,19 +133,22 @@ MultiProfileUserController::IsUserAllowedInSession( ...@@ -125,19 +133,22 @@ MultiProfileUserController::IsUserAllowedInSession(
primary_user_profile) primary_user_profile)
: NULL; : NULL;
if (service && service->has_policy_certificates()) if (service && service->has_policy_certificates())
return NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED; return SetUserAllowedReason(reason,
NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED);
// No user is allowed if the primary user policy forbids it. // No user is allowed if the primary user policy forbids it.
const std::string primary_user_behavior = const std::string primary_user_behavior =
primary_user_profile->GetPrefs()->GetString( primary_user_profile->GetPrefs()->GetString(
prefs::kMultiProfileUserBehavior); prefs::kMultiProfileUserBehavior);
if (primary_user_behavior == kBehaviorNotAllowed) if (primary_user_behavior == kBehaviorNotAllowed)
return NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS; return SetUserAllowedReason(reason,
NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS);
// The user must have 'unrestricted' policy to be a secondary user. // The user must have 'unrestricted' policy to be a secondary user.
const std::string behavior = GetCachedValue(user_email); const std::string behavior = GetCachedValue(user_email);
return behavior == kBehaviorUnrestricted ? ALLOWED : return SetUserAllowedReason(
NOT_ALLOWED_POLICY_FORBIDS; reason,
behavior == kBehaviorUnrestricted ? ALLOWED : NOT_ALLOWED_POLICY_FORBIDS);
} }
void MultiProfileUserController::StartObserving(Profile* user_profile) { void MultiProfileUserController::StartObserving(Profile* user_profile) {
...@@ -173,10 +184,6 @@ std::string MultiProfileUserController::GetCachedValue( ...@@ -173,10 +184,6 @@ std::string MultiProfileUserController::GetCachedValue(
if (dict && dict->GetStringWithoutPathExpansion(user_email, &value)) if (dict && dict->GetStringWithoutPathExpansion(user_email, &value))
return SanitizeBehaviorValue(value); return SanitizeBehaviorValue(value);
// Owner is not allowed to be secondary user (see http://crbug.com/385034).
if (UserManager::Get()->GetOwnerEmail() == user_email)
return std::string(kBehaviorOwnerPrimaryOnly);
return std::string(kBehaviorUnrestricted); return std::string(kBehaviorUnrestricted);
} }
...@@ -194,7 +201,7 @@ void MultiProfileUserController::CheckSessionUsers() { ...@@ -194,7 +201,7 @@ void MultiProfileUserController::CheckSessionUsers() {
for (user_manager::UserList::const_iterator it = users.begin(); for (user_manager::UserList::const_iterator it = users.begin();
it != users.end(); it != users.end();
++it) { ++it) {
if (IsUserAllowedInSession((*it)->email()) != ALLOWED) { if (!IsUserAllowedInSession((*it)->email(), NULL)) {
delegate_->OnUserNotAllowed((*it)->email()); delegate_->OnUserNotAllowed((*it)->email());
return; return;
} }
......
...@@ -30,8 +30,8 @@ class UserManager; ...@@ -30,8 +30,8 @@ class UserManager;
// user login and checks if the meaning of the value is respected. // user login and checks if the meaning of the value is respected.
class MultiProfileUserController { class MultiProfileUserController {
public: public:
// Return value of IsUserAllowedInSession(). // Second return value of IsUserAllowedInSession().
enum UserAllowedInSessionResult { enum UserAllowedInSessionReason {
// User is allowed in multi-profile session. // User is allowed in multi-profile session.
ALLOWED, ALLOWED,
...@@ -65,10 +65,10 @@ class MultiProfileUserController { ...@@ -65,10 +65,10 @@ class MultiProfileUserController {
// Returns the cached policy value for |user_email|. // Returns the cached policy value for |user_email|.
std::string GetCachedValue(const std::string& user_email) const; std::string GetCachedValue(const std::string& user_email) const;
// Returns UserAllowedInSessionResult enum that describe whether the user is // Returns true if user allowed to be in the current session. If |reason| not
// allowed to be in the current session. // null stores UserAllowedInSessionReason enum that describes actual reason.
UserAllowedInSessionResult IsUserAllowedInSession( bool IsUserAllowedInSession(const std::string& user_email,
const std::string& user_email) const; UserAllowedInSessionReason* reason) const;
// Starts to observe the multiprofile user behavior pref of the given profile. // Starts to observe the multiprofile user behavior pref of the given profile.
void StartObserving(Profile* user_profile); void StartObserving(Profile* user_profile);
......
...@@ -34,7 +34,7 @@ const char* kUsers[] = {"a@gmail.com", "b@gmail.com" }; ...@@ -34,7 +34,7 @@ const char* kUsers[] = {"a@gmail.com", "b@gmail.com" };
struct BehaviorTestCase { struct BehaviorTestCase {
const char* primary; const char* primary;
const char* secondary; const char* secondary;
MultiProfileUserController::UserAllowedInSessionResult expected_allowed; MultiProfileUserController::UserAllowedInSessionReason expected_allowed;
}; };
const BehaviorTestCase kBehaviorTestCases[] = { const BehaviorTestCase kBehaviorTestCases[] = {
...@@ -212,9 +212,10 @@ TEST_F(MultiProfileUserControllerTest, AllAllowedBeforeLogin) { ...@@ -212,9 +212,10 @@ TEST_F(MultiProfileUserControllerTest, AllAllowedBeforeLogin) {
}; };
for (size_t i = 0; i < arraysize(kTestCases); ++i) { for (size_t i = 0; i < arraysize(kTestCases); ++i) {
SetCachedBehavior(0, kTestCases[i]); SetCachedBehavior(0, kTestCases[i]);
EXPECT_EQ(MultiProfileUserController::ALLOWED, MultiProfileUserController::UserAllowedInSessionReason reason;
controller()->IsUserAllowedInSession(kUsers[0])) EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0], &reason))
<< "Case " << i; << "Case " << i;
EXPECT_EQ(MultiProfileUserController::ALLOWED, reason) << "Case " << i;
} }
} }
...@@ -271,8 +272,9 @@ TEST_F(MultiProfileUserControllerTest, IsSecondaryAllowed) { ...@@ -271,8 +272,9 @@ TEST_F(MultiProfileUserControllerTest, IsSecondaryAllowed) {
for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) { for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) {
SetPrefBehavior(0, kBehaviorTestCases[i].primary); SetPrefBehavior(0, kBehaviorTestCases[i].primary);
SetCachedBehavior(1, kBehaviorTestCases[i].secondary); SetCachedBehavior(1, kBehaviorTestCases[i].secondary);
EXPECT_EQ(kBehaviorTestCases[i].expected_allowed, MultiProfileUserController::UserAllowedInSessionReason reason;
controller()->IsUserAllowedInSession(kUsers[1])) << "Case " << i; controller()->IsUserAllowedInSession(kUsers[1], &reason);
EXPECT_EQ(kBehaviorTestCases[i].expected_allowed, reason) << "Case " << i;
} }
} }
...@@ -303,10 +305,9 @@ TEST_F(MultiProfileUserControllerTest, NoSecondaryOwner) { ...@@ -303,10 +305,9 @@ TEST_F(MultiProfileUserControllerTest, NoSecondaryOwner) {
LoginUser(0); LoginUser(0);
SetOwner(1); SetOwner(1);
EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_OWNER_AS_SECONDARY, MultiProfileUserController::UserAllowedInSessionReason reason;
controller()->IsUserAllowedInSession(kUsers[1])); EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1], &reason));
EXPECT_EQ(MultiProfileUserController::kBehaviorOwnerPrimaryOnly, EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_OWNER_AS_SECONDARY, reason);
GetCachedBehavior(1));
EXPECT_EQ(0, user_not_allowed_count()); EXPECT_EQ(0, user_not_allowed_count());
LoginUser(1); LoginUser(1);
...@@ -318,10 +319,11 @@ TEST_F(MultiProfileUserControllerTest, ...@@ -318,10 +319,11 @@ TEST_F(MultiProfileUserControllerTest,
// Verifies that any user can sign-in as the primary user, regardless of the // Verifies that any user can sign-in as the primary user, regardless of the
// tainted state. // tainted state.
policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]); policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]);
EXPECT_EQ(MultiProfileUserController::ALLOWED, MultiProfileUserController::UserAllowedInSessionReason reason;
controller()->IsUserAllowedInSession(kUsers[0])); EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0], &reason));
EXPECT_EQ(MultiProfileUserController::ALLOWED, EXPECT_EQ(MultiProfileUserController::ALLOWED, reason);
controller()->IsUserAllowedInSession(kUsers[1])); EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[1], &reason));
EXPECT_EQ(MultiProfileUserController::ALLOWED, reason);
} }
TEST_F(MultiProfileUserControllerTest, TEST_F(MultiProfileUserControllerTest,
...@@ -334,11 +336,14 @@ TEST_F(MultiProfileUserControllerTest, ...@@ -334,11 +336,14 @@ TEST_F(MultiProfileUserControllerTest,
// changed back to enabled. // changed back to enabled.
SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted); SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted);
EXPECT_EQ(MultiProfileUserController::ALLOWED, MultiProfileUserController::UserAllowedInSessionReason reason;
controller()->IsUserAllowedInSession(kUsers[0])); EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0], &reason));
EXPECT_EQ(MultiProfileUserController::ALLOWED, reason);
policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]); policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]);
EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[0], &reason));
EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_POLICY_CERT_TAINTED, EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_POLICY_CERT_TAINTED,
controller()->IsUserAllowedInSession(kUsers[0])); reason);
} }
TEST_F(MultiProfileUserControllerTest, TEST_F(MultiProfileUserControllerTest,
...@@ -354,11 +359,14 @@ TEST_F(MultiProfileUserControllerTest, ...@@ -354,11 +359,14 @@ TEST_F(MultiProfileUserControllerTest,
policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse( policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
profile(0), TestPolicyCertServiceFactory)); profile(0), TestPolicyCertServiceFactory));
MultiProfileUserController::UserAllowedInSessionReason reason;
EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1], &reason));
EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED, EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED,
controller()->IsUserAllowedInSession(kUsers[1])); reason);
policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[1]); policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[1]);
EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1], &reason));
EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_POLICY_CERT_TAINTED, EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_POLICY_CERT_TAINTED,
controller()->IsUserAllowedInSession(kUsers[1])); reason);
// Flush tasks posted to IO. // Flush tasks posted to IO.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
...@@ -384,16 +392,18 @@ TEST_F(MultiProfileUserControllerTest, ...@@ -384,16 +392,18 @@ TEST_F(MultiProfileUserControllerTest,
ASSERT_TRUE(service); ASSERT_TRUE(service);
EXPECT_FALSE(service->has_policy_certificates()); EXPECT_FALSE(service->has_policy_certificates());
EXPECT_EQ(MultiProfileUserController::ALLOWED, MultiProfileUserController::UserAllowedInSessionReason reason;
controller()->IsUserAllowedInSession(kUsers[1])); EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[1], &reason));
EXPECT_EQ(MultiProfileUserController::ALLOWED, reason);
net::CertificateList certificates; net::CertificateList certificates;
certificates.push_back(new net::X509Certificate( certificates.push_back(new net::X509Certificate(
"subject", "issuer", base::Time(), base::Time())); "subject", "issuer", base::Time(), base::Time()));
service->OnTrustAnchorsChanged(certificates); service->OnTrustAnchorsChanged(certificates);
EXPECT_TRUE(service->has_policy_certificates()); EXPECT_TRUE(service->has_policy_certificates());
EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1], &reason));
EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED, EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED,
controller()->IsUserAllowedInSession(kUsers[1])); reason);
// Flush tasks posted to IO. // Flush tasks posted to IO.
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment