1 /*
2  * Copyright (c) 2023-2024 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 OHOS_DP_TRUST_PROFILE_MANAGER_H
17 #define OHOS_DP_TRUST_PROFILE_MANAGER_H
18 
19 
20 #include <map>
21 #include <string>
22 #include <memory>
23 #include <mutex>
24 #include <vector>
25 
26 #include "irdb_adapter.h"
27 #include "access_control_profile.h"
28 #include "trust_device_profile.h"
29 #include "values_bucket.h"
30 #include "single_instance.h"
31 
32 
33 namespace OHOS {
34 namespace DistributedDeviceProfile {
35 using namespace OHOS::NativeRdb;
36 
37 class TrustProfileManager {
38     DECLARE_SINGLE_INSTANCE(TrustProfileManager);
39 
40 public:
41     int32_t Init();
42     int32_t UnInit();
43     int32_t PutTrustDeviceProfile(const TrustDeviceProfile& profile);
44     int32_t PutAccessControlProfile(const AccessControlProfile& profile);
45     int32_t UpdateTrustDeviceProfile(const TrustDeviceProfile& profile);
46     int32_t UpdateAccessControlProfile(const AccessControlProfile& profile);
47     int32_t GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& profile);
48     int32_t GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& profile);
49     int32_t GetAllAccessControlProfile(std::vector<AccessControlProfile>& profile);
50     int32_t GetAccessControlProfile(const std::map<std::string, std::string>& params,
51         std::vector<AccessControlProfile>& profile);
52     int32_t DeleteTrustDeviceProfile(const std::string& deviceId);
53     int32_t DeleteAccessControlProfile(int64_t accessControlId);
54 
55 private:
56     int32_t CreateTable();
57     int32_t CreateUniqueIndex();
58     int32_t GetAccessControlProfile(const std::string& bundleName, int32_t bindType,
59         int32_t status, std::vector<AccessControlProfile>& profile);
60     int32_t GetAccessControlProfile(const std::string& bundleName,
61         const std::string& trustDeviceId, int32_t status, std::vector<AccessControlProfile>& profile);
62     int32_t GetAccessControlProfile(int32_t userId, const std::string& bundleName,
63         int32_t bindType, int32_t status, std::vector<AccessControlProfile>& profile);
64     int32_t GetAccessControlProfile(int32_t userId, const std::string& bundleName,
65         const std::string& trustDeviceId, int32_t status, std::vector<AccessControlProfile>& profile);
66     int32_t GetAccessControlProfile(int32_t userId, const std::string& accountId,
67         std::vector<AccessControlProfile>& profile);
68     int32_t GetAccessControlProfile(int32_t userId, std::vector<AccessControlProfile>& profile);
69     int32_t GetAccessControlProfileByTokenId(int64_t tokenId, const std::string& trustDeviceId,
70         int32_t status, std::vector<AccessControlProfile>& profile);
71     int32_t ConvertToTrustDeviceProfile(const AccessControlProfile& accessControlProfile,
72         TrustDeviceProfile& trustDeviceProfile);
73     int32_t GetAclProfileByUserIdAndBundleName(std::shared_ptr<ResultSet> resultSet,
74         int32_t userId, const std::string& bundleName, std::vector<AccessControlProfile>& profile);
75     int32_t GetAclProfileByUserIdAndAccountId(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
76         int64_t accesseeId, int32_t userId, const std::string& accountId, std::vector<AccessControlProfile>& profile);
77     int32_t GetAclProfileByTokenId(std::shared_ptr<ResultSet> resultSet, const std::string& trustDeviceId,
78         int64_t tokenId, std::vector<AccessControlProfile>& profile);
79     int32_t GetAclProfileByBundleName(std::shared_ptr<ResultSet> resultSet,
80         const std::string& bundleName, std::vector<AccessControlProfile>& profile);
81     int32_t ConvertToAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,
82         std::shared_ptr<ResultSet> accesserResultSet, std::shared_ptr<ResultSet> accesseeResultSet,
83         std::vector<AccessControlProfile>& profile);
84     int32_t PutAccesserProfile(const AccessControlProfile& profile);
85     int32_t PutAccesseeProfile(const AccessControlProfile& profile);
86     int32_t SetAccessControlId(AccessControlProfile& profile);
87     int32_t SetAccesserId(AccessControlProfile& profile);
88     int32_t SetAccesseeId(AccessControlProfile& profile);
89     int32_t UpdateAccesserProfile(int64_t accesserId, const AccessControlProfile& profile);
90     int32_t UpdateAccesseeProfile(int64_t accesseeId, const AccessControlProfile& profile);
91     int32_t UpdateTrustDeviceProfileNotify(const TrustDeviceProfile& oldProfile,
92         const TrustDeviceProfile& newProfile);
93     int32_t GetResultStatus(const std::string& trustDeviceId, int32_t& trustDeviceStatus);
94     int32_t GetAccessControlProfile(std::shared_ptr<ResultSet> resultSet,
95         int64_t accesserId, int64_t accesseeId, std::vector<AccessControlProfile>& profile);
96     int32_t GetAccessControlProfilesByDeviceId(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
97         int64_t accesseeId, const std::string& trustDeviceId, std::vector<AccessControlProfile>& profile);
98     int32_t DeleteAccessControlProfileCheck(std::shared_ptr<ResultSet> resultSet);
99     int32_t ConvertToTrustDeviceProfile(std::shared_ptr<ResultSet> trustResultSet,
100         TrustDeviceProfile& trustDeviceProfile);
101     int32_t ConvertToAccesser(std::shared_ptr<ResultSet> accesserResultSet, Accesser& accesser);
102     int32_t ConvertToAccessee(std::shared_ptr<ResultSet> accesseeResultSet, Accessee& accessee);
103     int32_t ConvertToAccessControlProfile(std::shared_ptr<ResultSet> accessControlResultSet,
104         AccessControlProfile& accessControlProfile);
105     std::shared_ptr<ResultSet> GetResultSet(const std::string& sql, std::vector<ValueObject> condition);
106     int32_t SetAccessControlProfileId(AccessControlProfile& accessControlProfile);
107     int32_t GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
108         int64_t accesseeId, int32_t userId, std::vector<AccessControlProfile>& profile);
109     int32_t GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,
110         int64_t accesserId, int64_t accesseeId, int32_t userId, const std::string& bundleName,
111         std::vector<AccessControlProfile>& profile);
112     int32_t GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
113         int64_t accesseeId, const std::string& bundleName, std::vector<AccessControlProfile>& profile);
114     int32_t GetAccessControlProfilesByTokenId(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
115         int64_t accesseeId, const std::string& trustDeviceId, int64_t tokenId,
116         std::vector<AccessControlProfile>& profile);
117     int32_t DeleteAccesserCheck(int64_t accesserId);
118     int32_t DeleteAccesseeCheck(int64_t accesseeId);
119     int32_t DeleteTrustDeviceCheck(const AccessControlProfile& profile);
120     int32_t UpdateAclCheck(const AccessControlProfile& profile);
121     int32_t PutAclCheck(const AccessControlProfile& profile);
122     int32_t IsAclExists(const AccessControlProfile& profile);
123 
124 private:
125     std::shared_ptr<IRdbAdapter> rdbStore_;
126     std::mutex rdbMutex_;
127 };
128 
129 } // namespace DistributedDeviceProfile
130 } // namespace OHOS
131 
132 #endif // OHOS_DP_TRUST_PROFILE_MANAGER_H
133