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