1 /*
2  * Copyright (c) 2023 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 "add_os_account_plugin.h"
17 
18 #include "account_info.h"
19 #include "edm_ipc_interface_code.h"
20 #include "edm_utils.h"
21 #include "ohos_account_kits.h"
22 #include "os_account_info.h"
23 #include "os_account_manager.h"
24 #include "want.h"
25 #include "plugin_manager.h"
26 
27 namespace OHOS {
28 namespace EDM {
29 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(AddOsAccountPlugin::GetPlugin());
30 
InitPlugin(std::shared_ptr<IPluginTemplate<AddOsAccountPlugin,std::map<std::string,std::string>>> ptr)31 void AddOsAccountPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<AddOsAccountPlugin,
32     std::map<std::string, std::string>>> ptr)
33 {
34     EDMLOGI("AddOsAccountPlugin InitPlugin...");
35     ptr->InitAttribute(EdmInterfaceCode::ADD_OS_ACCOUNT, "add_os_account",
36         "ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
37     ptr->SetSerializer(MapStringSerializer::GetInstance());
38     ptr->SetOnHandlePolicyListener(&AddOsAccountPlugin::OnSetPolicy, FuncOperateType::SET);
39 }
40 
OnSetPolicy(std::map<std::string,std::string> & data,MessageParcel & reply)41 ErrCode AddOsAccountPlugin::OnSetPolicy(std::map<std::string, std::string> &data, MessageParcel &reply)
42 {
43     EDMLOGI("AddOsAccountPlugin OnSetPolicy");
44     auto it = data.begin();
45     if (it == data.end()) {
46         return ERR_OK;
47     }
48     std::string accountName = it -> first;
49     int32_t type = -1;
50     ErrCode parseRet = EdmUtils::ParseStringToInt(it -> second, type);
51     if (FAILED(parseRet)) {
52         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
53         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
54     }
55     OHOS::AccountSA::OsAccountType accountType = ParseOsAccountType(type);
56     if (accountType == OHOS::AccountSA::OsAccountType::END) {
57         EDMLOGE("AddOsAccountPlugin accountType invalid");
58         return EdmReturnErrCode::PARAM_ERROR;
59     }
60     EDMLOGI("AddOsAccountPlugin::CreateOsAccount: name.len -- %{public}zu, type -- %{public}d",
61         accountName.length(), type);
62     OHOS::AccountSA::OsAccountInfo accountInfo;
63     ErrCode ret = externalManagerFactory_->CreateOsAccountManager()->CreateOsAccount(accountName,
64         accountType, accountInfo);
65     if (FAILED(ret)) {
66         EDMLOGE("AddOsAccountPlugin CreateOsAccount failed");
67         reply.WriteInt32(EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED);
68         return EdmReturnErrCode::ADD_OS_ACCOUNT_FAILED;
69     }
70     std::pair<bool, OHOS::AccountSA::OhosAccountInfo> dbAccountInfo = OHOS::AccountSA::OhosAccountKits::GetInstance()
71         .QueryOhosAccountInfo();
72     if (!dbAccountInfo.first) {
73         EDMLOGE("AddOsAccountPlugin::QueryOhosAccountInfo failed.");
74         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
75     }
76     std::string distributedInfoName = dbAccountInfo.second.name_;
77     std::string distributedInfoId = dbAccountInfo.second.uid_;
78     if (distributedInfoName.empty() || distributedInfoId.empty()) {
79         EDMLOGE("AddOsAccountPlugin::QueryOhosAccountInfo empty.");
80         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
81     }
82     reply.WriteInt32(ERR_OK);
83     accountInfo.Marshalling(reply);
84     reply.WriteString(distributedInfoName);
85     reply.WriteString(distributedInfoId);
86     EDMLOGI("AddOsAccountPlugin OnSetPolicy end");
87     return ERR_OK;
88 }
89 
ParseOsAccountType(int32_t type)90 OHOS::AccountSA::OsAccountType AddOsAccountPlugin::ParseOsAccountType(int32_t type)
91 {
92     if (type >= static_cast<int32_t>(OHOS::AccountSA::OsAccountType::ADMIN)
93         && type < static_cast<int32_t>(OHOS::AccountSA::OsAccountType::END)) {
94         return static_cast<OHOS::AccountSA::OsAccountType>(type);
95     }
96     return OHOS::AccountSA::OsAccountType::END;
97 }
98 } // namespace EDM
99 } // namespace OHOS
100