1 /*
2 * Copyright (c) 2021-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 #include "iinner_os_account_manager.h"
16 #include "account_log_wrapper.h"
17 #ifdef HAS_CES_PART
18 #include "common_event_support.h"
19 #endif // HAS_CES_PART
20 #include "hitrace_meter.h"
21 #include "hisysevent_adapter.h"
22 #include "ohos_account_kits.h"
23 #include "os_account_constants.h"
24 #include "os_account_control_file_manager.h"
25 #include "os_account_subscribe_manager.h"
26
27 namespace OHOS {
28 namespace AccountSA {
29 namespace {
30 const std::string CONSTRAINT_CREATE_ACCOUNT_DIRECTLY = "constraint.os.account.create.directly";
31 }
32
IInnerOsAccountManager()33 IInnerOsAccountManager::IInnerOsAccountManager() : subscribeManagerPtr_(OsAccountSubscribeManager::GetInstance())
34 {
35 counterForStandard_ = 0;
36 counterForStandardCreate_ = 0;
37 counterForAccountStart_ = 0;
38 isSendToStorageCreate_ = false;
39 isSendToStorageStart_ = false;
40 activeAccountId_.clear();
41 operatingId_.clear();
42 osAccountControl_ = std::make_shared<OsAccountControlFileManager>();
43 osAccountControl_->Init();
44 osAccountControl_->GetDeviceOwnerId(deviceOwnerId_);
45 ACCOUNT_LOGD("OsAccountAccountMgr Init end");
46 }
47
SetOsAccountControl(std::shared_ptr<IOsAccountControl> ptr)48 void IInnerOsAccountManager::SetOsAccountControl(std::shared_ptr<IOsAccountControl> ptr)
49 {
50 osAccountControl_ = ptr;
51 }
52
CreateBaseAdminAccount()53 void IInnerOsAccountManager::CreateBaseAdminAccount()
54 {
55 bool isExistsAccount = false;
56 osAccountControl_->IsOsAccountExists(Constants::ADMIN_LOCAL_ID, isExistsAccount);
57 if (!isExistsAccount) {
58 int64_t serialNumber =
59 Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID;
60 OsAccountInfo osAccountInfo(
61 Constants::ADMIN_LOCAL_ID, Constants::ADMIN_LOCAL_NAME, OsAccountType::ADMIN, serialNumber);
62 int64_t time =
63 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
64 .count();
65 osAccountInfo.SetCreateTime(time);
66 osAccountInfo.SetIsCreateCompleted(true);
67 osAccountInfo.SetIsActived(true); // admin local account is always active
68 osAccountControl_->InsertOsAccount(osAccountInfo);
69 ACCOUNT_LOGI("OsAccountAccountMgr created admin account end");
70 }
71 }
72
CreateBaseStandardAccount()73 void IInnerOsAccountManager::CreateBaseStandardAccount()
74 {
75 bool isExistsAccount = false;
76 osAccountControl_->IsOsAccountExists(Constants::START_USER_ID, isExistsAccount);
77 if (!isExistsAccount) {
78 int64_t serialNumber = 0;
79 osAccountControl_->GetSerialNumber(serialNumber);
80 OsAccountInfo osAccountInfo(
81 Constants::START_USER_ID, Constants::STANDARD_LOCAL_NAME, OsAccountType::ADMIN, serialNumber);
82 std::vector<std::string> constants;
83 ErrCode errCode = osAccountControl_->GetConstraintsByType(OsAccountType::ADMIN, constants);
84 if (errCode != ERR_OK) {
85 ACCOUNT_LOGE("find first standard type err, errCode %{public}d.", errCode);
86 return;
87 }
88 osAccountInfo.SetConstraints(constants);
89 int64_t time =
90 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
91 .count();
92 osAccountInfo.SetCreateTime(time);
93 osAccountInfo.SetIsCreateCompleted(false);
94 osAccountControl_->InsertOsAccount(osAccountInfo);
95 ACCOUNT_LOGI("OsAccountAccountMgr created base account end");
96 }
97 }
98
StartAccount()99 void IInnerOsAccountManager::StartAccount()
100 {
101 ResetAccountStatus();
102 GetEventHandler();
103 OsAccountInfo osAccountInfo;
104 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
105 if (errCode != ERR_OK) {
106 ACCOUNT_LOGE(
107 "OsAccountAccountMgr init start base account failed. cannot find account, errCode %{public}d.", errCode);
108 return;
109 }
110 if (!osAccountInfo.GetIsCreateCompleted()) {
111 ACCOUNT_LOGI("OsAccountAccountMgr send to storage and bm for start");
112 OHOS::AppExecFwk::InnerEvent::Callback callbackStartStandard =
113 std::bind(&IInnerOsAccountManager::CreateBaseStandardAccountSendToOther, this);
114 handler_->PostTask(callbackStartStandard);
115 return;
116 }
117 ACCOUNT_LOGI("OsAccountAccountMgr send to storage and am for start");
118 OHOS::AppExecFwk::InnerEvent::Callback callbackStartStandard =
119 std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this, osAccountInfo);
120 handler_->PostTask(callbackStartStandard);
121 }
122
RestartActiveAccount()123 void IInnerOsAccountManager::RestartActiveAccount()
124 {
125 // query active account to restart and refresh into list
126 std::vector<OsAccountInfo> osAccountInfos;
127 if (QueryAllCreatedOsAccounts(osAccountInfos) != ERR_OK) {
128 return;
129 }
130 for (size_t i = 0; i < osAccountInfos.size(); ++i) {
131 if (osAccountInfos[i].GetIsActived() && osAccountInfos[i].GetLocalId() != Constants::START_USER_ID) {
132 // reactivate account state
133 GetEventHandler();
134 OHOS::AppExecFwk::InnerEvent::Callback callbackForRestart =
135 std::bind(&IInnerOsAccountManager::StartActivatedAccount, this, osAccountInfos[i].GetLocalId());
136 handler_->PostTask(callbackForRestart, DELAY_FOR_FOUNDATION_SERVICE);
137 }
138 }
139 }
140
StartActivatedAccount(int32_t id)141 void IInnerOsAccountManager::StartActivatedAccount(int32_t id)
142 {
143 OsAccountInfo osAccountInfo;
144 osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
145 if (!IsOsAccountIDInActiveList(id)) {
146 ErrCode errCode = ActivateOsAccount(id);
147 if (errCode != ERR_OK) {
148 if (++counterForAccountStart_ == MAX_TRY_TIMES) {
149 ACCOUNT_LOGE("failed to reactivate account, id = %{public}d", id);
150 } else {
151 GetEventHandler();
152 OHOS::AppExecFwk::InnerEvent::Callback callbackForRestart =
153 std::bind(&IInnerOsAccountManager::StartActivatedAccount, this, id);
154 handler_->PostTask(callbackForRestart, DELAY_FOR_FOUNDATION_SERVICE);
155 }
156 return;
157 }
158 ACCOUNT_LOGI("reactive account ok");
159 counterForAccountStart_ = 0;
160 }
161 int64_t time = std::chrono::duration_cast<std::chrono::seconds>(
162 std::chrono::system_clock::now().time_since_epoch()).count();
163 osAccountInfo.SetLastLoginTime(time);
164 osAccountControl_->UpdateOsAccount(osAccountInfo);
165 PushIdIntoActiveList(id);
166 subscribeManagerPtr_->PublishActivatedOsAccount(Constants::START_USER_ID);
167 OsAccountInterface::SendToCESAccountSwitched(osAccountInfo);
168 ACCOUNT_LOGI("restart account ok");
169 }
170
CreateBaseStandardAccountSendToOther(void)171 void IInnerOsAccountManager::CreateBaseStandardAccountSendToOther(void)
172 {
173 OsAccountInfo osAccountInfo;
174 if (!isSendToStorageCreate_) {
175 osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
176 ErrCode errCode = OsAccountInterface::SendToStorageAccountCreate(osAccountInfo);
177 if (errCode != ERR_OK) {
178 if (++counterForStandardCreate_ == MAX_TRY_TIMES) {
179 ACCOUNT_LOGE("failed connect storage to create account, errCode %{public}d.", errCode);
180 } else {
181 GetEventHandler();
182 OHOS::AppExecFwk::InnerEvent::Callback callback =
183 std::bind(&IInnerOsAccountManager::CreateBaseStandardAccountSendToOther, this);
184 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
185 }
186 return;
187 } else {
188 ACCOUNT_LOGI("connect storage to create account ok");
189 counterForStandardCreate_ = 0;
190 isSendToStorageCreate_ = true;
191 }
192 }
193 osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
194 ErrCode errCodeForBM = OsAccountInterface::SendToBMSAccountCreate(osAccountInfo);
195 if (errCodeForBM != ERR_OK) {
196 if (++counterForStandardCreate_ == MAX_TRY_TIMES) {
197 ACCOUNT_LOGE("failed connect BM to create account, errCodeForBM %{public}d.", errCodeForBM);
198 } else {
199 GetEventHandler();
200 OHOS::AppExecFwk::InnerEvent::Callback callback =
201 std::bind(&IInnerOsAccountManager::CreateBaseStandardAccountSendToOther, this);
202 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
203 }
204 return;
205 }
206 osAccountInfo.SetIsCreateCompleted(true);
207 osAccountControl_->UpdateOsAccount(osAccountInfo);
208 ACCOUNT_LOGI("connect BM to create account ok");
209 GetEventHandler();
210 OHOS::AppExecFwk::InnerEvent::Callback callbackStartStandard =
211 std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this, osAccountInfo);
212 handler_->PostTask(callbackStartStandard);
213 }
214
ResetAccountStatus(void)215 void IInnerOsAccountManager::ResetAccountStatus(void)
216 {
217 std::vector<OsAccountInfo> osAccountInfos;
218 if (QueryAllCreatedOsAccounts(osAccountInfos) != ERR_OK) {
219 return;
220 }
221 for (size_t i = 0; i < osAccountInfos.size(); ++i) {
222 osAccountInfos[i].SetIsVerified(false);
223 #ifndef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
224 osAccountInfos[i].SetIsActived(false);
225 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
226 osAccountControl_->UpdateOsAccount(osAccountInfos[i]);
227 }
228 }
229
StartBaseStandardAccount(OsAccountInfo & osAccountInfo)230 void IInnerOsAccountManager::StartBaseStandardAccount(OsAccountInfo &osAccountInfo)
231 {
232 if (!isSendToStorageStart_) {
233 ErrCode errCode = OsAccountInterface::SendToStorageAccountStart(osAccountInfo);
234 if (errCode != ERR_OK) {
235 if (++counterForStandard_ == MAX_TRY_TIMES) {
236 ACCOUNT_LOGE("failed connect storage to start account, errCode %{public}d.", errCode);
237 } else {
238 GetEventHandler();
239 OHOS::AppExecFwk::InnerEvent::Callback callback =
240 std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this, osAccountInfo);
241 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
242 }
243 return;
244 }
245 ACCOUNT_LOGI("connect storage to start account ok");
246 counterForStandard_ = 0;
247 isSendToStorageStart_ = true;
248 }
249 ErrCode errCodeForAM = OsAccountInterface::SendToAMSAccountStart(osAccountInfo);
250 if (errCodeForAM != ERR_OK) {
251 if (++counterForStandard_ == MAX_TRY_TIMES) {
252 ACCOUNT_LOGE("failed connect AM to start account, errCodeForAM %{public}d.", errCodeForAM);
253 } else {
254 GetEventHandler();
255 OHOS::AppExecFwk::InnerEvent::Callback callback =
256 std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this, osAccountInfo);
257 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
258 }
259 return;
260 }
261 osAccountInfo.SetIsActived(true);
262 int64_t time = std::chrono::duration_cast<std::chrono::seconds>(
263 std::chrono::system_clock::now().time_since_epoch()).count();
264 osAccountInfo.SetLastLoginTime(time);
265 osAccountControl_->UpdateOsAccount(osAccountInfo);
266 PushIdIntoActiveList(Constants::START_USER_ID);
267 subscribeManagerPtr_->PublishActivatedOsAccount(Constants::START_USER_ID);
268 OsAccountInterface::SendToCESAccountSwitched(osAccountInfo);
269 ACCOUNT_LOGI("connect AM to start account ok");
270 }
271
PrepareOsAccountInfo(const std::string & name,const OsAccountType & type,const DomainAccountInfo & domainInfo,OsAccountInfo & osAccountInfo)272 ErrCode IInnerOsAccountManager::PrepareOsAccountInfo(const std::string &name, const OsAccountType &type,
273 const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo)
274 {
275 int64_t serialNumber;
276 ErrCode errCode = osAccountControl_->GetSerialNumber(serialNumber);
277 if (errCode != ERR_OK) {
278 ACCOUNT_LOGE("failed to GetSerialNumber, errCode %{public}d.", errCode);
279 return ERR_OSACCOUNT_SERVICE_INNER_GET_SERIAL_NUMBER_ERROR;
280 }
281 int id = 0;
282 errCode = osAccountControl_->GetAllowCreateId(id);
283 if (errCode != ERR_OK) {
284 ACCOUNT_LOGE("failed to GetAllowCreateId, errCode %{public}d.", errCode);
285 return errCode;
286 }
287 std::vector<std::string> constraints;
288 constraints.clear();
289 errCode = osAccountControl_->GetConstraintsByType(type, constraints);
290 if (errCode != ERR_OK) {
291 ACCOUNT_LOGE("failed to GetConstraintsByType, errCode %{public}d.", errCode);
292 return ERR_OSACCOUNT_SERVICE_INNER_GET_TYPE_CONSTRAINTS_ERROR;
293 }
294 osAccountInfo = OsAccountInfo(id, name, type, serialNumber);
295 osAccountInfo.SetConstraints(constraints);
296 int64_t time =
297 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
298 osAccountInfo.SetCreateTime(time);
299 if (!osAccountInfo.SetDomainInfo(domainInfo)) {
300 ACCOUNT_LOGE("failed to SetDomainInfo");
301 return ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR;
302 }
303
304 errCode = osAccountControl_->InsertOsAccount(osAccountInfo);
305 if (errCode != ERR_OK) {
306 ACCOUNT_LOGE("insert os account info err, errCode %{public}d.", errCode);
307 return ERR_OSACCOUNT_SERVICE_INNER_CREATE_ACCOUNT_ERROR;
308 }
309 errCode = osAccountControl_->UpdateBaseOAConstraints(std::to_string(id), constraints, true);
310 if (errCode != ERR_OK) {
311 ACCOUNT_LOGE("UpdateBaseOAConstraints err");
312 return ERR_OSACCOUNT_SERVICE_INNER_CREATE_ACCOUNT_ERROR;
313 }
314 return ERR_OK;
315 }
316
SendMsgForAccountCreate(OsAccountInfo & osAccountInfo)317 ErrCode IInnerOsAccountManager::SendMsgForAccountCreate(OsAccountInfo &osAccountInfo)
318 {
319 ErrCode errCode = OsAccountInterface::SendToStorageAccountCreate(osAccountInfo);
320 if (errCode != ERR_OK) {
321 ACCOUNT_LOGE("create os account SendToStorageAccountCreate failed, errCode %{public}d.", errCode);
322 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
323 }
324 errCode = OsAccountInterface::SendToBMSAccountCreate(osAccountInfo);
325 if (errCode != ERR_OK) {
326 ACCOUNT_LOGE("create os account SendToBMSAccountCreate failed, errCode %{public}d.", errCode);
327 return ERR_OSACCOUNT_SERVICE_INNER_SEND_BM_ACCOUNT_CREATE_ERROR;
328 }
329
330 osAccountInfo.SetIsCreateCompleted(true);
331 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
332 if (errCode != ERR_OK) {
333 ACCOUNT_LOGE("create os account when update isCreateCompleted");
334 ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_CREATE,
335 errCode, "UpdateOsAccount failed!");
336 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
337 }
338 ReportOsAccountLifeCycle(osAccountInfo.GetLocalId(), Constants::OPERATION_CREATE);
339 OsAccountInterface::SendToCESAccountCreate(osAccountInfo);
340
341 return ERR_OK;
342 }
343
CreateOsAccount(const std::string & name,const OsAccountType & type,OsAccountInfo & osAccountInfo)344 ErrCode IInnerOsAccountManager::CreateOsAccount(
345 const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo)
346 {
347 DomainAccountInfo domainInfo; // default empty domain info
348 ErrCode errCode = PrepareOsAccountInfo(name, type, domainInfo, osAccountInfo);
349 if (errCode != ERR_OK) {
350 return errCode;
351 }
352 return SendMsgForAccountCreate(osAccountInfo);
353 }
354
CreateOsAccountForDomain(const OsAccountType & type,const DomainAccountInfo & domainInfo,OsAccountInfo & osAccountInfo)355 ErrCode IInnerOsAccountManager::CreateOsAccountForDomain(
356 const OsAccountType &type, const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo)
357 {
358 // check whether if the target domain has already been bound to an os account or not
359 std::vector<OsAccountInfo> osAccountInfos;
360 ErrCode errCode = QueryAllCreatedOsAccounts(osAccountInfos);
361 if (errCode != ERR_OK) {
362 ACCOUNT_LOGE("cannot get current os account list, err %{public}d.", errCode);
363 return errCode;
364 }
365 for (size_t i = 0; i < osAccountInfos.size(); ++i) {
366 DomainAccountInfo curDomainInfo;
367 osAccountInfos[i].GetDomainInfo(curDomainInfo);
368 if (curDomainInfo.accountName_ == domainInfo.accountName_ &&
369 curDomainInfo.domain_ == domainInfo.domain_) {
370 return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR;
371 }
372 }
373
374 std::string osAccountName = domainInfo.domain_ + "/" + domainInfo.accountName_;
375 bool isEnabled = false;
376 (void)IsOsAccountConstraintEnable(Constants::START_USER_ID, CONSTRAINT_CREATE_ACCOUNT_DIRECTLY, isEnabled);
377 if (isEnabled && (osAccountInfos.size() == 1) && (osAccountInfos[0].GetLocalId() == Constants::START_USER_ID)) {
378 DomainAccountInfo curDomainInfo;
379 osAccountInfos[0].GetDomainInfo(curDomainInfo);
380 if (curDomainInfo.domain_.empty()) {
381 osAccountInfos[0].SetLocalName(osAccountName);
382 osAccountInfos[0].SetDomainInfo(domainInfo);
383 osAccountInfo = osAccountInfos[0];
384 return osAccountControl_->UpdateOsAccount(osAccountInfos[0]);
385 }
386 }
387
388 errCode = PrepareOsAccountInfo(osAccountName, type, domainInfo, osAccountInfo);
389 if (errCode != ERR_OK) {
390 return errCode;
391 }
392 return SendMsgForAccountCreate(osAccountInfo);
393 }
394
RemoveOsAccount(const int id)395 ErrCode IInnerOsAccountManager::RemoveOsAccount(const int id)
396 {
397 ACCOUNT_LOGI("RemoveOsAccount delete id is %{public}d", id);
398 if (IsLocalIdInOperating(id)) {
399 ACCOUNT_LOGE("the %{public}d already in operating", id);
400 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
401 }
402 AddLocalIdToOperating(id);
403 #ifndef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
404 if (IsOsAccountIDInActiveList(id)) {
405 ACCOUNT_LOGI("RemoveOsAccount started account to inactive, account id : %{public}d.", id);
406 ErrCode activeErrCode = ActivateOsAccount(Constants::START_USER_ID);
407 if (activeErrCode != ERR_OK) {
408 RemoveLocalIdToOperating(id);
409 ACCOUNT_LOGE("RemoveOsAccount active base account failed");
410 return ERR_OSACCOUNT_SERVICE_INNER_REMOVE_ACCOUNT_ACTIVED_ERROR;
411 }
412 }
413 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
414 OsAccountInfo osAccountInfo;
415 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
416 if (errCode != ERR_OK) {
417 RemoveLocalIdToOperating(id);
418 ACCOUNT_LOGE("RemoveOsAccount cannot find os account info, errCode %{public}d.", errCode);
419 return ERR_OSACCOUNT_SERVICE_INNER_CANNOT_FIND_OSACCOUNT_ERROR;
420 }
421
422 // set remove flag first
423 osAccountInfo.SetToBeRemoved(true);
424 osAccountControl_->UpdateOsAccount(osAccountInfo);
425
426 // stop account first
427 errCode = SendMsgForAccountStop(osAccountInfo);
428 if (errCode != ERR_OK) {
429 RemoveLocalIdToOperating(id);
430 return errCode;
431 }
432
433 // then remove account
434 errCode = SendMsgForAccountRemove(osAccountInfo);
435 if (errCode != ERR_OK) {
436 RemoveLocalIdToOperating(id);
437 return errCode;
438 }
439 RemoveLocalIdToOperating(id);
440
441 errCode = osAccountControl_->RemoveOAConstraintsInfo(id);
442 if (errCode != ERR_OK) {
443 ACCOUNT_LOGE("RemoveOsAccount failed to remove os account constraints info");
444 return errCode;
445 }
446 if (id == deviceOwnerId_) {
447 osAccountControl_->UpdateDeviceOwnerId(-1);
448 }
449 ACCOUNT_LOGI("IInnerOsAccountManager RemoveOsAccount end");
450 return ERR_OK;
451 }
452
SendMsgForAccountStop(OsAccountInfo & osAccountInfo)453 ErrCode IInnerOsAccountManager::SendMsgForAccountStop(OsAccountInfo &osAccountInfo)
454 {
455 ErrCode errCode = OsAccountInterface::SendToAMSAccountStop(osAccountInfo);
456 if (errCode != ERR_OK) {
457 ACCOUNT_LOGE("SendToAMSAccountStop failed, id %{public}d, errCode %{public}d",
458 osAccountInfo.GetLocalId(), errCode);
459 return ERR_OSACCOUNT_SERVICE_INNER_SEND_AM_ACCOUNT_STOP_ERROR;
460 }
461 errCode = OsAccountInterface::SendToStorageAccountStop(osAccountInfo);
462 if (errCode != ERR_OK) {
463 ACCOUNT_LOGE("SendToStorageAccountStop failed, id %{public}d, errCode %{public}d",
464 osAccountInfo.GetLocalId(), errCode);
465 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
466 }
467 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
468 errCode = DeActivateOsAccount(osAccountInfo.GetLocalId());
469 if (errCode != ERR_OK) {
470 ACCOUNT_LOGE("DeActivateOsAccount failed, id %{public}d, errCode %{public}d",
471 osAccountInfo.GetLocalId(), errCode);
472 return errCode;
473 }
474 ACCOUNT_LOGI("SendMsgForAccountStop ok");
475 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
476 return errCode;
477 }
478
SendMsgForAccountRemove(OsAccountInfo & osAccountInfo)479 ErrCode IInnerOsAccountManager::SendMsgForAccountRemove(OsAccountInfo &osAccountInfo)
480 {
481 ErrCode errCode = OsAccountInterface::SendToBMSAccountDelete(osAccountInfo);
482 if (errCode != ERR_OK) {
483 ACCOUNT_LOGE("SendToBMSAccountDelete failed, id %{public}d, errCode %{public}d",
484 osAccountInfo.GetLocalId(), errCode);
485 return ERR_OSACCOUNT_SERVICE_INNER_SEND_BM_ACCOUNT_DELE_ERROR;
486 }
487 errCode = OsAccountInterface::SendToStorageAccountRemove(osAccountInfo);
488 if (errCode != ERR_OK) {
489 ACCOUNT_LOGE("SendToStorageAccountRemove failed, id %{public}d, errCode %{public}d",
490 osAccountInfo.GetLocalId(), errCode);
491 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
492 }
493 #ifdef HAS_USER_IDM_PART
494 errCode = OsAccountInterface::SendToIDMAccountDelete(osAccountInfo);
495 if (errCode != ERR_OK) {
496 ACCOUNT_LOGE("SendToIDMAccountDelete failed, id %{public}d, errCode %{public}d",
497 osAccountInfo.GetLocalId(), errCode);
498 return ERR_OSACCOUNT_SERVICE_INNER_SEND_IAM_ACCOUNT_DELE_ERROR;
499 }
500 #endif // HAS_USER_IDM_PART
501 errCode = osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId());
502 if (errCode != ERR_OK) {
503 ACCOUNT_LOGE("remove osaccount info failed, id: %{public}d, errCode %{public}d",
504 osAccountInfo.GetLocalId(), errCode);
505 return errCode;
506 }
507 OsAccountInterface::SendToCESAccountDelete(osAccountInfo);
508 ReportOsAccountLifeCycle(osAccountInfo.GetLocalId(), Constants::OPERATION_DELETE);
509 return errCode;
510 }
511
Init()512 void IInnerOsAccountManager::Init()
513 {
514 CreateBaseAdminAccount();
515 CreateBaseStandardAccount();
516 StartAccount();
517 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
518 RestartActiveAccount();
519 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
520 CleanGarbageAccounts();
521 }
522
IsOsAccountExists(const int id,bool & isOsAccountExits)523 ErrCode IInnerOsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExits)
524 {
525 isOsAccountExits = false;
526 osAccountControl_->IsOsAccountExists(id, isOsAccountExits);
527 return ERR_OK;
528 }
529
IsOsAccountActived(const int id,bool & isOsAccountActived)530 ErrCode IInnerOsAccountManager::IsOsAccountActived(const int id, bool &isOsAccountActived)
531 {
532 isOsAccountActived = false;
533
534 // check if os account exists
535 OsAccountInfo osAccountInfo;
536 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
537 if (errCode != ERR_OK) {
538 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
539 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
540 }
541 if (id == Constants::ADMIN_LOCAL_ID) {
542 isOsAccountActived = true;
543 return ERR_OK;
544 }
545 isOsAccountActived = IsOsAccountIDInActiveList(id);
546 return ERR_OK;
547 }
548
IsOsAccountConstraintEnable(const int id,const std::string & constraint,bool & isOsAccountConstraintEnable)549 ErrCode IInnerOsAccountManager::IsOsAccountConstraintEnable(
550 const int id, const std::string &constraint, bool &isOsAccountConstraintEnable)
551 {
552 isOsAccountConstraintEnable = false;
553 OsAccountInfo osAccountInfo;
554 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
555 if (errCode != ERR_OK) {
556 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
557 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
558 }
559 std::vector<std::string> constraints;
560 constraints = osAccountInfo.GetConstraints();
561 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
562 isOsAccountConstraintEnable = true;
563 return ERR_OK;
564 }
565 constraints.clear();
566 if (osAccountControl_->GetGlobalOAConstraintsList(constraints) == ERR_OK) {
567 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
568 isOsAccountConstraintEnable = true;
569 return ERR_OK;
570 }
571 }
572 constraints.clear();
573 if (osAccountControl_->GetSpecificOAConstraintsList(id, constraints) == ERR_OK) {
574 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
575 isOsAccountConstraintEnable = true;
576 return ERR_OK;
577 }
578 }
579 return ERR_OK;
580 }
581
IsOsAccountVerified(const int id,bool & isVerified)582 ErrCode IInnerOsAccountManager::IsOsAccountVerified(const int id, bool &isVerified)
583 {
584 OsAccountInfo osAccountInfo;
585 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
586 if (errCode != ERR_OK) {
587 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
588 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
589 }
590 isVerified = osAccountInfo.GetIsVerified();
591 return ERR_OK;
592 }
593
GetCreatedOsAccountsCount(unsigned int & createdOsAccountCount)594 ErrCode IInnerOsAccountManager::GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount)
595 {
596 std::vector<OsAccountInfo> osAccountInfos;
597 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
598 if (errCode != ERR_OK) {
599 ACCOUNT_LOGE("get osaccount info list error, errCode %{public}d.", errCode);
600 return errCode;
601 }
602 createdOsAccountCount = osAccountInfos.size();
603 return ERR_OK;
604 }
605
QueryMaxOsAccountNumber(int & maxOsAccountNumber)606 ErrCode IInnerOsAccountManager::QueryMaxOsAccountNumber(int &maxOsAccountNumber)
607 {
608 ErrCode errCode = osAccountControl_->GetMaxCreatedOsAccountNum(maxOsAccountNumber);
609 if (errCode != ERR_OK) {
610 ACCOUNT_LOGE("get max created osaccount num error, errCode %{public}d.", errCode);
611 return errCode;
612 }
613 return ERR_OK;
614 }
615
GetOsAccountAllConstraints(const int id,std::vector<std::string> & constraints)616 ErrCode IInnerOsAccountManager::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints)
617 {
618 OsAccountInfo osAccountInfo;
619 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
620 if (errCode != ERR_OK) {
621 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
622 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
623 }
624 constraints = osAccountInfo.GetConstraints();
625 std::vector<std::string> globalConstraints;
626 errCode = osAccountControl_->GetGlobalOAConstraintsList(globalConstraints);
627 if (errCode != ERR_OK) {
628 ACCOUNT_LOGE("get globalConstraints info error");
629 return errCode;
630 }
631 for (auto it = globalConstraints.begin(); it != globalConstraints.end(); it++) {
632 if (std::find(constraints.begin(), constraints.end(), *it) == constraints.end()) {
633 constraints.push_back(*it);
634 }
635 }
636 std::vector<std::string> specificConstraints;
637 errCode = osAccountControl_->GetSpecificOAConstraintsList(id, specificConstraints);
638 if (errCode != ERR_OK) {
639 ACCOUNT_LOGE("get specificConstraints info error");
640 return errCode;
641 }
642 for (auto it = specificConstraints.begin(); it != specificConstraints.end(); it++) {
643 if (std::find(constraints.begin(), constraints.end(), *it) == constraints.end()) {
644 constraints.push_back(*it);
645 }
646 }
647 return ERR_OK;
648 }
649
QueryOsAccountConstraintSourceTypes(const int32_t id,const std::string & constraint,std::vector<ConstraintSourceTypeInfo> & constraintSourceTypeInfos)650 ErrCode IInnerOsAccountManager::QueryOsAccountConstraintSourceTypes(const int32_t id,
651 const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos)
652 {
653 ACCOUNT_LOGD("enter.");
654 bool isOsAccountConstraintEnable = false;
655 ErrCode errCode = IsOsAccountConstraintEnable(id, constraint, isOsAccountConstraintEnable);
656 if (errCode != ERR_OK) {
657 ACCOUNT_LOGE("get os account constraint enable info error");
658 return errCode;
659 }
660 if (!isOsAccountConstraintEnable) {
661 ACCOUNT_LOGI("constraint not exist");
662 ConstraintSourceTypeInfo constraintSourceTypeInfo;
663 constraintSourceTypeInfo.localId = -1;
664 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_NOT_EXIST;
665 constraintSourceTypeInfos.push_back(constraintSourceTypeInfo);
666 return ERR_OK;
667 }
668
669 bool isExits;
670 if (osAccountControl_->IsFromBaseOAConstraintsList(id, constraint, isExits) == ERR_OK) {
671 if (isExits) {
672 ACCOUNT_LOGI("constraint is exist in base os account constraints list");
673 ConstraintSourceTypeInfo constraintSourceTypeInfo;
674 constraintSourceTypeInfo.localId = -1;
675 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_BASE;
676 constraintSourceTypeInfos.push_back(constraintSourceTypeInfo);
677 }
678 }
679 std::vector<ConstraintSourceTypeInfo> globalSourceList;
680 errCode = osAccountControl_->IsFromGlobalOAConstraintsList(id, deviceOwnerId_, constraint, globalSourceList);
681 if (errCode == ERR_OK && globalSourceList.size() != 0) {
682 ACCOUNT_LOGI("constraint is exist in global os account constraints list");
683 constraintSourceTypeInfos.insert(
684 constraintSourceTypeInfos.end(), globalSourceList.begin(), globalSourceList.end());
685 }
686 std::vector<ConstraintSourceTypeInfo> specificSourceList;
687 errCode = osAccountControl_->IsFromSpecificOAConstraintsList(id, deviceOwnerId_, constraint, specificSourceList);
688 if (errCode == ERR_OK && specificSourceList.size() != 0) {
689 ACCOUNT_LOGI("constraint is exist in specific os account constraints list");
690 constraintSourceTypeInfos.insert(
691 constraintSourceTypeInfos.end(), specificSourceList.begin(), specificSourceList.end());
692 }
693 return ERR_OK;
694 }
695
SetBaseOsAccountConstraints(const int32_t id,const std::vector<std::string> & constraints,const bool enable)696 ErrCode IInnerOsAccountManager::SetBaseOsAccountConstraints(const int32_t id,
697 const std::vector<std::string> &constraints, const bool enable)
698 {
699 ErrCode errCode = SetOsAccountConstraints(id, constraints, enable);
700 if (errCode != ERR_OK) {
701 ACCOUNT_LOGE("set os account %{public}d constraints failed! errCode %{public}d.", id, errCode);
702 return errCode;
703 }
704
705 errCode = osAccountControl_->UpdateBaseOAConstraints(std::to_string(id), constraints, enable);
706 if (errCode != ERR_OK) {
707 ACCOUNT_LOGE("update base os account %{public}d constraints failed! errCode %{public}d.", id, errCode);
708 return errCode;
709 }
710 return ERR_OK;
711 }
712
SetGlobalOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t enforcerId,const bool isDeviceOwner)713 ErrCode IInnerOsAccountManager::SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
714 const bool enable, const int32_t enforcerId, const bool isDeviceOwner)
715 {
716 OsAccountInfo osAccountInfo;
717 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(enforcerId, osAccountInfo);
718 if (errCode != ERR_OK) {
719 ACCOUNT_LOGE("get osaccount info error %{public}d", enforcerId);
720 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
721 }
722 if (osAccountInfo.GetToBeRemoved()) {
723 ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", enforcerId);
724 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
725 }
726
727 bool isExists = false;
728 bool isOverSize = false;
729 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize);
730 if (errCode != ERR_OK || !isExists || isOverSize) {
731 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!");
732 return ERR_OSACCOUNT_SERVICE_INNER_SER_CONSTRAINTS_ERROR;
733 }
734
735 osAccountControl_->UpdateGlobalOAConstraints(std::to_string(enforcerId), constraints, enable);
736
737 errCode = DealWithDeviceOwnerId(isDeviceOwner, enforcerId);
738 if (errCode != ERR_OK) {
739 ACCOUNT_LOGE("deal with device owner id error");
740 return errCode;
741 }
742 return ERR_OK;
743 }
744
SetSpecificOsAccountConstraints(const std::vector<std::string> & constraints,const bool enable,const int32_t targetId,const int32_t enforcerId,const bool isDeviceOwner)745 ErrCode IInnerOsAccountManager::SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
746 const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner)
747 {
748 OsAccountInfo enforcerOsAccountInfo;
749 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(enforcerId, enforcerOsAccountInfo);
750 if (errCode != ERR_OK) {
751 ACCOUNT_LOGE("get osaccount info error");
752 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
753 }
754
755 OsAccountInfo targetOsAccountInfo;
756 errCode = osAccountControl_->GetOsAccountInfoById(targetId, targetOsAccountInfo);
757 if (errCode != ERR_OK) {
758 ACCOUNT_LOGE("get osaccount info error");
759 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
760 }
761 if (targetOsAccountInfo.GetToBeRemoved() || enforcerOsAccountInfo.GetToBeRemoved()) {
762 ACCOUNT_LOGE("account %{public}d or %{public}d will be removed, cannot change constraints!",
763 enforcerId, targetId);
764 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
765 }
766
767 bool isExists = false;
768 bool isOverSize = false;
769 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize);
770 if (errCode != ERR_OK || !isExists || isOverSize) {
771 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!");
772 return ERR_OSACCOUNT_SERVICE_INNER_SER_CONSTRAINTS_ERROR;
773 }
774
775 osAccountControl_->UpdateSpecificOAConstraints(
776 std::to_string(enforcerId), std::to_string(targetId), constraints, enable);
777
778 errCode = DealWithDeviceOwnerId(isDeviceOwner, enforcerId);
779 if (errCode != ERR_OK) {
780 ACCOUNT_LOGE("deal with device owner id error");
781 return errCode;
782 }
783 return ERR_OK;
784 }
785
QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> & osAccountInfos)786 ErrCode IInnerOsAccountManager::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos)
787 {
788 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
789 if (errCode != ERR_OK) {
790 ACCOUNT_LOGE("get osaccount info list error, errCode %{public}d.", errCode);
791 return ERR_OSACCOUNT_SERVICE_INNER_GET_ACCOUNT_LIST_ERROR;
792 }
793 #ifndef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
794 for (auto osAccountInfosPtr = osAccountInfos.begin(); osAccountInfosPtr != osAccountInfos.end();
795 ++osAccountInfosPtr) {
796 if (IsOsAccountIDInActiveList(osAccountInfosPtr->GetLocalId())) {
797 osAccountInfosPtr->SetIsActived(true);
798 } else {
799 osAccountInfosPtr->SetIsActived(false);
800 }
801 }
802 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
803 return ERR_OK;
804 }
805
DealWithDeviceOwnerId(const bool isDeviceOwner,const int32_t localId)806 ErrCode IInnerOsAccountManager::DealWithDeviceOwnerId(const bool isDeviceOwner, const int32_t localId)
807 {
808 ACCOUNT_LOGD("enter.");
809 if (isDeviceOwner && localId != deviceOwnerId_) {
810 ACCOUNT_LOGI("this device owner os account id is changed!");
811 deviceOwnerId_ = localId;
812 return osAccountControl_->UpdateDeviceOwnerId(localId);
813 }
814 if (isDeviceOwner == false && localId == deviceOwnerId_) {
815 deviceOwnerId_ = -1;
816 return osAccountControl_->UpdateDeviceOwnerId(-1);
817 }
818 return ERR_OK;
819 }
820
CleanGarbageAccounts()821 void IInnerOsAccountManager::CleanGarbageAccounts()
822 {
823 ACCOUNT_LOGD("enter.");
824 std::vector<OsAccountInfo> osAccountInfos;
825 if (QueryAllCreatedOsAccounts(osAccountInfos) != ERR_OK) {
826 ACCOUNT_LOGI("QueryAllCreatedOsAccounts failed.");
827 return;
828 }
829
830 // check status and remove garbage accounts data
831 for (size_t i = 0; i < osAccountInfos.size(); ++i) {
832 if (!osAccountInfos[i].GetToBeRemoved()) {
833 continue;
834 }
835
836 if (osAccountInfos[i].GetLocalId() == Constants::START_USER_ID ||
837 osAccountInfos[i].GetLocalId() == Constants::ADMIN_LOCAL_ID) {
838 continue;
839 }
840
841 ErrCode errCode = SendMsgForAccountRemove(osAccountInfos[i]);
842 if (errCode != ERR_OK) {
843 ACCOUNT_LOGE("remove account %{public}d failed! errCode %{public}d.",
844 osAccountInfos[i].GetLocalId(), errCode);
845 } else {
846 ACCOUNT_LOGI("remove account %{public}d succeed!", osAccountInfos[i].GetLocalId());
847 }
848 }
849 ACCOUNT_LOGI("finished.");
850 }
851
GetOsAccountLocalIdFromDomain(const DomainAccountInfo & domainInfo,int & id)852 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id)
853 {
854 if (domainInfo.domain_.empty() ||
855 domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
856 ACCOUNT_LOGE("invalid domain name length %{public}zu.", domainInfo.domain_.size());
857 return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_NAME_LEN_ERROR;
858 }
859
860 if (domainInfo.accountName_.empty() ||
861 domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
862 ACCOUNT_LOGE("invalid domain account name length %{public}zu.", domainInfo.accountName_.size());
863 return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ACCOUNT_NAME_LEN_ERROR;
864 }
865
866 id = -1;
867 std::vector<OsAccountInfo> osAccountInfos;
868 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
869 if (errCode != ERR_OK) {
870 return ERR_OSACCOUNT_SERVICE_INNER_GET_ACCOUNT_LIST_ERROR;
871 }
872
873 DomainAccountInfo curDomainInfo;
874 for (auto osAccountInfosPtr = osAccountInfos.begin(); osAccountInfosPtr != osAccountInfos.end();
875 ++osAccountInfosPtr) {
876 osAccountInfosPtr->GetDomainInfo(curDomainInfo);
877 if (curDomainInfo.accountName_ == domainInfo.accountName_ &&
878 curDomainInfo.domain_ == domainInfo.domain_) {
879 id = osAccountInfosPtr->GetLocalId();
880 return ERR_OK;
881 }
882 }
883 return ERR_OSACCOUNT_KIT_GET_OS_ACCOUNT_LOCAL_ID_FOR_DOMAIN_ERROR;
884 }
885
QueryOsAccountById(const int id,OsAccountInfo & osAccountInfo)886 ErrCode IInnerOsAccountManager::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo)
887 {
888 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
889 if (errCode != ERR_OK) {
890 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
891 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
892 }
893
894 if (IsOsAccountIDInActiveList(id)) {
895 osAccountInfo.SetIsActived(true);
896 } else {
897 osAccountInfo.SetIsActived(false);
898 }
899
900 if (osAccountInfo.GetPhoto() != "") {
901 std::string photo = osAccountInfo.GetPhoto();
902 errCode = osAccountControl_->GetPhotoById(osAccountInfo.GetLocalId(), photo);
903 if (errCode != ERR_OK) {
904 ACCOUNT_LOGE("get osaccount photo error, errCode %{public}d.", errCode);
905 return errCode;
906 }
907 osAccountInfo.SetPhoto(photo);
908 }
909 return ERR_OK;
910 }
911
GetOsAccountType(const int id,OsAccountType & type)912 ErrCode IInnerOsAccountManager::GetOsAccountType(const int id, OsAccountType &type)
913 {
914 OsAccountInfo osAccountInfo;
915 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
916 if (errCode != ERR_OK) {
917 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
918 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
919 }
920 type = osAccountInfo.GetType();
921 return ERR_OK;
922 }
923
GetOsAccountProfilePhoto(const int id,std::string & photo)924 ErrCode IInnerOsAccountManager::GetOsAccountProfilePhoto(const int id, std::string &photo)
925 {
926 OsAccountInfo osAccountInfo;
927 ErrCode errCode = QueryOsAccountById(id, osAccountInfo);
928 if (errCode != ERR_OK) {
929 ACCOUNT_LOGE("QueryOsAccountById return error, errCode %{public}d.", errCode);
930 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
931 }
932 photo = osAccountInfo.GetPhoto();
933 return ERR_OK;
934 }
935
IsMultiOsAccountEnable(bool & isMultiOsAccountEnable)936 ErrCode IInnerOsAccountManager::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
937 {
938 ErrCode errCode = osAccountControl_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable);
939 if (errCode != ERR_OK) {
940 ACCOUNT_LOGE("GetIsMultiOsAccountEnable error, errCode %{public}d.", errCode);
941 return errCode;
942 }
943 return ERR_OK;
944 }
945
SetOsAccountName(const int id,const std::string & name)946 ErrCode IInnerOsAccountManager::SetOsAccountName(const int id, const std::string &name)
947 {
948 OsAccountInfo osAccountInfo;
949 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
950 if (errCode != ERR_OK) {
951 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
952 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
953 }
954
955 // to be removed, cannot change any thing
956 if (osAccountInfo.GetToBeRemoved()) {
957 ACCOUNT_LOGE("account %{public}d will be removed, cannot change name!", id);
958 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
959 }
960
961 std::string localName = osAccountInfo.GetLocalName();
962 if (localName == name) {
963 return ERR_OK;
964 }
965
966 osAccountInfo.SetLocalName(name);
967 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
968 if (errCode != ERR_OK) {
969 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId());
970 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
971 }
972 OsAccountInterface::PublishCommonEvent(
973 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE);
974 return ERR_OK;
975 }
976
SetOsAccountConstraints(const int id,const std::vector<std::string> & constraints,const bool enable)977 ErrCode IInnerOsAccountManager::SetOsAccountConstraints(
978 const int id, const std::vector<std::string> &constraints, const bool enable)
979 {
980 OsAccountInfo osAccountInfo;
981 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
982 if (errCode != ERR_OK) {
983 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
984 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
985 }
986
987 // to be removed, cannot change any thing
988 if (osAccountInfo.GetToBeRemoved()) {
989 ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", id);
990 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
991 }
992
993 bool isExists = false;
994 bool isOverSize = false;
995 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize);
996 if (errCode != ERR_OK || !isExists || isOverSize) {
997 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!");
998 return ERR_OSACCOUNT_SERVICE_INNER_SER_CONSTRAINTS_ERROR;
999 }
1000 std::vector<std::string> oldConstraints = osAccountInfo.GetConstraints();
1001 for (auto it = constraints.begin(); it != constraints.end(); it++) {
1002 if (enable) {
1003 if (std::find(oldConstraints.begin(), oldConstraints.end(), *it) == oldConstraints.end()) {
1004 oldConstraints.push_back(*it);
1005 }
1006 } else {
1007 oldConstraints.erase(
1008 std::remove(oldConstraints.begin(), oldConstraints.end(), *it), oldConstraints.end());
1009 }
1010 }
1011 osAccountInfo.SetConstraints(oldConstraints);
1012 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1013 if (errCode != ERR_OK) {
1014 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId());
1015 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1016 }
1017 return ERR_OK;
1018 }
1019
SetOsAccountProfilePhoto(const int id,const std::string & photo)1020 ErrCode IInnerOsAccountManager::SetOsAccountProfilePhoto(const int id, const std::string &photo)
1021 {
1022 OsAccountInfo osAccountInfo;
1023 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1024 if (errCode != ERR_OK) {
1025 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
1026 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
1027 }
1028
1029 // to be removed, cannot change any thing
1030 if (osAccountInfo.GetToBeRemoved()) {
1031 ACCOUNT_LOGE("account %{public}d will be removed, cannot change photo!", id);
1032 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1033 }
1034
1035 if (osAccountInfo.GetPhoto() == photo) {
1036 return ERR_OK;
1037 }
1038 errCode = osAccountControl_->SetPhotoById(id, photo);
1039 if (errCode != ERR_OK) {
1040 ACCOUNT_LOGE("set photo by id error, errCode %{public}d.", errCode);
1041 return errCode;
1042 }
1043 auto sizeType = photo.find(Constants::USER_PHOTO_BASE_JPG_HEAD);
1044 if (sizeType == std::string::npos) {
1045 osAccountInfo.SetPhoto(Constants::USER_PHOTO_FILE_PNG_NAME);
1046 } else {
1047 osAccountInfo.SetPhoto(Constants::USER_PHOTO_FILE_JPG_NAME);
1048 }
1049 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1050 if (errCode != ERR_OK) {
1051 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId());
1052 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1053 }
1054 OsAccountInterface::PublishCommonEvent(
1055 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE);
1056 return ERR_OK;
1057 }
1058
DeActivateOsAccount(const int id)1059 ErrCode IInnerOsAccountManager::DeActivateOsAccount(const int id)
1060 {
1061 if (id == Constants::ADMIN_LOCAL_ID) {
1062 ACCOUNT_LOGI("this osaccount can't deactive, id: %{public}d", Constants::ADMIN_LOCAL_ID);
1063 return ERR_OK;
1064 }
1065 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1066 if (id == Constants::START_USER_ID) {
1067 ACCOUNT_LOGI("this osaccount can't deactive, id: %{public}d", Constants::START_USER_ID);
1068 return ERR_OK;
1069 }
1070 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1071
1072 OsAccountInfo osAccountInfo;
1073 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1074 if (errCode != ERR_OK) {
1075 ACCOUNT_LOGE("DeActivateOsAccount cannot get os account %{public}d info. error %{public}d.",
1076 id, errCode);
1077 return ERR_OSACCOUNT_SERVICE_INNER_CANNOT_FIND_OSACCOUNT_ERROR;
1078 }
1079 osAccountInfo.SetIsActived(false);
1080 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1081 if (errCode != ERR_OK) {
1082 ACCOUNT_LOGE("update %{public}d account info failed, errCode %{public}d.",
1083 osAccountInfo.GetLocalId(), errCode);
1084 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1085 }
1086 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1087 EraseIdFromActiveList(osAccountInfo.GetLocalId());
1088 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1089 return ERR_OK;
1090 }
1091
ActivateOsAccount(const int id)1092 ErrCode IInnerOsAccountManager::ActivateOsAccount(const int id)
1093 {
1094 if (IsLocalIdInOperating(id)) {
1095 ACCOUNT_LOGE("the %{public}d already in operating", id);
1096 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
1097 }
1098 AddLocalIdToOperating(id);
1099 if (IsOsAccountIDInActiveList(id)) {
1100 RemoveLocalIdToOperating(id);
1101 ACCOUNT_LOGE("account is %{public}d already active", id);
1102 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREADY_ACTIVE_ERROR;
1103 }
1104
1105 // get information
1106 OsAccountInfo osAccountInfo;
1107 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1108 if (errCode != ERR_OK) {
1109 RemoveLocalIdToOperating(id);
1110 ACCOUNT_LOGE("cannot find os account info by id:%{public}d, errCode %{public}d.", id, errCode);
1111 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
1112 }
1113
1114 // check complete
1115 if (!osAccountInfo.GetIsCreateCompleted()) {
1116 RemoveLocalIdToOperating(id);
1117 ACCOUNT_LOGE("account %{public}d is not completed", id);
1118 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_IS_UNVERIFIED_ERROR;
1119 }
1120
1121 // check to be removed
1122 if (osAccountInfo.GetToBeRemoved()) {
1123 RemoveLocalIdToOperating(id);
1124 ACCOUNT_LOGE("account %{public}d will be removed, cannot be activated!", id);
1125 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1126 }
1127
1128 // activate
1129 subscribeManagerPtr_->PublishActivatingOsAccount(id);
1130 errCode = SendMsgForAccountActivate(osAccountInfo);
1131 if (errCode != ERR_OK) {
1132 RemoveLocalIdToOperating(id);
1133 ACCOUNT_LOGE("update %{public}d account info failed, errCode %{public}d.", id, errCode);
1134 return errCode;
1135 }
1136 RemoveLocalIdToOperating(id);
1137 subscribeManagerPtr_->PublishActivatedOsAccount(id);
1138 ACCOUNT_LOGI("IInnerOsAccountManager ActivateOsAccount end");
1139 return ERR_OK;
1140 }
1141
SendMsgForAccountActivate(OsAccountInfo & osAccountInfo)1142 ErrCode IInnerOsAccountManager::SendMsgForAccountActivate(OsAccountInfo &osAccountInfo)
1143 {
1144 ErrCode errCode = OsAccountInterface::SendToStorageAccountStart(osAccountInfo);
1145 if (errCode != ERR_OK) {
1146 ACCOUNT_LOGE("account %{public}d call storage active failed, errCode %{public}d.",
1147 osAccountInfo.GetLocalId(), errCode);
1148 return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
1149 }
1150 errCode = OsAccountInterface::SendToAMSAccountStart(osAccountInfo);
1151 if (errCode != ERR_OK) {
1152 ACCOUNT_LOGE("account %{public}d call ams active failed, errCode %{public}d.",
1153 osAccountInfo.GetLocalId(), errCode);
1154 return ERR_OSACCOUNT_SERVICE_INNER_SEND_AM_ACCOUNT_SWITCH_ERROR;
1155 }
1156 // update info
1157 osAccountInfo.SetIsActived(true);
1158 int64_t time =
1159 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
1160 osAccountInfo.SetLastLoginTime(time);
1161 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1162 if (errCode != ERR_OK) {
1163 ACCOUNT_LOGE("update %{public}d account info failed, errCode %{public}d.",
1164 osAccountInfo.GetLocalId(), errCode);
1165 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1166 }
1167 RefreshActiveList(osAccountInfo.GetLocalId());
1168 OsAccountInterface::SendToCESAccountSwitched(osAccountInfo);
1169 ACCOUNT_LOGI("SendMsgForAccountActivate ok");
1170 return errCode;
1171 }
1172
StartOsAccount(const int id)1173 ErrCode IInnerOsAccountManager::StartOsAccount(const int id)
1174 {
1175 return ERR_OK;
1176 }
1177
StopOsAccount(const int id)1178 ErrCode IInnerOsAccountManager::StopOsAccount(const int id)
1179 {
1180 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1181 if (id == Constants::START_USER_ID) {
1182 ACCOUNT_LOGW("the %{public}d os account can't stop", id);
1183 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_STOP_ACTIVE_ERROR;
1184 }
1185
1186 if (IsLocalIdInOperating(id)) {
1187 ACCOUNT_LOGW("the %{public}d already in operating", id);
1188 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
1189 }
1190 AddLocalIdToOperating(id);
1191 if (!IsOsAccountIDInActiveList(id)) {
1192 RemoveLocalIdToOperating(id);
1193 ACCOUNT_LOGW("account is %{public}d already stop", id);
1194 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREADY_ACTIVE_ERROR;
1195 }
1196 // get information
1197 OsAccountInfo osAccountInfo;
1198 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1199 if (errCode != ERR_OK) {
1200 RemoveLocalIdToOperating(id);
1201 ACCOUNT_LOGW("cannot find os account info by id:%{public}d, errCode %{public}d.", id, errCode);
1202 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
1203 }
1204
1205 // check complete
1206 if (!osAccountInfo.GetIsCreateCompleted()) {
1207 RemoveLocalIdToOperating(id);
1208 ACCOUNT_LOGW("account %{public}d is not completed", id);
1209 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_IS_UNVERIFIED_ERROR;
1210 }
1211
1212 // check to be removed
1213 if (osAccountInfo.GetToBeRemoved()) {
1214 RemoveLocalIdToOperating(id);
1215 ACCOUNT_LOGW("account %{public}d will be removed, don't need to stop!", id);
1216 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1217 }
1218
1219 // stop
1220 errCode = SendMsgForAccountStop(osAccountInfo);
1221 if (errCode != ERR_OK) {
1222 RemoveLocalIdToOperating(id);
1223 ACCOUNT_LOGE("update %{public}d account info failed, errCode %{public}d.", id, errCode);
1224 return errCode;
1225 }
1226 RemoveLocalIdToOperating(id);
1227 ACCOUNT_LOGI("IInnerOsAccountManager ActivateOsAccount end");
1228 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1229 return ERR_OK;
1230 }
1231
GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber,int & id)1232 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id)
1233 {
1234 if (serialNumber ==
1235 Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID) {
1236 id = Constants::ADMIN_LOCAL_ID;
1237 return ERR_OK;
1238 }
1239 std::vector<OsAccountInfo> osAccountInfos;
1240 id = -1;
1241 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
1242 if (errCode != ERR_OK) {
1243 ACCOUNT_LOGE("get osaccount info list error");
1244 return ERR_OSACCOUNT_SERVICE_INNER_GET_ACCOUNT_LIST_ERROR;
1245 }
1246 for (auto it = osAccountInfos.begin(); it != osAccountInfos.end(); it++) {
1247 if (serialNumber == it->GetSerialNumber()) {
1248 id = it->GetLocalId();
1249 break;
1250 }
1251 }
1252 if (id == -1) {
1253 ACCOUNT_LOGE("cannot find id by serialNumber");
1254 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_ERROR;
1255 }
1256 return ERR_OK;
1257 }
1258
GetSerialNumberByOsAccountLocalId(const int & id,int64_t & serialNumber)1259 ErrCode IInnerOsAccountManager::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber)
1260 {
1261 OsAccountInfo osAccountInfo;
1262 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1263 if (errCode != ERR_OK) {
1264 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
1265 }
1266 serialNumber = osAccountInfo.GetSerialNumber();
1267 return ERR_OK;
1268 }
1269
SubscribeOsAccount(const OsAccountSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & eventListener)1270 ErrCode IInnerOsAccountManager::SubscribeOsAccount(
1271 const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
1272 {
1273 if (!subscribeManagerPtr_) {
1274 ACCOUNT_LOGE("subscribeManagerPtr_ is nullptr");
1275 return ERR_OSACCOUNT_SERVICE_SUBSCRIBE_MANAGER_PTR_IS_NULLPTR;
1276 }
1277
1278 auto subscribeInfoPtr = std::make_shared<OsAccountSubscribeInfo>(subscribeInfo);
1279 if (subscribeInfoPtr == nullptr) {
1280 ACCOUNT_LOGE("subscribeInfoPtr is nullptr");
1281 }
1282 return subscribeManagerPtr_->SubscribeOsAccount(subscribeInfoPtr, eventListener);
1283 }
1284
UnsubscribeOsAccount(const sptr<IRemoteObject> & eventListener)1285 ErrCode IInnerOsAccountManager::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
1286 {
1287 if (!subscribeManagerPtr_) {
1288 ACCOUNT_LOGE("controlManagerPtr_ is nullptr");
1289 return ERR_OSACCOUNT_SERVICE_SUBSCRIBE_MANAGER_PTR_IS_NULLPTR;
1290 }
1291 return subscribeManagerPtr_->UnsubscribeOsAccount(eventListener);
1292 }
1293
GetOsAccountSwitchMod()1294 OS_ACCOUNT_SWITCH_MOD IInnerOsAccountManager::GetOsAccountSwitchMod()
1295 {
1296 return Constants::NOW_OS_ACCOUNT_SWITCH_MOD;
1297 }
1298
IsOsAccountCompleted(const int id,bool & isOsAccountCompleted)1299 ErrCode IInnerOsAccountManager::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted)
1300 {
1301 OsAccountInfo osAccountInfo;
1302 (void)osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1303 isOsAccountCompleted = osAccountInfo.GetIsCreateCompleted();
1304 return ERR_OK;
1305 }
1306
SetOsAccountIsVerified(const int id,const bool isVerified)1307 ErrCode IInnerOsAccountManager::SetOsAccountIsVerified(const int id, const bool isVerified)
1308 {
1309 OsAccountInfo osAccountInfo;
1310 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1311 if (errCode != ERR_OK) {
1312 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
1313 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
1314 }
1315
1316 // to be removed, cannot change any thing
1317 if (osAccountInfo.GetToBeRemoved()) {
1318 ACCOUNT_LOGE("account %{public}d will be removed, cannot change verify state!", id);
1319 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1320 }
1321
1322 if (isVerified && !osAccountInfo.GetIsVerified()) {
1323 OsAccountInterface::PublishCommonEvent(osAccountInfo,
1324 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED, Constants::OPERATION_UNLOCK);
1325 }
1326
1327 osAccountInfo.SetIsVerified(isVerified);
1328 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1329 if (errCode != ERR_OK) {
1330 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d",
1331 errCode, osAccountInfo.GetLocalId());
1332 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1333 }
1334 return ERR_OK;
1335 }
1336
GetEventHandler(void)1337 ErrCode IInnerOsAccountManager::GetEventHandler(void)
1338 {
1339 if (!handler_) {
1340 handler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(OHOS::AppExecFwk::EventRunner::Create());
1341 if (handler_ == nullptr) {
1342 ACCOUNT_LOGE("failed to create event handler");
1343 return ERR_OSACCOUNT_SERVICE_CREATE_EVENT_HANDLER;
1344 }
1345 }
1346
1347 return ERR_OK;
1348 }
1349
IsAllowedCreateAdmin(bool & isAllowedCreateAdmin)1350 ErrCode IInnerOsAccountManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
1351 {
1352 return osAccountControl_->IsAllowedCreateAdmin(isAllowedCreateAdmin);
1353 }
1354
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)1355 ErrCode IInnerOsAccountManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
1356 int &createdOsAccountNum)
1357 {
1358 return osAccountControl_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
1359 }
1360
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)1361 ErrCode IInnerOsAccountManager::GetSerialNumberFromDatabase(const std::string& storeID,
1362 int64_t &serialNumber)
1363 {
1364 return osAccountControl_->GetSerialNumberFromDatabase(storeID, serialNumber);
1365 }
1366
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)1367 ErrCode IInnerOsAccountManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
1368 {
1369 return osAccountControl_->GetMaxAllowCreateIdFromDatabase(storeID, id);
1370 }
1371
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1372 ErrCode IInnerOsAccountManager::GetOsAccountFromDatabase(const std::string& storeID, const int id,
1373 OsAccountInfo &osAccountInfo)
1374 {
1375 return osAccountControl_->GetOsAccountFromDatabase(storeID, id, osAccountInfo);
1376 }
1377
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1378 ErrCode IInnerOsAccountManager::GetOsAccountListFromDatabase(const std::string& storeID,
1379 std::vector<OsAccountInfo> &osAccountList)
1380 {
1381 return osAccountControl_->GetOsAccountListFromDatabase(storeID, osAccountList);
1382 }
1383
AddLocalIdToOperating(int32_t localId)1384 void IInnerOsAccountManager::AddLocalIdToOperating(int32_t localId)
1385 {
1386 std::lock_guard<std::mutex> lock(operatingMutex_);
1387 operatingId_.push_back(localId);
1388 }
1389
RemoveLocalIdToOperating(int32_t localId)1390 void IInnerOsAccountManager::RemoveLocalIdToOperating(int32_t localId)
1391 {
1392 std::lock_guard<std::mutex> lock(operatingMutex_);
1393 auto it = std::find(operatingId_.begin(), operatingId_.end(), localId);
1394 if (it != operatingId_.end()) {
1395 operatingId_.erase(it);
1396 }
1397 }
1398
IsLocalIdInOperating(int32_t localId)1399 bool IInnerOsAccountManager::IsLocalIdInOperating(int32_t localId)
1400 {
1401 std::lock_guard<std::mutex> lock(operatingMutex_);
1402 return std::find(operatingId_.begin(), operatingId_.end(), localId) != operatingId_.end();
1403 }
1404
QueryActiveOsAccountIds(std::vector<int32_t> & ids)1405 ErrCode IInnerOsAccountManager::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
1406 {
1407 CopyFromActiveList(ids);
1408 return ERR_OK;
1409 }
1410
PushIdIntoActiveList(int32_t id)1411 void IInnerOsAccountManager::PushIdIntoActiveList(int32_t id)
1412 {
1413 std::lock_guard<std::mutex> lock(ativeMutex_);
1414 if (std::find(activeAccountId_.begin(), activeAccountId_.end(), id) == activeAccountId_.end()) {
1415 activeAccountId_.push_back(id);
1416 CountTrace(HITRACE_TAG_ACCOUNT_MANAGER, "activeId", (int64_t)id);
1417 }
1418 return;
1419 }
1420
EraseIdFromActiveList(int32_t id)1421 void IInnerOsAccountManager::EraseIdFromActiveList(int32_t id)
1422 {
1423 std::lock_guard<std::mutex> lock(ativeMutex_);
1424 if (std::find(activeAccountId_.begin(), activeAccountId_.end(), id) != activeAccountId_.end()) {
1425 ACCOUNT_LOGE("EraseIdFromActiveList enter0");
1426 activeAccountId_.erase(
1427 std::remove(activeAccountId_.begin(), activeAccountId_.end(), id), activeAccountId_.end());
1428 } else {
1429 ACCOUNT_LOGI("os account is not in active list, no need to erase!");
1430 }
1431 CountTrace(HITRACE_TAG_ACCOUNT_MANAGER, "deActiveId", (int64_t)id);
1432 }
1433
IsOsAccountIDInActiveList(int32_t id)1434 bool IInnerOsAccountManager::IsOsAccountIDInActiveList(int32_t id)
1435 {
1436 std::lock_guard<std::mutex> lock(ativeMutex_);
1437 auto it = std::find(activeAccountId_.begin(), activeAccountId_.end(), id);
1438 return (it != activeAccountId_.end());
1439 }
1440
CopyFromActiveList(std::vector<int32_t> & idList)1441 void IInnerOsAccountManager::CopyFromActiveList(std::vector<int32_t>& idList)
1442 {
1443 idList.clear();
1444 std::lock_guard<std::mutex> lock(ativeMutex_);
1445 for (auto it = activeAccountId_.begin(); it != activeAccountId_.end(); it++) {
1446 idList.push_back(*it);
1447 }
1448 }
1449
RefreshActiveList(int32_t newId)1450 void IInnerOsAccountManager::RefreshActiveList(int32_t newId)
1451 {
1452 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1453 PushIdIntoActiveList(newId);
1454 return;
1455 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
1456 // deactivate old ids first
1457 for (size_t i = 0; i < activeAccountId_.size(); ++i) {
1458 DeActivateOsAccount(activeAccountId_[i]);
1459 }
1460 int32_t oldId = (activeAccountId_.empty() ? -1 : activeAccountId_[0]);
1461 ReportOsAccountSwitch(newId, oldId);
1462 activeAccountId_.clear();
1463 PushIdIntoActiveList(newId);
1464 }
1465 } // namespace AccountSA
1466 } // namespace OHOS
1467