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 "os_account_manager_helper.h"
17 #include "event_log_wrapper.h"
18 
19 #ifdef HAS_OS_ACCOUNT_PART
20 #include "os_account_manager.h"
21 #include "os_account_constants.h"
22 #endif // HAS_OS_ACCOUNT_PART
23 
24 namespace OHOS {
25 namespace EventFwk {
26 #ifndef HAS_OS_ACCOUNT_PART
27 namespace {
28 const int32_t DEFAULT_OS_ACCOUNT_ID = 100; // default id when there is no os_account part
29 const int32_t UID_TRANSFORM_DIVISOR = 200000;
30 }
31 #endif // HAS_OS_ACCOUNT_PART
32 
QueryActiveOsAccountIds(std::vector<int32_t> & ids)33 ErrCode OsAccountManagerHelper::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
34 {
35 #ifndef HAS_OS_ACCOUNT_PART
36     ids.emplace_back(DEFAULT_OS_ACCOUNT_ID);
37     return ERR_OK;
38 #else
39     return AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
40 #endif // HAS_OS_ACCOUNT_PART
41 }
42 
GetOsAccountLocalIdFromUid(const int32_t uid,int32_t & id)43 ErrCode OsAccountManagerHelper::GetOsAccountLocalIdFromUid(const int32_t uid, int32_t &id)
44 {
45 #ifndef HAS_OS_ACCOUNT_PART
46     id = uid / UID_TRANSFORM_DIVISOR;
47     return ERR_OK;
48 #else
49     return AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, id);
50 #endif // HAS_OS_ACCOUNT_PART
51 }
52 
CheckUserExists(const int32_t & userId)53 bool OsAccountManagerHelper::CheckUserExists(const int32_t &userId)
54 {
55     bool isAccountExists = false;
56     int32_t ret = OHOS::AccountSA::OsAccountManager::IsOsAccountExists(userId, isAccountExists);
57     if (ret != ERR_OK) {
58         EVENT_LOGE("Failed to call AccountSA::IsOsAccountExists, code is %{public}d", ret);
59     }
60     return isAccountExists;
61 }
62 
IsSystemAccount(int32_t userId)63 bool OsAccountManagerHelper::IsSystemAccount(int32_t userId)
64 {
65     return userId >= AccountSA::Constants::START_USER_ID && userId <= AccountSA::Constants::MAX_USER_ID;
66 }
67 }  // namespace EventFwk
68 }  // namespace OHOS