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 #include "ans_log_wrapper.h"
16 #include "ans_inner_errors.h"
17 #include "errors.h"
18 #include "ipc_skeleton.h"
19 #include "os_account_manager_helper.h"
20 #include "os_account_constants.h"
21 #include "os_account_info.h"
22 #include "os_account_manager.h"
23 #include <vector>
24 #include "notification_analytics_util.h"
25 
26 namespace OHOS {
27 namespace Notification {
GetOsAccountLocalIdFromUid(const int32_t uid,int32_t & userId)28 ErrCode OsAccountManagerHelper::GetOsAccountLocalIdFromUid(const int32_t uid, int32_t &userId)
29 {
30     int32_t ret = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId);
31     if (ret != ERR_OK) {
32         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_1)
33             .Message("Get userId failed, uid = " + std::to_string(uid) + " ret " + std::to_string(ret));
34         NotificationAnalyticsUtil::ReportModifyEvent(message);
35         ANS_LOGE("Get userId failed, uid = <%{public}d>, code is %{public}d", uid, ret);
36         return ret;
37     }
38     ANS_LOGD("Get userId Success, uid = <%{public}d> userId = <%{public}d>", uid, userId);
39     return ret;
40 }
41 
GetCurrentCallingUserId(int32_t & userId)42 ErrCode OsAccountManagerHelper::GetCurrentCallingUserId(int32_t &userId)
43 {
44     int32_t callingUid = IPCSkeleton::GetCallingUid();
45     int32_t ret = AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId);
46     if (ret != ERR_OK) {
47         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_2)
48             .Message("Get userId failed, callingUid = " + std::to_string(callingUid) +
49             " ret " + std::to_string(ret));
50         NotificationAnalyticsUtil::ReportModifyEvent(message);
51         ANS_LOGE("Get userId failed, callingUid = <%{public}d>, code is %{public}d", callingUid, ret);
52         return ERR_ANS_INVALID_PARAM;
53     }
54     ANS_LOGD("Get userId Success, callingUid = <%{public}d> userId = <%{public}d>", callingUid, userId);
55     return ERR_OK;
56 }
57 
GetCurrentActiveUserId(int32_t & id)58 ErrCode OsAccountManagerHelper::GetCurrentActiveUserId(int32_t &id)
59 {
60     int32_t ret = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(id);
61     if (ret != ERR_OK) {
62         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_4)
63             .Message("Get foreground os account failed ret " + std::to_string(ret));
64         NotificationAnalyticsUtil::ReportModifyEvent(message);
65         ANS_LOGE("Failed to call OsAccountManager::GetForegroundOsAccountLocalId, code is %{public}d", ret);
66     }
67     return ret;
68 }
69 
GetAllOsAccount(std::vector<int32_t> & userIds)70 ErrCode OsAccountManagerHelper::GetAllOsAccount(std::vector<int32_t> &userIds)
71 {
72     std::vector<AccountSA::OsAccountInfo> accounts;
73     int32_t ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accounts);
74     if (ret != ERR_OK) {
75         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_3)
76             .Message("Get all userId failed ret " + std::to_string(ret));
77         NotificationAnalyticsUtil::ReportModifyEvent(message);
78         ANS_LOGE("Failed to call OsAccountManager::QueryAllCreatedOsAccounts, code is %{public}d", ret);
79         return ret;
80     }
81     for (auto item : accounts) {
82         userIds.emplace_back(item.GetLocalId());
83     }
84     return ret;
85 }
86 
GetAllActiveOsAccount(std::vector<int32_t> & userIds)87 ErrCode OsAccountManagerHelper::GetAllActiveOsAccount(std::vector<int32_t> &userIds)
88 {
89     int32_t ret = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(userIds);
90     if (ret != ERR_OK) {
91         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_4)
92             .Message("Get all active failed ret " + std::to_string(ret));
93         NotificationAnalyticsUtil::ReportModifyEvent(message);
94         ANS_LOGE("Failed to call OsAccountManager::QueryActiveOsAccountIds, code is %{public}d", ret);
95     }
96     return ret;
97 }
98 
CheckUserExists(const int32_t & userId)99 bool OsAccountManagerHelper::CheckUserExists(const int32_t &userId)
100 {
101     bool isAccountExists = false;
102     int32_t ret = OHOS::AccountSA::OsAccountManager::IsOsAccountExists(userId, isAccountExists);
103     if (ret != ERR_OK) {
104         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_5)
105             .Message("Get all exist failed ret " + std::to_string(ret));
106         NotificationAnalyticsUtil::ReportModifyEvent(message);
107         ANS_LOGE("Failed to call OsAccountManager::IsOsAccountExists, code is %{public}d", ret);
108         return false;
109     }
110     ANS_LOGD("Call IsOsAccountExists Success, user = %{public}d userExists = %{public}d", userId, isAccountExists);
111     return isAccountExists;
112 }
113 
GetInstance()114 OsAccountManagerHelper &OsAccountManagerHelper::GetInstance()
115 {
116     return DelayedRefSingleton<OsAccountManagerHelper>::GetInstance();
117 }
118 
IsSystemAccount(int32_t userId)119 bool OsAccountManagerHelper::IsSystemAccount(int32_t userId)
120 {
121     return userId >= AccountSA::Constants::START_USER_ID && userId <= AccountSA::Constants::MAX_USER_ID;
122 }
123 }
124 }
125