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