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