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 "default_permission_profile.h"
17
18 #include "app_log_wrapper.h"
19
20 namespace OHOS {
21 namespace AppExecFwk {
22 namespace {
23 static const std::string PERMISSIONS_PROFILE_KEY_BUNDLENAME = "bundleName";
24 static const std::string PERMISSIONS_PROFILE_KEY_PERMISSIONS = "permissions";
25 static const std::string PERMISSIONS_PROFILE_KEY_NAME = "name";
26 static const std::string PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE = "userCancellable";
27 static const std::string PERMISSIONS_PROFILE_KEY_APP_SIGNATURE = "app_signature";
28 }
29 thread_local int32_t parseResult;
30
from_json(const nlohmann::json & jsonObject,PermissionInfo & permissionInfo)31 void from_json(const nlohmann::json &jsonObject, PermissionInfo &permissionInfo)
32 {
33 const auto &jsonObjectEnd = jsonObject.end();
34 GetValueIfFindKey<std::string>(jsonObject,
35 jsonObjectEnd,
36 PERMISSIONS_PROFILE_KEY_NAME,
37 permissionInfo.name,
38 JsonType::STRING,
39 true,
40 parseResult,
41 ArrayType::NOT_ARRAY);
42 GetValueIfFindKey<uint32_t>(jsonObject,
43 jsonObjectEnd,
44 PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE,
45 permissionInfo.userCancellable,
46 JsonType::BOOLEAN,
47 true,
48 parseResult,
49 ArrayType::NOT_ARRAY);
50 }
51
TransformTo(const nlohmann::json & jsonObject,std::set<DefaultPermission> & defaultPermissions) const52 ErrCode DefaultPermissionProfile::TransformTo(const nlohmann::json &jsonObject,
53 std::set<DefaultPermission> &defaultPermissions) const
54 {
55 if (jsonObject.is_array() && !jsonObject.is_discarded()) {
56 for (const auto &object : jsonObject) {
57 if (!object.is_object()) {
58 APP_LOGE("object is not json object");
59 return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
60 }
61 DefaultPermission defaultPermission;
62 const auto &objectEnd = object.end();
63 GetValueIfFindKey<std::string>(object,
64 objectEnd,
65 PERMISSIONS_PROFILE_KEY_BUNDLENAME,
66 defaultPermission.bundleName,
67 JsonType::STRING,
68 true,
69 parseResult,
70 ArrayType::NOT_ARRAY);
71 GetValueIfFindKey<std::vector<std::string>>(object,
72 objectEnd,
73 PERMISSIONS_PROFILE_KEY_APP_SIGNATURE,
74 defaultPermission.appSignature,
75 JsonType::ARRAY,
76 false,
77 parseResult,
78 ArrayType::STRING);
79 GetValueIfFindKey<std::vector<PermissionInfo>>(object,
80 objectEnd,
81 PERMISSIONS_PROFILE_KEY_PERMISSIONS,
82 defaultPermission.grantPermission,
83 JsonType::ARRAY,
84 false,
85 parseResult,
86 ArrayType::OBJECT);
87 if (parseResult != ERR_OK) {
88 APP_LOGE("parseResult is %{public}d", parseResult);
89 int32_t ret = parseResult;
90 // need recover parse result to ERR_OK
91 parseResult = ERR_OK;
92 return ret;
93 }
94
95 auto iter = defaultPermissions.find(defaultPermission);
96 if (iter != defaultPermissions.end()) {
97 APP_LOGD("Replace old defaultPermission(%{public}s)",
98 defaultPermission.bundleName.c_str());
99 defaultPermissions.erase(iter);
100 }
101
102 defaultPermissions.insert(defaultPermission);
103 }
104 }
105 return ERR_OK;
106 }
107 } // namespace AppExecFwk
108 } // namespace OHOS
109
110