1dfe32fa1Soh_ci/* 2dfe32fa1Soh_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3dfe32fa1Soh_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4dfe32fa1Soh_ci * you may not use this file except in compliance with the License. 5dfe32fa1Soh_ci * You may obtain a copy of the License at 6dfe32fa1Soh_ci * 7dfe32fa1Soh_ci * http://www.apache.org/licenses/LICENSE-2.0 8dfe32fa1Soh_ci * 9dfe32fa1Soh_ci * Unless required by applicable law or agreed to in writing, software 10dfe32fa1Soh_ci * distributed under the License is distributed on an "AS IS" BASIS, 11dfe32fa1Soh_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12dfe32fa1Soh_ci * See the License for the specific language governing permissions and 13dfe32fa1Soh_ci * limitations under the License. 14dfe32fa1Soh_ci */ 15dfe32fa1Soh_ci 16dfe32fa1Soh_ci#include "os_account_wrapper.h" 17dfe32fa1Soh_ci 18dfe32fa1Soh_ci#include "os_account_manager.h" 19dfe32fa1Soh_ci 20dfe32fa1Soh_ci#include "asset_type.h" 21dfe32fa1Soh_ci#include "asset_log.h" 22dfe32fa1Soh_ci 23dfe32fa1Soh_cibool GetUserIdByUid(uint64_t uid, uint32_t *userId) 24dfe32fa1Soh_ci{ 25dfe32fa1Soh_ci int userIdTmp; 26dfe32fa1Soh_ci int res = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userIdTmp); 27dfe32fa1Soh_ci if (res != 0 || userIdTmp < 0) { 28dfe32fa1Soh_ci LOGE("[FATAL]Get user id from uid failed! res is %{public}d, user id is %{public}d.", res, userIdTmp); 29dfe32fa1Soh_ci return false; 30dfe32fa1Soh_ci } 31dfe32fa1Soh_ci *userId = userIdTmp; 32dfe32fa1Soh_ci return true; 33dfe32fa1Soh_ci} 34dfe32fa1Soh_ci 35dfe32fa1Soh_cibool IsUserIdExist(int32_t userId, bool *exist) 36dfe32fa1Soh_ci{ 37dfe32fa1Soh_ci bool isUserIdExist; 38dfe32fa1Soh_ci int ret = OHOS::AccountSA::OsAccountManager::IsOsAccountExists(userId, isUserIdExist); 39dfe32fa1Soh_ci if (ret != 0) { 40dfe32fa1Soh_ci LOGE("[FATAL]Check user id failed! res is %{public}d", ret); 41dfe32fa1Soh_ci return false; 42dfe32fa1Soh_ci } 43dfe32fa1Soh_ci *exist = isUserIdExist; 44dfe32fa1Soh_ci return true; 45dfe32fa1Soh_ci} 46dfe32fa1Soh_ci 47dfe32fa1Soh_ciint32_t GetUserIds(int32_t *userIdsPtr, uint32_t *userIdsSize) 48dfe32fa1Soh_ci{ 49dfe32fa1Soh_ci std::vector<OHOS::AccountSA::OsAccountInfo> accountInfos = {}; 50dfe32fa1Soh_ci int32_t ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accountInfos); 51dfe32fa1Soh_ci if (ret != OHOS::ERR_OK) { 52dfe32fa1Soh_ci LOGE("[FATAL]Query all account id failed! res is %{public}d", ret); 53dfe32fa1Soh_ci return ASSET_ACCOUNT_ERROR; 54dfe32fa1Soh_ci } 55dfe32fa1Soh_ci if (accountInfos.empty()) { 56dfe32fa1Soh_ci LOGE("[FATAL]accountInfos is empty"); 57dfe32fa1Soh_ci return ASSET_ACCOUNT_ERROR; 58dfe32fa1Soh_ci } 59dfe32fa1Soh_ci std::vector<int32_t> userIdsVec = { 0 }; 60dfe32fa1Soh_ci std::transform(accountInfos.begin(), accountInfos.end(), std::back_inserter(userIdsVec), 61dfe32fa1Soh_ci [](auto &iter) { return iter.GetLocalId(); }); 62dfe32fa1Soh_ci if (userIdsVec.size() > *userIdsSize) { 63dfe32fa1Soh_ci LOGE("[FATAL]Users size increased after getting users size."); 64dfe32fa1Soh_ci return ASSET_ACCOUNT_ERROR; 65dfe32fa1Soh_ci } 66dfe32fa1Soh_ci for (uint32_t i = 0; i < *userIdsSize; i++) { 67dfe32fa1Soh_ci userIdsPtr[i] = userIdsVec[i]; 68dfe32fa1Soh_ci } 69dfe32fa1Soh_ci *userIdsSize = static_cast<uint32_t>(userIdsVec.size()); 70dfe32fa1Soh_ci 71dfe32fa1Soh_ci return ASSET_SUCCESS; 72dfe32fa1Soh_ci} 73dfe32fa1Soh_ci 74dfe32fa1Soh_ciint32_t GetUsersSize(uint32_t *userIdsSize) 75dfe32fa1Soh_ci{ 76dfe32fa1Soh_ci std::vector<OHOS::AccountSA::OsAccountInfo> accountInfos = {}; 77dfe32fa1Soh_ci int32_t ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accountInfos); 78dfe32fa1Soh_ci if (ret != OHOS::ERR_OK) { 79dfe32fa1Soh_ci LOGE("[FATAL]Query all account id failed! res is %{public}d", ret); 80dfe32fa1Soh_ci return ASSET_ACCOUNT_ERROR; 81dfe32fa1Soh_ci } 82dfe32fa1Soh_ci if (accountInfos.empty()) { 83dfe32fa1Soh_ci LOGE("[FATAL]accountInfos is empty"); 84dfe32fa1Soh_ci return ASSET_ACCOUNT_ERROR; 85dfe32fa1Soh_ci } 86dfe32fa1Soh_ci std::vector<int32_t> userIdsVec = { 0 }; 87dfe32fa1Soh_ci std::transform(accountInfos.begin(), accountInfos.end(), std::back_inserter(userIdsVec), 88dfe32fa1Soh_ci [](auto &iter) { return iter.GetLocalId(); }); 89dfe32fa1Soh_ci *userIdsSize = static_cast<uint32_t>(userIdsVec.size()); 90dfe32fa1Soh_ci 91dfe32fa1Soh_ci return ASSET_SUCCESS; 92dfe32fa1Soh_ci}