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 <sys/stat.h>
19 #include "app_account_constants.h"
20 #include "account_file_operator.h"
21 #include "account_log_wrapper.h"
22
23 namespace OHOS {
24 namespace AccountSA {
25 namespace {
26 const std::string AUTHORIZED_ACCOUNTS = "authorizedAccounts";
27 }
28
AppAccountDataStorage(const std::string & storeId,const AccountDataStorageOptions & options)29 AppAccountDataStorage::AppAccountDataStorage(const std::string &storeId, const AccountDataStorageOptions &options)
30 : AccountDataStorage(Constants::APP_ACCOUNT_APP_ID, storeId, options)
31 {
32 if (!options.baseDir.empty()) {
33 AccountFileOperator fileOperator;
34 fileOperator.CreateDir(options.baseDir, S_IRWXU | S_IRWXG);
35 }
36 }
37
GetAccessibleAccountsFromAuthorizedAccounts(const std::string & authorizedAccounts,const std::string & authorizedApp,std::vector<std::string> & accessibleAccounts)38 Json AppAccountDataStorage::GetAccessibleAccountsFromAuthorizedAccounts(const std::string &authorizedAccounts,
39 const std::string &authorizedApp, std::vector<std::string> &accessibleAccounts)
40 {
41 accessibleAccounts.clear();
42
43 auto jsonObject = Json::parse(authorizedAccounts, nullptr, false);
44 if (jsonObject.is_discarded()) {
45 jsonObject = Json::object();
46 } else {
47 auto value = jsonObject.find(authorizedApp);
48 if (value == jsonObject.end()) {
49 jsonObject.emplace(authorizedApp, Json::array());
50 } else if (value->is_array()) {
51 accessibleAccounts = jsonObject[authorizedApp].get<std::vector<std::string>>();
52 }
53 }
54
55 return jsonObject;
56 }
57
GetAccessibleAccountsFromDataStorage(const std::string & authorizedApp,std::vector<std::string> & accessibleAccounts)58 ErrCode AppAccountDataStorage::GetAccessibleAccountsFromDataStorage(
59 const std::string &authorizedApp, std::vector<std::string> &accessibleAccounts)
60 {
61 std::string authorizedAccounts;
62 ErrCode result = GetValueFromKvStore(AUTHORIZED_ACCOUNTS, authorizedAccounts);
63 if (result != ERR_OK) {
64 ACCOUNT_LOGE("failed to get config by id from data storage");
65 }
66
67 GetAccessibleAccountsFromAuthorizedAccounts(authorizedAccounts, authorizedApp, accessibleAccounts);
68
69 return ERR_OK;
70 }
71
GetAccountInfoFromDataStorage(AppAccountInfo & appAccountInfo)72 ErrCode AppAccountDataStorage::GetAccountInfoFromDataStorage(AppAccountInfo &appAccountInfo)
73 {
74 ErrCode result = GetAccountInfoById(appAccountInfo.GetPrimeKey(), appAccountInfo);
75 if (result != ERR_OK) {
76 ACCOUNT_LOGE("failed to get account info by id, result %{public}d.", result);
77 return ERR_APPACCOUNT_SERVICE_GET_ACCOUNT_INFO_BY_ID;
78 }
79
80 return ERR_OK;
81 }
82
AddAccountInfoIntoDataStorage(AppAccountInfo & appAccountInfo)83 ErrCode AppAccountDataStorage::AddAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo)
84 {
85 ErrCode result = AddAccountInfo(appAccountInfo);
86 if (result != ERR_OK) {
87 ACCOUNT_LOGE("failed to add account info, result = %{public}d", result);
88 return result;
89 }
90
91 return ERR_OK;
92 }
93
SaveAccountInfoIntoDataStorage(AppAccountInfo & appAccountInfo)94 ErrCode AppAccountDataStorage::SaveAccountInfoIntoDataStorage(AppAccountInfo &appAccountInfo)
95 {
96 ErrCode result = SaveAccountInfo(appAccountInfo);
97 if (result != ERR_OK) {
98 ACCOUNT_LOGE("failed to save account info, result = %{public}d", result);
99 return result;
100 }
101
102 return ERR_OK;
103 }
104
DeleteAccountInfoFromDataStorage(AppAccountInfo & appAccountInfo)105 ErrCode AppAccountDataStorage::DeleteAccountInfoFromDataStorage(AppAccountInfo &appAccountInfo)
106 {
107 ErrCode ret = RemoveValueFromKvStore(appAccountInfo.GetPrimeKey());
108 if (ret != ERR_OK) {
109 ACCOUNT_LOGE("RemoveValueFromKvStore failed! ret = %{public}d.", ret);
110 }
111 return ret;
112 }
113
SaveEntries(std::vector<OHOS::DistributedKv::Entry> allEntries,std::map<std::string,std::shared_ptr<IAccountInfo>> & infos)114 void AppAccountDataStorage::SaveEntries(
115 std::vector<OHOS::DistributedKv::Entry> allEntries, std::map<std::string, std::shared_ptr<IAccountInfo>> &infos)
116 {
117 for (auto const &item : allEntries) {
118 Json jsonObject = Json::parse(item.value.ToString(), nullptr, false);
119 if (jsonObject.is_discarded()) {
120 ACCOUNT_LOGE("error key: %{private}s", item.key.ToString().c_str());
121 // it's a bad json, delete it
122 {
123 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
124 kvStorePtr_->Delete(item.key);
125 }
126 continue;
127 }
128
129 AppAccountInfo appAccountInfo;
130 appAccountInfo.FromJson(jsonObject);
131 infos.emplace(item.key.ToString(), std::make_shared<AppAccountInfo>(appAccountInfo));
132 }
133 }
134 } // namespace AccountSA
135 } // namespace OHOS