1 /*
2 * Copyright (c) 2022-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 "permission_manager.h"
17 #include "edm_log.h"
18
19 namespace OHOS {
20 namespace EDM {
PermissionManager()21 PermissionManager::PermissionManager() {}
22
~PermissionManager()23 PermissionManager::~PermissionManager()
24 {
25 permissions_.clear();
26 }
27
AddPermission(const std::string & permission,IPlugin::PermissionType permissionType)28 ErrCode PermissionManager::AddPermission(const std::string &permission, IPlugin::PermissionType permissionType)
29 {
30 if (permission.empty()) {
31 return ERR_OK;
32 }
33 if (static_cast<std::int32_t>(permissionType) <
34 static_cast<std::int32_t>(IPlugin::PermissionType::NORMAL_DEVICE_ADMIN) ||
35 static_cast<std::int32_t>(permissionType) >= static_cast<std::int32_t>(IPlugin::PermissionType::UNKNOWN)) {
36 EDMLOGE("AddPermission::unknow permission type");
37 return ERR_EDM_UNKNOWN_PERMISSION;
38 }
39 auto entry = permissions_.find(permission);
40 if (entry == permissions_.end()) {
41 AdminPermission adminPermission(permission, static_cast<AdminType>(permissionType));
42 permissions_.insert(std::make_pair(permission, adminPermission));
43 EDMLOGI("AddPermission::insert permission : %{public}s permissionType : %{public}d",
44 permission.c_str(), static_cast<int32_t>(permissionType));
45 } else if (entry->second.adminType != static_cast<AdminType>(permissionType)) {
46 EDMLOGE("AddPermission::conflict permission type");
47 return ERR_EDM_DENY_PERMISSION;
48 } else {
49 EDMLOGI("AddPermission::same permission has been added : %{public}s", permission.c_str());
50 }
51 EDMLOGD("AddPermission::return ok");
52 return ERR_OK;
53 }
54
GetReqPermission(const std::vector<std::string> & permissions,std::vector<AdminPermission> & reqPermission)55 void PermissionManager::GetReqPermission(const std::vector<std::string> &permissions,
56 std::vector<AdminPermission> &reqPermission)
57 {
58 reqPermission.clear();
59 if (permissions.empty()) {
60 return;
61 }
62
63 for (const auto &item : permissions) {
64 auto entry = permissions_.find(item);
65 if (entry != permissions_.end()) {
66 reqPermission.emplace_back(entry->second);
67 }
68 }
69 }
70
GetReqPermission(const std::vector<std::string> & permissions,std::vector<EdmPermission> & reqPermission)71 void PermissionManager::GetReqPermission(const std::vector<std::string> &permissions,
72 std::vector<EdmPermission> &reqPermission)
73 {
74 reqPermission.clear();
75 for (const auto &item : permissions) {
76 auto entry = permissions_.find(item);
77 if (entry != permissions_.end()) {
78 EdmPermission edmPermission(entry->second.permissionName, entry->second.adminType);
79 reqPermission.emplace_back(edmPermission);
80 }
81 }
82 }
83 } // namespace EDM
84 } // namespace OHOS
85