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