• 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 #include "session_manager.h"
17 
18 #define LOG_TAG "SessionManager"
19 
20 #include <algorithm>
21 
22 #include "auth_delegate.h"
23 #include "checker/checker_manager.h"
24 #include "log_print.h"
25 #include "metadata/meta_data_manager.h"
26 #include "metadata/store_meta_data.h"
27 #include "user_delegate.h"
28 #include "utils/anonymous.h"
29 #include "utils/converter.h"
30 
31 namespace OHOS::DistributedData {
32 using namespace OHOS::DistributedKv;
GetInstance()33 SessionManager &SessionManager::GetInstance()
34 {
35     static SessionManager instance;
36     return instance;
37 }
38 
GetSession(const SessionPoint & from,const std::string & targetDeviceId) const39 Session SessionManager::GetSession(const SessionPoint &from, const std::string &targetDeviceId) const
40 {
41     ZLOGD("begin. peer device:%{public}s", Anonymous::Change(targetDeviceId).c_str());
42     Session session;
43     session.appId = from.appId;
44     session.sourceUserId = from.userId;
45     session.sourceDeviceId = from.deviceId;
46     session.targetDeviceId = targetDeviceId;
47     auto users = UserDelegate::GetInstance().GetRemoteUserStatus(targetDeviceId);
48     // system service
49     if (from.userId == UserDelegate::SYSTEM_USER) {
50         StoreMetaData metaData;
51         metaData.deviceId = from.deviceId;
52         metaData.user = std::to_string(from.userId);
53         metaData.bundleName = from.appId;
54         metaData.storeId = from.storeId;
55         if (MetaDataManager::GetInstance().LoadMeta(metaData.GetKey(), metaData) &&
56             CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(metaData)) == from.appId) {
57             session.targetUserIds.push_back(UserDelegate::SYSTEM_USER);
58         }
59     }
60 
61     for (const auto &user : users) {
62         bool isPermitted = AuthDelegate::GetInstance()->CheckAccess(from.userId, user.id, targetDeviceId, from.appId);
63         ZLOGD("access to peer user %{public}d is %{public}d", user.id, isPermitted);
64         if (isPermitted) {
65             auto it = std::find(session.targetUserIds.begin(), session.targetUserIds.end(), user.id);
66             if (it == session.targetUserIds.end()) {
67                 session.targetUserIds.push_back(user.id);
68             }
69         }
70     }
71     ZLOGD("end");
72     return session;
73 }
74 
CheckSession(const SessionPoint & from,const SessionPoint & to) const75 bool SessionManager::CheckSession(const SessionPoint &from, const SessionPoint &to) const
76 {
77     return AuthDelegate::GetInstance()->CheckAccess(from.userId, to.userId, to.deviceId, from.appId);
78 }
79 
Marshal(json & node) const80 bool Session::Marshal(json &node) const
81 {
82     bool ret = true;
83     ret = SetValue(node[GET_NAME(sourceDeviceId)], sourceDeviceId) && ret;
84     ret = SetValue(node[GET_NAME(targetDeviceId)], targetDeviceId) && ret;
85     ret = SetValue(node[GET_NAME(sourceUserId)], sourceUserId) && ret;
86     ret = SetValue(node[GET_NAME(targetUserIds)], targetUserIds) && ret;
87     ret = SetValue(node[GET_NAME(appId)], appId) && ret;
88     return ret;
89 }
90 
Unmarshal(const json & node)91 bool Session::Unmarshal(const json &node)
92 {
93     bool ret = true;
94     ret = GetValue(node, GET_NAME(sourceDeviceId), sourceDeviceId) && ret;
95     ret = GetValue(node, GET_NAME(targetDeviceId), targetDeviceId) && ret;
96     ret = GetValue(node, GET_NAME(sourceUserId), sourceUserId) && ret;
97     ret = GetValue(node, GET_NAME(targetUserIds), targetUserIds) && ret;
98     ret = GetValue(node, GET_NAME(appId), appId) && ret;
99     return ret;
100 }
101 } // namespace OHOS::DistributedData
102