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 std::vector<int> activeUserId;
61 int32_t ret = GetAllActiveOsAccount(activeUserId);
62 if (activeUserId.size() > 0) {
63 id = activeUserId[0];
64 }
65 return ret;
66 }
67
GetAllOsAccount(std::vector<int32_t> & userIds)68 ErrCode OsAccountManagerHelper::GetAllOsAccount(std::vector<int32_t> &userIds)
69 {
70 std::vector<AccountSA::OsAccountInfo> accounts;
71 int32_t ret = OHOS::AccountSA::OsAccountManager::QueryAllCreatedOsAccounts(accounts);
72 if (ret != ERR_OK) {
73 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_3)
74 .Message("Get all userId failed ret " + std::to_string(ret));
75 NotificationAnalyticsUtil::ReportModifyEvent(message);
76 ANS_LOGE("Failed to call OsAccountManager::QueryAllCreatedOsAccounts, code is %{public}d", ret);
77 return ret;
78 }
79 for (auto item : accounts) {
80 userIds.emplace_back(item.GetLocalId());
81 }
82 return ret;
83 }
84
GetAllActiveOsAccount(std::vector<int32_t> & userIds)85 ErrCode OsAccountManagerHelper::GetAllActiveOsAccount(std::vector<int32_t> &userIds)
86 {
87 int32_t ret = OHOS::AccountSA::OsAccountManager::QueryActiveOsAccountIds(userIds);
88 if (ret != ERR_OK) {
89 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_4)
90 .Message("Get all active failed ret " + std::to_string(ret));
91 NotificationAnalyticsUtil::ReportModifyEvent(message);
92 ANS_LOGE("Failed to call OsAccountManager::QueryActiveOsAccountIds, code is %{public}d", ret);
93 }
94 return ret;
95 }
96
CheckUserExists(const int32_t & userId)97 bool OsAccountManagerHelper::CheckUserExists(const int32_t &userId)
98 {
99 bool isAccountExists = false;
100 int32_t ret = OHOS::AccountSA::OsAccountManager::IsOsAccountExists(userId, isAccountExists);
101 if (ret != ERR_OK) {
102 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_6, EventBranchId::BRANCH_5)
103 .Message("Get all exist failed ret " + std::to_string(ret));
104 NotificationAnalyticsUtil::ReportModifyEvent(message);
105 ANS_LOGE("Failed to call OsAccountManager::IsOsAccountExists, code is %{public}d", ret);
106 return false;
107 }
108 ANS_LOGD("Call IsOsAccountExists Success, user = %{public}d userExists = %{public}d", userId, isAccountExists);
109 return isAccountExists;
110 }
111
GetInstance()112 OsAccountManagerHelper &OsAccountManagerHelper::GetInstance()
113 {
114 return DelayedRefSingleton<OsAccountManagerHelper>::GetInstance();
115 }
116
IsSystemAccount(int32_t userId)117 bool OsAccountManagerHelper::IsSystemAccount(int32_t userId)
118 {
119 return userId >= AccountSA::Constants::START_USER_ID && userId <= AccountSA::Constants::MAX_USER_ID;
120 }
121 }
122 }
123