• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "admin.h"
23 #include "ent_info.h"
24 #include "edm_permission.h"
25 #include "json/json.h"
26 #include "permission_manager.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     ErrCode GetReqPermission(const std::vector<std::string> &permissions, std::vector<EdmPermission> &edmPermissions);
34     bool GetAdminByUserId(int32_t userId, std::vector<std::shared_ptr<Admin>> &userAdmin);
35     void GetAdminBySubscribeEvent(ManagedEvent event,
36         std::unordered_map<int32_t, std::vector<std::shared_ptr<Admin>>> &subscribeAdmins);
37     std::shared_ptr<Admin> GetAdminByPkgName(const std::string &packageName, int32_t userId);
38     ErrCode DeleteAdmin(const std::string &packageName, int32_t userId);
39     ErrCode UpdateAdmin(AppExecFwk::ExtensionAbilityInfo &abilityInfo, const std::vector<std::string> &permissions,
40         int32_t userId);
41     ErrCode GetGrantedPermission(AppExecFwk::ExtensionAbilityInfo &abilityInfo, std::vector<std::string> &permissions,
42         AdminType type);
43     bool IsSuperAdminExist();
44     bool IsSuperAdmin(const std::string &bundleName);
45     void GetEnabledAdmin(AdminType role, std::vector<std::string> &packageNameList, int32_t userId);
46     void Init();
47     void RestoreAdminFromFile();
48     ErrCode SetAdminValue(AppExecFwk::ExtensionAbilityInfo &abilityInfo, EntInfo &entInfo, AdminType role,
49         std::vector<std::string> &permissions, int32_t userId);
50     ErrCode GetEntInfo(const std::string &packageName, EntInfo &entInfo, int32_t userId);
51     ErrCode SetEntInfo(const std::string &packageName, EntInfo &entInfo, int32_t userId);
52     void SaveSubscribeEvents(const std::vector<uint32_t> &events, std::shared_ptr<Admin> &admin, int32_t userId);
53     void RemoveSubscribeEvents(const std::vector<uint32_t> &events, std::shared_ptr<Admin> &admin, int32_t userId);
54     virtual ~AdminManager();
55 
56 private:
57     AdminManager();
58     void SaveAdmin(int32_t userId);
59     void ReadJsonAdminType(Json::Value &admin, std::vector<std::shared_ptr<Admin>> &adminVector);
60     void ReadJsonAdmin(const std::string &filePath, int32_t userId);
61     void WriteJsonAdminType(std::shared_ptr<Admin> &enabledAdmin, Json::Value &tree);
62     void WriteJsonAdmin(const std::string &filePath, int32_t userId);
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