1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "app_account_data_storage.h"
17
18 #include "app_account_constants.h"
19 #include "account_log_wrapper.h"
20
21 namespace OHOS {
22 namespace AccountSA {
23 const std::string AppAccountDataStorage::DATA_STORAGE_SUFFIX = "_sync";
24 const std::string AppAccountDataStorage::AUTHORIZED_ACCOUNTS = "authorizedAccounts";
25
AppAccountDataStorage(const std::string & storeId,const bool & autoSync)26 AppAccountDataStorage::AppAccountDataStorage(const std::string &storeId, const bool &autoSync)
27 : AccountDataStorage(Constants::APP_ACCOUNT_APP_ID, storeId, autoSync)
28 {}
29
GetAccessibleAccountsFromAuthorizedAccounts(const std::string & authorizedAccounts,const std::string & authorizedApp,std::vector<std::string> & accessibleAccounts)30 Json AppAccountDataStorage::GetAccessibleAccountsFromAuthorizedAccounts(const std::string &authorizedAccounts,
31 const std::string &authorizedApp, std::vector<std::string> &accessibleAccounts)
32 {
33 accessibleAccounts.clear();
34
35 auto jsonObject = Json::parse(authorizedAccounts, nullptr, false);
36 if (jsonObject.is_discarded()) {
37 jsonObject = Json::object();
38 } else {
39 auto value = jsonObject.find(authorizedApp);
40 if (value == jsonObject.end()) {
41 jsonObject.emplace(authorizedApp, Json::array());
42 } else if (value->is_array()) {
43 accessibleAccounts = jsonObject[authorizedApp].get<std::vector<std::string>>();
44 }
45 }
46
47 return jsonObject;
48 }
49
GetAccessibleAccountsFromDataStorage(const std::string & authorizedApp,std::vector<std::string> & accessibleAccounts)50 ErrCode AppAccountDataStorage::GetAccessibleAccountsFromDataStorage(
51 const std::string &authorizedApp, std::vector<std::string> &accessibleAccounts)
52 {
53 std::string authorizedAccounts;
54 ErrCode result = GetValueFromKvStore(AUTHORIZED_ACCOUNTS, authorizedAccounts);
55 if (result != ERR_OK) {
56 ACCOUNT_LOGE("failed to get config by id from data storage");
57 }
58
59 GetAccessibleAccountsFromAuthorizedAccounts(authorizedAccounts, authorizedApp, accessibleAccounts);
60
61 return ERR_OK;
62 }
63
GetAccountInfoFromDataStorage(AppAccountInfo & appAccountInfo)64 ErrCode AppAccountDataStorage::GetAccountInfoFromDataStorage(AppAccountInfo &appAccountInfo)
65 {
66 ErrCode result = GetAccountInfoById(appAccountInfo.GetPrimeKey(), appAccountInfo);
67 if (result != ERR_OK) {
68 ACCOUNT_LOGE("failed to get account info by id, result %{public}d.", result);
69 return ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_INFO_BY_ID;
70 }
71
72 return ERR_OK;
73 }
74
AddAccountInfoIntoDataStorage(AppAccountInfo & appAccountInfo)75 ErrCode AppAccountDataStorage::AddAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo)
76 {
77 ErrCode result = AddAccountInfo(appAccountInfo);
78 if (result != ERR_OK) {
79 ACCOUNT_LOGE("failed to add account info, result = %{public}d", result);
80 return ERR_APPACCOUNT_SERVICE_ADD_ACCOUNT_INFO;
81 }
82
83 return ERR_OK;
84 }
85
SaveAccountInfoIntoDataStorage(AppAccountInfo & appAccountInfo)86 ErrCode AppAccountDataStorage::SaveAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo)
87 {
88 ErrCode result = SaveAccountInfo(appAccountInfo);
89 if (result != ERR_OK) {
90 ACCOUNT_LOGE("failed to save account info, result = %{public}d", result);
91 return ERR_APPACCOUNT_SERVICE_SAVE_ACCOUNT_INFO;
92 }
93
94 return ERR_OK;
95 }
96
DeleteAccountInfoFromDataStorage(AppAccountInfo & appAccountInfo)97 ErrCode AppAccountDataStorage::DeleteAccountInfoFromDataStorage(AppAccountInfo &appAccountInfo)
98 {
99 ErrCode ret = RemoveValueFromKvStore(appAccountInfo.GetPrimeKey());
100 if (ret != ERR_OK) {
101 ACCOUNT_LOGE("RemoveValueFromKvStore failed! ret = %{public}d.", ret);
102 }
103 return ret;
104 }
105
SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries,std::map<std::string,std::shared_ptr<IAccountInfo>> & infos)106 void AppAccountDataStorage::SaveEntries(
107 std::vector<OHOS::DistributedKv::Entry> allEntries, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos)
108 {
109 for (auto const &item : allEntries) {
110 Json jsonObject = Json::parse(item.value.ToString(), nullptr, false);
111 if (jsonObject.is_discarded()) {
112 ACCOUNT_LOGE("error key: %{private}s", item.key.ToString().c_str());
113 // it's a bad json, delete it
114 {
115 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
116 kvStorePtr_->Delete(item.key);
117 }
118 continue;
119 }
120
121 AppAccountInfo appAccountInfo;
122 appAccountInfo.FromJson(jsonObject);
123 infos.emplace(item.key.ToString(), std::make_shared<AppAccountInfo>(appAccountInfo));
124 }
125 }
126 } // namespace AccountSA
127 } // namespace OHOS