1 /*
2 * Copyright (c) 2025 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 "set_domain_account_policy_plugin.h"
17
18 #include "domain_account_client.h"
19 #include "domain_account_common.h"
20 #include "domain_account_policy.h"
21 #include "edm_ipc_interface_code.h"
22 #include "edm_log.h"
23 #include "func_code_utils.h"
24 #include "iplugin_manager.h"
25
26 namespace OHOS {
27 namespace EDM {
28 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(std::make_shared<SetDomainAccountPolicyPlugin>());
29
SetDomainAccountPolicyPlugin()30 SetDomainAccountPolicyPlugin::SetDomainAccountPolicyPlugin()
31 {
32 policyCode_ = EdmInterfaceCode::DOMAIN_ACCOUNT_POLICY;
33 policyName_ = "set_domain_account_policy";
34 permissionConfig_.typePermissions.emplace(IPlugin::PermissionType::SUPER_DEVICE_ADMIN,
35 "ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY");
36 permissionConfig_.apiType = IPlugin::ApiType::PUBLIC;
37 needSave_ = false;
38 }
39
OnHandlePolicy(std::uint32_t funcCode,MessageParcel & data,MessageParcel & reply,HandlePolicyData & policyData,int32_t userId)40 ErrCode SetDomainAccountPolicyPlugin::OnHandlePolicy(std::uint32_t funcCode, MessageParcel &data, MessageParcel &reply,
41 HandlePolicyData &policyData, int32_t userId)
42 {
43 uint32_t typeCode = FUNC_TO_OPERATE(funcCode);
44 FuncOperateType type = FuncCodeUtils::ConvertOperateType(typeCode);
45 if (type == FuncOperateType::SET) {
46 return SetPolicy(data);
47 }
48 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
49 }
50
SetPolicy(MessageParcel & data)51 ErrCode SetDomainAccountPolicyPlugin::SetPolicy(MessageParcel &data)
52 {
53 OHOS::AccountSA::DomainAccountInfo domainAccountInfo;
54 if (!domainAccountInfo.ReadFromParcel(data)) {
55 EDMLOGE("SetDomainAccountPolicyPlugin domainAccountInfo ReadFromParcel error");
56 return EdmReturnErrCode::PARAM_ERROR;
57 }
58 DomainAccountPolicy domainAccountPolicy;
59 if (!DomainAccountPolicy::Unmarshalling(data, domainAccountPolicy)) {
60 EDMLOGE("SetDomainAccountPolicyPlugin domainAccountpolicy Unmarshalling error");
61 return EdmReturnErrCode::PARAM_ERROR;
62 }
63 if (!domainAccountPolicy.CheckParameterValidity()) {
64 EDMLOGE("SetDomainAccountPolicyPlugin domainAccountpolicy param invalid");
65 return EdmReturnErrCode::PARAM_ERROR;
66 }
67 std::string policy;
68 if (!domainAccountPolicy.ConvertDomainAccountPolicyToJsonStr(policy)) {
69 EDMLOGE("SetDomainAccountPolicyPlugin ConvertDomainAccountPolicyToJsonStr error, should not happen");
70 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
71 }
72 int32_t ret = OHOS::AccountSA::DomainAccountClient::GetInstance().SetAccountPolicy(domainAccountInfo, policy);
73 if (FAILED(ret)) {
74 EDMLOGE("SetDomainAccountPolicyPlugin SetAccountPolicy error");
75 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
76 }
77 return ERR_OK;
78 }
79
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)80 ErrCode SetDomainAccountPolicyPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
81 int32_t userId)
82 {
83 OHOS::AccountSA::DomainAccountInfo domainAccountInfo;
84 if (!domainAccountInfo.ReadFromParcel(data)) {
85 EDMLOGE("SetDomainAccountPolicyPlugin::OnGetPolicy domainAccountInfo ReadFromParcel error");
86 reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
87 return EdmReturnErrCode::PARAM_ERROR;
88 }
89 std::string policy;
90 int32_t ret = OHOS::AccountSA::DomainAccountClient::GetInstance().GetAccountPolicy(domainAccountInfo, policy);
91 if (FAILED(ret)) {
92 EDMLOGE("SetDomainAccountPolicyPlugin::OnGetPolicy GetAccountPolicy error");
93 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
94 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
95 }
96 DomainAccountPolicy domainAccountPolicy;
97 if (!DomainAccountPolicy::JsonStrToDomainAccountPolicy(policy, domainAccountPolicy)) {
98 EDMLOGE("SetDomainAccountPolicyPlugin::OnGetPolicy JsonStrToDomainAccountPolicy error");
99 reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
100 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
101 }
102 reply.WriteInt32(ERR_OK);
103 if (!domainAccountPolicy.Marshalling(reply)) {
104 EDMLOGE("SetDomainAccountPolicyPlugin::OnGetPolicy Marshalling error");
105 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
106 }
107 return ERR_OK;
108 }
109 } // namespace EDM
110 } // namespace OHOS