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_ADMIN_MANAGER_H
17 #define SERVICES_EDM_INCLUDE_ADMIN_MANAGER_H
18 
19 #include <map>
20 #include <memory>
21 #include <unordered_map>
22 
23 #include "admin.h"
24 #include "admin_policies_storage_rdb.h"
25 #include "ent_info.h"
26 #include "json/json.h"
27 
28 namespace OHOS {
29 namespace EDM {
30 class AdminManager : public std::enable_shared_from_this<AdminManager> {
31 public:
32     static std::shared_ptr<AdminManager> GetInstance();
33     bool GetAdminByUserId(int32_t userId, std::vector<std::shared_ptr<Admin>> &userAdmin);
34     void GetAdminBySubscribeEvent(ManagedEvent event,
35         std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> &subscribeAdmins);
36     std::shared_ptr<Admin> GetAdminByPkgName(const std::string &packageName, int32_t userId);
37     ErrCode DeleteAdmin(const std::string &packageName, int32_t userId);
38     ErrCode UpdateAdmin(std::shared_ptr<Admin> getAdmin, int32_t userId, const Admin &adminItem);
39     bool IsSuperAdminExist();
40     bool IsSuperAdmin(const std::string &bundleName);
41     bool IsAdminExist();
42     bool IsSuperOrSubSuperAdmin(const std::string &bundleName);
43     bool HasPermissionToHandlePolicy(std::shared_ptr<Admin> admin, const std::string &policyName);
44     void GetEnabledAdmin(AdminType role, std::vector<std::string> &packageNameList, int32_t userId);
45     std::shared_ptr<Admin> GetSuperAdmin();
46     void Init();
47     ErrCode SetAdminValue(int32_t userId, const Admin &adminItem);
48     ErrCode GetEntInfo(const std::string &packageName, EntInfo &entInfo, int32_t userId);
49     ErrCode SetEntInfo(const std::string &packageName, EntInfo &entInfo, int32_t userId);
50     ErrCode SaveSubscribeEvents(const std::vector<uint32_t> &events, const std::string &bundleName, int32_t userId);
51     ErrCode RemoveSubscribeEvents(const std::vector<uint32_t> &events, const std::string &bundleName, int32_t userId);
52     ErrCode GetPoliciesByVirtualAdmin(const std::string &bundleName, const std::string &parentName,
53         std::vector<std::string> &policies);
54     void GetVirtualAdminsByPolicy(const std::string &policyName, const std::string &parentName,
55         std::vector<std::string> &bundleNames);
56     ErrCode GetSubOrSuperAdminByPkgName(const std::string &subAdminName, std::shared_ptr<Admin> &subOrSuperAdmin);
57     ErrCode GetSubSuperAdminsByParentName(const std::string &parentName, std::vector<std::string> &subAdmins);
58     ~AdminManager();
59     void Dump();
60 
61 private:
62     AdminManager();
63 
64     std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> admins_;
65     static std::mutex mutexLock_;
66     static std::shared_ptr<AdminManager> instance_;
67 };
68 } // namespace EDM
69 } // namespace OHOS
70 
71 #endif // SERVICES_EDM_INCLUDE_ADMIN_MANAGER_H
72