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 
28 namespace OHOS {
29 namespace Notification {
30 
PublishPreWork(const sptr<NotificationRequest> & request,bool isUpdateByOwnerAllowed)31 ErrCode BasePublishProcess::PublishPreWork(const sptr<NotificationRequest> &request, bool isUpdateByOwnerAllowed)
32 {
33     if (!request->IsRemoveAllowed()) {
34         if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_SET_UNREMOVABLE_NOTIFICATION)) {
35             request->SetRemoveAllowed(true);
36         }
37     }
38     return ERR_OK;
39 }
40 
CommonPublishCheck(const sptr<NotificationRequest> & request)41 ErrCode BasePublishProcess::CommonPublishCheck(const sptr<NotificationRequest> &request)
42 {
43     HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_1);
44     if (request->GetReceiverUserId() != SUBSCRIBE_USER_INIT) {
45         if (!AccessTokenHelper::IsSystemApp()) {
46             message.Message("Not SystemApp");
47             message.ErrorCode(ERR_ANS_NON_SYSTEM_APP);
48             NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
49             return ERR_ANS_NON_SYSTEM_APP;
50         }
51         if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) {
52             message.BranchId(EventBranchId::BRANCH_3);
53             message.Message("CheckPermission denied");
54             message.ErrorCode(ERR_ANS_NON_SYSTEM_APP);
55             NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
56             return ERR_ANS_PERMISSION_DENIED;
57         }
58     }
59     return ERR_OK;
60 }
61 
CommonPublishProcess(const sptr<NotificationRequest> & request)62 ErrCode BasePublishProcess::CommonPublishProcess(const sptr<NotificationRequest> &request)
63 {
64     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
65     if (AccessTokenHelper::IsDlpHap(callerToken)) {
66         ANS_LOGE("DLP hap not allowed to send notifications");
67         HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_2, EventBranchId::BRANCH_5)
68             .ErrorCode(ERR_ANS_DLP_HAP).Message("CommonPublishProcess failed");
69         NotificationAnalyticsUtil::ReportPublishFailedEvent(request, message);
70         return ERR_ANS_DLP_HAP;
71     }
72     return ERR_OK;
73 }
74 }  // namespace Notification
75 }  // namespace OHOS
76