1600cc4afSopenharmony_ci/*
2600cc4afSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3600cc4afSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4600cc4afSopenharmony_ci * you may not use this file except in compliance with the License.
5600cc4afSopenharmony_ci * You may obtain a copy of the License at
6600cc4afSopenharmony_ci *
7600cc4afSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8600cc4afSopenharmony_ci *
9600cc4afSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10600cc4afSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11600cc4afSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12600cc4afSopenharmony_ci * See the License for the specific language governing permissions and
13600cc4afSopenharmony_ci * limitations under the License.
14600cc4afSopenharmony_ci */
15600cc4afSopenharmony_ci
16600cc4afSopenharmony_ci#include "account_helper.h"
17600cc4afSopenharmony_ci
18600cc4afSopenharmony_ci#include "app_log_wrapper.h"
19600cc4afSopenharmony_ci#include "bundle_constants.h"
20600cc4afSopenharmony_ci
21600cc4afSopenharmony_ci#ifdef ACCOUNT_ENABLE
22600cc4afSopenharmony_ci#include "os_account_manager.h"
23600cc4afSopenharmony_ci#endif
24600cc4afSopenharmony_ci
25600cc4afSopenharmony_cinamespace OHOS {
26600cc4afSopenharmony_cinamespace AppExecFwk {
27600cc4afSopenharmony_ciint32_t AccountHelper::IsOsAccountExists(const int32_t id, bool &isOsAccountExists)
28600cc4afSopenharmony_ci{
29600cc4afSopenharmony_ci#ifdef ACCOUNT_ENABLE
30600cc4afSopenharmony_ci    return AccountSA::OsAccountManager::IsOsAccountCompleted(id, isOsAccountExists);
31600cc4afSopenharmony_ci#else
32600cc4afSopenharmony_ci    APP_LOGI("ACCOUNT_ENABLE is false");
33600cc4afSopenharmony_ci    // ACCOUNT_ENABLE is false, do nothing and return -1.
34600cc4afSopenharmony_ci    return -1;
35600cc4afSopenharmony_ci#endif
36600cc4afSopenharmony_ci}
37600cc4afSopenharmony_ci
38600cc4afSopenharmony_ciint32_t AccountHelper::GetCurrentActiveUserId()
39600cc4afSopenharmony_ci{
40600cc4afSopenharmony_ci#ifdef ACCOUNT_ENABLE
41600cc4afSopenharmony_ci    int32_t localId;
42600cc4afSopenharmony_ci    int32_t ret = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(localId);
43600cc4afSopenharmony_ci    if (ret != 0) {
44600cc4afSopenharmony_ci        APP_LOGE("GetForegroundOsAccountLocalId failed ret:%{public}d", ret);
45600cc4afSopenharmony_ci        return Constants::INVALID_USERID;
46600cc4afSopenharmony_ci    }
47600cc4afSopenharmony_ci    return localId;
48600cc4afSopenharmony_ci#else
49600cc4afSopenharmony_ci    APP_LOGI("ACCOUNT_ENABLE is false");
50600cc4afSopenharmony_ci    return 0;
51600cc4afSopenharmony_ci#endif
52600cc4afSopenharmony_ci}
53600cc4afSopenharmony_ci
54600cc4afSopenharmony_cibool AccountHelper::IsOsAccountVerified(const int32_t userId)
55600cc4afSopenharmony_ci{
56600cc4afSopenharmony_ci#ifdef ACCOUNT_ENABLE
57600cc4afSopenharmony_ci    bool isOsAccountVerified = false;
58600cc4afSopenharmony_ci    int32_t ret = AccountSA::OsAccountManager::IsOsAccountVerified(userId, isOsAccountVerified);
59600cc4afSopenharmony_ci    if (ret != 0) {
60600cc4afSopenharmony_ci        APP_LOGE("IsOsAccountVerified failed ret:%{public}d", ret);
61600cc4afSopenharmony_ci        return false;
62600cc4afSopenharmony_ci    }
63600cc4afSopenharmony_ci    return isOsAccountVerified;
64600cc4afSopenharmony_ci#else
65600cc4afSopenharmony_ci    APP_LOGI("ACCOUNT_ENABLE is false");
66600cc4afSopenharmony_ci    return false;
67600cc4afSopenharmony_ci#endif
68600cc4afSopenharmony_ci}
69600cc4afSopenharmony_ci
70600cc4afSopenharmony_ciint32_t AccountHelper::GetOsAccountLocalIdFromUid(const int32_t callingUid)
71600cc4afSopenharmony_ci{
72600cc4afSopenharmony_ci#ifdef ACCOUNT_ENABLE
73600cc4afSopenharmony_ci    int32_t localId;
74600cc4afSopenharmony_ci    ErrCode err = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, localId);
75600cc4afSopenharmony_ci    if (err != ERR_OK || localId == Constants::DEFAULT_USERID) {
76600cc4afSopenharmony_ci        APP_LOGW_NOFUNC("GetOsAccountLocalIdFromUid fail uid:%{public}d req from active userid", callingUid);
77600cc4afSopenharmony_ci        return AccountHelper::GetCurrentActiveUserId();
78600cc4afSopenharmony_ci    }
79600cc4afSopenharmony_ci    return localId;
80600cc4afSopenharmony_ci#else
81600cc4afSopenharmony_ci    APP_LOGI("ACCOUNT_ENABLE is false");
82600cc4afSopenharmony_ci    // ACCOUNT_ENABLE is false, do nothing and return -1.
83600cc4afSopenharmony_ci    return -1;
84600cc4afSopenharmony_ci#endif
85600cc4afSopenharmony_ci}
86600cc4afSopenharmony_ci}  // namespace AppExecFwk
87600cc4afSopenharmony_ci}  // namespace OHOS
88