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