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 #include "account_manager_proxy.h"
17
18 #include "edm_log.h"
19 #include "func_code.h"
20 #ifdef OS_ACCOUNT_EDM_ENABLE
21 #include "os_account_info.h"
22 #endif
23
24 namespace OHOS {
25 namespace EDM {
26 std::shared_ptr<AccountManagerProxy> AccountManagerProxy::instance_ = nullptr;
27 std::mutex AccountManagerProxy::mutexLock_;
28 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
29
AccountManagerProxy()30 AccountManagerProxy::AccountManagerProxy() {}
31
~AccountManagerProxy()32 AccountManagerProxy::~AccountManagerProxy() {}
33
GetAccountManagerProxy()34 std::shared_ptr<AccountManagerProxy> AccountManagerProxy::GetAccountManagerProxy()
35 {
36 if (instance_ == nullptr) {
37 std::lock_guard<std::mutex> lock(mutexLock_);
38 if (instance_ == nullptr) {
39 std::shared_ptr<AccountManagerProxy> temp = std::make_shared<AccountManagerProxy>();
40 instance_ = temp;
41 }
42 }
43 return instance_;
44 }
45
DisallowAddLocalAccount(AppExecFwk::ElementName & admin,bool isDisallow)46 int32_t AccountManagerProxy::DisallowAddLocalAccount(AppExecFwk::ElementName &admin, bool isDisallow)
47 {
48 #ifdef OS_ACCOUNT_EDM_ENABLE
49 EDMLOGD("AccountManagerProxy::DisallowAddLocalAccount");
50 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
51 return proxy->SetPolicyDisabled(admin, isDisallow, EdmInterfaceCode::DISALLOW_ADD_LOCAL_ACCOUNT);
52 #else
53 EDMLOGW("AccountManagerProxy::DisallowAddLocalAccount Unsupported Capabilities.");
54 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
55 #endif
56 }
57
IsAddLocalAccountDisallowed(AppExecFwk::ElementName * admin,bool & result)58 int32_t AccountManagerProxy::IsAddLocalAccountDisallowed(AppExecFwk::ElementName *admin, bool &result)
59 {
60 #ifdef OS_ACCOUNT_EDM_ENABLE
61 EDMLOGD("AccountManagerProxy::IsAddLocalAccountDisallowed");
62 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
63 return proxy->IsPolicyDisabled(admin, EdmInterfaceCode::DISALLOW_ADD_LOCAL_ACCOUNT, result);
64 #else
65 EDMLOGW("AccountManagerProxy::IsAddLocalAccountDisallowed Unsupported Capabilities.");
66 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
67 #endif
68 }
69
DisallowAddOsAccountByUser(AppExecFwk::ElementName & admin,int32_t userId,bool isDisallow)70 int32_t AccountManagerProxy::DisallowAddOsAccountByUser(AppExecFwk::ElementName &admin, int32_t userId, bool isDisallow)
71 {
72 #ifdef OS_ACCOUNT_EDM_ENABLE
73 EDMLOGD("AccountManagerProxy::DisallowAddOsAccountByUser");
74 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
75 MessageParcel data;
76 std::uint32_t funcCode =
77 POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::DISALLOW_ADD_OS_ACCOUNT_BY_USER);
78 data.WriteInterfaceToken(DESCRIPTOR);
79 data.WriteInt32(WITHOUT_USERID);
80 data.WriteParcelable(&admin);
81 data.WriteString(WITHOUT_PERMISSION_TAG);
82 std::vector<std::string> key {std::to_string(userId)};
83 std::vector<std::string> value {isDisallow ? "true" : "false"};
84 data.WriteStringVector(key);
85 data.WriteStringVector(value);
86 return proxy->HandleDevicePolicy(funcCode, data);
87 #else
88 EDMLOGW("AccountManagerProxy::DisallowAddOsAccountByUser Unsupported Capabilities.");
89 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
90 #endif
91 }
92
IsAddOsAccountByUserDisallowed(AppExecFwk::ElementName * admin,int32_t userId,bool & result)93 int32_t AccountManagerProxy::IsAddOsAccountByUserDisallowed(AppExecFwk::ElementName *admin, int32_t userId,
94 bool &result)
95 {
96 #ifdef OS_ACCOUNT_EDM_ENABLE
97 EDMLOGD("AccountManagerProxy::IsAddOsAccountByUserDisallowed");
98 MessageParcel data;
99 data.WriteInterfaceToken(DESCRIPTOR);
100 data.WriteInt32(WITHOUT_USERID);
101 data.WriteString(WITHOUT_PERMISSION_TAG);
102 if (admin != nullptr) {
103 data.WriteInt32(HAS_ADMIN);
104 data.WriteParcelable(admin);
105 } else {
106 if (!EnterpriseDeviceMgrProxy::GetInstance()->IsEdmEnabled()) {
107 result = false;
108 return ERR_OK;
109 }
110 data.WriteInt32(WITHOUT_ADMIN);
111 }
112 data.WriteInt32(userId);
113 MessageParcel reply;
114 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
115 proxy->GetPolicy(EdmInterfaceCode::DISALLOW_ADD_OS_ACCOUNT_BY_USER, data, reply);
116 int32_t ret = ERR_INVALID_VALUE;
117 bool isSuccess = reply.ReadInt32(ret) && (ret == ERR_OK);
118 if (!isSuccess) {
119 EDMLOGE("IsAddOsAccountByUserDisallowed:GetPolicy fail. %{public}d", ret);
120 return ret;
121 }
122 reply.ReadBool(result);
123 return ERR_OK;
124 #else
125 EDMLOGW("AccountManagerProxy::IsAddOsAccountByUserDisallowed Unsupported Capabilities.");
126 return EdmReturnErrCode::INTERFACE_UNSUPPORTED;
127 #endif
128 }
129
130 #ifdef OS_ACCOUNT_EDM_ENABLE
AddOsAccount(AppExecFwk::ElementName & admin,std::string name,int32_t type,OHOS::AccountSA::OsAccountInfo & accountInfo,std::string & distributedInfoName,std::string & distributedInfoId)131 int32_t AccountManagerProxy::AddOsAccount(AppExecFwk::ElementName &admin, std::string name, int32_t type,
132 OHOS::AccountSA::OsAccountInfo &accountInfo, std::string &distributedInfoName, std::string &distributedInfoId)
133 {
134 EDMLOGD("AccountManagerProxy::AddOsAccount");
135 MessageParcel data;
136 MessageParcel reply;
137 data.WriteInterfaceToken(DESCRIPTOR);
138 data.WriteInt32(WITHOUT_USERID);
139 data.WriteParcelable(&admin);
140 data.WriteString(WITHOUT_PERMISSION_TAG);
141 std::vector<std::string> key {name};
142 std::vector<std::string> value {std::to_string(type)};
143 data.WriteStringVector(key);
144 data.WriteStringVector(value);
145 auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
146 std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::ADD_OS_ACCOUNT);
147 ErrCode ret = proxy->HandleDevicePolicy(funcCode, data, reply);
148 if (ret == ERR_OK) {
149 OHOS::AccountSA::OsAccountInfo *result = OHOS::AccountSA::OsAccountInfo::Unmarshalling(reply);
150 accountInfo = *result;
151 distributedInfoName = reply.ReadString();
152 distributedInfoId = reply.ReadString();
153 }
154 return ret;
155 }
156 #endif
157 } // namespace EDM
158 } // namespace OHOS
159