1 /*
2 * Copyright (c) 2023 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 "os_account_wrapper.h"
17
18 #include "os_account_manager.h"
19
20 #include "asset_type.h"
21 #include "asset_log.h"
22
GetUserIdByUid(uint64_t uid,uint32_t * userId)23 bool GetUserIdByUid(uint64_t uid, uint32_t *userId)
24 {
25 int userIdTmp;
26 int res = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userIdTmp);
27 if (res != 0 || userIdTmp < 0) {
28 LOGE("[FATAL]Get user id from uid failed! res is %{public}d, user id is %{public}d.", res, userIdTmp);
29 return false;
30 }
31 *userId = userIdTmp;
32 return true;
33 }
34
IsUserIdExist(int32_t userId,bool * exist)35 bool IsUserIdExist(int32_t userId, bool *exist)
36 {
37 bool isUserIdExist;
38 int ret = OHOS::AccountSA::OsAccountManager::IsOsAccountExists(userId, isUserIdExist);
39 if (ret != 0) {
40 LOGE("[FATAL]Check user id failed! res is %{public}d", ret);
41 return false;
42 }
43 *exist = isUserIdExist;
44 return true;
45 }
46
GetUserIds(int32_t * userIdsPtr,uint32_t * userIdsSize)47 int32_t GetUserIds(int32_t *userIdsPtr, uint32_t *userIdsSize)
48 {
49 std::vector<OHOS::AccountSA::OsAccountInfo> accountInfos = {};
50 int32_t ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accountInfos);
51 if (ret != OHOS::ERR_OK) {
52 LOGE("[FATAL]Query all account id failed! res is %{public}d", ret);
53 return ASSET_ACCOUNT_ERROR;
54 }
55 if (accountInfos.empty()) {
56 LOGE("[FATAL]accountInfos is empty");
57 return ASSET_ACCOUNT_ERROR;
58 }
59 std::vector<int32_t> userIdsVec = { 0 };
60 std::transform(accountInfos.begin(), accountInfos.end(), std::back_inserter(userIdsVec),
61 [](auto &iter) { return iter.GetLocalId(); });
62 if (userIdsVec.size() > *userIdsSize) {
63 LOGE("[FATAL]Users size increased after getting users size.");
64 return ASSET_ACCOUNT_ERROR;
65 }
66 for (uint32_t i = 0; i < *userIdsSize; i++) {
67 userIdsPtr[i] = userIdsVec[i];
68 }
69 *userIdsSize = static_cast<uint32_t>(userIdsVec.size());
70
71 return ASSET_SUCCESS;
72 }
73
GetUsersSize(uint32_t * userIdsSize)74 int32_t GetUsersSize(uint32_t *userIdsSize)
75 {
76 std::vector<OHOS::AccountSA::OsAccountInfo> accountInfos = {};
77 int32_t ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accountInfos);
78 if (ret != OHOS::ERR_OK) {
79 LOGE("[FATAL]Query all account id failed! res is %{public}d", ret);
80 return ASSET_ACCOUNT_ERROR;
81 }
82 if (accountInfos.empty()) {
83 LOGE("[FATAL]accountInfos is empty");
84 return ASSET_ACCOUNT_ERROR;
85 }
86 std::vector<int32_t> userIdsVec = { 0 };
87 std::transform(accountInfos.begin(), accountInfos.end(), std::back_inserter(userIdsVec),
88 [](auto &iter) { return iter.GetLocalId(); });
89 *userIdsSize = static_cast<uint32_t>(userIdsVec.size());
90
91 return ASSET_SUCCESS;
92 }