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 int32_t localId = osAccountInfo.GetLocalId();
893 int32_t foregroundId = -1;
894 if (foregroundAccountMap_.Find(Constants::DEFAULT_DISPALY_ID, foregroundId) && foregroundId == localId) {
895 foregroundAccountMap_.Erase(Constants::DEFAULT_DISPALY_ID);
896 }
897 ErrCode errCode = OsAccountInterface::SendToAMSAccountDeactivate(osAccountInfo);
898 if (errCode != ERR_OK) {
899 ACCOUNT_LOGE("SendToAMSAccountDeactivate failed, id %{public}d, errCode %{public}d",
900 osAccountInfo.GetLocalId(), errCode);
901 return errCode;
902 }
903 if (isStopStorage) {
904 errCode = OsAccountInterface::SendToStorageAccountStop(osAccountInfo);
905 if (errCode != ERR_OK) {
906 ACCOUNT_LOGE("SendToStorageAccountStop failed, id %{public}d, errCode %{public}d",
907 osAccountInfo.GetLocalId(), errCode);
908 return errCode;
909 }
910 }
911
912 return DeactivateOsAccountByInfo(osAccountInfo);
913 }
914
915 bool IInnerOsAccountManager::IsToBeRemoved(int32_t localId)
916 {
917 OsAccountInfo osAccountInfo;
918 ErrCode ret = QueryOsAccountById(localId, osAccountInfo);
919 if (ret == ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR) {
920 return true;
921 }
922 return osAccountInfo.GetToBeRemoved();
923 }
924
925 ErrCode IInnerOsAccountManager::ValidateOsAccount(const OsAccountInfo &osAccountInfo)
926 {
927 if (osAccountInfo.GetType() == OsAccountType::PRIVATE) {
928 return ERR_OK;
929 }
930 Json accountIndexJson;
931 ErrCode result = osAccountControl_->GetAccountIndexFromFile(accountIndexJson);
932 if (result != ERR_OK) {
933 return result;
934 }
935 std::string localIdStr = std::to_string(osAccountInfo.GetLocalId());
936 for (const auto& element : accountIndexJson.items()) {
937 std::string localIdKey = element.key();
938 auto value = element.value();
939 std::string localName = value[Constants::LOCAL_NAME].get<std::string>();
940 if ((osAccountInfo.GetLocalName() == localName) && (localIdKey != localIdStr)
941 && !IsToBeRemoved(std::stoi(localIdKey))) {
942 return ERR_ACCOUNT_COMMON_NAME_HAD_EXISTED;
943 }
944 if (!osAccountInfo.GetShortName().empty() && value.contains(Constants::SHORT_NAME)) {
945 std::string shortName = value[Constants::SHORT_NAME].get<std::string>();
946 if ((osAccountInfo.GetShortName() == shortName) && (localIdKey != localIdStr)
947 && !IsToBeRemoved(std::stoi(localIdKey))) {
948 return ERR_ACCOUNT_COMMON_SHORT_NAME_HAD_EXISTED;
949 }
950 }
951 }
952
953 return ERR_OK;
954 }
955
956 ErrCode IInnerOsAccountManager::GetTypeNumber(const OsAccountType& type, int32_t& typeNumber)
957 {
958 typeNumber = 0;
959 std::vector<OsAccountInfo> osAccountList;
960 ErrCode result = QueryAllCreatedOsAccounts(osAccountList);
961 if (result != ERR_OK) {
962 ACCOUNT_LOGE("Get os account list failed.");
963 return result;
964 }
965
966 typeNumber = std::count_if(osAccountList.begin(), osAccountList.end(),
967 [&type](const OsAccountInfo& info) { return info.GetType() == type; });
968 return ERR_OK;
969 }
970
971 ErrCode IInnerOsAccountManager::CheckTypeNumber(const OsAccountType& type)
972 {
973 if (type != OsAccountType::PRIVATE) {
974 return ERR_OK;
975 }
976 int32_t typeNumber = 0;
977 ErrCode result = GetTypeNumber(type, typeNumber);
978 if (result != ERR_OK) {
979 ACCOUNT_LOGE("Count type number failed.");
980 return result;
981 }
982 if (type == OsAccountType::PRIVATE && typeNumber >= MAX_PRIVATE_TYPE_NUMBER) {
983 ACCOUNT_LOGE("Check type number failed, private type number=%{public}d", typeNumber);
984 return ERR_OSACCOUNT_SERVICE_CONTROL_MAX_CAN_CREATE_ERROR;
985 }
986 return ERR_OK;
987 }
988
989 ErrCode IInnerOsAccountManager::SendMsgForAccountRemove(OsAccountInfo &osAccountInfo)
990 {
991 int32_t localId = osAccountInfo.GetLocalId();
992 AppAccountControlManager::GetInstance().SetOsAccountRemoved(localId, true);
993 ErrCode errCode = OsAccountInterface::SendToBMSAccountDelete(osAccountInfo);
994 if (errCode != ERR_OK) {
995 ACCOUNT_LOGE("SendToBMSAccountDelete failed, id %{public}d, errCode %{public}d", localId, errCode);
996 return errCode;
997 }
998 errCode = OsAccountInterface::SendToStorageAccountRemove(osAccountInfo);
999 if (errCode != ERR_OK) {
1000 ACCOUNT_LOGE("SendToStorageAccountRemove failed, id %{public}d, errCode %{public}d", localId, errCode);
1001 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER;
1002 }
1003 errCode = osAccountControl_->DelOsAccount(localId);
1004 if (errCode != ERR_OK) {
1005 ACCOUNT_LOGE("remove osaccount info failed, id: %{public}d, errCode %{public}d", localId, errCode);
1006 return errCode;
1007 }
1008 OsAccountInterface::SendToCESAccountDelete(osAccountInfo);
1009 ReportOsAccountLifeCycle(localId, Constants::OPERATION_REMOVE);
1010 return errCode;
1011 }
1012
1013 void IInnerOsAccountManager::Init()
1014 {
1015 ACCOUNT_LOGI("Start to create base os accounts");
1016 CreateBaseAdminAccount();
1017 CreateBaseStandardAccount();
1018 SetParameter(PARAM_LOGIN_NAME_MAX.c_str(), std::to_string(Constants::LOCAL_NAME_MAX_SIZE).c_str());
1019 ACCOUNT_LOGI("End to create base os accounts");
1020 }
1021
1022 ErrCode IInnerOsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExits)
1023 {
1024 isOsAccountExits = false;
1025 osAccountControl_->IsOsAccountExists(id, isOsAccountExits);
1026 return ERR_OK;
1027 }
1028
1029 ErrCode IInnerOsAccountManager::IsOsAccountActived(const int id, bool &isOsAccountActived)
1030 {
1031 isOsAccountActived = false;
1032
1033 // check if os account exists
1034 OsAccountInfo osAccountInfo;
1035 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1036 if (errCode != ERR_OK) {
1037 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
1038 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1039 }
1040 if (id == Constants::ADMIN_LOCAL_ID) {
1041 isOsAccountActived = true;
1042 return ERR_OK;
1043 }
1044 isOsAccountActived = osAccountInfo.GetIsActived();
1045 return ERR_OK;
1046 }
1047
1048 ErrCode IInnerOsAccountManager::IsOsAccountConstraintEnable(
1049 const int id, const std::string &constraint, bool &isOsAccountConstraintEnable)
1050 {
1051 isOsAccountConstraintEnable = false;
1052 OsAccountInfo osAccountInfo;
1053 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1054 if (errCode != ERR_OK) {
1055 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1056 }
1057 std::vector<std::string> constraints;
1058 constraints = osAccountInfo.GetConstraints();
1059 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
1060 isOsAccountConstraintEnable = true;
1061 return ERR_OK;
1062 }
1063 constraints.clear();
1064 if (osAccountControl_->GetGlobalOAConstraintsList(constraints) == ERR_OK) {
1065 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
1066 isOsAccountConstraintEnable = true;
1067 return ERR_OK;
1068 }
1069 }
1070 constraints.clear();
1071 if (osAccountControl_->GetSpecificOAConstraintsList(id, constraints) == ERR_OK) {
1072 if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
1073 isOsAccountConstraintEnable = true;
1074 return ERR_OK;
1075 }
1076 }
1077 return ERR_OK;
1078 }
1079
1080 ErrCode IInnerOsAccountManager::IsOsAccountVerified(const int id, bool &isVerified)
1081 {
1082 OsAccountInfo osAccountInfo;
1083 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1084 if (errCode != ERR_OK) {
1085 ACCOUNT_LOGE("Get osaccount fail, errCode=%{public}d, id=%{public}d", errCode, id);
1086 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1087 }
1088 isVerified = osAccountInfo.GetIsVerified();
1089 return ERR_OK;
1090 }
1091
1092 ErrCode IInnerOsAccountManager::IsOsAccountDeactivating(const int id, bool &isDeactivating)
1093 {
1094 isDeactivating = false;
1095 deactivatingAccounts_.Find(id, isDeactivating);
1096 return ERR_OK;
1097 }
1098
1099 ErrCode IInnerOsAccountManager::GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount)
1100 {
1101 std::vector<int32_t> idList;
1102 ErrCode errCode = osAccountControl_->GetOsAccountIdList(idList);
1103 if (errCode != ERR_OK) {
1104 ACCOUNT_LOGE("get osaccount info list error, errCode %{public}d.", errCode);
1105 return errCode;
1106 }
1107 createdOsAccountCount = idList.size();
1108 return ERR_OK;
1109 }
1110
1111 ErrCode IInnerOsAccountManager::QueryMaxOsAccountNumber(uint32_t &maxOsAccountNumber)
1112 {
1113 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1114 maxOsAccountNumber = config_.maxOsAccountNum;
1115 #else
1116 maxOsAccountNumber = 1;
1117 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1118 return ERR_OK;
1119 }
1120
1121 ErrCode IInnerOsAccountManager::QueryMaxLoggedInOsAccountNumber(uint32_t &maxNum)
1122 {
1123 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1124 maxNum = config_.maxLoggedInOsAccountNum;
1125 #else
1126 maxNum = 1;
1127 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1128 return ERR_OK;
1129 }
1130
1131 ErrCode IInnerOsAccountManager::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints)
1132 {
1133 OsAccountInfo osAccountInfo;
1134 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1135 if (errCode != ERR_OK) {
1136 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1137 }
1138 constraints = osAccountInfo.GetConstraints();
1139 std::vector<std::string> globalConstraints;
1140 errCode = osAccountControl_->GetGlobalOAConstraintsList(globalConstraints);
1141 if (errCode != ERR_OK) {
1142 ACCOUNT_LOGE("get globalConstraints info error");
1143 return errCode;
1144 }
1145 for (auto it = globalConstraints.begin(); it != globalConstraints.end(); it++) {
1146 if (std::find(constraints.begin(), constraints.end(), *it) == constraints.end()) {
1147 constraints.push_back(*it);
1148 }
1149 }
1150 std::vector<std::string> specificConstraints;
1151 errCode = osAccountControl_->GetSpecificOAConstraintsList(id, specificConstraints);
1152 if (errCode != ERR_OK) {
1153 ACCOUNT_LOGE("get specificConstraints info error");
1154 return errCode;
1155 }
1156 for (auto it = specificConstraints.begin(); it != specificConstraints.end(); it++) {
1157 if (std::find(constraints.begin(), constraints.end(), *it) == constraints.end()) {
1158 constraints.push_back(*it);
1159 }
1160 }
1161 return ERR_OK;
1162 }
1163
1164 ErrCode IInnerOsAccountManager::QueryOsAccountConstraintSourceTypes(const int32_t id,
1165 const std::string &constraint, std::vector<ConstraintSourceTypeInfo> &constraintSourceTypeInfos)
1166 {
1167 ACCOUNT_LOGD("enter.");
1168 bool isOsAccountConstraintEnable = false;
1169 ErrCode errCode = IsOsAccountConstraintEnable(id, constraint, isOsAccountConstraintEnable);
1170 if (errCode != ERR_OK) {
1171 ACCOUNT_LOGE("get os account constraint enable info error");
1172 return errCode;
1173 }
1174 if (!isOsAccountConstraintEnable) {
1175 ACCOUNT_LOGI("constraint not exist");
1176 ConstraintSourceTypeInfo constraintSourceTypeInfo;
1177 constraintSourceTypeInfo.localId = -1;
1178 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_NOT_EXIST;
1179 constraintSourceTypeInfos.push_back(constraintSourceTypeInfo);
1180 return ERR_OK;
1181 }
1182
1183 bool isExits;
1184 if (osAccountControl_->IsFromBaseOAConstraintsList(id, constraint, isExits) == ERR_OK) {
1185 if (isExits) {
1186 ACCOUNT_LOGI("constraint is exist in base os account constraints list");
1187 ConstraintSourceTypeInfo constraintSourceTypeInfo;
1188 constraintSourceTypeInfo.localId = -1;
1189 constraintSourceTypeInfo.typeInfo = ConstraintSourceType::CONSTRAINT_TYPE_BASE;
1190 constraintSourceTypeInfos.push_back(constraintSourceTypeInfo);
1191 }
1192 }
1193 std::vector<ConstraintSourceTypeInfo> globalSourceList;
1194 errCode = osAccountControl_->IsFromGlobalOAConstraintsList(id, deviceOwnerId_, constraint, globalSourceList);
1195 if (errCode == ERR_OK && globalSourceList.size() != 0) {
1196 ACCOUNT_LOGI("constraint is exist in global os account constraints list");
1197 constraintSourceTypeInfos.insert(
1198 constraintSourceTypeInfos.end(), globalSourceList.begin(), globalSourceList.end());
1199 }
1200 std::vector<ConstraintSourceTypeInfo> specificSourceList;
1201 errCode = osAccountControl_->IsFromSpecificOAConstraintsList(id, deviceOwnerId_, constraint, specificSourceList);
1202 if (errCode == ERR_OK && specificSourceList.size() != 0) {
1203 ACCOUNT_LOGI("constraint is exist in specific os account constraints list");
1204 constraintSourceTypeInfos.insert(
1205 constraintSourceTypeInfos.end(), specificSourceList.begin(), specificSourceList.end());
1206 }
1207 return ERR_OK;
1208 }
1209
1210 ErrCode IInnerOsAccountManager::SetBaseOsAccountConstraints(const int32_t id,
1211 const std::vector<std::string> &constraints, const bool enable)
1212 {
1213 ErrCode errCode = SetOsAccountConstraints(id, constraints, enable);
1214 if (errCode != ERR_OK) {
1215 ACCOUNT_LOGE("set os account %{public}d constraints failed! errCode %{public}d.", id, errCode);
1216 return errCode;
1217 }
1218
1219 errCode = osAccountControl_->UpdateBaseOAConstraints(std::to_string(id), constraints, enable);
1220 if (errCode != ERR_OK) {
1221 ACCOUNT_LOGE("update base os account %{public}d constraints failed! errCode %{public}d.", id, errCode);
1222 return errCode;
1223 }
1224 return ERR_OK;
1225 }
1226
1227 ErrCode IInnerOsAccountManager::SetGlobalOsAccountConstraints(const std::vector<std::string> &constraints,
1228 const bool enable, const int32_t enforcerId, const bool isDeviceOwner)
1229 {
1230 OsAccountInfo osAccountInfo;
1231 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(enforcerId, osAccountInfo);
1232 if (errCode != ERR_OK) {
1233 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1234 }
1235 if (osAccountInfo.GetToBeRemoved()) {
1236 ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", enforcerId);
1237 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1238 }
1239
1240 bool isExists = false;
1241 bool isOverSize = false;
1242 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize);
1243 if (errCode != ERR_OK || !isExists || isOverSize) {
1244 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!");
1245 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1246 }
1247
1248 osAccountControl_->UpdateGlobalOAConstraints(std::to_string(enforcerId), constraints, enable);
1249
1250 errCode = DealWithDeviceOwnerId(isDeviceOwner, enforcerId);
1251 if (errCode != ERR_OK) {
1252 ACCOUNT_LOGE("deal with device owner id error");
1253 return errCode;
1254 }
1255 return ERR_OK;
1256 }
1257
1258 ErrCode IInnerOsAccountManager::SetSpecificOsAccountConstraints(const std::vector<std::string> &constraints,
1259 const bool enable, const int32_t targetId, const int32_t enforcerId, const bool isDeviceOwner)
1260 {
1261 OsAccountInfo enforcerOsAccountInfo;
1262 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(enforcerId, enforcerOsAccountInfo);
1263 if (errCode != ERR_OK) {
1264 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1265 }
1266
1267 OsAccountInfo targetOsAccountInfo;
1268 errCode = osAccountControl_->GetOsAccountInfoById(targetId, targetOsAccountInfo);
1269 if (errCode != ERR_OK) {
1270 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1271 }
1272 if (targetOsAccountInfo.GetToBeRemoved() || enforcerOsAccountInfo.GetToBeRemoved()) {
1273 ACCOUNT_LOGE("account %{public}d or %{public}d will be removed, cannot change constraints!",
1274 enforcerId, targetId);
1275 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1276 }
1277
1278 bool isExists = false;
1279 bool isOverSize = false;
1280 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize);
1281 if (errCode != ERR_OK || !isExists || isOverSize) {
1282 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!");
1283 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1284 }
1285
1286 osAccountControl_->UpdateSpecificOAConstraints(
1287 std::to_string(enforcerId), std::to_string(targetId), constraints, enable);
1288
1289 errCode = DealWithDeviceOwnerId(isDeviceOwner, enforcerId);
1290 if (errCode != ERR_OK) {
1291 ACCOUNT_LOGE("deal with device owner id error");
1292 return errCode;
1293 }
1294 return ERR_OK;
1295 }
1296
1297 ErrCode IInnerOsAccountManager::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &createdOsAccounts)
1298 {
1299 std::vector<OsAccountInfo> allOsAccounts;
1300 ErrCode errCode = osAccountControl_->GetOsAccountList(allOsAccounts);
1301 if (errCode != ERR_OK) {
1302 ACCOUNT_LOGE("get osaccount info list error, errCode %{public}d.", errCode);
1303 return errCode;
1304 }
1305 for (auto osAccountInfo : allOsAccounts) {
1306 if (osAccountInfo.GetIsCreateCompleted() && !osAccountInfo.GetToBeRemoved()) {
1307 createdOsAccounts.push_back(osAccountInfo);
1308 }
1309 }
1310 return ERR_OK;
1311 }
1312
1313 ErrCode IInnerOsAccountManager::DealWithDeviceOwnerId(const bool isDeviceOwner, const int32_t localId)
1314 {
1315 ACCOUNT_LOGD("enter.");
1316 if (isDeviceOwner && localId != deviceOwnerId_) {
1317 ACCOUNT_LOGI("this device owner os account id is changed!");
1318 deviceOwnerId_ = localId;
1319 return osAccountControl_->UpdateDeviceOwnerId(localId);
1320 }
1321 if (isDeviceOwner == false && localId == deviceOwnerId_) {
1322 deviceOwnerId_ = -1;
1323 return osAccountControl_->UpdateDeviceOwnerId(-1);
1324 }
1325 return ERR_OK;
1326 }
1327
1328 int32_t IInnerOsAccountManager::CleanGarbageOsAccounts(int32_t excludeId)
1329 {
1330 ACCOUNT_LOGI("enter");
1331 std::vector<int32_t> idList;
1332 if (osAccountControl_->GetOsAccountIdList(idList) != ERR_OK) {
1333 ACCOUNT_LOGI("GetOsAccountIdList failed.");
1334 return 0;
1335 }
1336
1337 int32_t removeNum = 0;
1338
1339 for (auto id : idList) {
1340 if (id == Constants::START_USER_ID || id == Constants::ADMIN_LOCAL_ID || id == excludeId) {
1341 continue;
1342 }
1343 if (!CheckAndAddLocalIdOperating(id)) {
1344 ACCOUNT_LOGI("Account id = %{public}d already in operating", id);
1345 continue;
1346 }
1347 OsAccountInfo osAccountInfo;
1348 ErrCode ret = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1349 if (ret != ERR_OK && ret != ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR) {
1350 continue;
1351 }
1352 osAccountInfo.SetLocalId(id);
1353
1354 if (!osAccountInfo.GetToBeRemoved() && osAccountInfo.GetIsCreateCompleted()) {
1355 RemoveLocalIdToOperating(id);
1356 continue;
1357 }
1358 ErrCode errCode = RemoveOsAccountOperate(id, osAccountInfo, true);
1359 RemoveLocalIdToOperating(id);
1360 if (errCode != ERR_OK) {
1361 ACCOUNT_LOGE("remove account %{public}d failed! errCode %{public}d.", id, errCode);
1362 } else {
1363 ACCOUNT_LOGI("remove account %{public}d succeed!", id);
1364 removeNum++;
1365 }
1366 }
1367 ACCOUNT_LOGI("finished.");
1368 return removeNum;
1369 }
1370
1371 bool IInnerOsAccountManager::IsSameAccount(
1372 const DomainAccountInfo &domainInfoSrc, const DomainAccountInfo &domainInfoTar)
1373 {
1374 return (((!domainInfoSrc.accountId_.empty()) && (domainInfoSrc.accountId_ == domainInfoTar.accountId_)) ||
1375 ((!domainInfoSrc.accountName_.empty()) && (domainInfoSrc.accountName_ == domainInfoTar.accountName_) &&
1376 (!domainInfoSrc.domain_.empty()) && (domainInfoSrc.domain_ == domainInfoTar.domain_)));
1377 }
1378
1379 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id)
1380 {
1381 if (domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
1382 ACCOUNT_LOGE("invalid domain name length %{public}zu.", domainInfo.domain_.size());
1383 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1384 }
1385
1386 if (domainInfo.accountName_.size() > Constants::LOCAL_NAME_MAX_SIZE) {
1387 ACCOUNT_LOGE("invalid domain account name length %{public}zu.", domainInfo.accountName_.size());
1388 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1389 }
1390
1391 id = -1;
1392 std::vector<OsAccountInfo> osAccountInfos;
1393 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
1394 if (errCode != ERR_OK) {
1395 return errCode;
1396 }
1397
1398 DomainAccountInfo curDomainInfo;
1399 for (auto osAccountInfosPtr = osAccountInfos.begin(); osAccountInfosPtr != osAccountInfos.end();
1400 ++osAccountInfosPtr) {
1401 osAccountInfosPtr->GetDomainInfo(curDomainInfo);
1402 if (IsSameAccount(curDomainInfo, domainInfo)) {
1403 id = osAccountInfosPtr->GetLocalId();
1404 return ERR_OK;
1405 }
1406 }
1407 return ERR_DOMAIN_ACCOUNT_SERVICE_NOT_DOMAIN_ACCOUNT;
1408 }
1409
1410 ErrCode IInnerOsAccountManager::GetOsAccountShortName(const int id, std::string &shortName)
1411 {
1412 OsAccountInfo osAccountInfo;
1413 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1414 if (errCode != ERR_OK) {
1415 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1416 }
1417 shortName = osAccountInfo.GetShortName();
1418 return ERR_OK;
1419 }
1420
1421 ErrCode IInnerOsAccountManager::GetOsAccountName(const int id, std::string &name)
1422 {
1423 OsAccountInfo osAccountInfo;
1424 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1425 if (errCode != ERR_OK) {
1426 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1427 }
1428 name = osAccountInfo.GetLocalName();
1429 return ERR_OK;
1430 }
1431
1432 ErrCode IInnerOsAccountManager::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo)
1433 {
1434 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1435 if (errCode != ERR_OK) {
1436 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
1437 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1438 }
1439
1440 if (osAccountInfo.GetPhoto() != "") {
1441 std::string photo = osAccountInfo.GetPhoto();
1442 errCode = osAccountControl_->GetPhotoById(osAccountInfo.GetLocalId(), photo);
1443 if (errCode != ERR_OK) {
1444 ACCOUNT_LOGE("get osaccount photo error, errCode %{public}d.", errCode);
1445 return errCode;
1446 }
1447 osAccountInfo.SetPhoto(photo);
1448 }
1449 GetDomainAccountStatus(osAccountInfo);
1450 return ERR_OK;
1451 }
1452
1453 ErrCode IInnerOsAccountManager::GetOsAccountType(const int id, OsAccountType &type)
1454 {
1455 OsAccountInfo osAccountInfo;
1456 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1457 if (errCode != ERR_OK) {
1458 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1459 }
1460 type = osAccountInfo.GetType();
1461 return ERR_OK;
1462 }
1463
1464 ErrCode IInnerOsAccountManager::GetOsAccountProfilePhoto(const int id, std::string &photo)
1465 {
1466 OsAccountInfo osAccountInfo;
1467 ErrCode errCode = QueryOsAccountById(id, osAccountInfo);
1468 if (errCode != ERR_OK) {
1469 ACCOUNT_LOGE("QueryOsAccountById return error, errCode %{public}d.", errCode);
1470 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1471 }
1472 photo = osAccountInfo.GetPhoto();
1473 return ERR_OK;
1474 }
1475
1476 ErrCode IInnerOsAccountManager::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
1477 {
1478 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
1479 ErrCode errCode = osAccountControl_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable);
1480 if (errCode != ERR_OK) {
1481 ACCOUNT_LOGE("GetIsMultiOsAccountEnable error, errCode %{public}d.", errCode);
1482 return errCode;
1483 }
1484 #else
1485 isMultiOsAccountEnable = false;
1486 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
1487 return ERR_OK;
1488 }
1489
1490 ErrCode IInnerOsAccountManager::SetOsAccountName(const int id, const std::string &name)
1491 {
1492 OsAccountInfo osAccountInfo;
1493 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1494 if (errCode != ERR_OK) {
1495 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1496 }
1497
1498 // to be removed, cannot change any thing
1499 if (osAccountInfo.GetToBeRemoved()) {
1500 ACCOUNT_LOGE("account %{public}d will be removed, cannot change name!", id);
1501 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1502 }
1503
1504 std::string localName = osAccountInfo.GetLocalName();
1505 if (localName == name) {
1506 return ERR_OK;
1507 }
1508
1509 osAccountInfo.SetLocalName(name);
1510 errCode = ValidateOsAccount(osAccountInfo);
1511 if (errCode != ERR_OK) {
1512 ACCOUNT_LOGE("account name already exist, errCode %{public}d.", errCode);
1513 return errCode;
1514 }
1515
1516 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1517 if (errCode != ERR_OK) {
1518 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId());
1519 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1520 }
1521 osAccountControl_->UpdateAccountIndex(osAccountInfo, false);
1522 OsAccountInterface::PublishCommonEvent(
1523 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE);
1524 return ERR_OK;
1525 }
1526
1527 ErrCode IInnerOsAccountManager::SetOsAccountConstraints(
1528 const int id, const std::vector<std::string> &constraints, const bool enable)
1529 {
1530 OsAccountInfo osAccountInfo;
1531 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1532 if (errCode != ERR_OK) {
1533 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1534 }
1535
1536 // to be removed, cannot change any thing
1537 if (osAccountInfo.GetToBeRemoved()) {
1538 ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", id);
1539 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1540 }
1541
1542 bool isExists = false;
1543 bool isOverSize = false;
1544 errCode = osAccountControl_->CheckConstraintsList(constraints, isExists, isOverSize);
1545 if (errCode != ERR_OK || !isExists || isOverSize) {
1546 ACCOUNT_LOGE("input constraints not in constraints list or is oversize!");
1547 return ERR_ACCOUNT_COMMON_INVALID_PARAMETER;
1548 }
1549 std::vector<std::string> oldConstraints = osAccountInfo.GetConstraints();
1550 for (auto it = constraints.begin(); it != constraints.end(); it++) {
1551 if (enable) {
1552 if (std::find(oldConstraints.begin(), oldConstraints.end(), *it) == oldConstraints.end()) {
1553 oldConstraints.push_back(*it);
1554 }
1555 } else {
1556 oldConstraints.erase(
1557 std::remove(oldConstraints.begin(), oldConstraints.end(), *it), oldConstraints.end());
1558 }
1559 }
1560 osAccountInfo.SetConstraints(oldConstraints);
1561 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1562 if (errCode != ERR_OK) {
1563 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d", errCode, osAccountInfo.GetLocalId());
1564 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1565 }
1566 return ERR_OK;
1567 }
1568
1569 ErrCode IInnerOsAccountManager::SetOsAccountProfilePhoto(const int id, const std::string &photo)
1570 {
1571 OsAccountInfo osAccountInfo;
1572 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1573 if (errCode != ERR_OK) {
1574 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1575 }
1576
1577 // to be removed, cannot change any thing
1578 if (osAccountInfo.GetToBeRemoved()) {
1579 ACCOUNT_LOGE("account %{public}d will be removed, cannot change photo!", id);
1580 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1581 }
1582
1583 if (osAccountInfo.GetPhoto() == photo) {
1584 return ERR_OK;
1585 }
1586 errCode = osAccountControl_->SetPhotoById(id, photo);
1587 if (errCode != ERR_OK) {
1588 ACCOUNT_LOGE("Set photo error, code=%{public}d, id=%{public}d.", errCode, id);
1589 return errCode;
1590 }
1591 osAccountInfo.SetPhoto(Constants::USER_PHOTO_FILE_TXT_NAME);
1592 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1593 if (errCode != ERR_OK) {
1594 ACCOUNT_LOGE("Update osaccount info faile code=%{public}d, id=%{public}d", errCode, osAccountInfo.GetLocalId());
1595 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1596 }
1597 OsAccountInterface::PublishCommonEvent(
1598 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED, Constants::OPERATION_UPDATE);
1599 return ERR_OK;
1600 }
1601
1602 ErrCode IInnerOsAccountManager::DeactivateOsAccountByInfo(OsAccountInfo &osAccountInfo)
1603 {
1604 int localId = osAccountInfo.GetLocalId();
1605 loggedInAccounts_.Erase(localId);
1606 verifiedAccounts_.Erase(localId);
1607 int32_t foregroundId = -1;
1608 if (foregroundAccountMap_.Find(Constants::DEFAULT_DISPALY_ID, foregroundId) && foregroundId == localId) {
1609 foregroundAccountMap_.Erase(Constants::DEFAULT_DISPALY_ID);
1610 }
1611 EraseIdFromActiveList(localId);
1612
1613 osAccountInfo.SetIsActived(false);
1614 osAccountInfo.SetIsVerified(false);
1615 osAccountInfo.SetIsForeground(false);
1616 osAccountInfo.SetDisplayId(Constants::INVALID_DISPALY_ID);
1617 osAccountInfo.SetIsLoggedIn(false);
1618 DomainAccountInfo domainAccountInfo;
1619 osAccountInfo.GetDomainInfo(domainAccountInfo);
1620 domainAccountInfo.status_ = DomainAccountStatus::LOGOUT;
1621 osAccountInfo.SetDomainInfo(domainAccountInfo);
1622 ErrCode errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1623 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) {
1624 ACCOUNT_LOGE("Update account failed, id=%{public}d, errCode=%{public}d.", osAccountInfo.GetLocalId(), errCode);
1625 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1626 }
1627
1628 AccountInfoReport::ReportSecurityInfo(osAccountInfo.GetLocalName(), localId,
1629 ReportEvent::EVENT_LOGOUT, 0);
1630 return ERR_OK;
1631 }
1632
1633 ErrCode IInnerOsAccountManager::DeactivateOsAccountById(const int id)
1634 {
1635 OsAccountInfo osAccountInfo;
1636 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1637 if (errCode != ERR_OK) {
1638 ACCOUNT_LOGE("cannot get os account %{public}d info. error %{public}d.",
1639 id, errCode);
1640 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1641 }
1642 return DeactivateOsAccountByInfo(osAccountInfo);
1643 }
1644
1645 ErrCode IInnerOsAccountManager::ActivateOsAccount(const int id, const bool startStorage, const uint64_t displayId)
1646 {
1647 if (!CheckAndAddLocalIdOperating(id)) {
1648 ACCOUNT_LOGE("the %{public}d already in operating", id);
1649 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
1650 }
1651 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id));
1652 // get information
1653 OsAccountInfo osAccountInfo;
1654 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1655 if (errCode != ERR_OK) {
1656 RemoveLocalIdToOperating(id);
1657 ACCOUNT_LOGE("cannot find os account info by id:%{public}d, errCode %{public}d.", id, errCode);
1658 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1659 }
1660
1661 int32_t foregroundId = -1;
1662 if (foregroundAccountMap_.Find(displayId, foregroundId) && (foregroundId == id) && osAccountInfo.GetIsVerified()) {
1663 ACCOUNT_LOGI("Account %{public}d already is foreground", id);
1664 RemoveLocalIdToOperating(id);
1665 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREADY_ACTIVE_ERROR;
1666 }
1667
1668 errCode = IsValidOsAccount(osAccountInfo);
1669 if (errCode != ERR_OK) {
1670 RemoveLocalIdToOperating(id);
1671 return errCode;
1672 }
1673
1674 if (!osAccountInfo.GetIsActived() &&
1675 (static_cast<uint32_t>(loggedInAccounts_.Size()) >= config_.maxLoggedInOsAccountNum)) {
1676 RemoveLocalIdToOperating(id);
1677 ACCOUNT_LOGE("The number of logged in account reaches the upper limit, maxLoggedInNum: %{public}d",
1678 config_.maxLoggedInOsAccountNum);
1679 return ERR_OSACCOUNT_SERVICE_LOGGED_IN_ACCOUNTS_OVERSIZE;
1680 }
1681
1682 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVATING);
1683 errCode = SendMsgForAccountActivate(osAccountInfo, startStorage);
1684 RemoveLocalIdToOperating(id);
1685 if (errCode != ERR_OK) {
1686 return errCode;
1687 }
1688
1689 DomainAccountInfo domainInfo;
1690 osAccountInfo.GetDomainInfo(domainInfo);
1691 if (domainInfo.accountId_.empty() && (osAccountInfo.GetCredentialId() == 0)) {
1692 AccountInfoReport::ReportSecurityInfo(
1693 osAccountInfo.GetLocalName(), osAccountInfo.GetLocalId(), ReportEvent::EVENT_LOGIN, 0);
1694 }
1695 ACCOUNT_LOGI("IInnerOsAccountManager ActivateOsAccount end");
1696 return ERR_OK;
1697 }
1698
1699 ErrCode IInnerOsAccountManager::DeactivateOsAccount(const int id, bool isStopStorage)
1700 {
1701 if (!CheckAndAddLocalIdOperating(id)) {
1702 ACCOUNT_LOGW("the %{public}d already in operating", id);
1703 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
1704 }
1705 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id));
1706 OsAccountInfo osAccountInfo;
1707 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1708 if (errCode != ERR_OK) {
1709 RemoveLocalIdToOperating(id);
1710 ACCOUNT_LOGW("cannot find os account info by id:%{public}d, errCode %{public}d.", id, errCode);
1711 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1712 }
1713
1714 if ((!osAccountInfo.GetIsActived()) && (!osAccountInfo.GetIsVerified())) {
1715 RemoveLocalIdToOperating(id);
1716 ACCOUNT_LOGW("account %{public}d is neither active nor verified, don't need to deactivate!", id);
1717 return ERR_OK;
1718 }
1719 errCode = IsValidOsAccount(osAccountInfo);
1720 if (errCode != ERR_OK) {
1721 RemoveLocalIdToOperating(id);
1722 return errCode;
1723 }
1724
1725 deactivatingAccounts_.EnsureInsert(id, true);
1726
1727 OsAccountInterface::PublishCommonEvent(
1728 osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPING, Constants::OPERATION_STOP);
1729 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPING);
1730
1731 errCode = SendMsgForAccountDeactivate(osAccountInfo, isStopStorage);
1732 deactivatingAccounts_.Erase(id);
1733 if (errCode != ERR_OK) {
1734 RemoveLocalIdToOperating(id);
1735 ReportOsAccountOperationFail(id, "deactivate", errCode, "deactivate os account failed");
1736 return errCode;
1737 }
1738
1739 OsAccountInterface::PublishCommonEvent(osAccountInfo, OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_STOPPED,
1740 Constants::OPERATION_STOP);
1741 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::STOPPED);
1742 ReportOsAccountLifeCycle(id, Constants::OPERATION_STOP);
1743
1744 RemoveLocalIdToOperating(id);
1745 ACCOUNT_LOGI("IInnerOsAccountManager DeactivateOsAccount end");
1746 return ERR_OK;
1747 }
1748
1749 void IInnerOsAccountManager::RollBackToEarlierAccount(int32_t fromId, int32_t toId)
1750 {
1751 ACCOUNT_LOGI("Enter.");
1752 if (fromId == toId) {
1753 return;
1754 }
1755 subscribeManager_.Publish(fromId, toId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED);
1756 ReportOsAccountSwitch(fromId, toId);
1757 ACCOUNT_LOGI("End pushlishing pre switch event.");
1758 OsAccountInfo osAccountInfo;
1759 osAccountInfo.SetLocalId(toId);
1760 subscribeManager_.Publish(toId, fromId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHING);
1761 OsAccountInterface::PublishCommonEvent(osAccountInfo,
1762 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_FOREGROUND, Constants::OPERATION_SWITCH);
1763 subscribeManager_.Publish(toId, fromId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED);
1764 ReportOsAccountSwitch(toId, fromId);
1765 ACCOUNT_LOGI("End pushlishing post switch event.");
1766 }
1767
1768 ErrCode IInnerOsAccountManager::SendMsgForAccountActivate(OsAccountInfo &osAccountInfo, const bool startStorage,
1769 const uint64_t displayId)
1770 {
1771 // activate
1772 int32_t oldId = -1;
1773 bool oldIdExist = foregroundAccountMap_.Find(displayId, oldId);
1774 int32_t localId = static_cast<int32_t>(osAccountInfo.GetLocalId());
1775 bool preActivated = osAccountInfo.GetIsActived();
1776 subscribeManager_.Publish(localId, oldId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHING);
1777
1778 if (startStorage) {
1779 ErrCode errCode = SendToStorageAccountStart(osAccountInfo);
1780 if (errCode != ERR_OK) {
1781 RollBackToEarlierAccount(localId, oldId);
1782 return errCode;
1783 }
1784 }
1785 ErrCode errCode = SendToAMSAccountStart(osAccountInfo, displayId);
1786 if (errCode != ERR_OK) {
1787 RollBackToEarlierAccount(localId, oldId);
1788 return errCode;
1789 }
1790 if (oldId != localId) {
1791 OsAccountInterface::PublishCommonEvent(osAccountInfo,
1792 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_FOREGROUND, Constants::OPERATION_SWITCH);
1793 }
1794 errCode = UpdateAccountToForeground(displayId, osAccountInfo);
1795 if (errCode != ERR_OK) {
1796 return errCode;
1797 }
1798
1799 if (oldIdExist && (oldId != localId)) {
1800 if ((errCode = UpdateAccountToBackground(oldId)) != ERR_OK) {
1801 return errCode;
1802 }
1803 }
1804
1805 OsAccountInterface::SendToCESAccountSwitched(localId, oldId);
1806 subscribeManager_.Publish(localId, OS_ACCOUNT_SUBSCRIBE_TYPE::ACTIVED);
1807 subscribeManager_.Publish(localId, oldId, OS_ACCOUNT_SUBSCRIBE_TYPE::SWITCHED);
1808 if (!preActivated) {
1809 ReportOsAccountLifeCycle(defaultActivatedId_, Constants::OPERATION_ACTIVATE);
1810 }
1811 ACCOUNT_LOGI("SendMsgForAccountActivate ok");
1812 ReportOsAccountSwitch(localId, oldId);
1813 return errCode;
1814 }
1815
1816 ErrCode IInnerOsAccountManager::SendToStorageAccountStart(OsAccountInfo &osAccountInfo)
1817 {
1818 bool preVerified = osAccountInfo.GetIsVerified();
1819 int32_t localId = osAccountInfo.GetLocalId();
1820 ErrCode err = OsAccountInterface::SendToStorageAccountStart(osAccountInfo);
1821 if (err != ERR_OK) {
1822 ACCOUNT_LOGE("Failed to SendToStorageAccountStart, localId %{public}d, error: %{public}d.", localId, err);
1823 return ERR_ACCOUNT_COMMON_GET_SYSTEM_ABILITY_MANAGER;
1824 }
1825 if (osAccountInfo.GetIsVerified()) {
1826 verifiedAccounts_.EnsureInsert(osAccountInfo.GetLocalId(), true);
1827 }
1828 if (osAccountInfo.GetIsLoggedIn()) {
1829 loggedInAccounts_.EnsureInsert(osAccountInfo.GetLocalId(), true);
1830 }
1831
1832 if (!preVerified && osAccountInfo.GetIsVerified()) {
1833 OsAccountInterface::PublishCommonEvent(osAccountInfo,
1834 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED, Constants::OPERATION_UNLOCK);
1835 subscribeManager_.Publish(localId, OS_ACCOUNT_SUBSCRIBE_TYPE::UNLOCKED);
1836 ReportOsAccountLifeCycle(localId, Constants::OPERATION_UNLOCK);
1837
1838 auto task = [] { IInnerOsAccountManager::GetInstance().CleanGarbageOsAccounts(); };
1839 std::thread cleanThread(task);
1840 pthread_setname_np(cleanThread.native_handle(), "CleanGarbageOsAccounts");
1841 cleanThread.detach();
1842 }
1843 return ERR_OK;
1844 }
1845
1846 ErrCode IInnerOsAccountManager::SendToAMSAccountStart(OsAccountInfo &osAccountInfo,
1847 uint64_t displayId)
1848 {
1849 OsAccountStartCallbackFunc callbackFunc = [this, displayId](int32_t localId) {
1850 this->PushIdIntoActiveList(localId);
1851 this->foregroundAccountMap_.EnsureInsert(displayId, localId);
1852 };
1853 ErrCode errCode = OsAccountInterface::SendToAMSAccountStart(osAccountInfo, callbackFunc);
1854 if (errCode != ERR_OK) {
1855 ACCOUNT_LOGE("Failed to call SendToAMSAccountStart, localId: %{public}d, error: %{public}d.",
1856 osAccountInfo.GetLocalId(), errCode);
1857 return errCode;
1858 }
1859
1860 return ERR_OK;
1861 }
1862
1863 ErrCode IInnerOsAccountManager::StartOsAccount(const int id)
1864 {
1865 return ERR_OK;
1866 }
1867
1868 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id)
1869 {
1870 if (serialNumber ==
1871 Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID) {
1872 id = Constants::ADMIN_LOCAL_ID;
1873 return ERR_OK;
1874 }
1875 std::vector<OsAccountInfo> osAccountInfos;
1876 id = -1;
1877 ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
1878 if (errCode != ERR_OK) {
1879 ACCOUNT_LOGE("get osaccount info list error");
1880 return errCode;
1881 }
1882 for (auto it = osAccountInfos.begin(); it != osAccountInfos.end(); it++) {
1883 if (serialNumber == it->GetSerialNumber()) {
1884 id = it->GetLocalId();
1885 break;
1886 }
1887 }
1888 if (id == -1) {
1889 ACCOUNT_LOGE("cannot find id by serialNumber");
1890 return ERR_OSACCOUNT_SERVICE_INNER_SELECT_ERROR;
1891 }
1892 return ERR_OK;
1893 }
1894
1895 ErrCode IInnerOsAccountManager::GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo)
1896 {
1897 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1898 if (errCode != ERR_OK) {
1899 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
1900 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1901 }
1902 return ERR_OK;
1903 }
1904
1905 ErrCode IInnerOsAccountManager::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber)
1906 {
1907 OsAccountInfo osAccountInfo;
1908 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1909 if (errCode != ERR_OK) {
1910 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1911 }
1912 serialNumber = osAccountInfo.GetSerialNumber();
1913 return ERR_OK;
1914 }
1915
1916 ErrCode IInnerOsAccountManager::SubscribeOsAccount(
1917 const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
1918 {
1919 auto subscribeInfoPtr = std::make_shared<OsAccountSubscribeInfo>(subscribeInfo);
1920 if (subscribeInfoPtr == nullptr) {
1921 ACCOUNT_LOGE("subscribeInfoPtr is nullptr");
1922 }
1923 return subscribeManager_.SubscribeOsAccount(subscribeInfoPtr, eventListener);
1924 }
1925
1926 ErrCode IInnerOsAccountManager::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
1927 {
1928 return subscribeManager_.UnsubscribeOsAccount(eventListener);
1929 }
1930
1931 const std::shared_ptr<OsAccountSubscribeInfo> IInnerOsAccountManager::GetSubscribeRecordInfo(
1932 const sptr<IRemoteObject> &eventListener)
1933 {
1934 return subscribeManager_.GetSubscribeRecordInfo(eventListener);
1935 }
1936
1937 OS_ACCOUNT_SWITCH_MOD IInnerOsAccountManager::GetOsAccountSwitchMod()
1938 {
1939 return Constants::NOW_OS_ACCOUNT_SWITCH_MOD;
1940 }
1941
1942 ErrCode IInnerOsAccountManager::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted)
1943 {
1944 OsAccountInfo osAccountInfo;
1945 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
1946 if (errCode != ERR_OK) {
1947 return errCode;
1948 }
1949 isOsAccountCompleted = osAccountInfo.GetIsCreateCompleted();
1950 return ERR_OK;
1951 }
1952
1953 ErrCode IInnerOsAccountManager::SetOsAccountIsVerified(const int id, const bool isVerified)
1954 {
1955 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id));
1956 OsAccountInfo osAccountInfo;
1957 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
1958 if (errCode != ERR_OK) {
1959 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
1960 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
1961 }
1962
1963 // to be removed, cannot change any thing
1964 if (osAccountInfo.GetToBeRemoved()) {
1965 ACCOUNT_LOGE("account %{public}d will be removed, cannot change verify state!", id);
1966 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
1967 }
1968 bool preVerified = osAccountInfo.GetIsVerified();
1969
1970 osAccountInfo.SetIsVerified(isVerified);
1971 if (isVerified) {
1972 verifiedAccounts_.EnsureInsert(id, true);
1973 } else {
1974 verifiedAccounts_.Erase(id);
1975 }
1976 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
1977 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) {
1978 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d",
1979 errCode, osAccountInfo.GetLocalId());
1980 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
1981 }
1982 if (isVerified && !preVerified) {
1983 OsAccountInterface::PublishCommonEvent(osAccountInfo,
1984 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_UNLOCKED, Constants::OPERATION_UNLOCK);
1985 subscribeManager_.Publish(id, OS_ACCOUNT_SUBSCRIBE_TYPE::UNLOCKED);
1986 ReportOsAccountLifeCycle(id, Constants::OPERATION_UNLOCK);
1987
1988 auto task = [] { IInnerOsAccountManager::GetInstance().CleanGarbageOsAccounts(); };
1989 std::thread cleanThread(task);
1990 pthread_setname_np(cleanThread.native_handle(), "CleanGarbageOsAccounts");
1991 cleanThread.detach();
1992 }
1993 return ERR_OK;
1994 }
1995
1996 ErrCode IInnerOsAccountManager::SetOsAccountIsLoggedIn(const int32_t id, const bool isLoggedIn)
1997 {
1998 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(id));
1999 OsAccountInfo osAccountInfo;
2000 ErrCode errCode = GetRealOsAccountInfoById(id, osAccountInfo);
2001 if (errCode != ERR_OK) {
2002 ACCOUNT_LOGE("get osaccount info error, errCode %{public}d.", errCode);
2003 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
2004 }
2005 if (osAccountInfo.GetToBeRemoved()) {
2006 ACCOUNT_LOGE("account %{public}d will be removed, cannot change verify state!", id);
2007 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
2008 }
2009 if (isLoggedIn) {
2010 loggedInAccounts_.EnsureInsert(id, true);
2011 } else {
2012 loggedInAccounts_.Erase(id);
2013 }
2014 if (!osAccountInfo.GetIsLoggedIn()) {
2015 #ifdef ACTIVATE_LAST_LOGGED_IN_ACCOUNT
2016 osAccountControl_->SetDefaultActivatedOsAccount(id);
2017 #endif
2018 osAccountInfo.SetIsLoggedIn(isLoggedIn);
2019 osAccountInfo.SetLastLoginTime(std::chrono::duration_cast<std::chrono::seconds>(
2020 std::chrono::system_clock::now().time_since_epoch()).count());
2021 }
2022 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
2023 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) {
2024 ACCOUNT_LOGE("Update account info failed, errCode: %{public}d, id: %{public}d", errCode, id);
2025 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
2026 }
2027 return ERR_OK;
2028 }
2029
2030 ErrCode IInnerOsAccountManager::GetOsAccountCredentialId(const int id, uint64_t &credentialId)
2031 {
2032 credentialId = 0;
2033 OsAccountInfo osAccountInfo;
2034 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
2035 if (errCode == ERR_OK) {
2036 credentialId = osAccountInfo.GetCredentialId();
2037 }
2038 return errCode;
2039 }
2040
2041 ErrCode IInnerOsAccountManager::SetOsAccountCredentialId(const int id, uint64_t credentialId)
2042 {
2043 OsAccountInfo osAccountInfo;
2044 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
2045 if (errCode != ERR_OK) {
2046 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
2047 }
2048
2049 osAccountInfo.SetCredentialId(credentialId);
2050 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
2051 if (errCode != ERR_OK) {
2052 ACCOUNT_LOGE("update osaccount info error %{public}d, id: %{public}d",
2053 errCode, osAccountInfo.GetLocalId());
2054 return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
2055 }
2056 return ERR_OK;
2057 }
2058
2059 ErrCode IInnerOsAccountManager::SetDefaultActivatedOsAccount(const int32_t id)
2060 {
2061 std::lock_guard<std::mutex> lock(operatingMutex_);
2062 if (id == defaultActivatedId_) {
2063 ACCOUNT_LOGW("no need to repeat set initial start id %{public}d", id);
2064 return ERR_OK;
2065 }
2066 OsAccountInfo osAccountInfo;
2067 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
2068 if (errCode != ERR_OK) {
2069 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
2070 }
2071
2072 errCode = IsValidOsAccount(osAccountInfo);
2073 if (errCode != ERR_OK) {
2074 return errCode;
2075 }
2076 errCode = osAccountControl_->SetDefaultActivatedOsAccount(id);
2077 if (errCode != ERR_OK) {
2078 ACCOUNT_LOGE("set default activated account id error %{public}d, id: %{public}d", errCode, id);
2079 return errCode;
2080 }
2081 defaultActivatedId_ = id;
2082 return ERR_OK;
2083 }
2084
2085 ErrCode IInnerOsAccountManager::IsOsAccountForeground(const int32_t localId, const uint64_t displayId,
2086 bool &isForeground)
2087 {
2088 int32_t id;
2089 if (!foregroundAccountMap_.Find(displayId, id)) {
2090 ACCOUNT_LOGE("No foreground account in displayId %{public}llu.", static_cast<unsigned long long>(displayId));
2091 return ERR_ACCOUNT_COMMON_ACCOUNT_IN_DISPLAY_ID_NOT_FOUND_ERROR;
2092 }
2093 isForeground = (id == localId);
2094 return ERR_OK;
2095 }
2096
2097 ErrCode IInnerOsAccountManager::GetForegroundOsAccountLocalId(const uint64_t displayId, int32_t &localId)
2098 {
2099 if (!foregroundAccountMap_.Find(displayId, localId)) {
2100 ACCOUNT_LOGE("No foreground account in displayId %{public}llu.", static_cast<unsigned long long>(displayId));
2101 return ERR_ACCOUNT_COMMON_ACCOUNT_IN_DISPLAY_ID_NOT_FOUND_ERROR;
2102 }
2103 return ERR_OK;
2104 }
2105
2106 ErrCode IInnerOsAccountManager::GetForegroundOsAccounts(std::vector<ForegroundOsAccount> &accounts)
2107 {
2108 accounts.clear();
2109 auto it = [&](uint64_t displayId, int32_t localId) {
2110 ForegroundOsAccount foregroundOsAccount;
2111 foregroundOsAccount.displayId = displayId;
2112 foregroundOsAccount.localId = localId;
2113 accounts.emplace_back(foregroundOsAccount);
2114 };
2115 foregroundAccountMap_.Iterate(it);
2116 ACCOUNT_LOGI("Get foreground list successful, total=%{public}zu.", accounts.size());
2117 return ERR_OK;
2118 }
2119
2120 ErrCode IInnerOsAccountManager::GetBackgroundOsAccountLocalIds(std::vector<int32_t> &localIds)
2121 {
2122 localIds.clear();
2123 std::vector<int32_t> activatedIds;
2124 CopyFromActiveList(activatedIds);
2125
2126 std::vector<int32_t> foregroundIds;
2127 auto it = [&](uint64_t displayId, int32_t localId) {
2128 foregroundIds.emplace_back(localId);
2129 };
2130 foregroundAccountMap_.Iterate(it);
2131 std::unordered_set<int32_t> foregroundSet(foregroundIds.begin(), foregroundIds.end());
2132 for (const auto &id : activatedIds) {
2133 if (foregroundSet.find(id) == foregroundSet.end()) {
2134 localIds.emplace_back(id);
2135 }
2136 }
2137 ACCOUNT_LOGI("Get background list successful, total=%{public}zu.", localIds.size());
2138 return ERR_OK;
2139 }
2140
2141 ErrCode IInnerOsAccountManager::GetDefaultActivatedOsAccount(int32_t &id)
2142 {
2143 std::lock_guard<std::mutex> lock(operatingMutex_);
2144 id = defaultActivatedId_;
2145 return ERR_OK;
2146 }
2147
2148 ErrCode IInnerOsAccountManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
2149 {
2150 return osAccountControl_->IsAllowedCreateAdmin(isAllowedCreateAdmin);
2151 }
2152
2153 ErrCode IInnerOsAccountManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
2154 int &createdOsAccountNum)
2155 {
2156 return osAccountControl_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
2157 }
2158
2159 ErrCode IInnerOsAccountManager::GetSerialNumberFromDatabase(const std::string& storeID,
2160 int64_t &serialNumber)
2161 {
2162 return osAccountControl_->GetSerialNumberFromDatabase(storeID, serialNumber);
2163 }
2164
2165 ErrCode IInnerOsAccountManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
2166 {
2167 #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS
2168 return osAccountControl_->GetMaxAllowCreateIdFromDatabase(storeID, id);
2169 #else
2170 id = Constants::START_USER_ID;
2171 return ERR_OK;
2172 #endif // ENABLE_MULTIPLE_OS_ACCOUNTS
2173 }
2174
2175 ErrCode IInnerOsAccountManager::GetOsAccountFromDatabase(const std::string& storeID, const int id,
2176 OsAccountInfo &osAccountInfo)
2177 {
2178 return osAccountControl_->GetOsAccountFromDatabase(storeID, id, osAccountInfo);
2179 }
2180
2181 ErrCode IInnerOsAccountManager::GetOsAccountListFromDatabase(const std::string& storeID,
2182 std::vector<OsAccountInfo> &osAccountList)
2183 {
2184 return osAccountControl_->GetOsAccountListFromDatabase(storeID, osAccountList);
2185 }
2186
2187 void IInnerOsAccountManager::RemoveLocalIdToOperating(int32_t localId)
2188 {
2189 std::lock_guard<std::mutex> lock(operatingMutex_);
2190 auto it = std::find(operatingId_.begin(), operatingId_.end(), localId);
2191 if (it != operatingId_.end()) {
2192 operatingId_.erase(it);
2193 }
2194 }
2195
2196 bool IInnerOsAccountManager::CheckAndAddLocalIdOperating(int32_t localId)
2197 {
2198 std::lock_guard<std::mutex> lock(operatingMutex_);
2199 if (std::find(operatingId_.begin(), operatingId_.end(), localId) != operatingId_.end()) {
2200 return false;
2201 }
2202 operatingId_.push_back(localId);
2203 return true;
2204 }
2205
2206 ErrCode IInnerOsAccountManager::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
2207 {
2208 CopyFromActiveList(ids);
2209 return ERR_OK;
2210 }
2211
2212 void IInnerOsAccountManager::PushIdIntoActiveList(int32_t id)
2213 {
2214 std::lock_guard<std::mutex> lock(ativeMutex_);
2215 if (std::find(activeAccountId_.begin(), activeAccountId_.end(), id) == activeAccountId_.end()) {
2216 CountTraceAdapter("activeId", (int64_t)id);
2217 }
2218 #ifndef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
2219 activeAccountId_.clear();
2220 #else
2221 activeAccountId_.erase(std::remove(activeAccountId_.begin(), activeAccountId_.end(), id), activeAccountId_.end());
2222 #endif
2223 //Compatible with the QueryActiveOsAccountIds
2224 activeAccountId_.insert(activeAccountId_.begin(), id);
2225 return;
2226 }
2227
2228 void IInnerOsAccountManager::EraseIdFromActiveList(int32_t id)
2229 {
2230 std::lock_guard<std::mutex> lock(ativeMutex_);
2231 if (std::find(activeAccountId_.begin(), activeAccountId_.end(), id) != activeAccountId_.end()) {
2232 ACCOUNT_LOGE("EraseIdFromActiveList enter0");
2233 activeAccountId_.erase(
2234 std::remove(activeAccountId_.begin(), activeAccountId_.end(), id), activeAccountId_.end());
2235 } else {
2236 ACCOUNT_LOGI("os account is not in active list, no need to erase!");
2237 }
2238 CountTraceAdapter("deActiveId", (int64_t)id);
2239 }
2240
2241 bool IInnerOsAccountManager::IsOsAccountIDInActiveList(int32_t id)
2242 {
2243 std::lock_guard<std::mutex> lock(ativeMutex_);
2244 auto it = std::find(activeAccountId_.begin(), activeAccountId_.end(), id);
2245 return (it != activeAccountId_.end());
2246 }
2247
2248 void IInnerOsAccountManager::CopyFromActiveList(std::vector<int32_t>& idList)
2249 {
2250 idList.clear();
2251 std::lock_guard<std::mutex> lock(ativeMutex_);
2252 for (auto it = activeAccountId_.begin(); it != activeAccountId_.end(); it++) {
2253 idList.push_back(*it);
2254 }
2255 }
2256
2257 ErrCode IInnerOsAccountManager::UpdateAccountInfoByDomainAccountInfo(
2258 int32_t userId, const DomainAccountInfo &newDomainAccountInfo)
2259 {
2260 if (!CheckAndAddLocalIdOperating(userId)) {
2261 ACCOUNT_LOGW("Account id = %{public}d already in operating", userId);
2262 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
2263 }
2264 OsAccountInfo accountInfo;
2265 ErrCode result = osAccountControl_->GetOsAccountInfoById(userId, accountInfo);
2266 if (result != ERR_OK) {
2267 RemoveLocalIdToOperating(userId);
2268 return result;
2269 }
2270 DomainAccountInfo oldDomainAccountInfo;
2271 accountInfo.GetDomainInfo(oldDomainAccountInfo);
2272 if (!newDomainAccountInfo.accountName_.empty()) {
2273 oldDomainAccountInfo.accountName_ = newDomainAccountInfo.accountName_;
2274 }
2275 if (!newDomainAccountInfo.accountId_.empty()) {
2276 oldDomainAccountInfo.accountId_ = newDomainAccountInfo.accountId_;
2277 }
2278 accountInfo.SetDomainInfo(oldDomainAccountInfo);
2279 accountInfo.SetLocalName(newDomainAccountInfo.domain_ + "/" + newDomainAccountInfo.accountName_);
2280 result = osAccountControl_->UpdateOsAccount(accountInfo);
2281 if (result != ERR_OK) {
2282 ACCOUNT_LOGE("Update account info failed, result = %{public}d", result);
2283 ReportOsAccountOperationFail(userId, Constants::OPERATION_UPDATE, result,
2284 "Failed to update domain account info");
2285 RemoveLocalIdToOperating(userId);
2286 return result;
2287 }
2288 RemoveLocalIdToOperating(userId);
2289 #ifdef HAS_CES_PART
2290 AccountEventProvider::EventPublish(EventFwk::CommonEventSupport::COMMON_EVENT_USER_INFO_UPDATED,
2291 userId, nullptr);
2292 #endif // HAS_CES_PART
2293 return ERR_OK;
2294 }
2295
2296 ErrCode IInnerOsAccountManager::UpdateAccountToForeground(const uint64_t displayId, OsAccountInfo &osAccountInfo)
2297 {
2298 int32_t localId = osAccountInfo.GetLocalId();
2299 osAccountInfo.SetIsActived(true);
2300 osAccountInfo.SetDisplayId(displayId);
2301 osAccountInfo.SetIsForeground(true);
2302 if (osAccountInfo.GetIsLoggedIn()) {
2303 #ifdef ACTIVATE_LAST_LOGGED_IN_ACCOUNT
2304 osAccountControl_->SetDefaultActivatedOsAccount(localId);
2305 #endif
2306 }
2307 ErrCode errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
2308 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) {
2309 ACCOUNT_LOGE("Update account failed, localId=%{public}d, errCode=%{public}d.",
2310 localId, errCode);
2311 }
2312 return ERR_OK;
2313 }
2314
2315 ErrCode IInnerOsAccountManager::UpdateAccountToBackground(int32_t oldId)
2316 {
2317 OsAccountInfo oldOsAccountInfo;
2318 {
2319 std::lock_guard<std::mutex> lock(*GetOrInsertUpdateLock(oldId));
2320 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(oldId, oldOsAccountInfo);
2321 if (errCode != ERR_OK) {
2322 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
2323 }
2324 oldOsAccountInfo.SetIsForeground(false);
2325 oldOsAccountInfo.SetDisplayId(Constants::INVALID_DISPALY_ID);
2326 errCode = osAccountControl_->UpdateOsAccount(oldOsAccountInfo);
2327 if (errCode != ERR_OK && errCode != ERR_ACCOUNT_COMMON_DATA_NO_SPACE) {
2328 ACCOUNT_LOGE("Update osaccount failed, errCode=%{public}d, id=%{public}d",
2329 errCode, oldOsAccountInfo.GetLocalId());
2330 }
2331 }
2332 OsAccountInterface::PublishCommonEvent(oldOsAccountInfo,
2333 OHOS::EventFwk::CommonEventSupport::COMMON_EVENT_USER_BACKGROUND, Constants::OPERATION_SWITCH);
2334
2335 #ifdef ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
2336 #ifndef SUPPORT_STOP_MAIN_OS_ACCOUNT
2337 if (oldId == Constants::START_USER_ID) {
2338 return ERR_OK;
2339 }
2340 #endif
2341 bool isLoggedIn = false;
2342 if ((oldOsAccountInfo.GetType() != OsAccountType::PRIVATE) && (!loggedInAccounts_.Find(oldId, isLoggedIn))) {
2343 DeactivateOsAccount(oldId, false);
2344 }
2345 #else
2346 DeactivateOsAccountByInfo(oldOsAccountInfo);
2347 #endif // ENABLE_MULTIPLE_ACTIVE_ACCOUNTS
2348 return ERR_OK;
2349 }
2350
2351 std::shared_ptr<std::mutex> IInnerOsAccountManager::GetOrInsertUpdateLock(int32_t id)
2352 {
2353 std::lock_guard<std::mutex> lock(updateLockMutex_);
2354 auto it = updateLocks_.find(id);
2355 if (it == updateLocks_.end()) {
2356 auto mutexPtr = std::make_shared<std::mutex>();
2357 updateLocks_.insert(std::make_pair(id, mutexPtr));
2358 return mutexPtr;
2359 } else {
2360 return it->second;
2361 }
2362 }
2363
2364 ErrCode IInnerOsAccountManager::SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved)
2365 {
2366 if (!CheckAndAddLocalIdOperating(localId)) {
2367 ACCOUNT_LOGE("The account %{public}d already in operating", localId);
2368 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
2369 }
2370 OsAccountInfo osAccountInfo;
2371 ErrCode errCode = osAccountControl_->GetOsAccountInfoById(localId, osAccountInfo);
2372 if (errCode != ERR_OK) {
2373 RemoveLocalIdToOperating(localId);
2374 return ERR_ACCOUNT_COMMON_ACCOUNT_NOT_EXIST_ERROR;
2375 }
2376 osAccountInfo.SetToBeRemoved(toBeRemoved);
2377 errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
2378 RemoveLocalIdToOperating(localId);
2379 return errCode;
2380 }
2381
2382 ErrCode IInnerOsAccountManager::IsValidOsAccount(const OsAccountInfo &osAccountInfo)
2383 {
2384 if (!osAccountInfo.GetIsCreateCompleted()) {
2385 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_IS_UNCOMPLETED_ERROR;
2386 }
2387
2388 if (osAccountInfo.GetToBeRemoved()) {
2389 return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
2390 }
2391 return ERR_OK;
2392 }
2393
2394 ErrCode IInnerOsAccountManager::GetOsAccountDomainInfo(const int32_t localId, DomainAccountInfo &domainInfo)
2395 {
2396 return InnerDomainAccountManager::GetInstance().GetDomainAccountInfoByUserId(localId, domainInfo);
2397 }
2398 } // namespace AccountSA
2399 } // namespace OHOS