1 /*
2 * Copyright (c) 2024-2024 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 "base_publish_process.h"
17
18 #include "access_token_helper.h"
19 #include "ans_const_define.h"
20 #include "ans_log_wrapper.h"
21 #include "ans_inner_errors.h"
22 #include "ans_permission_def.h"
23 #include "os_account_manager_helper.h"
24 #include "ipc_skeleton.h"
25 #include "parameters.h"
26 #include "notification_analytics_util.h"
27 #include "ans_status.h"
28
29 namespace OHOS {
30 namespace Notification {
31
PublishPreWork(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed)32 AnsStatus BasePublishProcess::PublishPreWork(const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed)
33 {
34 if (!request->IsRemoveAllowed()) {
35 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) {
36 request->SetRemoveAllowed(true);
37 }
38 }
39 return AnsStatus();
40 }
41
CommonPublishCheck(const sptr<NotificationRequest> & request)42 ErrCode BasePublishProcess::CommonPublishCheck(const sptr<NotificationRequest> &request)
43 {
44 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_1);
45 if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT) {
46 if (!AccessTokenHelper::IsSystemApp()) {
47 message.Message("Not SystemApp");
48 message.ErrorCode(ERR_ANS_NON_SYSTEM_APP);
49 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
50 return ERR_ANS_NON_SYSTEM_APP;
51 }
52 if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)
53 && !AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SEND_NOTIFICATION_CROSS_USER)) {
54 message.BranchId(EventBranchId::BRANCH_3);
55 message.Message("CheckPermission denied");
56 message.ErrorCode(ERR_ANS_NON_SYSTEM_APP);
57 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
58 return ERR_ANS_PERMISSION_DENIED;
59 }
60 }
61 return ERR_OK;
62 }
63
CommonPublishProcess(const sptr<NotificationRequest> & request)64 ErrCode BasePublishProcess::CommonPublishProcess(const sptr<NotificationRequest> &request)
65 {
66 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
67 if (AccessTokenHelper::IsDlpHap(callerToken)) {
68 ANS_LOGE("DLP hap not allowed to send notifications");
69 HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_5)
70 .ErrorCode(ERR_ANS_DLP_HAP).Message("CommonPublishProcess failed");
71 NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
72 return ERR_ANS_DLP_HAP;
73 }
74 return ERR_OK;
75 }
76 } // namespace Notification
77 } // namespace OHOS
78