Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / managed_mode / chromeos / manager_password_service.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/managed_mode/chromeos/manager_password_service.h"
6
7 #include "base/bind.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/login/managed/supervised_user_authentication.h"
10 #include "chrome/browser/chromeos/login/supervised_user_manager.h"
11 #include "chrome/browser/chromeos/login/user_manager.h"
12 #include "chrome/browser/managed_mode/managed_user_constants.h"
13 #include "chrome/browser/managed_mode/managed_user_sync_service.h"
14
15 ManagerPasswordService::ManagerPasswordService() : weak_ptr_factory_(this) {}
16
17 ManagerPasswordService::~ManagerPasswordService() {}
18
19 void ManagerPasswordService::Init(
20     const std::string& user_id,
21     ManagedUserSyncService* user_service,
22     ManagedUserSharedSettingsService* shared_settings_service) {
23   user_id_ = user_id;
24   user_service_ = user_service;
25   settings_service_ = shared_settings_service;
26   settings_service_subscription_ = settings_service_->Subscribe(
27       base::Bind(&ManagerPasswordService::OnSharedSettingsChange,
28                  weak_ptr_factory_.GetWeakPtr()));
29
30   chromeos::UserManager* user_manager = chromeos::UserManager::Get();
31
32   chromeos::SupervisedUserManager* supervised_user_manager =
33       user_manager->GetSupervisedUserManager();
34
35   const chromeos::UserList& users = user_manager->GetUsers();
36
37   for (chromeos::UserList::const_iterator it = users.begin();
38       it != users.end(); ++it) {
39     if ((*it)->GetType() !=  chromeos::User::USER_TYPE_LOCALLY_MANAGED)
40       continue;
41     if (user_id != supervised_user_manager->GetManagerUserId((*it)->email()))
42       continue;
43     OnSharedSettingsChange(
44         supervised_user_manager->GetUserSyncId((*it)->email()),
45         managed_users::kUserPasswordRecord);
46   }
47 }
48
49 void ManagerPasswordService::OnSharedSettingsChange(
50     const std::string& mu_id,
51     const std::string& key) {
52   if (key != managed_users::kUserPasswordRecord)
53     return;
54
55   chromeos::SupervisedUserManager* supervised_user_manager =
56       chromeos::UserManager::Get()->GetSupervisedUserManager();
57   const chromeos::User* user = supervised_user_manager->FindBySyncId(mu_id);
58   // No user on device.
59   if (user == NULL)
60     return;
61
62   const base::Value* value = settings_service_->GetValue(mu_id, key);
63
64   if (value == NULL) {
65     LOG(WARNING) << "Got empty value from sync.";
66     return;
67   }
68   const base::DictionaryValue* dict;
69   if (!value->GetAsDictionary(&dict)) {
70     LOG(WARNING) << "Got non-dictionary value from sync.";
71     return;
72   }
73
74   chromeos::SupervisedUserAuthentication* auth =
75       supervised_user_manager->GetAuthentication();
76
77   if (!auth->NeedPasswordChange(user->email(), dict))
78     return;
79   user_service_->GetManagedUsersAsync(
80       base::Bind(&ManagerPasswordService::GetManagedUsersCallback,
81                  weak_ptr_factory_.GetWeakPtr(),
82                  mu_id,
83                  user->email(),
84                  dict));
85 }
86
87 void ManagerPasswordService::GetManagedUsersCallback(
88     const std::string& sync_mu_id,
89     const std::string& user_id,
90     const base::DictionaryValue* password_data,
91     const base::DictionaryValue* managed_users) {
92   const base::DictionaryValue* managed_user = NULL;
93   if (!managed_users->GetDictionary(sync_mu_id, &managed_user))
94     return;
95   std::string master_key;
96   if (!managed_user->GetString(ManagedUserSyncService::kMasterKey, &master_key))
97     return;
98   chromeos::SupervisedUserAuthentication* auth = chromeos::UserManager::Get()->
99       GetSupervisedUserManager()->GetAuthentication();
100   auth->ChangeSupervisedUserPassword(
101       user_id_,
102       master_key,
103       user_id,
104       password_data);
105 }
106
107 void ManagerPasswordService::Shutdown() {
108   settings_service_subscription_.reset();
109 }