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 "os_account_domain_account_callback.h"
17
18 #include "account_error_no.h"
19 #include "account_event_provider.h"
20 #include "account_log_wrapper.h"
21 #ifdef HAS_CES_PART
22 #include "common_event_support.h"
23 #endif // HAS_CES_PART
24 #include "iinner_os_account_manager.h"
25 #include "ios_account_control.h"
26 #include "os_account_constants.h"
27 #include "os_account_control_file_manager.h"
28
29 namespace OHOS {
30 namespace AccountSA {
CheckAndCreateDomainAccountCallback(const OsAccountType & type,const DomainAccountInfo & domainAccountInfo,const sptr<IDomainAccountCallback> & callback,const CreateOsAccountForDomainOptions & accountOptions)31 CheckAndCreateDomainAccountCallback::CheckAndCreateDomainAccountCallback(
32 const OsAccountType &type, const DomainAccountInfo &domainAccountInfo,
33 const sptr<IDomainAccountCallback> &callback, const CreateOsAccountForDomainOptions &accountOptions)
34 : type_(type), domainAccountInfo_(domainAccountInfo), accountOptions_(accountOptions), innerCallback_(callback)
35 {}
36
OnResult(int32_t errCode,Parcel & parcel)37 void CheckAndCreateDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
38 {
39 if (innerCallback_ == nullptr) {
40 ACCOUNT_LOGE("innerPlugin_ is nullptr");
41 return;
42 }
43 OsAccountInfo osAccountInfo;
44 Parcel resultParcel;
45 osAccountInfo.Marshalling(resultParcel);
46 if (errCode != ERR_OK) {
47 ACCOUNT_LOGE("check domain account failed");
48 return innerCallback_->OnResult(errCode, resultParcel);
49 }
50 std::shared_ptr<AAFwk::WantParams> parameters(AAFwk::WantParams::Unmarshalling(parcel));
51 if (parameters == nullptr) {
52 ACCOUNT_LOGE("parameters unmarshalling error");
53 return innerCallback_->OnResult(ERR_JS_SYSTEM_SERVICE_EXCEPTION, resultParcel);
54 }
55 DomainAccountInfo domainAccountInfo;
56 domainAccountInfo.accountName_ = parameters->GetStringParam("accountName");
57 domainAccountInfo.domain_ = parameters->GetStringParam("domain");
58 domainAccountInfo.accountId_ = parameters->GetStringParam("accountId");
59 domainAccountInfo.serverConfigId_ = parameters->GetStringParam("serverConfigId");
60 if ((domainAccountInfo.accountName_.empty()) || (domainAccountInfo.domain_.empty())) {
61 ACCOUNT_LOGE("domain account not found");
62 return innerCallback_->OnResult(ERR_JS_ACCOUNT_NOT_FOUND, resultParcel);
63 }
64 errCode = IInnerOsAccountManager::GetInstance().BindDomainAccount(type_, domainAccountInfo,
65 innerCallback_, accountOptions_);
66 if (errCode != ERR_OK) {
67 return innerCallback_->OnResult(errCode, resultParcel);
68 }
69 }
70
BindDomainAccountCallback(std::shared_ptr<IOsAccountControl> & osAccountControl,const DomainAccountInfo & domainAccountInfo,const OsAccountInfo & osAccountInfo,const sptr<IDomainAccountCallback> & callback)71 BindDomainAccountCallback::BindDomainAccountCallback(
72 std::shared_ptr<IOsAccountControl> &osAccountControl, const DomainAccountInfo &domainAccountInfo,
73 const OsAccountInfo &osAccountInfo, const sptr<IDomainAccountCallback> &callback)
74 : osAccountControl_(osAccountControl), domainAccountInfo_(domainAccountInfo),
75 osAccountInfo_(osAccountInfo), innerCallback_(callback)
76 {}
77
OnResult(int32_t errCode,Parcel & parcel)78 void BindDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
79 {
80 if (innerCallback_ == nullptr) {
81 ACCOUNT_LOGE("inner callback is nullptr");
82 return;
83 }
84 if (errCode != ERR_OK) {
85 ACCOUNT_LOGE("failed to bind domain account");
86 if (osAccountInfo_.GetLocalId() != Constants::START_USER_ID) {
87 (void)osAccountControl_->DelOsAccount(osAccountInfo_.GetLocalId());
88 }
89 return innerCallback_->OnResult(errCode, parcel);
90 }
91 Parcel resultParcel;
92 if (osAccountInfo_.GetLocalId() != Constants::START_USER_ID) {
93 errCode = IInnerOsAccountManager::GetInstance().SendMsgForAccountCreate(osAccountInfo_);
94 osAccountInfo_.Marshalling(resultParcel);
95 return innerCallback_->OnResult(errCode, resultParcel);
96 }
97 osAccountControl_->UpdateAccountIndex(osAccountInfo_, false);
98 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo_);
99 if ((osAccountInfo_.GetLocalId() == Constants::START_USER_ID) && (errCode == ERR_OK)) {
100 #ifdef HAS_CES_PART
101 AccountEventProvider::EventPublish(
102 EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::START_USER_ID, nullptr);
103 #else // HAS_CES_PART
104 ACCOUNT_LOGI("No common event part! Publish nothing!");
105 #endif // HAS_CES_PART
106 }
107 osAccountInfo_.Marshalling(resultParcel);
108 innerCallback_->OnResult(errCode, resultParcel);
109 }
110 } // namespace AccountSA
111 } // namespace OHOS