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 #ifndef SERVICES_EDM_INCLUDE_EDM_POLICY_MANAGER_H 17 #define SERVICES_EDM_INCLUDE_EDM_POLICY_MANAGER_H 18 19 #include <memory> 20 #include <mutex> 21 #include <string> 22 #include <unordered_map> 23 #include "device_policies_storage_rdb.h" 24 #include "edm_errors.h" 25 #include "json/json.h" 26 #include "ipolicy_manager.h" 27 28 namespace OHOS { 29 namespace EDM { 30 using PolicyItemsMap = std::unordered_map<std::string, std::string>; /* PolicyName and PolicyValue pair */ 31 using AdminValueItemsMap = std::unordered_map<std::string, std::string>; /* AdminName and PolicyValue pair */ 32 33 /* 34 * This class is used to load and store /data/service/el1/public/edm/device_policies.json file. 35 * provide the Get and Set api to operate on json file, the read and write json 36 * file depend on jsoncpp library 37 */ 38 class PolicyManager : public IPolicyManager { 39 public: 40 PolicyManager(int32_t userId); 41 42 void SetUserIdState(int32_t userId); 43 44 /* 45 * This function is used to get all policy items of an admin, an admin represent an EDM application 46 * 47 * @param adminName the application's bundle name 48 * @param allAdminPolicy the all policy item packaged in std::unordered_map 49 * @return return thr ErrCode of this function 50 */ 51 ErrCode GetAllPolicyByAdmin(const std::string &adminName, PolicyItemsMap &allAdminPolicy); 52 53 /* 54 * This function is used to get policy items by admin name policy name 55 * If the adminName is null, will get the combined policy, otherwise will 56 * get the admin policy 57 * 58 * @param adminName the application's bundle name 59 * @param policyName the policy item name 60 * @param policyValue the policy value which the caller wanted to get 61 * @return return thr ErrCode of this function 62 */ 63 ErrCode GetPolicy(const std::string &adminName, const std::string &policyName, std::string &policyValue); 64 65 /* 66 * This function is used to set policy items by admin name policy name. If the adminName is null, 67 * will set the combined policy. If the policyName is null, will set the admin policy, otherwise will 68 * set both the admin policy and merged policy, if the policy value is null, the policy item will be 69 * deleted, this function will write json file. write merged policy and admin policy simultaneously 70 * is very useful for atomic operation 71 * 72 * @param adminName the application's bundle name 73 * @param policyName the policy item name 74 * @param adminPolicyValue the admin policy value which the caller wanted to set 75 * @param mergedPolicyValue the merged policy value which the caller wanted to set 76 * @return return thr ErrCode of this function 77 */ 78 ErrCode SetPolicy(const std::string &adminName, const std::string &policyName, const std::string &adminPolicyValue, 79 const std::string &mergedPolicyValue); 80 81 /* 82 * This function is used to get admin name by policy name, then the caller will know 83 * which application set the policy 84 * 85 * @param policyName the policy item name 86 * @param adminValueItems the all admin name and policy value packaged in std::unordered_map 87 * @return return thr ErrCode of this function 88 */ 89 ErrCode GetAdminByPolicyName(const std::string &policyName, AdminValueItemsMap &adminValueItems); 90 91 /* 92 * This function is used to init the PolicyManager, must be called before any of other api 93 * init function will read and parse json file and construct some std::unordered_map to 94 * provide get and set operation 95 */ 96 void Init(); 97 98 /* 99 * This function is debug api used to print all admin policy 100 */ 101 void DumpAdminPolicy(); 102 103 /* 104 * This function is debug api used to print all admin list 105 */ 106 void DumpAdminList(); 107 108 /* 109 * This function is debug api used to print all combined policy 110 */ 111 void DumpCombinedPolicy(); 112 113 virtual ~PolicyManager(); 114 115 private: 116 PolicyManager(); 117 ErrCode DeleteAdminPolicy(const std::string &adminName, const std::string &policyName); 118 ErrCode DeleteCombinedPolicy(const std::string &policyName); 119 ErrCode GetAdminPolicy(const std::string &adminName, const std::string &policyName, std::string &policyValue); 120 ErrCode GetCombinedPolicy(const std::string &policyName, std::string &policyValue); 121 ErrCode SetAdminPolicy(const std::string &adminName, const std::string &policyName, const std::string &policyValue); 122 ErrCode SetCombinedPolicy(const std::string &policyName, const std::string &policyValue); 123 void DeleteAdminList(const std::string &adminName, const std::string &policyName); 124 void SetAdminList(const std::string &adminName, const std::string &policyName, const std::string &policyValue); 125 126 /* 127 * This member is the combined policy and combined value pair 128 */ 129 PolicyItemsMap combinedPolicies_; 130 131 /* 132 * This member is the admin name and policyName, policyValue pairs 133 */ 134 std::unordered_map<std::string, PolicyItemsMap> adminPolicies_; 135 136 /* 137 * This member is the policy name and adminName, policyValue pairs 138 */ 139 std::unordered_map<std::string, AdminValueItemsMap> policyAdmins_; 140 141 int32_t userIdState_ = 100; 142 }; 143 } // namespace EDM 144 } // namespace OHOS 145 146 #endif // SERVICES_EDM_INCLUDE_EDM_POLICY_MANAGER_H 147