1/*
2 * Copyright (c) 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 "account_helper.h"
17
18#include "app_log_wrapper.h"
19#include "bundle_constants.h"
20
21#ifdef ACCOUNT_ENABLE
22#include "os_account_manager.h"
23#endif
24
25namespace OHOS {
26namespace AppExecFwk {
27int32_t AccountHelper::IsOsAccountExists(const int32_t id, bool &isOsAccountExists)
28{
29#ifdef ACCOUNT_ENABLE
30    return AccountSA::OsAccountManager::IsOsAccountCompleted(id, isOsAccountExists);
31#else
32    APP_LOGI("ACCOUNT_ENABLE is false");
33    // ACCOUNT_ENABLE is false, do nothing and return -1.
34    return -1;
35#endif
36}
37
38int32_t AccountHelper::GetCurrentActiveUserId()
39{
40#ifdef ACCOUNT_ENABLE
41    int32_t localId;
42    int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(localId);
43    if (ret != 0) {
44        APP_LOGE("GetForegroundOsAccountLocalId failed ret:%{public}d", ret);
45        return Constants::INVALID_USERID;
46    }
47    return localId;
48#else
49    APP_LOGI("ACCOUNT_ENABLE is false");
50    return 0;
51#endif
52}
53
54bool AccountHelper::IsOsAccountVerified(const int32_t userId)
55{
56#ifdef ACCOUNT_ENABLE
57    bool isOsAccountVerified = false;
58    int32_t ret = AccountSA::OsAccountManager::IsOsAccountVerified(userId, isOsAccountVerified);
59    if (ret != 0) {
60        APP_LOGE("IsOsAccountVerified failed ret:%{public}d", ret);
61        return false;
62    }
63    return isOsAccountVerified;
64#else
65    APP_LOGI("ACCOUNT_ENABLE is false");
66    return false;
67#endif
68}
69
70int32_t AccountHelper::GetOsAccountLocalIdFromUid(const int32_t callingUid)
71{
72#ifdef ACCOUNT_ENABLE
73    int32_t localId;
74    ErrCode err = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, localId);
75    if (err != ERR_OK || localId == Constants::DEFAULT_USERID) {
76        APP_LOGW_NOFUNC("GetOsAccountLocalIdFromUid fail uid:%{public}d req from active userid", callingUid);
77        return AccountHelper::GetCurrentActiveUserId();
78    }
79    return localId;
80#else
81    APP_LOGI("ACCOUNT_ENABLE is false");
82    // ACCOUNT_ENABLE is false, do nothing and return -1.
83    return -1;
84#endif
85}
86}  // namespace AppExecFwk
87}  // namespace OHOS
88