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 "os_account_constants.h"
26 #include "os_account_control_file_manager.h"
27
28 namespace OHOS {
29 namespace AccountSA {
CheckAndCreateDomainAccountCallback(const OsAccountType & type,const DomainAccountInfo & domainAccountInfo,const sptr<IDomainAccountCallback> & callback)30 CheckAndCreateDomainAccountCallback::CheckAndCreateDomainAccountCallback(
31 const OsAccountType &type, const DomainAccountInfo &domainAccountInfo, const sptr<IDomainAccountCallback> &callback)
32 : type_(type), domainAccountInfo_(domainAccountInfo), innerCallback_(callback)
33 {}
34
OnResult(int32_t errCode,Parcel & parcel)35 void CheckAndCreateDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
36 {
37 if (innerCallback_ == nullptr) {
38 ACCOUNT_LOGE("innerPlugin_ is nullptr");
39 return;
40 }
41 OsAccountInfo osAccountInfo;
42 Parcel resultParcel;
43 osAccountInfo.Marshalling(resultParcel);
44 if (errCode != ERR_OK) {
45 ACCOUNT_LOGE("check domain account failed");
46 return innerCallback_->OnResult(errCode, resultParcel);
47 }
48 std::shared_ptr<AAFwk::WantParams> parameters(AAFwk::WantParams::Unmarshalling(parcel));
49 if (parameters == nullptr) {
50 ACCOUNT_LOGE("parameters unmarshalling error");
51 return innerCallback_->OnResult(ERR_JS_SYSTEM_SERVICE_EXCEPTION, resultParcel);
52 }
53 DomainAccountInfo domainAccountInfo;
54 domainAccountInfo.accountName_ = parameters->GetStringParam("accountName");
55 domainAccountInfo.domain_ = parameters->GetStringParam("domain");
56 domainAccountInfo.accountId_ = parameters->GetStringParam("accountId");
57 if ((domainAccountInfo.accountName_.empty()) || (domainAccountInfo.domain_.empty())) {
58 ACCOUNT_LOGE("domain account not found");
59 return innerCallback_->OnResult(ERR_JS_ACCOUNT_NOT_FOUND, resultParcel);
60 }
61 errCode = IInnerOsAccountManager::GetInstance().BindDomainAccount(type_, domainAccountInfo, innerCallback_);
62 if (errCode != ERR_OK) {
63 return innerCallback_->OnResult(errCode, resultParcel);
64 }
65 }
66
BindDomainAccountCallback(const DomainAccountInfo & domainAccountInfo,const OsAccountInfo & osAccountInfo,const sptr<IDomainAccountCallback> & callback)67 BindDomainAccountCallback::BindDomainAccountCallback(const DomainAccountInfo &domainAccountInfo,
68 const OsAccountInfo &osAccountInfo, const sptr<IDomainAccountCallback> &callback)
69 : domainAccountInfo_(domainAccountInfo), osAccountInfo_(osAccountInfo), innerCallback_(callback)
70 {}
71
OnResult(int32_t errCode,Parcel & parcel)72 void BindDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel)
73 {
74 if (innerCallback_ == nullptr) {
75 ACCOUNT_LOGE("inner callback is nullptr");
76 return;
77 }
78 auto osAccountControl = std::make_shared<OsAccountControlFileManager>();
79 if (osAccountControl == nullptr) {
80 ACCOUNT_LOGE("failed to create OsAccountControlFileManager");
81 return innerCallback_->OnResult(ERR_JS_SYSTEM_SERVICE_EXCEPTION, parcel);
82 }
83 osAccountControl->Init();
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 errCode = osAccountControl->UpdateOsAccount(osAccountInfo_);
98 if ((osAccountInfo_.GetLocalId() == Constants::START_USER_ID) && (errCode == ERR_OK)) {
99 #ifdef HAS_CES_PART
100 AccountEventProvider::EventPublish(
101 EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::START_USER_ID, nullptr);
102 #else // HAS_CES_PART
103 ACCOUNT_LOGI("No common event part! Publish nothing!");
104 #endif // HAS_CES_PART
105 }
106 osAccountInfo_.Marshalling(resultParcel);
107 innerCallback_->OnResult(errCode, resultParcel);
108 }
109 } // namespace AccountSA
110 } // namespace OHOS