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 
16 #include "constant.h"
17 
18 namespace OHOS {
19 namespace Security {
20 namespace AccessToken {
21 const std::map<std::string, int32_t> Constant::PERMISSION_OPCODE_MAP = {
22     std::map<std::string, int32_t>::value_type("ohos.permission.ANSWER_CALL", Constant::OP_ANSWER_CALL),
23     std::map<std::string, int32_t>::value_type("ohos.permission.READ_CALENDAR", Constant::OP_READ_CALENDAR),
24     std::map<std::string, int32_t>::value_type("ohos.permission.READ_CALL_LOG", Constant::OP_READ_CALL_LOG),
25     std::map<std::string, int32_t>::value_type("ohos.permission.READ_CELL_MESSAGES", Constant::OP_READ_CELL_MESSAGES),
26     std::map<std::string, int32_t>::value_type("ohos.permission.READ_CONTACTS", Constant::OP_READ_CONTACTS),
27     std::map<std::string, int32_t>::value_type("ohos.permission.READ_MESSAGES", Constant::OP_READ_MESSAGES),
28     std::map<std::string, int32_t>::value_type("ohos.permission.RECEIVE_MMS", Constant::OP_RECEIVE_MMS),
29     std::map<std::string, int32_t>::value_type("ohos.permission.RECEIVE_SMS", Constant::OP_RECEIVE_SMS),
30     std::map<std::string, int32_t>::value_type(
31         "ohos.permission.RECEIVE_WAP_MESSAGES", Constant::OP_RECEIVE_WAP_MESSAGES),
32     std::map<std::string, int32_t>::value_type("ohos.permission.MICROPHONE", Constant::OP_MICROPHONE),
33     std::map<std::string, int32_t>::value_type("ohos.permission.SEND_MESSAGES", Constant::OP_SEND_MESSAGES),
34     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_CALENDAR", Constant::OP_WRITE_CALENDAR),
35     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_CALL_LOG", Constant::OP_WRITE_CALL_LOG),
36     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_CONTACTS", Constant::OP_WRITE_CONTACTS),
37     std::map<std::string, int32_t>::value_type(
38         "ohos.permission.DISTRIBUTED_DATASYNC", Constant::OP_DISTRIBUTED_DATASYNC),
39     std::map<std::string, int32_t>::value_type("ohos.permission.MANAGE_VOICEMAIL", Constant::OP_MANAGE_VOICEMAIL),
40     std::map<std::string, int32_t>::value_type(
41         "ohos.permission.LOCATION_IN_BACKGROUND", Constant::OP_LOCATION_IN_BACKGROUND),
42     std::map<std::string, int32_t>::value_type("ohos.permission.LOCATION", Constant::OP_LOCATION),
43     std::map<std::string, int32_t>::value_type(
44         "ohos.permission.APPROXIMATELY_LOCATION", Constant::OP_APPROXIMATELY_LOCATION),
45     std::map<std::string, int32_t>::value_type("ohos.permission.MEDIA_LOCATION", Constant::OP_MEDIA_LOCATION),
46     std::map<std::string, int32_t>::value_type("ohos.permission.CAMERA", Constant::OP_CAMERA),
47     std::map<std::string, int32_t>::value_type("ohos.permission.READ_MEDIA", Constant::OP_READ_MEDIA),
48     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_MEDIA", Constant::OP_WRITE_MEDIA),
49     std::map<std::string, int32_t>::value_type("ohos.permission.ACTIVITY_MOTION", Constant::OP_ACTIVITY_MOTION),
50     std::map<std::string, int32_t>::value_type("ohos.permission.READ_HEALTH_DATA", Constant::OP_READ_HEALTH_DATA),
51     std::map<std::string, int32_t>::value_type("ohos.permission.READ_IMAGEVIDEO", Constant::OP_READ_IMAGEVIDEO),
52     std::map<std::string, int32_t>::value_type("ohos.permission.READ_AUDIO", Constant::OP_READ_AUDIO),
53     std::map<std::string, int32_t>::value_type("ohos.permission.READ_DOCUMENT", Constant::OP_READ_DOCUMENT),
54     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_IMAGEVIDEO", Constant::OP_WRITE_IMAGEVIDEO),
55     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_AUDIO", Constant::OP_WRITE_AUDIO),
56     std::map<std::string, int32_t>::value_type("ohos.permission.WRITE_DOCUMENT", Constant::OP_WRITE_DOCUMENT),
57     std::map<std::string, int32_t>::value_type("ohos.permission.READ_WHOLE_CALENDAR", Constant::OP_READ_WHOLE_CALENDAR),
58     std::map<std::string, int32_t>::value_type(
59         "ohos.permission.WRITE_WHOLE_CALENDAR", Constant::OP_WRITE_WHOLE_CALENDAR),
60     std::map<std::string, int32_t>::value_type(
61         "ohos.permission.APP_TRACKING_CONSENT", Constant::OP_APP_TRACKING_CONSENT),
62     std::map<std::string, int32_t>::value_type(
63         "ohos.permission.GET_INSTALLED_BUNDLE_LIST", Constant::OP_GET_INSTALLED_BUNDLE_LIST),
64     std::map<std::string, int32_t>::value_type("ohos.permission.ACCESS_BLUETOOTH", Constant::OP_ACCESS_BLUETOOTH),
65     std::map<std::string, int32_t>::value_type("ohos.permission.READ_PASTEBOARD", Constant::OP_READ_PASTEBOARD),
66     std::map<std::string, int32_t>::value_type(
67         "ohos.permission.READ_WRITE_DOWNLOAD_DIRECTORY", Constant::OP_READ_WRITE_DOWNLOAD_DIRECTORY),
68     std::map<std::string, int32_t>::value_type(
69         "ohos.permission.READ_WRITE_DOCUMENTS_DIRECTORY", Constant::OP_READ_WRITE_DOCUMENTS_DIRECTORY),
70     std::map<std::string, int32_t>::value_type(
71         "ohos.permission.READ_WRITE_DESKTOP_DIRECTORY", Constant::OP_READ_WRITE_DESKTOP_DIRECTORY),
72     std::map<std::string, int32_t>::value_type(
73         "ohos.permission.ACCESS_NEARLINK", Constant::OP_ACCESS_NEARLINK),
74     std::map<std::string, int32_t>::value_type(
75         "ohos.permission.CAPTURE_SCREEN", Constant::OP_CAPTURE_SCREEN),
76     std::map<std::string, int32_t>::value_type(
77         "ohos.permission.SHORT_TERM_WRITE_IMAGEVIDEO", Constant::SHORT_TERM_WRITE_IMAGEVIDEO),
78     std::map<std::string, int32_t>::value_type(
79         "ohos.permission.CAMERA_BACKGROUND", Constant::CAMERA_BACKGROUND),
80     std::map<std::string, int32_t>::value_type(
81         "ohos.permission.CUSTOM_SCREEN_CAPTURE", Constant::OP_CUSTOM_SCREEN_CAPTURE),
82 };
83 
TransferPermissionToOpcode(const std::string & permissionName,int32_t & opCode)84 bool Constant::TransferPermissionToOpcode(const std::string& permissionName, int32_t& opCode)
85 {
86     if (PERMISSION_OPCODE_MAP.count(permissionName) == 0) {
87         return false;
88     }
89     opCode = PERMISSION_OPCODE_MAP.at(permissionName);
90     return true;
91 }
92 
TransferOpcodeToPermission(int32_t opCode,std::string & permissionName)93 bool Constant::TransferOpcodeToPermission(int32_t opCode, std::string& permissionName)
94 {
95     auto iter = std::find_if(PERMISSION_OPCODE_MAP.begin(), PERMISSION_OPCODE_MAP.end(),
96         [opCode](const std::map<std::string, int32_t>::value_type item) {
97             return item.second == opCode;
98         });
99     if (iter == PERMISSION_OPCODE_MAP.end()) {
100         return false;
101     }
102     permissionName = iter->first;
103     return true;
104 }
105 } // namespace AccessToken
106 } // namespace Security
107 } // namespace OHOS