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