• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "iplugin_manager.h"
26 
27 namespace OHOS {
28 namespace EDM {
29 const bool REGISTER_RESULT = IPluginManager::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, PolicyName::POLICY_ADD_OS_ACCOUNT,
36         EdmPermission::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     reply.WriteInt32(ERR_OK);
71     accountInfo.Marshalling(reply);
72     EDMLOGI("AddOsAccountPlugin OnSetPolicy end");
73     return ERR_OK;
74 }
75 
ParseOsAccountType(int32_t type)76 OHOS::AccountSA::OsAccountType AddOsAccountPlugin::ParseOsAccountType(int32_t type)
77 {
78     if (type >= static_cast<int32_t>(OHOS::AccountSA::OsAccountType::ADMIN)
79         && type < static_cast<int32_t>(OHOS::AccountSA::OsAccountType::END)) {
80         return static_cast<OHOS::AccountSA::OsAccountType>(type);
81     }
82     return OHOS::AccountSA::OsAccountType::END;
83 }
84 } // namespace EDM
85 } // namespace OHOS
86