1 /* 2 * Copyright (c) 2021-2024 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_event_provider.h" 17 #include <chrono> 18 #include <dlfcn.h> 19 #include <unistd.h> 20 #include "account_constants.h" 21 #include "account_info.h" 22 #include "account_info_report.h" 23 #include "account_log_wrapper.h" 24 #include "os_account_info.h" 25 #ifdef HAS_CES_PART 26 #include "common_event_support.h" 27 #endif // HAS_CES_PART 28 #include "domain_account_callback_service.h" 29 #include "hitrace_adapter.h" 30 #include "account_hisysevent_adapter.h" 31 #include "app_account_control_manager.h" 32 #include "ohos_account_kits.h" 33 #include "os_account_constants.h" 34 #include "os_account_control_file_manager.h" 35 #include "os_account_domain_account_callback.h" 36 #include "os_account_subscribe_manager.h" 37 #include "parameter.h" 38 #include "parcel.h" 39 #include <pthread.h> 40 #include <thread> 41 #include <unordered_set> 42 #ifdef HICOLLIE_ENABLE 43 #include "account_timer.h" 44 #include "xcollie/xcollie.h" 45 #endif // HICOLLIE_ENABLE 46 47 namespace OHOS { 48 namespace AccountSA { 49 namespace { 50 const std::string CONSTRAINT_CREATE_ACCOUNT_DIRECTLY = "constraint.os.account.create.directly"; 51 const std::string ACCOUNT_READY_EVENT = "bootevent.account.ready"; 52 const std::string PARAM_LOGIN_NAME_MAX = "persist.account.login_name_max"; 53 const std::string SPECIAL_CHARACTER_ARRAY = "<>|\":*?/\\"; 54 const std::vector<std::string> SHORT_NAME_CANNOT_BE_NAME_ARRAY = {".", ".."}; 55 constexpr int32_t DELAY_FOR_EXCEPTION = 50; 56 constexpr int32_t MAX_RETRY_TIMES = 50; 57 constexpr int32_t MAX_PRIVATE_TYPE_NUMBER = 1; 58 constexpr int32_t DELAY_FOR_REMOVING_FOREGROUND_OS_ACCOUNT = 1500; 59 } 60 GetDomainAccountStatus(OsAccountInfo & osAccountInfo)61 static ErrCode GetDomainAccountStatus(OsAccountInfo &osAccountInfo) 62 { 63 DomainAccountInfo domainAccountInfo; 64 osAccountInfo.GetDomainInfo(domainAccountInfo); 65 DomainAccountInfo resultInfo; 66 ErrCode errCode = InnerDomainAccountManager::GetInstance().GetDomainAccountInfo(domainAccountInfo, resultInfo); 67 if (errCode != ERR_OK) { 68 return errCode; 69 } 70 if (!resultInfo.isAuthenticated) { 71 domainAccountInfo.status_ = DomainAccountStatus::LOGOUT; 72 } else { 73 bool isActivated = false; 74 (void)IInnerOsAccountManager::GetInstance().IsOsAccountActived(osAccountInfo.GetLocalId(), isActivated); 75 domainAccountInfo.status_ = isActivated ? DomainAccountStatus::LOGIN : DomainAccountStatus::LOGIN_BACKGROUND; 76 } 77 osAccountInfo.SetDomainInfo(domainAccountInfo); 78 return ERR_OK; 79 } 80 IInnerOsAccountManager()81 IInnerOsAccountManager::IInnerOsAccountManager() : subscribeManager_(OsAccountSubscribeManager::GetInstance()), 82 pluginManager_(OsAccountPluginManager::GetInstance()) 83 { 84 activeAccountId_.clear(); 85 operatingId_.clear(); 86 osAccountControl_ = std::make_shared<OsAccountControlFileManager>(); 87 osAccountControl_->Init(); 88 osAccountControl_->GetDeviceOwnerId(deviceOwnerId_); 89 osAccountControl_->GetDefaultActivatedOsAccount(defaultActivatedId_); 90 osAccountControl_->GetOsAccountConfig(config_); 91 ACCOUNT_LOGI("Init end, maxOsAccountNum: %{public}d, maxLoggedInOsAccountNum: %{public}d", 92 config_.maxOsAccountNum, config_.maxLoggedInOsAccountNum); 93 } 94 GetInstance()95 IInnerOsAccountManager &IInnerOsAccountManager::GetInstance() 96 { 97 static IInnerOsAccountManager *instance = new (std::nothrow) IInnerOsAccountManager(); 98 return *instance; 99 } 100 SetOsAccountControl(std::shared_ptr<IOsAccountControl> ptr)101 void IInnerOsAccountManager::SetOsAccountControl(std::shared_ptr<IOsAccountControl> ptr) 102 { 103 osAccountControl_ = ptr; 104 } 105 CreateBaseAdminAccount()106 void IInnerOsAccountManager::CreateBaseAdminAccount() 107 { 108 ACCOUNT_LOGI("start to create admin account"); 109 bool isExistsAccount = false; 110 osAccountControl_->IsOsAccountExists(Constants::ADMIN_LOCAL_ID, isExistsAccount); 111 if (!isExistsAccount) { 112 int64_t serialNumber = 113 Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID; 114 OsAccountInfo osAccountInfo( 115 Constants::ADMIN_LOCAL_ID, Constants::ADMIN_LOCAL_NAME, OsAccountType::ADMIN, serialNumber); 116 int64_t time = 117 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) 118 .count(); 119 osAccountInfo.SetCreateTime(time); 120 osAccountInfo.SetIsCreateCompleted(true); 121 osAccountInfo.SetIsActived(true); // admin local account is always active 122 osAccountControl_->InsertOsAccount(osAccountInfo); 123 ReportOsAccountLifeCycle(osAccountInfo.GetLocalId(), Constants::OPERATION_CREATE); 124 ACCOUNT_LOGI("OsAccountAccountMgr created admin account end"); 125 } else { 126 ACCOUNT_LOGI("OsAccountAccountMgr admin account already exists"); 127 } 128 } 129 CreateBaseStandardAccount()130 void IInnerOsAccountManager::CreateBaseStandardAccount() 131 { 132 ACCOUNT_LOGI("start to create base account"); 133 int64_t serialNumber = 0; 134 osAccountControl_->GetSerialNumber(serialNumber); 135 #ifdef ENABLE_DEFAULT_ADMIN_NAME 136 OsAccountInfo osAccountInfo(Constants::START_USER_ID, Constants::STANDARD_LOCAL_NAME, 137 OsAccountType::ADMIN, serialNumber); 138 #else 139 OsAccountInfo osAccountInfo(Constants::START_USER_ID, "", OsAccountType::ADMIN, serialNumber); 140 #endif //ENABLE_DEFAULT_ADMIN_NAME 141 std::vector<std::string> constraints; 142 ErrCode errCode = osAccountControl_->GetConstraintsByType(OsAccountType::ADMIN, constraints); 143 if (errCode != ERR_OK) { 144 ACCOUNT_LOGE("Fail to get constraints by type for the system OS account, errCode %{public}d.", errCode); 145 return; 146 } 147 osAccountInfo.SetConstraints(constraints); 148 int64_t time = 149 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) 150 .count(); 151 osAccountInfo.SetCreateTime(time); 152 osAccountInfo.SetIsCreateCompleted(false); 153 osAccountInfo.SetIsDataRemovable(false); 154 osAccountControl_->InsertOsAccount(osAccountInfo); 155 if (SendMsgForAccountCreate(osAccountInfo) != ERR_OK) { 156 ACCOUNT_LOGE("First OS account not created completely"); 157 return; 158 } 159 SetDefaultActivatedOsAccount(Constants::START_USER_ID); 160 ACCOUNT_LOGI("OsAccountAccountMgr created base account end"); 161 } 162 RetryToGetAccount(OsAccountInfo & osAccountInfo)163 void IInnerOsAccountManager::RetryToGetAccount(OsAccountInfo &osAccountInfo) 164 { 165 int32_t retryTimes = 0; 166 while (retryTimes < MAX_RETRY_TIMES) { 167 std::vector<OsAccountInfo> osAccountInfos; 168 QueryAllCreatedOsAccounts(osAccountInfos); 169 if (!osAccountInfos.empty() && (IsValidOsAccount(osAccountInfos[0]) == ERR_OK)) { 170 osAccountInfo = osAccountInfos[0]; 171 return; 172 } 173 ACCOUNT_LOGE("fail to query accounts"); 174 retryTimes++; 175 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_EXCEPTION)); 176 } 177 178 #ifdef ENABLE_DEFAULT_ADMIN_NAME 179 while (CreateOsAccount(Constants::STANDARD_LOCAL_NAME, ADMIN, osAccountInfo) != ERR_OK) { 180 #else 181 while (CreateOsAccount("", ADMIN, osAccountInfo) != ERR_OK) { 182 #endif 183 ACCOUNT_LOGE("fail to create account"); 184 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_EXCEPTION)); 185 } 186 } 187 188 ErrCode IInnerOsAccountManager::GetRealOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo) 189 { 190 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 191 if (errCode != ERR_OK) { 192 return errCode; 193 } 194 195 bool isVerified = false; 196 verifiedAccounts_.Find(id, isVerified); 197 osAccountInfo.SetIsVerified(isVerified); 198 199 bool isLoggedIn = false; 200 loggedInAccounts_.Find(id, isLoggedIn); 201 osAccountInfo.SetIsLoggedIn(isLoggedIn); 202 203 bool isActivated = IsOsAccountIDInActiveList(id); 204 osAccountInfo.SetIsActived(isActivated); 205 206 int32_t foregroundId = -1; 207 foregroundAccountMap_.Find(Constants::DEFAULT_DISPALY_ID, foregroundId); 208 osAccountInfo.SetIsForeground(foregroundId == id); 209 210 return ERR_OK; 211 } 212 213 ErrCode IInnerOsAccountManager::ActivateDefaultOsAccount() 214 { 215 #ifdef HICOLLIE_ENABLE 216 int timerId = 217 HiviewDFX::XCollie::GetInstance().SetTimer(TIMER_NAME, TIMEOUT, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG); 218 #endif // HICOLLIE_ENABLE 219 ACCOUNT_LOGI("start to activate default account"); 220 OsAccountInfo osAccountInfo; 221 ErrCode errCode = GetRealOsAccountInfoById(defaultActivatedId_, osAccountInfo); 222 if ((errCode != ERR_OK) || (IsValidOsAccount(osAccountInfo) != ERR_OK)) { 223 ACCOUNT_LOGE("account not found, localId: %{public}d, error: %{public}d", defaultActivatedId_, errCode); 224 ReportOsAccountOperationFail(defaultActivatedId_, Constants::OPERATION_ACTIVATE, errCode, 225 "Account not found or to be removed"); 226 RetryToGetAccount(osAccountInfo); 227 ReportOsAccountOperationFail(osAccountInfo.GetLocalId(), Constants::OPERATION_ACTIVATE, errCode, 228 "Retry to get account"); 229 SetDefaultActivatedOsAccount(osAccountInfo.GetLocalId()); 230 } 231 // activate 232 errCode = SendMsgForAccountActivate(osAccountInfo); 233 if (errCode == ERR_OK) { 234 SetParameter(ACCOUNT_READY_EVENT.c_str(), "true"); 235 } 236 #ifdef HICOLLIE_ENABLE 237 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId); 238 #endif // HICOLLIE_ENABLE 239 return errCode; 240 } 241 242 void IInnerOsAccountManager::RestartActiveAccount() 243 { 244 // query active account to restart and refresh into list 245 std::vector<OsAccountInfo> osAccountInfos; 246 if (QueryAllCreatedOsAccounts(osAccountInfos) != ERR_OK) { 247 return; 248 } 249 for (size_t i = 0; i < osAccountInfos.size(); ++i) { 250 OsAccountInfo osAccountInfo = osAccountInfos[i]; 251 std::int32_t id = osAccountInfo.GetLocalId(); 252 if (osAccountInfo.GetIsActived() && id != Constants::START_USER_ID) { 253 // reactivate account state 254 if (ActivateOsAccount(id) != ERR_OK) { 255 ACCOUNT_LOGE("active base account failed"); 256 return; 257 } 258 } 259 } 260 } 261 262 void IInnerOsAccountManager::ResetAccountStatus(void) 263 { 264 std::vector<int32_t> idList; 265 (void) osAccountControl_->GetOsAccountIdList(idList); 266 for (const auto id : idList) { 267 DeactivateOsAccountById(id); 268 } 269 } 270 271 ErrCode IInnerOsAccountManager::PrepareOsAccountInfo(const std::string &name, const OsAccountType &type, 272 const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo) 273 { 274 return PrepareOsAccountInfo(name, "", type, domainInfo, osAccountInfo); 275 } 276 277 ErrCode IInnerOsAccountManager::PrepareOsAccountInfo(const std::string &localName, const std::string &shortName, 278 const OsAccountType &type, const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo) 279 { 280 ErrCode errCode = FillOsAccountInfo(localName, shortName, type, domainInfo, osAccountInfo); 281 if (errCode != ERR_OK) { 282 return errCode; 283 } 284 errCode = ValidateOsAccount(osAccountInfo); 285 if (errCode != ERR_OK) { 286 ACCOUNT_LOGE("account name already exist, errCode %{public}d.", errCode); 287 return errCode; 288 } 289 errCode = CheckTypeNumber(type); 290 if (errCode != ERR_OK) { 291 ACCOUNT_LOGE("Check type number failed."); 292 return errCode; 293 } 294 if (!CheckAndAddLocalIdOperating(osAccountInfo.GetLocalId())) { 295 ACCOUNT_LOGW("Account id = %{public}d already in operating", osAccountInfo.GetLocalId()); 296 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 297 } 298 errCode = osAccountControl_->InsertOsAccount(osAccountInfo); 299 if (errCode != ERR_OK) { 300 ACCOUNT_LOGE("insert os account info err, errCode %{public}d.", errCode); 301 return errCode; 302 } 303 osAccountControl_->UpdateAccountIndex(osAccountInfo, false); 304 305 errCode = osAccountControl_->UpdateBaseOAConstraints(std::to_string(osAccountInfo.GetLocalId()), 306 osAccountInfo.GetConstraints(), true); 307 if (errCode != ERR_OK) { 308 ACCOUNT_LOGE("UpdateBaseOAConstraints err"); 309 return errCode; 310 } 311 return ERR_OK; 312 } 313 314 ErrCode IInnerOsAccountManager::FillOsAccountInfo(const std::string &localName, const std::string &shortName, 315 const OsAccountType &type, const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo) 316 { 317 int64_t serialNumber; 318 ErrCode errCode = osAccountControl_->GetSerialNumber(serialNumber); 319 if (errCode != ERR_OK) { 320 ACCOUNT_LOGE("failed to GetSerialNumber, errCode %{public}d.", errCode); 321 return errCode; 322 } 323 int id = 0; 324 errCode = osAccountControl_->GetAllowCreateId(id); 325 if (errCode != ERR_OK) { 326 ACCOUNT_LOGE("failed to GetAllowCreateId, errCode %{public}d.", errCode); 327 return errCode; 328 } 329 std::vector<std::string> constraints; 330 constraints.clear(); 331 errCode = osAccountControl_->GetConstraintsByType(type, constraints); 332 if (errCode != ERR_OK) { 333 ACCOUNT_LOGE("Failed to GetConstraintsByType, errCode %{public}d.", errCode); 334 return errCode; 335 } 336 337 osAccountInfo = OsAccountInfo(id, localName, shortName, type, serialNumber); 338 osAccountInfo.SetConstraints(constraints); 339 int64_t time = 340 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count(); 341 osAccountInfo.SetCreateTime(time); 342 if (!osAccountInfo.SetDomainInfo(domainInfo)) { 343 ACCOUNT_LOGE("failed to SetDomainInfo"); 344 return ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR; 345 } 346 return ERR_OK; 347 } 348 349 ErrCode IInnerOsAccountManager::PrepareOsAccountInfoWithFullInfo(OsAccountInfo &osAccountInfo) 350 { 351 int64_t serialNumber; 352 ErrCode errCode = osAccountControl_->GetSerialNumber(serialNumber); 353 if (errCode != ERR_OK) { 354 ACCOUNT_LOGE("Failed to GetSerialNumber, errCode %{public}d.", errCode); 355 return errCode; 356 } 357 osAccountInfo.SetSerialNumber(serialNumber); 358 osAccountInfo.SetIsDataRemovable(false); 359 errCode = osAccountControl_->SetNextLocalId(osAccountInfo.GetLocalId() + 1); 360 if (errCode != ERR_OK) { 361 ACCOUNT_LOGE("Failed to SetNextLocalId, errCode %{public}d.", errCode); 362 return errCode; 363 } 364 errCode = osAccountControl_->InsertOsAccount(osAccountInfo); 365 if ((errCode != ERR_OK) && (errCode != ERR_OSACCOUNT_SERVICE_CONTROL_INSERT_FILE_EXISTS_ERROR)) { 366 ACCOUNT_LOGE("Insert os account info err, errCode %{public}d.", errCode); 367 return errCode; 368 } 369 370 osAccountControl_->UpdateAccountIndex(osAccountInfo, false); 371 372 std::vector<std::string> constraints; 373 constraints.clear(); 374 OsAccountType type = static_cast<OsAccountType>(osAccountInfo.GetType()); 375 errCode = osAccountControl_->GetConstraintsByType(type, constraints); 376 if (errCode != ERR_OK) { 377 ACCOUNT_LOGE("Failed to GetConstraintsByType, errCode %{public}d.", errCode); 378 return errCode; 379 } 380 std::vector<std::string> constraintsExists = osAccountInfo.GetConstraints(); 381 std::vector<std::string> constraintsResult; 382 std::merge(constraints.begin(), constraints.end(), constraintsExists.begin(), constraintsExists.end(), 383 std::back_inserter(constraintsResult)); 384 osAccountInfo.SetConstraints(constraintsResult); 385 errCode = osAccountControl_->UpdateBaseOAConstraints( 386 std::to_string(osAccountInfo.GetLocalId()), constraintsResult, true); 387 if (errCode != ERR_OK) { 388 ACCOUNT_LOGE("UpdateBaseOAConstraints err"); 389 return errCode; 390 } 391 return ERR_OK; 392 } 393 394 bool IInnerOsAccountManager::CheckAndCleanOsAccounts() 395 { 396 unsigned int osAccountNum = 0; 397 GetCreatedOsAccountsCount(osAccountNum); 398 399 if (osAccountNum < config_.maxOsAccountNum) { 400 return true; 401 } 402 403 ACCOUNT_LOGI("The number of OS accounts has oversize, attempting to clean garbage accounts."); 404 if (CleanGarbageOsAccounts() <= 0) { 405 ACCOUNT_LOGE("The number of OS accounts still oversize after cleaning, max num: %{public}d", 406 config_.maxOsAccountNum); 407 return false; 408 } 409 return true; 410 } 411 412 ErrCode IInnerOsAccountManager::SendMsgForAccountCreate( 413 OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options) 414 { 415 ErrCode errCode = OsAccountInterface::SendToStorageAccountCreate(osAccountInfo); 416 if (errCode != ERR_OK) { 417 ACCOUNT_LOGE("create os account SendToStorageAccountCreate failed, errCode %{public}d.", errCode); 418 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER; 419 } 420 int32_t localId = osAccountInfo.GetLocalId(); 421 #ifdef HAS_THEME_SERVICE_PART 422 auto task = [localId] { OsAccountInterface::InitThemeResource(localId); }; 423 std::thread theme_thread(task); 424 pthread_setname_np(theme_thread.native_handle(), "InitTheme"); 425 #endif 426 errCode = OsAccountInterface::SendToBMSAccountCreate(osAccountInfo, options.disallowedHapList); 427 if (errCode != ERR_OK) { 428 ACCOUNT_LOGE("create os account SendToBMSAccountCreate failed, errCode %{public}d.", errCode); 429 if (osAccountInfo.GetIsDataRemovable()) { 430 (void)OsAccountInterface::SendToStorageAccountRemove(osAccountInfo); 431 } 432 #ifdef HAS_THEME_SERVICE_PART 433 if (theme_thread.joinable()) { 434 theme_thread.join(); 435 } 436 #endif 437 return errCode; 438 } 439 #ifdef HAS_THEME_SERVICE_PART 440 if (theme_thread.joinable()) { 441 theme_thread.join(); 442 } 443 #endif 444 AppAccountControlManager::GetInstance().SetOsAccountRemoved(osAccountInfo.GetLocalId(), false); 445 osAccountInfo.SetIsCreateCompleted(true); 446 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 447 if (errCode != ERR_OK) { 448 ACCOUNT_LOGE("create os account when update isCreateCompleted"); 449 ReportOsAccountOperationFail(localId, Constants::OPERATION_CREATE, errCode, "Failed to update OS account"); 450 if (osAccountInfo.GetIsDataRemovable()) { 451 (void)OsAccountInterface::SendToBMSAccountDelete(osAccountInfo); 452 (void)OsAccountInterface::SendToStorageAccountRemove(osAccountInfo); 453 } 454 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 455 } 456 (void)OsAccountInterface::SendToStorageAccountCreateComplete(localId); 457 ReportOsAccountLifeCycle(localId, Constants::OPERATION_CREATE); 458 OsAccountInterface::SendToCESAccountCreate(osAccountInfo); 459 subscribeManager_.Publish(localId, OS_ACCOUNT_SUBSCRIBE_TYPE::CREATED); 460 ACCOUNT_LOGI("OsAccountAccountMgr send to storage and bm for start success"); 461 return ERR_OK; 462 } 463 464 ErrCode IInnerOsAccountManager::CreateOsAccount( 465 const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo) 466 { 467 if (!pluginManager_.IsCreationAllowed()) { 468 ACCOUNT_LOGI("Not allow creation account."); 469 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_PLUGIN_NOT_ALLOWED_CREATION_ERROR; 470 } 471 #ifdef HICOLLIE_ENABLE 472 AccountTimer timer; 473 #endif // HICOLLIE_ENABLE 474 if (!CheckAndCleanOsAccounts()) { 475 return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR; 476 } 477 DomainAccountInfo domainInfo; // default empty domain info 478 ErrCode errCode = PrepareOsAccountInfo(name, type, domainInfo, osAccountInfo); 479 if (errCode != ERR_OK) { 480 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 481 return errCode; 482 } 483 errCode = SendMsgForAccountCreate(osAccountInfo); 484 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 485 if (errCode != ERR_OK) { 486 (void)osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId()); 487 } 488 return errCode; 489 } 490 491 ErrCode IInnerOsAccountManager::CreateOsAccount(const std::string &localName, const std::string &shortName, 492 const OsAccountType &type, OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options) 493 { 494 if (!pluginManager_.IsCreationAllowed()) { 495 ACCOUNT_LOGI("Not allow creation account."); 496 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_PLUGIN_NOT_ALLOWED_CREATION_ERROR; 497 } 498 #ifdef HICOLLIE_ENABLE 499 AccountTimer timer; 500 #endif // HICOLLIE_ENABLE 501 osAccountInfo.SetLocalName(localName); 502 #ifdef ENABLE_ACCOUNT_SHORT_NAME 503 OsAccountInfo accountInfoOld; 504 ErrCode code = QueryOsAccountById(Constants::START_USER_ID, accountInfoOld); 505 if (code != ERR_OK) { 506 ACCOUNT_LOGE("QueryOsAccountById error, errCode %{public}d.", code); 507 return code; 508 } 509 if (accountInfoOld.GetShortName().empty()) { 510 accountInfoOld.SetType(type); 511 accountInfoOld.SetLocalName(localName); 512 accountInfoOld.SetShortName(shortName); 513 code = osAccountControl_->UpdateOsAccount(accountInfoOld); 514 if (code != ERR_OK) { 515 ReportOsAccountOperationFail(Constants::START_USER_ID, Constants::OPERATION_CREATE, code, 516 "Failed to update OS account"); 517 } 518 osAccountControl_->UpdateAccountIndex(accountInfoOld, false); 519 osAccountInfo = accountInfoOld; 520 return code; 521 } 522 #endif // ENABLE_ACCOUNT_SHORT_NAME 523 if (!CheckAndCleanOsAccounts()) { 524 return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR; 525 } 526 DomainAccountInfo domainInfo; // default empty domain info 527 ErrCode errCode = PrepareOsAccountInfo(localName, shortName, type, domainInfo, osAccountInfo); 528 if (errCode != ERR_OK) { 529 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 530 return errCode; 531 } 532 errCode = SendMsgForAccountCreate(osAccountInfo, options); 533 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 534 if (errCode != ERR_OK) { 535 (void)osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId()); 536 } 537 return errCode; 538 } 539 540 ErrCode IInnerOsAccountManager::ValidateShortName(const std::string &shortName) 541 { 542 size_t shortNameSize = shortName.size(); 543 if (shortNameSize == 0 || shortNameSize > Constants::SHORT_NAME_MAX_SIZE) { 544 ACCOUNT_LOGE("CreateOsAccount short name length %{public}zu is invalid!", shortNameSize); 545 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 546 } 547 548 if (shortName.find_first_of(SPECIAL_CHARACTER_ARRAY) != std::string::npos) { 549 ACCOUNT_LOGE("CreateOsAccount short name is invalidate, short name is %{public}s !", shortName.c_str()); 550 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 551 } 552 553 for (size_t i = 0; i < SHORT_NAME_CANNOT_BE_NAME_ARRAY.size(); i++) { 554 if (shortName == SHORT_NAME_CANNOT_BE_NAME_ARRAY[i]) { 555 ACCOUNT_LOGE("CreateOsAccount short name is invalidate, short name is %{public}s !", shortName.c_str()); 556 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 557 } 558 } 559 return ERR_OK; 560 } 561 562 ErrCode IInnerOsAccountManager::CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo, 563 const CreateOsAccountOptions &options) 564 { 565 if (!pluginManager_.IsCreationAllowed()) { 566 ACCOUNT_LOGI("Not allow creation account."); 567 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_PLUGIN_NOT_ALLOWED_CREATION_ERROR; 568 } 569 #ifdef HICOLLIE_ENABLE 570 AccountTimer timer; 571 #endif // HICOLLIE_ENABLE 572 if (!CheckAndAddLocalIdOperating(osAccountInfo.GetLocalId())) { 573 ACCOUNT_LOGW("Account id = %{public}d already in operating", osAccountInfo.GetLocalId()); 574 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 575 } 576 ErrCode errCode = PrepareOsAccountInfoWithFullInfo(osAccountInfo); 577 if (errCode != ERR_OK) { 578 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 579 return errCode; 580 } 581 errCode = SendMsgForAccountCreate(osAccountInfo, options); 582 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 583 if (errCode != ERR_OK) { 584 (void)osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId()); 585 } 586 return errCode; 587 } 588 589 ErrCode IInnerOsAccountManager::UpdateAccountStatusForDomain(const int id, DomainAccountStatus status) 590 { 591 OsAccountInfo accountInfo; 592 DomainAccountInfo domainInfo; 593 ErrCode errCode = GetOsAccountInfoById(id, accountInfo); 594 if (errCode != ERR_OK) { 595 return errCode; 596 } 597 accountInfo.GetDomainInfo(domainInfo); 598 domainInfo.status_ = status; 599 accountInfo.SetDomainInfo(domainInfo); 600 601 errCode = osAccountControl_->UpdateOsAccount(accountInfo); 602 if (errCode != ERR_OK) { 603 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, accountInfo.GetLocalId()); 604 return errCode; 605 } 606 return ERR_OK; 607 } 608 609 ErrCode IInnerOsAccountManager::UpdateOsAccountWithFullInfo(OsAccountInfo &newInfo) 610 { 611 int32_t localId = newInfo.GetLocalId(); 612 if (!CheckAndAddLocalIdOperating(localId)) { 613 ACCOUNT_LOGE("the %{public}d already in operating", localId); 614 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 615 } 616 OsAccountInfo oldInfo; 617 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(localId, oldInfo); 618 if (errCode != ERR_OK) { 619 RemoveLocalIdToOperating(localId); 620 return errCode; 621 } 622 oldInfo.SetLocalName(newInfo.GetLocalName()); 623 oldInfo.SetType(newInfo.GetType()); 624 oldInfo.SetPhoto(newInfo.GetPhoto()); 625 oldInfo.SetConstraints(newInfo.GetConstraints()); 626 errCode = osAccountControl_->UpdateOsAccount(oldInfo); 627 osAccountControl_->UpdateAccountIndex(oldInfo, false); 628 newInfo = oldInfo; 629 if (errCode != ERR_OK) { 630 ReportOsAccountOperationFail(localId, Constants::OPERATION_UPDATE, errCode, "UpdateOsAccount failed!"); 631 } else { 632 OsAccountInterface::PublishCommonEvent(oldInfo, 633 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE); 634 } 635 RemoveLocalIdToOperating(localId); 636 return errCode; 637 } 638 639 bool IInnerOsAccountManager::CheckDomainAccountBound( 640 const std::vector<OsAccountInfo> &osAccountInfos, const DomainAccountInfo &info) 641 { 642 for (size_t i = 0; i < osAccountInfos.size(); ++i) { 643 DomainAccountInfo curInfo; 644 osAccountInfos[i].GetDomainInfo(curInfo); 645 if ((!info.accountId_.empty() && curInfo.accountId_ == info.accountId_) || 646 ((curInfo.accountName_ == info.accountName_) && (curInfo.domain_ == info.domain_))) { 647 return true; 648 } 649 } 650 return false; 651 } 652 653 ErrCode IInnerOsAccountManager::BindDomainAccount(const OsAccountType &type, 654 const DomainAccountInfo &domainAccountInfo, OsAccountInfo &osAccountInfo, 655 const CreateOsAccountForDomainOptions &options) 656 { 657 std::vector<OsAccountInfo> osAccountInfos; 658 (void)QueryAllCreatedOsAccounts(osAccountInfos); 659 if (CheckDomainAccountBound(osAccountInfos, domainAccountInfo)) { 660 ACCOUNT_LOGE("the domain account is already bound"); 661 return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR; 662 } 663 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS 664 bool isEnabled = false; 665 (void)IsOsAccountConstraintEnable(Constants::START_USER_ID, CONSTRAINT_CREATE_ACCOUNT_DIRECTLY, isEnabled); 666 #else 667 bool isEnabled = true; 668 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS 669 if (isEnabled && (osAccountInfos.size() == 1) && (osAccountInfos[0].GetLocalId() == Constants::START_USER_ID)) { 670 DomainAccountInfo curDomainInfo; 671 osAccountInfos[0].GetDomainInfo(curDomainInfo); 672 if (curDomainInfo.domain_.empty()) { 673 osAccountInfos[0].SetLocalName(domainAccountInfo.accountName_); 674 osAccountInfos[0].SetShortName(options.shortName); 675 osAccountInfos[0].SetDomainInfo(domainAccountInfo); 676 osAccountInfo = osAccountInfos[0]; 677 } 678 } 679 if (osAccountInfo.GetLocalId() != Constants::START_USER_ID) { 680 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS 681 ErrCode errCode = PrepareOsAccountInfo(domainAccountInfo.accountName_, options.shortName, 682 type, domainAccountInfo, osAccountInfo); 683 if (errCode != ERR_OK) { 684 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 685 return errCode; 686 } 687 RemoveLocalIdToOperating(osAccountInfo.GetLocalId()); 688 #else 689 ACCOUNT_LOGW("multiple os accounts feature not enabled"); 690 return ERR_OSACCOUNT_SERVICE_MANAGER_NOT_ENABLE_MULTI_ERROR; 691 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS 692 } 693 ErrCode errCode = osAccountControl_->UpdateAccountIndex(osAccountInfo, false); 694 if (errCode != ERR_OK) { 695 ACCOUNT_LOGE("Failed to update account index."); 696 return errCode; 697 } 698 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 699 if (errCode != ERR_OK) { 700 ACCOUNT_LOGE("Failed to update osaccount."); 701 } 702 return errCode; 703 } 704 705 ErrCode IInnerOsAccountManager::CreateOsAccountForDomain( 706 const OsAccountType &type, const DomainAccountInfo &domainInfo, const sptr<IDomainAccountCallback> &callback, 707 const CreateOsAccountForDomainOptions &options) 708 { 709 if (!pluginManager_.IsCreationAllowed()) { 710 ACCOUNT_LOGI("Not allow creation account."); 711 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_PLUGIN_NOT_ALLOWED_CREATION_ERROR; 712 } 713 #ifdef HICOLLIE_ENABLE 714 AccountTimer timer; 715 #endif // HICOLLIE_ENABLE 716 std::vector<OsAccountInfo> osAccountInfos; 717 (void)QueryAllCreatedOsAccounts(osAccountInfos); 718 if (CheckDomainAccountBound(osAccountInfos, domainInfo)) { 719 ACCOUNT_LOGE("the domain account is already bound"); 720 return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR; 721 } 722 if (osAccountInfos.size() >= config_.maxOsAccountNum) { 723 ACCOUNT_LOGE("The number of OS accounts has oversize, max num: %{public}d", config_.maxOsAccountNum); 724 return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR; 725 } 726 if (!InnerDomainAccountManager::GetInstance().IsPluginAvailable()) { 727 ACCOUNT_LOGE("plugin is not available"); 728 return ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST; 729 } 730 sptr<CheckAndCreateDomainAccountCallback> callbackWrapper = 731 new (std::nothrow) CheckAndCreateDomainAccountCallback(osAccountControl_, type, callback, options); 732 if (callbackWrapper == nullptr) { 733 ACCOUNT_LOGE("new DomainCreateDomainCallback failed"); 734 return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR; 735 } 736 return InnerDomainAccountManager::GetInstance().GetDomainAccountInfo(domainInfo, callbackWrapper); 737 } 738 739 void IInnerOsAccountManager::CheckAndRefreshLocalIdRecord(const int id) 740 { 741 if (id == defaultActivatedId_) { 742 ACCOUNT_LOGI("remove default activated id %{public}d", id); 743 osAccountControl_->SetDefaultActivatedOsAccount(Constants::START_USER_ID); 744 defaultActivatedId_ = Constants::START_USER_ID; 745 } 746 if (id == deviceOwnerId_) { 747 osAccountControl_->UpdateDeviceOwnerId(-1); 748 } 749 return; 750 } 751 752 ErrCode IInnerOsAccountManager::PrepareRemoveOsAccount(OsAccountInfo &osAccountInfo, bool isCleanGarbage) 753 { 754 int32_t id = osAccountInfo.GetLocalId(); 755 ErrCode errCode = ERR_OK; 756 #ifdef HAS_USER_IDM_PART 757 errCode = OsAccountInterface::SendToIDMAccountDelete(osAccountInfo); 758 if (errCode != ERR_OK) { 759 ACCOUNT_LOGE("SendToIDMAccountDelete failed, id %{public}d, errCode %{public}d", 760 osAccountInfo.GetLocalId(), errCode); 761 return errCode; 762 } 763 #endif // HAS_USER_IDM_PART 764 DomainAccountInfo curDomainInfo; 765 osAccountInfo.GetDomainInfo(curDomainInfo); 766 if (!curDomainInfo.accountId_.empty()) { 767 InnerDomainAccountManager::GetInstance().OnAccountUnBound(curDomainInfo, nullptr); 768 InnerDomainAccountManager::GetInstance().RemoveTokenFromMap(id); 769 } 770 if (isCleanGarbage) { 771 ACCOUNT_LOGI("Clean garbage account data, no need to deal foreground status."); 772 return ERR_OK; 773 } 774 if (osAccountInfo.GetIsForeground()) { 775 ACCOUNT_LOGI("Remove foreground account id=%{public}d.", id); 776 if (ActivateOsAccount(Constants::START_USER_ID) != ERR_OK) { 777 ACCOUNT_LOGE("RemoveOsAccount active base account failed"); 778 return ERR_OSACCOUNT_SERVICE_INNER_REMOVE_ACCOUNT_ACTIVED_ERROR; 779 } 780 std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_FOR_REMOVING_FOREGROUND_OS_ACCOUNT)); 781 } 782 loggedInAccounts_.Erase(id); 783 verifiedAccounts_.Erase(id); 784 // stop account 785 errCode = SendMsgForAccountStop(osAccountInfo); 786 if (errCode != ERR_OK) { 787 ReportOsAccountOperationFail(id, "stop", errCode, "stop os account failed"); 788 return errCode; 789 } 790 return errCode; 791 } 792 793 ErrCode IInnerOsAccountManager::RemoveOsAccountOperate(const int id, OsAccountInfo &osAccountInfo, bool isCleanGarbage) 794 { 795 if (isCleanGarbage && (!osAccountInfo.GetIsCreateCompleted()) && (!osAccountInfo.GetIsDataRemovable())) { 796 ACCOUNT_LOGI("Account cannot be removed id=%{public}d.", id); 797 return ERR_OK; 798 } 799 ErrCode errCode = PrepareRemoveOsAccount(osAccountInfo, isCleanGarbage); 800 if (errCode != ERR_OK) { 801 ACCOUNT_LOGE("PrepareRemoveOsAccount failed, errCode %{public}d.", errCode); 802 return errCode; 803 } 804 DomainAccountInfo domainAccountInfo; 805 osAccountInfo.GetDomainInfo(domainAccountInfo); 806 if (!domainAccountInfo.accountId_.empty()) { 807 InnerDomainAccountManager::GetInstance().NotifyDomainAccountEvent( 808 id, DomainAccountEvent::LOG_OUT, DomainAccountStatus::LOGOUT, domainAccountInfo); 809 } 810 AccountInfo ohosInfo; 811 (void)OhosAccountManager::GetInstance().GetAccountInfoByUserId(id, ohosInfo); 812 if (ohosInfo.ohosAccountInfo_.name_ != DEFAULT_OHOS_ACCOUNT_NAME) { 813 #ifdef HAS_CES_PART 814 AccountEventProvider::EventPublishAsUser( 815 EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT, id); 816 AccountEventProvider::EventPublishAsUser( 817 EventFwk::CommonEventSupport::COMMON_EVENT_DISTRIBUTED_ACCOUNT_LOGOUT, id); 818 #else // HAS_CES_PART 819 ACCOUNT_LOGI("No common event part! Publish nothing!"); 820 #endif // HAS_CES_PART 821 } 822 errCode = SendMsgForAccountRemove(osAccountInfo); 823 if (errCode != ERR_OK) { 824 return errCode; 825 } 826 827 errCode = osAccountControl_->RemoveOAConstraintsInfo(id); 828 if (errCode != ERR_OK) { 829 ACCOUNT_LOGE("RemoveOsAccount failed to remove os account constraints info"); 830 return errCode; 831 } 832 CheckAndRefreshLocalIdRecord(id); 833 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::REMOVED); 834 return errCode; 835 } 836 837 ErrCode IInnerOsAccountManager::RemoveOsAccount(const int id) 838 { 839 ACCOUNT_LOGI("RemoveOsAccount delete id is %{public}d", id); 840 if (!CheckAndAddLocalIdOperating(id)) { 841 ACCOUNT_LOGE("the %{public}d already in operating", id); 842 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 843 } 844 845 OsAccountInfo osAccountInfo; 846 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 847 if (errCode != ERR_OK) { 848 RemoveLocalIdToOperating(id); 849 ACCOUNT_LOGE("RemoveOsAccount cannot find os account info, errCode %{public}d.", errCode); 850 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 851 } 852 errCode = OsAccountInterface::SendToStorageAccountCreateComplete(id); 853 if (errCode != ERR_OK) { 854 RemoveLocalIdToOperating(id); 855 ACCOUNT_LOGE("SendToStorageAccountCreateComplete failed, errCode=%{public}d, id=%{public}d", errCode, id); 856 return errCode; 857 } 858 // set remove flag first 859 osAccountInfo.SetToBeRemoved(true); 860 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 861 if (errCode != ERR_OK) { 862 ACCOUNT_LOGE("Failed to update ToBeRemoved status, errCode=%{public}d.", errCode); 863 ReportOsAccountOperationFail(id, Constants::OPERATION_REMOVE, errCode, "Failed to update ToBeRemoved status"); 864 return errCode; 865 } 866 867 // then remove account 868 errCode = RemoveOsAccountOperate(id, osAccountInfo); 869 RemoveLocalIdToOperating(id); 870 return errCode; 871 } 872 873 ErrCode IInnerOsAccountManager::SendMsgForAccountStop(OsAccountInfo &osAccountInfo) 874 { 875 ErrCode errCode = OsAccountInterface::SendToAMSAccountStop(osAccountInfo); 876 if (errCode != ERR_OK) { 877 ACCOUNT_LOGE("SendToAMSAccountStop failed, id %{public}d, errCode %{public}d", 878 osAccountInfo.GetLocalId(), errCode); 879 return errCode; 880 } 881 errCode = OsAccountInterface::SendToStorageAccountStop(osAccountInfo); 882 if (errCode != ERR_OK) { 883 ACCOUNT_LOGE("SendToStorageAccountStop failed, id %{public}d, errCode %{public}d", 884 osAccountInfo.GetLocalId(), errCode); 885 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER; 886 } 887 return DeactivateOsAccountByInfo(osAccountInfo); 888 } 889 890 ErrCode IInnerOsAccountManager::SendMsgForAccountDeactivate(OsAccountInfo &osAccountInfo, bool isStopStorage) 891 { 892 ErrCode errCode = OsAccountInterface::SendToAMSAccountDeactivate(osAccountInfo); 893 if (errCode != ERR_OK) { 894 ACCOUNT_LOGE("SendToAMSAccountDeactivate failed, id %{public}d, errCode %{public}d", 895 osAccountInfo.GetLocalId(), errCode); 896 return errCode; 897 } 898 if (isStopStorage) { 899 errCode = OsAccountInterface::SendToStorageAccountStop(osAccountInfo); 900 if (errCode != ERR_OK) { 901 ACCOUNT_LOGE("SendToStorageAccountStop failed, id %{public}d, errCode %{public}d", 902 osAccountInfo.GetLocalId(), errCode); 903 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER; 904 } 905 } 906 907 return DeactivateOsAccountByInfo(osAccountInfo); 908 } 909 910 bool IInnerOsAccountManager::IsToBeRemoved(int32_t localId) 911 { 912 OsAccountInfo osAccountInfo; 913 ErrCode ret = QueryOsAccountById(localId, osAccountInfo); 914 if (ret == ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR) { 915 return true; 916 } 917 return osAccountInfo.GetToBeRemoved(); 918 } 919 920 ErrCode IInnerOsAccountManager::ValidateOsAccount(const OsAccountInfo &osAccountInfo) 921 { 922 if (osAccountInfo.GetType() == OsAccountType::PRIVATE) { 923 return ERR_OK; 924 } 925 Json accountIndexJson; 926 ErrCode result = osAccountControl_->GetAccountIndexFromFile(accountIndexJson); 927 if (result != ERR_OK) { 928 return result; 929 } 930 std::string localIdStr = std::to_string(osAccountInfo.GetLocalId()); 931 for (const auto& element : accountIndexJson.items()) { 932 std::string localIdKey = element.key(); 933 auto value = element.value(); 934 std::string localName = value[Constants::LOCAL_NAME].get<std::string>(); 935 if ((osAccountInfo.GetLocalName() == localName) && (localIdKey != localIdStr) 936 && !IsToBeRemoved(std::stoi(localIdKey))) { 937 return ERR_ACCOUNT_COMMON_NAME_HAD_EXISTED; 938 } 939 if (!osAccountInfo.GetShortName().empty() && value.contains(Constants::SHORT_NAME)) { 940 std::string shortName = value[Constants::SHORT_NAME].get<std::string>(); 941 if ((osAccountInfo.GetShortName() == shortName) && (localIdKey != localIdStr) 942 && !IsToBeRemoved(std::stoi(localIdKey))) { 943 return ERR_ACCOUNT_COMMON_SHORT_NAME_HAD_EXISTED; 944 } 945 } 946 } 947 948 return ERR_OK; 949 } 950 951 ErrCode IInnerOsAccountManager::GetTypeNumber(const OsAccountType& type, int32_t& typeNumber) 952 { 953 typeNumber = 0; 954 std::vector<OsAccountInfo> osAccountList; 955 ErrCode result = QueryAllCreatedOsAccounts(osAccountList); 956 if (result != ERR_OK) { 957 ACCOUNT_LOGE("Get os account list failed."); 958 return result; 959 } 960 961 typeNumber = std::count_if(osAccountList.begin(), osAccountList.end(), 962 [&type](const OsAccountInfo& info) { return info.GetType() == type; }); 963 return ERR_OK; 964 } 965 966 ErrCode IInnerOsAccountManager::CheckTypeNumber(const OsAccountType& type) 967 { 968 if (type != OsAccountType::PRIVATE) { 969 return ERR_OK; 970 } 971 int32_t typeNumber = 0; 972 ErrCode result = GetTypeNumber(type, typeNumber); 973 if (result != ERR_OK) { 974 ACCOUNT_LOGE("Count type number failed."); 975 return result; 976 } 977 if (type == OsAccountType::PRIVATE && typeNumber >= MAX_PRIVATE_TYPE_NUMBER) { 978 ACCOUNT_LOGE("Check type number failed, private type number=%{public}d", typeNumber); 979 return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR; 980 } 981 return ERR_OK; 982 } 983 984 ErrCode IInnerOsAccountManager::SendMsgForAccountRemove(OsAccountInfo &osAccountInfo) 985 { 986 int32_t localId = osAccountInfo.GetLocalId(); 987 AppAccountControlManager::GetInstance().SetOsAccountRemoved(localId, true); 988 ErrCode errCode = OsAccountInterface::SendToBMSAccountDelete(osAccountInfo); 989 if (errCode != ERR_OK) { 990 ACCOUNT_LOGE("SendToBMSAccountDelete failed, id %{public}d, errCode %{public}d", localId, errCode); 991 return errCode; 992 } 993 errCode = OsAccountInterface::SendToStorageAccountRemove(osAccountInfo); 994 if (errCode != ERR_OK) { 995 ACCOUNT_LOGE("SendToStorageAccountRemove failed, id %{public}d, errCode %{public}d", localId, errCode); 996 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER; 997 } 998 errCode = osAccountControl_->DelOsAccount(localId); 999 if (errCode != ERR_OK) { 1000 ACCOUNT_LOGE("remove osaccount info failed, id: %{public}d, errCode %{public}d", localId, errCode); 1001 return errCode; 1002 } 1003 OsAccountInterface::SendToCESAccountDelete(osAccountInfo); 1004 ReportOsAccountLifeCycle(localId, Constants::OPERATION_REMOVE); 1005 return errCode; 1006 } 1007 1008 void IInnerOsAccountManager::Init() 1009 { 1010 ACCOUNT_LOGI("Start to create base os accounts"); 1011 CreateBaseAdminAccount(); 1012 CreateBaseStandardAccount(); 1013 SetParameter(PARAM_LOGIN_NAME_MAX.c_str(), std::to_string(Constants::LOCAL_NAME_MAX_SIZE).c_str()); 1014 ACCOUNT_LOGI("End to create base os accounts"); 1015 } 1016 1017 ErrCode IInnerOsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExits) 1018 { 1019 isOsAccountExits = false; 1020 osAccountControl_->IsOsAccountExists(id, isOsAccountExits); 1021 return ERR_OK; 1022 } 1023 1024 ErrCode IInnerOsAccountManager::IsOsAccountActived(const int id, bool &isOsAccountActived) 1025 { 1026 isOsAccountActived = false; 1027 1028 // check if os account exists 1029 OsAccountInfo osAccountInfo; 1030 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1031 if (errCode != ERR_OK) { 1032 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode); 1033 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1034 } 1035 if (id == Constants::ADMIN_LOCAL_ID) { 1036 isOsAccountActived = true; 1037 return ERR_OK; 1038 } 1039 isOsAccountActived = osAccountInfo.GetIsActived(); 1040 return ERR_OK; 1041 } 1042 1043 ErrCode IInnerOsAccountManager::IsOsAccountConstraintEnable( 1044 const int id, const std::string &constraint, bool &isOsAccountConstraintEnable) 1045 { 1046 isOsAccountConstraintEnable = false; 1047 OsAccountInfo osAccountInfo; 1048 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1049 if (errCode != ERR_OK) { 1050 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1051 } 1052 std::vector<std::string> constraints; 1053 constraints = osAccountInfo.GetConstraints(); 1054 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) { 1055 isOsAccountConstraintEnable = true; 1056 return ERR_OK; 1057 } 1058 constraints.clear(); 1059 if (osAccountControl_->GetGlobalOAConstraintsList(constraints) == ERR_OK) { 1060 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) { 1061 isOsAccountConstraintEnable = true; 1062 return ERR_OK; 1063 } 1064 } 1065 constraints.clear(); 1066 if (osAccountControl_->GetSpecificOAConstraintsList(id, constraints) == ERR_OK) { 1067 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) { 1068 isOsAccountConstraintEnable = true; 1069 return ERR_OK; 1070 } 1071 } 1072 return ERR_OK; 1073 } 1074 1075 ErrCode IInnerOsAccountManager::IsOsAccountVerified(const int id, bool &isVerified) 1076 { 1077 OsAccountInfo osAccountInfo; 1078 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1079 if (errCode != ERR_OK) { 1080 ACCOUNT_LOGE("Get osaccount fail, errCode=%{public}d, id=%{public}d", errCode, id); 1081 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1082 } 1083 isVerified = osAccountInfo.GetIsVerified(); 1084 return ERR_OK; 1085 } 1086 1087 ErrCode IInnerOsAccountManager::IsOsAccountDeactivating(const int id, bool &isDeactivating) 1088 { 1089 isDeactivating = false; 1090 deactivatingAccounts_.Find(id, isDeactivating); 1091 return ERR_OK; 1092 } 1093 1094 ErrCode IInnerOsAccountManager::GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount) 1095 { 1096 std::vector<int32_t> idList; 1097 ErrCode errCode = osAccountControl_->GetOsAccountIdList(idList); 1098 if (errCode != ERR_OK) { 1099 ACCOUNT_LOGE("get osaccount info list error, errCode %{public}d.", errCode); 1100 return errCode; 1101 } 1102 createdOsAccountCount = idList.size(); 1103 return ERR_OK; 1104 } 1105 1106 ErrCode IInnerOsAccountManager::QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber) 1107 { 1108 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS 1109 maxOsAccountNumber = config_.maxOsAccountNum; 1110 #else 1111 maxOsAccountNumber = 1; 1112 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS 1113 return ERR_OK; 1114 } 1115 1116 ErrCode IInnerOsAccountManager::QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum) 1117 { 1118 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS 1119 maxNum = config_.maxLoggedInOsAccountNum; 1120 #else 1121 maxNum = 1; 1122 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS 1123 return ERR_OK; 1124 } 1125 1126 ErrCode IInnerOsAccountManager::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints) 1127 { 1128 OsAccountInfo osAccountInfo; 1129 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1130 if (errCode != ERR_OK) { 1131 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1132 } 1133 constraints = osAccountInfo.GetConstraints(); 1134 std::vector<std::string> globalConstraints; 1135 errCode = osAccountControl_->GetGlobalOAConstraintsList(globalConstraints); 1136 if (errCode != ERR_OK) { 1137 ACCOUNT_LOGE("get globalConstraints info error"); 1138 return errCode; 1139 } 1140 for (auto it = globalConstraints.begin(); it != globalConstraints.end(); it++) { 1141 if (std::find(constraints.begin(), constraints.end(), *it) == constraints.end()) { 1142 constraints.push_back(*it); 1143 } 1144 } 1145 std::vector<std::string> specificConstraints; 1146 errCode = osAccountControl_->GetSpecificOAConstraintsList(id, specificConstraints); 1147 if (errCode != ERR_OK) { 1148 ACCOUNT_LOGE("get specificConstraints info error"); 1149 return errCode; 1150 } 1151 for (auto it = specificConstraints.begin(); it != specificConstraints.end(); it++) { 1152 if (std::find(constraints.begin(), constraints.end(), *it) == constraints.end()) { 1153 constraints.push_back(*it); 1154 } 1155 } 1156 return ERR_OK; 1157 } 1158 1159 ErrCode IInnerOsAccountManager::QueryOsAccountConstraintSourceTypes(const int32_t id, 1160 const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos) 1161 { 1162 ACCOUNT_LOGD("enter."); 1163 bool isOsAccountConstraintEnable = false; 1164 ErrCode errCode = IsOsAccountConstraintEnable(id, constraint, isOsAccountConstraintEnable); 1165 if (errCode != ERR_OK) { 1166 ACCOUNT_LOGE("get os account constraint enable info error"); 1167 return errCode; 1168 } 1169 if (!isOsAccountConstraintEnable) { 1170 ACCOUNT_LOGI("constraint not exist"); 1171 ConstraintSourceTypeInfo constraintSourceTypeInfo; 1172 constraintSourceTypeInfo.localId = -1; 1173 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_NOT_EXIST; 1174 constraintSourceTypeInfos.push_back(constraintSourceTypeInfo); 1175 return ERR_OK; 1176 } 1177 1178 bool isExits; 1179 if (osAccountControl_->IsFromBaseOAConstraintsList(id, constraint, isExits) == ERR_OK) { 1180 if (isExits) { 1181 ACCOUNT_LOGI("constraint is exist in base os account constraints list"); 1182 ConstraintSourceTypeInfo constraintSourceTypeInfo; 1183 constraintSourceTypeInfo.localId = -1; 1184 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_BASE; 1185 constraintSourceTypeInfos.push_back(constraintSourceTypeInfo); 1186 } 1187 } 1188 std::vector<ConstraintSourceTypeInfo> globalSourceList; 1189 errCode = osAccountControl_->IsFromGlobalOAConstraintsList(id, deviceOwnerId_, constraint, globalSourceList); 1190 if (errCode == ERR_OK && globalSourceList.size() != 0) { 1191 ACCOUNT_LOGI("constraint is exist in global os account constraints list"); 1192 constraintSourceTypeInfos.insert( 1193 constraintSourceTypeInfos.end(), globalSourceList.begin(), globalSourceList.end()); 1194 } 1195 std::vector<ConstraintSourceTypeInfo> specificSourceList; 1196 errCode = osAccountControl_->IsFromSpecificOAConstraintsList(id, deviceOwnerId_, constraint, specificSourceList); 1197 if (errCode == ERR_OK && specificSourceList.size() != 0) { 1198 ACCOUNT_LOGI("constraint is exist in specific os account constraints list"); 1199 constraintSourceTypeInfos.insert( 1200 constraintSourceTypeInfos.end(), specificSourceList.begin(), specificSourceList.end()); 1201 } 1202 return ERR_OK; 1203 } 1204 1205 ErrCode IInnerOsAccountManager::SetBaseOsAccountConstraints(const int32_t id, 1206 const std::vector<std::string> &constraints, const bool enable) 1207 { 1208 ErrCode errCode = SetOsAccountConstraints(id, constraints, enable); 1209 if (errCode != ERR_OK) { 1210 ACCOUNT_LOGE("set os account %{public}d constraints failed! errCode %{public}d.", id, errCode); 1211 return errCode; 1212 } 1213 1214 errCode = osAccountControl_->UpdateBaseOAConstraints(std::to_string(id), constraints, enable); 1215 if (errCode != ERR_OK) { 1216 ACCOUNT_LOGE("update base os account %{public}d constraints failed! errCode %{public}d.", id, errCode); 1217 return errCode; 1218 } 1219 return ERR_OK; 1220 } 1221 1222 ErrCode IInnerOsAccountManager::SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints, 1223 const bool enable, const int32_t enforcerId, const bool isDeviceOwner) 1224 { 1225 OsAccountInfo osAccountInfo; 1226 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(enforcerId, osAccountInfo); 1227 if (errCode != ERR_OK) { 1228 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1229 } 1230 if (osAccountInfo.GetToBeRemoved()) { 1231 ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", enforcerId); 1232 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 1233 } 1234 1235 bool isExists = false; 1236 bool isOverSize = false; 1237 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize); 1238 if (errCode != ERR_OK || !isExists || isOverSize) { 1239 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!"); 1240 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 1241 } 1242 1243 osAccountControl_->UpdateGlobalOAConstraints(std::to_string(enforcerId), constraints, enable); 1244 1245 errCode = DealWithDeviceOwnerId(isDeviceOwner, enforcerId); 1246 if (errCode != ERR_OK) { 1247 ACCOUNT_LOGE("deal with device owner id error"); 1248 return errCode; 1249 } 1250 return ERR_OK; 1251 } 1252 1253 ErrCode IInnerOsAccountManager::SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints, 1254 const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner) 1255 { 1256 OsAccountInfo enforcerOsAccountInfo; 1257 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(enforcerId, enforcerOsAccountInfo); 1258 if (errCode != ERR_OK) { 1259 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1260 } 1261 1262 OsAccountInfo targetOsAccountInfo; 1263 errCode = osAccountControl_->GetOsAccountInfoById(targetId, targetOsAccountInfo); 1264 if (errCode != ERR_OK) { 1265 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1266 } 1267 if (targetOsAccountInfo.GetToBeRemoved() || enforcerOsAccountInfo.GetToBeRemoved()) { 1268 ACCOUNT_LOGE("account %{public}d or %{public}d will be removed, cannot change constraints!", 1269 enforcerId, targetId); 1270 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 1271 } 1272 1273 bool isExists = false; 1274 bool isOverSize = false; 1275 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize); 1276 if (errCode != ERR_OK || !isExists || isOverSize) { 1277 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!"); 1278 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 1279 } 1280 1281 osAccountControl_->UpdateSpecificOAConstraints( 1282 std::to_string(enforcerId), std::to_string(targetId), constraints, enable); 1283 1284 errCode = DealWithDeviceOwnerId(isDeviceOwner, enforcerId); 1285 if (errCode != ERR_OK) { 1286 ACCOUNT_LOGE("deal with device owner id error"); 1287 return errCode; 1288 } 1289 return ERR_OK; 1290 } 1291 1292 ErrCode IInnerOsAccountManager::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &createdOsAccounts) 1293 { 1294 std::vector<OsAccountInfo> allOsAccounts; 1295 ErrCode errCode = osAccountControl_->GetOsAccountList(allOsAccounts); 1296 if (errCode != ERR_OK) { 1297 ACCOUNT_LOGE("get osaccount info list error, errCode %{public}d.", errCode); 1298 return errCode; 1299 } 1300 for (auto osAccountInfo : allOsAccounts) { 1301 if (osAccountInfo.GetIsCreateCompleted() && !osAccountInfo.GetToBeRemoved()) { 1302 createdOsAccounts.push_back(osAccountInfo); 1303 } 1304 } 1305 return ERR_OK; 1306 } 1307 1308 ErrCode IInnerOsAccountManager::DealWithDeviceOwnerId(const bool isDeviceOwner, const int32_t localId) 1309 { 1310 ACCOUNT_LOGD("enter."); 1311 if (isDeviceOwner && localId != deviceOwnerId_) { 1312 ACCOUNT_LOGI("this device owner os account id is changed!"); 1313 deviceOwnerId_ = localId; 1314 return osAccountControl_->UpdateDeviceOwnerId(localId); 1315 } 1316 if (isDeviceOwner == false && localId == deviceOwnerId_) { 1317 deviceOwnerId_ = -1; 1318 return osAccountControl_->UpdateDeviceOwnerId(-1); 1319 } 1320 return ERR_OK; 1321 } 1322 1323 int32_t IInnerOsAccountManager::CleanGarbageOsAccounts(int32_t excludeId) 1324 { 1325 ACCOUNT_LOGI("enter"); 1326 std::vector<int32_t> idList; 1327 if (osAccountControl_->GetOsAccountIdList(idList) != ERR_OK) { 1328 ACCOUNT_LOGI("GetOsAccountIdList failed."); 1329 return 0; 1330 } 1331 1332 int32_t removeNum = 0; 1333 1334 for (auto id : idList) { 1335 if (id == Constants::START_USER_ID || id == Constants::ADMIN_LOCAL_ID || id == excludeId) { 1336 continue; 1337 } 1338 if (!CheckAndAddLocalIdOperating(id)) { 1339 ACCOUNT_LOGI("Account id = %{public}d already in operating", id); 1340 continue; 1341 } 1342 OsAccountInfo osAccountInfo; 1343 ErrCode ret = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1344 if (ret != ERR_OK && ret != ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR) { 1345 continue; 1346 } 1347 osAccountInfo.SetLocalId(id); 1348 1349 if (!osAccountInfo.GetToBeRemoved() && osAccountInfo.GetIsCreateCompleted()) { 1350 RemoveLocalIdToOperating(id); 1351 continue; 1352 } 1353 ErrCode errCode = RemoveOsAccountOperate(id, osAccountInfo, true); 1354 RemoveLocalIdToOperating(id); 1355 if (errCode != ERR_OK) { 1356 ACCOUNT_LOGE("remove account %{public}d failed! errCode %{public}d.", id, errCode); 1357 } else { 1358 ACCOUNT_LOGI("remove account %{public}d succeed!", id); 1359 removeNum++; 1360 } 1361 } 1362 ACCOUNT_LOGI("finished."); 1363 return removeNum; 1364 } 1365 1366 bool IInnerOsAccountManager::IsSameAccount( 1367 const DomainAccountInfo &domainInfoSrc, const DomainAccountInfo &domainInfoTar) 1368 { 1369 return (((!domainInfoSrc.accountId_.empty()) && (domainInfoSrc.accountId_ == domainInfoTar.accountId_)) || 1370 ((!domainInfoSrc.accountName_.empty()) && (domainInfoSrc.accountName_ == domainInfoTar.accountName_) && 1371 (!domainInfoSrc.domain_.empty()) && (domainInfoSrc.domain_ == domainInfoTar.domain_))); 1372 } 1373 1374 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id) 1375 { 1376 if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) { 1377 ACCOUNT_LOGE("invalid domain name length %{public}zu.", domainInfo.domain_.size()); 1378 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 1379 } 1380 1381 if (domainInfo.accountName_.size() > Constants::LOCAL_NAME_MAX_SIZE) { 1382 ACCOUNT_LOGE("invalid domain account name length %{public}zu.", domainInfo.accountName_.size()); 1383 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 1384 } 1385 1386 id = -1; 1387 std::vector<OsAccountInfo> osAccountInfos; 1388 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos); 1389 if (errCode != ERR_OK) { 1390 return errCode; 1391 } 1392 1393 DomainAccountInfo curDomainInfo; 1394 for (auto osAccountInfosPtr = osAccountInfos.begin(); osAccountInfosPtr != osAccountInfos.end(); 1395 ++osAccountInfosPtr) { 1396 osAccountInfosPtr->GetDomainInfo(curDomainInfo); 1397 if (IsSameAccount(curDomainInfo, domainInfo)) { 1398 id = osAccountInfosPtr->GetLocalId(); 1399 return ERR_OK; 1400 } 1401 } 1402 return ERR_DOMAIN_ACCOUNT_SERVICE_NOT_DOMAIN_ACCOUNT; 1403 } 1404 1405 ErrCode IInnerOsAccountManager::GetOsAccountShortName(const int id, std::string &shortName) 1406 { 1407 OsAccountInfo osAccountInfo; 1408 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1409 if (errCode != ERR_OK) { 1410 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1411 } 1412 shortName = osAccountInfo.GetShortName(); 1413 return ERR_OK; 1414 } 1415 1416 ErrCode IInnerOsAccountManager::GetOsAccountName(const int id, std::string &name) 1417 { 1418 OsAccountInfo osAccountInfo; 1419 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1420 if (errCode != ERR_OK) { 1421 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1422 } 1423 name = osAccountInfo.GetLocalName(); 1424 return ERR_OK; 1425 } 1426 1427 ErrCode IInnerOsAccountManager::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo) 1428 { 1429 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1430 if (errCode != ERR_OK) { 1431 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode); 1432 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1433 } 1434 1435 if (osAccountInfo.GetPhoto() != "") { 1436 std::string photo = osAccountInfo.GetPhoto(); 1437 errCode = osAccountControl_->GetPhotoById(osAccountInfo.GetLocalId(), photo); 1438 if (errCode != ERR_OK) { 1439 ACCOUNT_LOGE("get osaccount photo error, errCode %{public}d.", errCode); 1440 return errCode; 1441 } 1442 osAccountInfo.SetPhoto(photo); 1443 } 1444 GetDomainAccountStatus(osAccountInfo); 1445 return ERR_OK; 1446 } 1447 1448 ErrCode IInnerOsAccountManager::GetOsAccountType(const int id, OsAccountType &type) 1449 { 1450 OsAccountInfo osAccountInfo; 1451 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1452 if (errCode != ERR_OK) { 1453 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1454 } 1455 type = osAccountInfo.GetType(); 1456 return ERR_OK; 1457 } 1458 1459 ErrCode IInnerOsAccountManager::GetOsAccountProfilePhoto(const int id, std::string &photo) 1460 { 1461 OsAccountInfo osAccountInfo; 1462 ErrCode errCode = QueryOsAccountById(id, osAccountInfo); 1463 if (errCode != ERR_OK) { 1464 ACCOUNT_LOGE("QueryOsAccountById return error, errCode %{public}d.", errCode); 1465 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1466 } 1467 photo = osAccountInfo.GetPhoto(); 1468 return ERR_OK; 1469 } 1470 1471 ErrCode IInnerOsAccountManager::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable) 1472 { 1473 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS 1474 ErrCode errCode = osAccountControl_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable); 1475 if (errCode != ERR_OK) { 1476 ACCOUNT_LOGE("GetIsMultiOsAccountEnable error, errCode %{public}d.", errCode); 1477 return errCode; 1478 } 1479 #else 1480 isMultiOsAccountEnable = false; 1481 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS 1482 return ERR_OK; 1483 } 1484 1485 ErrCode IInnerOsAccountManager::SetOsAccountName(const int id, const std::string &name) 1486 { 1487 OsAccountInfo osAccountInfo; 1488 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1489 if (errCode != ERR_OK) { 1490 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1491 } 1492 1493 // to be removed, cannot change any thing 1494 if (osAccountInfo.GetToBeRemoved()) { 1495 ACCOUNT_LOGE("account %{public}d will be removed, cannot change name!", id); 1496 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 1497 } 1498 1499 std::string localName = osAccountInfo.GetLocalName(); 1500 if (localName == name) { 1501 return ERR_OK; 1502 } 1503 1504 osAccountInfo.SetLocalName(name); 1505 errCode = ValidateOsAccount(osAccountInfo); 1506 if (errCode != ERR_OK) { 1507 ACCOUNT_LOGE("account name already exist, errCode %{public}d.", errCode); 1508 return errCode; 1509 } 1510 1511 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 1512 if (errCode != ERR_OK) { 1513 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId()); 1514 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 1515 } 1516 osAccountControl_->UpdateAccountIndex(osAccountInfo, false); 1517 OsAccountInterface::PublishCommonEvent( 1518 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE); 1519 return ERR_OK; 1520 } 1521 1522 ErrCode IInnerOsAccountManager::SetOsAccountConstraints( 1523 const int id, const std::vector<std::string> &constraints, const bool enable) 1524 { 1525 OsAccountInfo osAccountInfo; 1526 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1527 if (errCode != ERR_OK) { 1528 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1529 } 1530 1531 // to be removed, cannot change any thing 1532 if (osAccountInfo.GetToBeRemoved()) { 1533 ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", id); 1534 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 1535 } 1536 1537 bool isExists = false; 1538 bool isOverSize = false; 1539 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize); 1540 if (errCode != ERR_OK || !isExists || isOverSize) { 1541 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!"); 1542 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; 1543 } 1544 std::vector<std::string> oldConstraints = osAccountInfo.GetConstraints(); 1545 for (auto it = constraints.begin(); it != constraints.end(); it++) { 1546 if (enable) { 1547 if (std::find(oldConstraints.begin(), oldConstraints.end(), *it) == oldConstraints.end()) { 1548 oldConstraints.push_back(*it); 1549 } 1550 } else { 1551 oldConstraints.erase( 1552 std::remove(oldConstraints.begin(), oldConstraints.end(), *it), oldConstraints.end()); 1553 } 1554 } 1555 osAccountInfo.SetConstraints(oldConstraints); 1556 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 1557 if (errCode != ERR_OK) { 1558 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId()); 1559 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 1560 } 1561 return ERR_OK; 1562 } 1563 1564 ErrCode IInnerOsAccountManager::SetOsAccountProfilePhoto(const int id, const std::string &photo) 1565 { 1566 OsAccountInfo osAccountInfo; 1567 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1568 if (errCode != ERR_OK) { 1569 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1570 } 1571 1572 // to be removed, cannot change any thing 1573 if (osAccountInfo.GetToBeRemoved()) { 1574 ACCOUNT_LOGE("account %{public}d will be removed, cannot change photo!", id); 1575 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 1576 } 1577 1578 if (osAccountInfo.GetPhoto() == photo) { 1579 return ERR_OK; 1580 } 1581 errCode = osAccountControl_->SetPhotoById(id, photo); 1582 if (errCode != ERR_OK) { 1583 ACCOUNT_LOGE("Set photo error, code=%{public}d, id=%{public}d.", errCode, id); 1584 return errCode; 1585 } 1586 osAccountInfo.SetPhoto(Constants::USER_PHOTO_FILE_TXT_NAME); 1587 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 1588 if (errCode != ERR_OK) { 1589 ACCOUNT_LOGE("Update osaccount info faile code=%{public}d, id=%{public}d", errCode, osAccountInfo.GetLocalId()); 1590 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 1591 } 1592 OsAccountInterface::PublishCommonEvent( 1593 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE); 1594 return ERR_OK; 1595 } 1596 1597 ErrCode IInnerOsAccountManager::DeactivateOsAccountByInfo(OsAccountInfo &osAccountInfo) 1598 { 1599 int localId = osAccountInfo.GetLocalId(); 1600 loggedInAccounts_.Erase(localId); 1601 verifiedAccounts_.Erase(localId); 1602 int32_t foregroundId = -1; 1603 if (foregroundAccountMap_.Find(Constants::DEFAULT_DISPALY_ID, foregroundId) && foregroundId == localId) { 1604 foregroundAccountMap_.Erase(Constants::DEFAULT_DISPALY_ID); 1605 } 1606 EraseIdFromActiveList(localId); 1607 1608 osAccountInfo.SetIsActived(false); 1609 osAccountInfo.SetIsVerified(false); 1610 osAccountInfo.SetIsForeground(false); 1611 osAccountInfo.SetDisplayId(Constants::INVALID_DISPALY_ID); 1612 osAccountInfo.SetIsLoggedIn(false); 1613 DomainAccountInfo domainAccountInfo; 1614 osAccountInfo.GetDomainInfo(domainAccountInfo); 1615 domainAccountInfo.status_ = DomainAccountStatus::LOGOUT; 1616 osAccountInfo.SetDomainInfo(domainAccountInfo); 1617 ErrCode errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 1618 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) { 1619 ACCOUNT_LOGE("Update account failed, id=%{public}d, errCode=%{public}d.", osAccountInfo.GetLocalId(), errCode); 1620 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 1621 } 1622 1623 AccountInfoReport::ReportSecurityInfo(osAccountInfo.GetLocalName(), localId, 1624 ReportEvent::EVENT_LOGOUT, 0); 1625 return ERR_OK; 1626 } 1627 1628 ErrCode IInnerOsAccountManager::DeactivateOsAccountById(const int id) 1629 { 1630 OsAccountInfo osAccountInfo; 1631 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1632 if (errCode != ERR_OK) { 1633 ACCOUNT_LOGE("cannot get os account %{public}d info. error %{public}d.", 1634 id, errCode); 1635 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1636 } 1637 return DeactivateOsAccountByInfo(osAccountInfo); 1638 } 1639 1640 ErrCode IInnerOsAccountManager::ActivateOsAccount(const int id, const bool startStorage, const uint64_t displayId) 1641 { 1642 if (!CheckAndAddLocalIdOperating(id)) { 1643 ACCOUNT_LOGE("the %{public}d already in operating", id); 1644 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 1645 } 1646 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id)); 1647 // get information 1648 OsAccountInfo osAccountInfo; 1649 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1650 if (errCode != ERR_OK) { 1651 RemoveLocalIdToOperating(id); 1652 ACCOUNT_LOGE("cannot find os account info by id:%{public}d, errCode %{public}d.", id, errCode); 1653 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1654 } 1655 1656 int32_t foregroundId = -1; 1657 if (foregroundAccountMap_.Find(displayId, foregroundId) && (foregroundId == id) && osAccountInfo.GetIsVerified()) { 1658 ACCOUNT_LOGI("Account %{public}d already is foreground", id); 1659 RemoveLocalIdToOperating(id); 1660 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREADY_ACTIVE_ERROR; 1661 } 1662 1663 errCode = IsValidOsAccount(osAccountInfo); 1664 if (errCode != ERR_OK) { 1665 RemoveLocalIdToOperating(id); 1666 return errCode; 1667 } 1668 1669 if (!osAccountInfo.GetIsActived() && 1670 (static_cast<uint32_t>(loggedInAccounts_.Size()) >= config_.maxLoggedInOsAccountNum)) { 1671 RemoveLocalIdToOperating(id); 1672 ACCOUNT_LOGE("The number of logged in account reaches the upper limit, maxLoggedInNum: %{public}d", 1673 config_.maxLoggedInOsAccountNum); 1674 return ERR_OSACCOUNT_SERVICE_LOGGED_IN_ACCOUNTS_OVERSIZE; 1675 } 1676 1677 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVATING); 1678 errCode = SendMsgForAccountActivate(osAccountInfo, startStorage); 1679 RemoveLocalIdToOperating(id); 1680 if (errCode != ERR_OK) { 1681 return errCode; 1682 } 1683 1684 DomainAccountInfo domainInfo; 1685 osAccountInfo.GetDomainInfo(domainInfo); 1686 if (domainInfo.accountId_.empty() && (osAccountInfo.GetCredentialId() == 0)) { 1687 AccountInfoReport::ReportSecurityInfo( 1688 osAccountInfo.GetLocalName(), osAccountInfo.GetLocalId(), ReportEvent::EVENT_LOGIN, 0); 1689 } 1690 ACCOUNT_LOGI("IInnerOsAccountManager ActivateOsAccount end"); 1691 return ERR_OK; 1692 } 1693 1694 ErrCode IInnerOsAccountManager::DeactivateOsAccount(const int id, bool isStopStorage) 1695 { 1696 if (!CheckAndAddLocalIdOperating(id)) { 1697 ACCOUNT_LOGW("the %{public}d already in operating", id); 1698 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 1699 } 1700 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id)); 1701 OsAccountInfo osAccountInfo; 1702 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1703 if (errCode != ERR_OK) { 1704 RemoveLocalIdToOperating(id); 1705 ACCOUNT_LOGW("cannot find os account info by id:%{public}d, errCode %{public}d.", id, errCode); 1706 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1707 } 1708 1709 if ((!osAccountInfo.GetIsActived()) && (!osAccountInfo.GetIsVerified())) { 1710 RemoveLocalIdToOperating(id); 1711 ACCOUNT_LOGW("account %{public}d is neither active nor verified, don't need to deactivate!", id); 1712 return ERR_OK; 1713 } 1714 errCode = IsValidOsAccount(osAccountInfo); 1715 if (errCode != ERR_OK) { 1716 RemoveLocalIdToOperating(id); 1717 return errCode; 1718 } 1719 1720 deactivatingAccounts_.EnsureInsert(id, true); 1721 1722 OsAccountInterface::PublishCommonEvent( 1723 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING, Constants::OPERATION_STOP); 1724 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING); 1725 1726 errCode = SendMsgForAccountDeactivate(osAccountInfo, isStopStorage); 1727 deactivatingAccounts_.Erase(id); 1728 if (errCode != ERR_OK) { 1729 RemoveLocalIdToOperating(id); 1730 ReportOsAccountOperationFail(id, "deactivate", errCode, "deactivate os account failed"); 1731 return errCode; 1732 } 1733 1734 OsAccountInterface::PublishCommonEvent(osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED, 1735 Constants::OPERATION_STOP); 1736 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED); 1737 ReportOsAccountLifeCycle(id, Constants::OPERATION_STOP); 1738 1739 RemoveLocalIdToOperating(id); 1740 ACCOUNT_LOGI("IInnerOsAccountManager DeactivateOsAccount end"); 1741 return ERR_OK; 1742 } 1743 1744 void IInnerOsAccountManager::RollBackToEarlierAccount(int32_t fromId, int32_t toId) 1745 { 1746 ACCOUNT_LOGI("Enter."); 1747 if (fromId == toId) { 1748 return; 1749 } 1750 subscribeManager_.Publish(fromId, toId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED); 1751 ReportOsAccountSwitch(fromId, toId); 1752 ACCOUNT_LOGI("End pushlishing pre switch event."); 1753 OsAccountInfo osAccountInfo; 1754 osAccountInfo.SetLocalId(toId); 1755 subscribeManager_.Publish(toId, fromId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHING); 1756 OsAccountInterface::PublishCommonEvent(osAccountInfo, 1757 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_FOREGROUND, Constants::OPERATION_SWITCH); 1758 subscribeManager_.Publish(toId, fromId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED); 1759 ReportOsAccountSwitch(toId, fromId); 1760 ACCOUNT_LOGI("End pushlishing post switch event."); 1761 } 1762 1763 ErrCode IInnerOsAccountManager::SendMsgForAccountActivate(OsAccountInfo &osAccountInfo, const bool startStorage, 1764 const uint64_t displayId) 1765 { 1766 // activate 1767 int32_t oldId = -1; 1768 bool oldIdExist = foregroundAccountMap_.Find(displayId, oldId); 1769 int32_t localId = static_cast<int32_t>(osAccountInfo.GetLocalId()); 1770 bool preActivated = osAccountInfo.GetIsActived(); 1771 subscribeManager_.Publish(localId, oldId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHING); 1772 1773 if (startStorage) { 1774 ErrCode errCode = SendToStorageAccountStart(osAccountInfo); 1775 if (errCode != ERR_OK) { 1776 RollBackToEarlierAccount(localId, oldId); 1777 return errCode; 1778 } 1779 } 1780 ErrCode errCode = SendToAMSAccountStart(osAccountInfo, displayId); 1781 if (errCode != ERR_OK) { 1782 RollBackToEarlierAccount(localId, oldId); 1783 return errCode; 1784 } 1785 if (oldId != localId) { 1786 OsAccountInterface::PublishCommonEvent(osAccountInfo, 1787 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_FOREGROUND, Constants::OPERATION_SWITCH); 1788 } 1789 errCode = UpdateAccountToForeground(displayId, osAccountInfo); 1790 if (errCode != ERR_OK) { 1791 return errCode; 1792 } 1793 1794 if (oldIdExist && (oldId != localId)) { 1795 if ((errCode = UpdateAccountToBackground(oldId)) != ERR_OK) { 1796 return errCode; 1797 } 1798 } 1799 1800 OsAccountInterface::SendToCESAccountSwitched(localId, oldId); 1801 subscribeManager_.Publish(localId, OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED); 1802 subscribeManager_.Publish(localId, oldId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED); 1803 if (!preActivated) { 1804 ReportOsAccountLifeCycle(defaultActivatedId_, Constants::OPERATION_ACTIVATE); 1805 } 1806 ACCOUNT_LOGI("SendMsgForAccountActivate ok"); 1807 ReportOsAccountSwitch(localId, oldId); 1808 return errCode; 1809 } 1810 1811 ErrCode IInnerOsAccountManager::SendToStorageAccountStart(OsAccountInfo &osAccountInfo) 1812 { 1813 bool preVerified = osAccountInfo.GetIsVerified(); 1814 int32_t localId = osAccountInfo.GetLocalId(); 1815 ErrCode err = OsAccountInterface::SendToStorageAccountStart(osAccountInfo); 1816 if (err != ERR_OK) { 1817 ACCOUNT_LOGE("Failed to SendToStorageAccountStart, localId %{public}d, error: %{public}d.", localId, err); 1818 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER; 1819 } 1820 if (osAccountInfo.GetIsVerified()) { 1821 verifiedAccounts_.EnsureInsert(osAccountInfo.GetLocalId(), true); 1822 } 1823 if (osAccountInfo.GetIsLoggedIn()) { 1824 loggedInAccounts_.EnsureInsert(osAccountInfo.GetLocalId(), true); 1825 } 1826 1827 if (!preVerified && osAccountInfo.GetIsVerified()) { 1828 OsAccountInterface::PublishCommonEvent(osAccountInfo, 1829 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED, Constants::OPERATION_UNLOCK); 1830 subscribeManager_.Publish(localId, OS_ACCOUNT_SUBSCRIBE_TYPE::UNLOCKED); 1831 ReportOsAccountLifeCycle(localId, Constants::OPERATION_UNLOCK); 1832 1833 auto task = [] { IInnerOsAccountManager::GetInstance().CleanGarbageOsAccounts(); }; 1834 std::thread cleanThread(task); 1835 pthread_setname_np(cleanThread.native_handle(), "CleanGarbageOsAccounts"); 1836 cleanThread.detach(); 1837 } 1838 return ERR_OK; 1839 } 1840 1841 ErrCode IInnerOsAccountManager::SendToAMSAccountStart(OsAccountInfo &osAccountInfo, 1842 uint64_t displayId) 1843 { 1844 OsAccountStartCallbackFunc callbackFunc = [this, displayId](int32_t localId) { 1845 this->PushIdIntoActiveList(localId); 1846 this->foregroundAccountMap_.EnsureInsert(displayId, localId); 1847 }; 1848 ErrCode errCode = OsAccountInterface::SendToAMSAccountStart(osAccountInfo, callbackFunc); 1849 if (errCode != ERR_OK) { 1850 ACCOUNT_LOGE("Failed to call SendToAMSAccountStart, localId: %{public}d, error: %{public}d.", 1851 osAccountInfo.GetLocalId(), errCode); 1852 return errCode; 1853 } 1854 1855 return ERR_OK; 1856 } 1857 1858 ErrCode IInnerOsAccountManager::StartOsAccount(const int id) 1859 { 1860 return ERR_OK; 1861 } 1862 1863 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id) 1864 { 1865 if (serialNumber == 1866 Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID) { 1867 id = Constants::ADMIN_LOCAL_ID; 1868 return ERR_OK; 1869 } 1870 std::vector<OsAccountInfo> osAccountInfos; 1871 id = -1; 1872 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos); 1873 if (errCode != ERR_OK) { 1874 ACCOUNT_LOGE("get osaccount info list error"); 1875 return errCode; 1876 } 1877 for (auto it = osAccountInfos.begin(); it != osAccountInfos.end(); it++) { 1878 if (serialNumber == it->GetSerialNumber()) { 1879 id = it->GetLocalId(); 1880 break; 1881 } 1882 } 1883 if (id == -1) { 1884 ACCOUNT_LOGE("cannot find id by serialNumber"); 1885 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_ERROR; 1886 } 1887 return ERR_OK; 1888 } 1889 1890 ErrCode IInnerOsAccountManager::GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo) 1891 { 1892 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1893 if (errCode != ERR_OK) { 1894 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode); 1895 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1896 } 1897 return ERR_OK; 1898 } 1899 1900 ErrCode IInnerOsAccountManager::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber) 1901 { 1902 OsAccountInfo osAccountInfo; 1903 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1904 if (errCode != ERR_OK) { 1905 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1906 } 1907 serialNumber = osAccountInfo.GetSerialNumber(); 1908 return ERR_OK; 1909 } 1910 1911 ErrCode IInnerOsAccountManager::SubscribeOsAccount( 1912 const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener) 1913 { 1914 auto subscribeInfoPtr = std::make_shared<OsAccountSubscribeInfo>(subscribeInfo); 1915 if (subscribeInfoPtr == nullptr) { 1916 ACCOUNT_LOGE("subscribeInfoPtr is nullptr"); 1917 } 1918 return subscribeManager_.SubscribeOsAccount(subscribeInfoPtr, eventListener); 1919 } 1920 1921 ErrCode IInnerOsAccountManager::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener) 1922 { 1923 return subscribeManager_.UnsubscribeOsAccount(eventListener); 1924 } 1925 1926 const std::shared_ptr<OsAccountSubscribeInfo> IInnerOsAccountManager::GetSubscribeRecordInfo( 1927 const sptr<IRemoteObject> &eventListener) 1928 { 1929 return subscribeManager_.GetSubscribeRecordInfo(eventListener); 1930 } 1931 1932 OS_ACCOUNT_SWITCH_MOD IInnerOsAccountManager::GetOsAccountSwitchMod() 1933 { 1934 return Constants::NOW_OS_ACCOUNT_SWITCH_MOD; 1935 } 1936 1937 ErrCode IInnerOsAccountManager::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted) 1938 { 1939 OsAccountInfo osAccountInfo; 1940 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 1941 if (errCode != ERR_OK) { 1942 return errCode; 1943 } 1944 isOsAccountCompleted = osAccountInfo.GetIsCreateCompleted(); 1945 return ERR_OK; 1946 } 1947 1948 ErrCode IInnerOsAccountManager::SetOsAccountIsVerified(const int id, const bool isVerified) 1949 { 1950 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id)); 1951 OsAccountInfo osAccountInfo; 1952 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1953 if (errCode != ERR_OK) { 1954 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode); 1955 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1956 } 1957 1958 // to be removed, cannot change any thing 1959 if (osAccountInfo.GetToBeRemoved()) { 1960 ACCOUNT_LOGE("account %{public}d will be removed, cannot change verify state!", id); 1961 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 1962 } 1963 bool preVerified = osAccountInfo.GetIsVerified(); 1964 1965 osAccountInfo.SetIsVerified(isVerified); 1966 if (isVerified) { 1967 verifiedAccounts_.EnsureInsert(id, true); 1968 } else { 1969 verifiedAccounts_.Erase(id); 1970 } 1971 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 1972 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) { 1973 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", 1974 errCode, osAccountInfo.GetLocalId()); 1975 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 1976 } 1977 if (isVerified && !preVerified) { 1978 OsAccountInterface::PublishCommonEvent(osAccountInfo, 1979 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED, Constants::OPERATION_UNLOCK); 1980 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::UNLOCKED); 1981 ReportOsAccountLifeCycle(id, Constants::OPERATION_UNLOCK); 1982 1983 auto task = [] { IInnerOsAccountManager::GetInstance().CleanGarbageOsAccounts(); }; 1984 std::thread cleanThread(task); 1985 pthread_setname_np(cleanThread.native_handle(), "CleanGarbageOsAccounts"); 1986 cleanThread.detach(); 1987 } 1988 return ERR_OK; 1989 } 1990 1991 ErrCode IInnerOsAccountManager::SetOsAccountIsLoggedIn(const int32_t id, const bool isLoggedIn) 1992 { 1993 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id)); 1994 OsAccountInfo osAccountInfo; 1995 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo); 1996 if (errCode != ERR_OK) { 1997 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode); 1998 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 1999 } 2000 if (osAccountInfo.GetToBeRemoved()) { 2001 ACCOUNT_LOGE("account %{public}d will be removed, cannot change verify state!", id); 2002 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 2003 } 2004 if (isLoggedIn) { 2005 loggedInAccounts_.EnsureInsert(id, true); 2006 } else { 2007 loggedInAccounts_.Erase(id); 2008 } 2009 if (!osAccountInfo.GetIsLoggedIn()) { 2010 #ifdef ACTIVATE_LAST_LOGGED_IN_ACCOUNT 2011 osAccountControl_->SetDefaultActivatedOsAccount(id); 2012 #endif 2013 osAccountInfo.SetIsLoggedIn(isLoggedIn); 2014 osAccountInfo.SetLastLoginTime(std::chrono::duration_cast<std::chrono::seconds>( 2015 std::chrono::system_clock::now().time_since_epoch()).count()); 2016 } 2017 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 2018 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) { 2019 ACCOUNT_LOGE("Update account info failed, errCode: %{public}d, id: %{public}d", errCode, id); 2020 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 2021 } 2022 return ERR_OK; 2023 } 2024 2025 ErrCode IInnerOsAccountManager::GetOsAccountCredentialId(const int id, uint64_t &credentialId) 2026 { 2027 credentialId = 0; 2028 OsAccountInfo osAccountInfo; 2029 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 2030 if (errCode == ERR_OK) { 2031 credentialId = osAccountInfo.GetCredentialId(); 2032 } 2033 return errCode; 2034 } 2035 2036 ErrCode IInnerOsAccountManager::SetOsAccountCredentialId(const int id, uint64_t credentialId) 2037 { 2038 OsAccountInfo osAccountInfo; 2039 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 2040 if (errCode != ERR_OK) { 2041 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 2042 } 2043 2044 osAccountInfo.SetCredentialId(credentialId); 2045 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 2046 if (errCode != ERR_OK) { 2047 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", 2048 errCode, osAccountInfo.GetLocalId()); 2049 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR; 2050 } 2051 return ERR_OK; 2052 } 2053 2054 ErrCode IInnerOsAccountManager::SetDefaultActivatedOsAccount(const int32_t id) 2055 { 2056 std::lock_guard<std::mutex> lock(operatingMutex_); 2057 if (id == defaultActivatedId_) { 2058 ACCOUNT_LOGW("no need to repeat set initial start id %{public}d", id); 2059 return ERR_OK; 2060 } 2061 OsAccountInfo osAccountInfo; 2062 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo); 2063 if (errCode != ERR_OK) { 2064 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 2065 } 2066 2067 errCode = IsValidOsAccount(osAccountInfo); 2068 if (errCode != ERR_OK) { 2069 return errCode; 2070 } 2071 errCode = osAccountControl_->SetDefaultActivatedOsAccount(id); 2072 if (errCode != ERR_OK) { 2073 ACCOUNT_LOGE("set default activated account id error %{public}d, id: %{public}d", errCode, id); 2074 return errCode; 2075 } 2076 defaultActivatedId_ = id; 2077 return ERR_OK; 2078 } 2079 2080 ErrCode IInnerOsAccountManager::IsOsAccountForeground(const int32_t localId, const uint64_t displayId, 2081 bool &isForeground) 2082 { 2083 int32_t id; 2084 if (!foregroundAccountMap_.Find(displayId, id)) { 2085 ACCOUNT_LOGE("No foreground account in displayId %{public}llu.", static_cast<unsigned long long>(displayId)); 2086 return ERR_ACCOUNT_COMMON_ACCOUNT_IN_DISPLAY_ID_NOT_FOUND_ERROR; 2087 } 2088 isForeground = (id == localId); 2089 return ERR_OK; 2090 } 2091 2092 ErrCode IInnerOsAccountManager::GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId) 2093 { 2094 if (!foregroundAccountMap_.Find(displayId, localId)) { 2095 ACCOUNT_LOGE("No foreground account in displayId %{public}llu.", static_cast<unsigned long long>(displayId)); 2096 return ERR_ACCOUNT_COMMON_ACCOUNT_IN_DISPLAY_ID_NOT_FOUND_ERROR; 2097 } 2098 return ERR_OK; 2099 } 2100 2101 ErrCode IInnerOsAccountManager::GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts) 2102 { 2103 accounts.clear(); 2104 auto it = [&](uint64_t displayId, int32_t localId) { 2105 ForegroundOsAccount foregroundOsAccount; 2106 foregroundOsAccount.displayId = displayId; 2107 foregroundOsAccount.localId = localId; 2108 accounts.emplace_back(foregroundOsAccount); 2109 }; 2110 foregroundAccountMap_.Iterate(it); 2111 ACCOUNT_LOGI("Get foreground list successful, total=%{public}zu.", accounts.size()); 2112 return ERR_OK; 2113 } 2114 2115 ErrCode IInnerOsAccountManager::GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds) 2116 { 2117 localIds.clear(); 2118 std::vector<int32_t> activatedIds; 2119 CopyFromActiveList(activatedIds); 2120 2121 std::vector<int32_t> foregroundIds; 2122 auto it = [&](uint64_t displayId, int32_t localId) { 2123 foregroundIds.emplace_back(localId); 2124 }; 2125 foregroundAccountMap_.Iterate(it); 2126 std::unordered_set<int32_t> foregroundSet(foregroundIds.begin(), foregroundIds.end()); 2127 for (const auto &id : activatedIds) { 2128 if (foregroundSet.find(id) == foregroundSet.end()) { 2129 localIds.emplace_back(id); 2130 } 2131 } 2132 ACCOUNT_LOGI("Get background list successful, total=%{public}zu.", localIds.size()); 2133 return ERR_OK; 2134 } 2135 2136 ErrCode IInnerOsAccountManager::GetDefaultActivatedOsAccount(int32_t &id) 2137 { 2138 std::lock_guard<std::mutex> lock(operatingMutex_); 2139 id = defaultActivatedId_; 2140 return ERR_OK; 2141 } 2142 2143 ErrCode IInnerOsAccountManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin) 2144 { 2145 return osAccountControl_->IsAllowedCreateAdmin(isAllowedCreateAdmin); 2146 } 2147 2148 ErrCode IInnerOsAccountManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID, 2149 int &createdOsAccountNum) 2150 { 2151 return osAccountControl_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum); 2152 } 2153 2154 ErrCode IInnerOsAccountManager::GetSerialNumberFromDatabase(const std::string& storeID, 2155 int64_t &serialNumber) 2156 { 2157 return osAccountControl_->GetSerialNumberFromDatabase(storeID, serialNumber); 2158 } 2159 2160 ErrCode IInnerOsAccountManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id) 2161 { 2162 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS 2163 return osAccountControl_->GetMaxAllowCreateIdFromDatabase(storeID, id); 2164 #else 2165 id = Constants::START_USER_ID; 2166 return ERR_OK; 2167 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS 2168 } 2169 2170 ErrCode IInnerOsAccountManager::GetOsAccountFromDatabase(const std::string& storeID, const int id, 2171 OsAccountInfo &osAccountInfo) 2172 { 2173 return osAccountControl_->GetOsAccountFromDatabase(storeID, id, osAccountInfo); 2174 } 2175 2176 ErrCode IInnerOsAccountManager::GetOsAccountListFromDatabase(const std::string& storeID, 2177 std::vector<OsAccountInfo> &osAccountList) 2178 { 2179 return osAccountControl_->GetOsAccountListFromDatabase(storeID, osAccountList); 2180 } 2181 2182 void IInnerOsAccountManager::RemoveLocalIdToOperating(int32_t localId) 2183 { 2184 std::lock_guard<std::mutex> lock(operatingMutex_); 2185 auto it = std::find(operatingId_.begin(), operatingId_.end(), localId); 2186 if (it != operatingId_.end()) { 2187 operatingId_.erase(it); 2188 } 2189 } 2190 2191 bool IInnerOsAccountManager::CheckAndAddLocalIdOperating(int32_t localId) 2192 { 2193 std::lock_guard<std::mutex> lock(operatingMutex_); 2194 if (std::find(operatingId_.begin(), operatingId_.end(), localId) != operatingId_.end()) { 2195 return false; 2196 } 2197 operatingId_.push_back(localId); 2198 return true; 2199 } 2200 2201 ErrCode IInnerOsAccountManager::QueryActiveOsAccountIds(std::vector<int32_t>& ids) 2202 { 2203 CopyFromActiveList(ids); 2204 return ERR_OK; 2205 } 2206 2207 void IInnerOsAccountManager::PushIdIntoActiveList(int32_t id) 2208 { 2209 std::lock_guard<std::mutex> lock(ativeMutex_); 2210 if (std::find(activeAccountId_.begin(), activeAccountId_.end(), id) == activeAccountId_.end()) { 2211 CountTraceAdapter("activeId", (int64_t)id); 2212 } 2213 #ifndef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS 2214 activeAccountId_.clear(); 2215 #else 2216 activeAccountId_.erase(std::remove(activeAccountId_.begin(), activeAccountId_.end(), id), activeAccountId_.end()); 2217 #endif 2218 //Compatible with the QueryActiveOsAccountIds 2219 activeAccountId_.insert(activeAccountId_.begin(), id); 2220 return; 2221 } 2222 2223 void IInnerOsAccountManager::EraseIdFromActiveList(int32_t id) 2224 { 2225 std::lock_guard<std::mutex> lock(ativeMutex_); 2226 if (std::find(activeAccountId_.begin(), activeAccountId_.end(), id) != activeAccountId_.end()) { 2227 ACCOUNT_LOGE("EraseIdFromActiveList enter0"); 2228 activeAccountId_.erase( 2229 std::remove(activeAccountId_.begin(), activeAccountId_.end(), id), activeAccountId_.end()); 2230 } else { 2231 ACCOUNT_LOGI("os account is not in active list, no need to erase!"); 2232 } 2233 CountTraceAdapter("deActiveId", (int64_t)id); 2234 } 2235 2236 bool IInnerOsAccountManager::IsOsAccountIDInActiveList(int32_t id) 2237 { 2238 std::lock_guard<std::mutex> lock(ativeMutex_); 2239 auto it = std::find(activeAccountId_.begin(), activeAccountId_.end(), id); 2240 return (it != activeAccountId_.end()); 2241 } 2242 2243 void IInnerOsAccountManager::CopyFromActiveList(std::vector<int32_t>& idList) 2244 { 2245 idList.clear(); 2246 std::lock_guard<std::mutex> lock(ativeMutex_); 2247 for (auto it = activeAccountId_.begin(); it != activeAccountId_.end(); it++) { 2248 idList.push_back(*it); 2249 } 2250 } 2251 2252 ErrCode IInnerOsAccountManager::UpdateAccountInfoByDomainAccountInfo( 2253 int32_t userId, const DomainAccountInfo &newDomainAccountInfo) 2254 { 2255 if (!CheckAndAddLocalIdOperating(userId)) { 2256 ACCOUNT_LOGW("Account id = %{public}d already in operating", userId); 2257 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 2258 } 2259 OsAccountInfo accountInfo; 2260 ErrCode result = osAccountControl_->GetOsAccountInfoById(userId, accountInfo); 2261 if (result != ERR_OK) { 2262 RemoveLocalIdToOperating(userId); 2263 return result; 2264 } 2265 DomainAccountInfo oldDomainAccountInfo; 2266 accountInfo.GetDomainInfo(oldDomainAccountInfo); 2267 if (!newDomainAccountInfo.accountName_.empty()) { 2268 oldDomainAccountInfo.accountName_ = newDomainAccountInfo.accountName_; 2269 } 2270 if (!newDomainAccountInfo.accountId_.empty()) { 2271 oldDomainAccountInfo.accountId_ = newDomainAccountInfo.accountId_; 2272 } 2273 accountInfo.SetDomainInfo(oldDomainAccountInfo); 2274 accountInfo.SetLocalName(newDomainAccountInfo.domain_ + "/" + newDomainAccountInfo.accountName_); 2275 result = osAccountControl_->UpdateOsAccount(accountInfo); 2276 if (result != ERR_OK) { 2277 ACCOUNT_LOGE("Update account info failed, result = %{public}d", result); 2278 ReportOsAccountOperationFail(userId, Constants::OPERATION_UPDATE, result, 2279 "Failed to update domain account info"); 2280 RemoveLocalIdToOperating(userId); 2281 return result; 2282 } 2283 RemoveLocalIdToOperating(userId); 2284 #ifdef HAS_CES_PART 2285 AccountEventProvider::EventPublish(EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, 2286 userId, nullptr); 2287 #endif // HAS_CES_PART 2288 return ERR_OK; 2289 } 2290 2291 ErrCode IInnerOsAccountManager::UpdateAccountToForeground(const uint64_t displayId, OsAccountInfo &osAccountInfo) 2292 { 2293 int32_t localId = osAccountInfo.GetLocalId(); 2294 osAccountInfo.SetIsActived(true); 2295 osAccountInfo.SetDisplayId(displayId); 2296 osAccountInfo.SetIsForeground(true); 2297 if (osAccountInfo.GetIsLoggedIn()) { 2298 #ifdef ACTIVATE_LAST_LOGGED_IN_ACCOUNT 2299 osAccountControl_->SetDefaultActivatedOsAccount(localId); 2300 #endif 2301 } 2302 ErrCode errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 2303 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) { 2304 ACCOUNT_LOGE("Update account failed, localId=%{public}d, errCode=%{public}d.", 2305 localId, errCode); 2306 } 2307 return ERR_OK; 2308 } 2309 2310 ErrCode IInnerOsAccountManager::UpdateAccountToBackground(int32_t oldId) 2311 { 2312 OsAccountInfo oldOsAccountInfo; 2313 { 2314 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(oldId)); 2315 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(oldId, oldOsAccountInfo); 2316 if (errCode != ERR_OK) { 2317 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 2318 } 2319 oldOsAccountInfo.SetIsForeground(false); 2320 oldOsAccountInfo.SetDisplayId(Constants::INVALID_DISPALY_ID); 2321 errCode = osAccountControl_->UpdateOsAccount(oldOsAccountInfo); 2322 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) { 2323 ACCOUNT_LOGE("Update osaccount failed, errCode=%{public}d, id=%{public}d", 2324 errCode, oldOsAccountInfo.GetLocalId()); 2325 } 2326 } 2327 OsAccountInterface::PublishCommonEvent(oldOsAccountInfo, 2328 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_BACKGROUND, Constants::OPERATION_SWITCH); 2329 2330 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS 2331 #ifndef SUPPORT_STOP_MAIN_OS_ACCOUNT 2332 if (oldId == Constants::START_USER_ID) { 2333 return ERR_OK; 2334 } 2335 #endif 2336 bool isLoggedIn = false; 2337 if ((oldOsAccountInfo.GetType() != OsAccountType::PRIVATE) && (!loggedInAccounts_.Find(oldId, isLoggedIn))) { 2338 DeactivateOsAccount(oldId, false); 2339 } 2340 #else 2341 DeactivateOsAccountByInfo(oldOsAccountInfo); 2342 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS 2343 return ERR_OK; 2344 } 2345 2346 std::shared_ptr<std::mutex> IInnerOsAccountManager::GetOrInsertUpdateLock(int32_t id) 2347 { 2348 std::lock_guard<std::mutex> lock(updateLockMutex_); 2349 auto it = updateLocks_.find(id); 2350 if (it == updateLocks_.end()) { 2351 auto mutexPtr = std::make_shared<std::mutex>(); 2352 updateLocks_.insert(std::make_pair(id, mutexPtr)); 2353 return mutexPtr; 2354 } else { 2355 return it->second; 2356 } 2357 } 2358 2359 ErrCode IInnerOsAccountManager::SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved) 2360 { 2361 if (!CheckAndAddLocalIdOperating(localId)) { 2362 ACCOUNT_LOGE("The account %{public}d already in operating", localId); 2363 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR; 2364 } 2365 OsAccountInfo osAccountInfo; 2366 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(localId, osAccountInfo); 2367 if (errCode != ERR_OK) { 2368 RemoveLocalIdToOperating(localId); 2369 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR; 2370 } 2371 osAccountInfo.SetToBeRemoved(toBeRemoved); 2372 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo); 2373 RemoveLocalIdToOperating(localId); 2374 return errCode; 2375 } 2376 2377 ErrCode IInnerOsAccountManager::IsValidOsAccount(const OsAccountInfo &osAccountInfo) 2378 { 2379 if (!osAccountInfo.GetIsCreateCompleted()) { 2380 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_IS_UNCOMPLETED_ERROR; 2381 } 2382 2383 if (osAccountInfo.GetToBeRemoved()) { 2384 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR; 2385 } 2386 return ERR_OK; 2387 } 2388 } // namespace AccountSA 2389 } // namespace OHOS