1 /*
2 * Copyright (c) 2023-2023 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 "publish_event_adapter.h"
17
18 #include "common_event_manager.h"
19 #include "iam_logger.h"
20
21 #ifndef LOG_LABEL
22 #define LOG_TAG "USER_AUTH_SA"
23 namespace OHOS {
24 namespace UserIam {
25 namespace UserAuth {
26 namespace {
27 const std::string TAG_USERID = "userId";
28 const std::string TAG_AUTHTYPE = "authType";
29 const std::string TAG_CREDENTIALCOUNT = "credentialCount";
30 const std::string TAG_SCHEDULEID = "scheduleId";
31 const std::string USER_PIN_CREATED_EVENT = "USER_PIN_CREATED_EVENT";
32 const std::string USER_PIN_DELETED_EVENT = "USER_PIN_DELETED_EVENT";
33 const std::string USER_PIN_UPDATED_EVENT = "USER_PIN_UPDATED_EVENT";
34 const std::string USER_CREDENTIAL_UPDATED_EVENT = "USER_CREDENTIAL_UPDATED_EVENT";
35 const std::string USERIAM_COMMON_EVENT_PERMISSION = "ohos.permission.USE_USER_IDM";
36 const std::string USERIAM_COMMON_EVENT_SAMGR_PERMISSION = "ohos.permission.INTERACT_ACROSS_LOCAL_ACCOUNTS";
37
PublishEvent(EventFwk::CommonEventData data,const std::string permission)38 void PublishEvent(EventFwk::CommonEventData data, const std::string permission)
39 {
40 EventFwk::CommonEventPublishInfo publishInfo;
41 std::vector<std::string> permissions;
42 permissions.push_back(permission);
43 publishInfo.SetSubscriberPermissions(permissions);
44 publishInfo.SetSticky(false);
45 if (!EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo)) {
46 IAM_LOGE("PublishCommonEvent failed, eventAction is %{public}s", data.GetWant().GetAction().c_str());
47 return;
48 }
49 IAM_LOGI("PublishCommonEvent succeed, eventAction is %{public}s", data.GetWant().GetAction().c_str());
50 }
51 } // namespace
52
GetInstance()53 PublishEventAdapter &PublishEventAdapter::GetInstance()
54 {
55 static PublishEventAdapter instance;
56 return instance;
57 }
58
PublishDeletedEvent(int32_t userId)59 void PublishEventAdapter::PublishDeletedEvent(int32_t userId)
60 {
61 EventFwk::Want want;
62 want.SetAction(USER_PIN_DELETED_EVENT);
63 EventFwk::CommonEventData data(want);
64 data.SetCode(userId);
65 PublishEvent(data, USERIAM_COMMON_EVENT_SAMGR_PERMISSION);
66 return;
67 }
68
PublishCreatedEvent(int32_t userId,uint64_t scheduleId)69 void PublishEventAdapter::PublishCreatedEvent(int32_t userId, uint64_t scheduleId)
70 {
71 if (scheduleId == 0) {
72 IAM_LOGE("Bad Parameter!");
73 return;
74 }
75 EventFwk::Want want;
76 want.SetAction(USER_PIN_CREATED_EVENT);
77 want.SetParam(TAG_SCHEDULEID, std::to_string(scheduleId));
78 EventFwk::CommonEventData data(want);
79 data.SetCode(userId);
80 PublishEvent(data, USERIAM_COMMON_EVENT_SAMGR_PERMISSION);
81 return;
82 }
83
PublishUpdatedEvent(int32_t userId,uint64_t credentialId)84 void PublishEventAdapter::PublishUpdatedEvent(int32_t userId, uint64_t credentialId)
85 {
86 std::lock_guard<std::mutex> lock(mutex_);
87 if (userId != userId_ || credentialId != credentialId_) {
88 IAM_LOGE("Bad Parameter!");
89 return;
90 }
91 EventFwk::Want want;
92 want.SetAction(USER_PIN_UPDATED_EVENT);
93 want.SetParam(TAG_SCHEDULEID, std::to_string(scheduleId_));
94 EventFwk::CommonEventData data(want);
95 data.SetCode(userId);
96 PublishEvent(data, USERIAM_COMMON_EVENT_SAMGR_PERMISSION);
97 return;
98 }
99
CachePinUpdateParam(int32_t userId,uint64_t scheduleId,uint64_t credentialId)100 void PublishEventAdapter::CachePinUpdateParam(int32_t userId, uint64_t scheduleId, uint64_t credentialId)
101 {
102 std::lock_guard<std::mutex> lock(mutex_);
103 userId_ = userId;
104 scheduleId_ = scheduleId;
105 credentialId_ = credentialId;
106 return;
107 }
108
PublishCredentialUpdatedEvent(int32_t userId,int32_t authType,uint32_t credentialCount)109 void PublishEventAdapter::PublishCredentialUpdatedEvent(int32_t userId, int32_t authType, uint32_t credentialCount)
110 {
111 EventFwk::Want want;
112 want.SetAction(USER_CREDENTIAL_UPDATED_EVENT);
113 want.SetParam(TAG_USERID, std::to_string(userId));
114 want.SetParam(TAG_AUTHTYPE, std::to_string(authType));
115 want.SetParam(TAG_CREDENTIALCOUNT, std::to_string(credentialCount));
116 EventFwk::CommonEventData data(want);
117 data.SetCode(0);
118 PublishEvent(data, USERIAM_COMMON_EVENT_PERMISSION);
119 IAM_LOGI("PublishCredentialUpdatedEvent, userId: %{public}d, authType: %{public}d, credentialCount: %{public}u",
120 userId, authType, credentialCount);
121 return;
122 }
123 } // namespace UserAuth
124 } // namespace UserIam
125 } // namespace OHOS
126 #endif