• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "iinner_os_account_manager.h"
16 #include "account_log_wrapper.h"
17 #include "ohos_account_kits.h"
18 #include "os_account_constants.h"
19 #include "os_account_control_file_manager.h"
20 #include "os_account_subscribe_manager.h"
21 
22 namespace OHOS {
23 namespace AccountSA {
IInnerOsAccountManager()24 IInnerOsAccountManager::IInnerOsAccountManager() : subscribeManagerPtr_(OsAccountSubscribeManager::GetInstance())
25 {
26     counterForStandard_ = 0;
27     counterForStandardCreate_ = 0;
28     isSendToStorageCreate_ = false;
29     isSendToStorageStart_ = false;
30     activeAccountId_.clear();
31     operatingId_.clear();
32     osAccountControl_ = std::make_shared<OsAccountControlFileManager>();
33     osAccountControl_->Init();
34     ACCOUNT_LOGI("OsAccountAccountMgr Init end");
35 }
36 
CreateBaseAdminAccount()37 void IInnerOsAccountManager::CreateBaseAdminAccount()
38 {
39     bool isExistsAccount = false;
40     osAccountControl_->IsOsAccountExists(Constants::ADMIN_LOCAL_ID, isExistsAccount);
41     if (!isExistsAccount) {
42         int64_t serialNumber =
43             Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID;
44         OsAccountInfo osAccountInfo(
45             Constants::ADMIN_LOCAL_ID, Constants::ADMIN_LOCAL_NAME, OsAccountType::ADMIN, serialNumber);
46         int64_t time =
47             std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
48                 .count();
49         osAccountInfo.SetCreateTime(time);
50         osAccountInfo.SetIsCreateCompleted(true);
51         osAccountInfo.SetIsActived(true);  // admin local account is always active
52         osAccountControl_->InsertOsAccount(osAccountInfo);
53         ACCOUNT_LOGI("OsAccountAccountMgr created admin account end");
54     }
55 }
56 
CreateBaseStandardAccount()57 void IInnerOsAccountManager::CreateBaseStandardAccount()
58 {
59     bool isExistsAccount = false;
60     osAccountControl_->IsOsAccountExists(Constants::START_USER_ID, isExistsAccount);
61     if (!isExistsAccount) {
62         int64_t serialNumber = 0;
63         osAccountControl_->GetSerialNumber(serialNumber);
64         OsAccountInfo osAccountInfo(
65             Constants::START_USER_ID, Constants::STANDARD_LOCAL_NAME, OsAccountType::ADMIN, serialNumber);
66         std::vector<std::string> constants;
67         ErrCode errCode = osAccountControl_->GetConstraintsByType(OsAccountType::ADMIN, constants);
68         if (errCode != ERR_OK) {
69             ACCOUNT_LOGE("find first standard type err");
70             return;
71         }
72         osAccountInfo.SetConstraints(constants);
73         int64_t time =
74             std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch())
75                 .count();
76         osAccountInfo.SetCreateTime(time);
77         osAccountInfo.SetIsCreateCompleted(false);
78         osAccountControl_->InsertOsAccount(osAccountInfo);
79         ACCOUNT_LOGI("OsAccountAccountMgr created base account end");
80     }
81 }
82 
StartAccount()83 void IInnerOsAccountManager::StartAccount()
84 {
85     OsAccountInfo osAccountInfo;
86     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
87     if (errCode != ERR_OK) {
88         ACCOUNT_LOGE("OsAccountAccountMgr init start base account failed. cannot find account");
89         return;
90     }
91     ResetActiveStatus();
92     GetEventHandler();
93     if (!osAccountInfo.GetIsCreateCompleted()) {
94         ACCOUNT_LOGI("OsAccountAccountMgr send to storage and bm for start");
95         OHOS::AppExecFwk::InnerEvent::Callback callbackStartStandard =
96             std::bind(&IInnerOsAccountManager::CreateBaseStandardAccountSendToOther, this);
97         handler_->PostTask(callbackStartStandard, DELAY_FOR_FOUNDATION_SERVICE);
98     }
99     ACCOUNT_LOGI("OsAccountAccountMgr send to storage and am for start");
100     OHOS::AppExecFwk::InnerEvent::Callback callbackStartStandard =
101         std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this);
102     handler_->PostTask(callbackStartStandard, DELAY_FOR_FOUNDATION_SERVICE);
103 }
104 
CreateBaseStandardAccountSendToOther(void)105 void IInnerOsAccountManager::CreateBaseStandardAccountSendToOther(void)
106 {
107     OsAccountInfo osAccountInfo;
108     if (!isSendToStorageCreate_) {
109         osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
110         ErrCode errCode = OsAccountStandardInterface::SendToStorageAccountCreate(osAccountInfo);
111         if (errCode != ERR_OK) {
112             if (++counterForStandardCreate_ == MAX_TRY_TIMES) {
113                 ACCOUNT_LOGE("failed connect storage to create account");
114             } else {
115                 GetEventHandler();
116                 OHOS::AppExecFwk::InnerEvent::Callback callback =
117                     std::bind(&IInnerOsAccountManager::CreateBaseStandardAccountSendToOther, this);
118                 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
119             }
120             return;
121         } else {
122             ACCOUNT_LOGI("connect storage to create account ok");
123             counterForStandardCreate_ = 0;
124             isSendToStorageCreate_ = true;
125         }
126     }
127     osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
128     ErrCode errCodeForBM = OsAccountStandardInterface::SendToBMSAccountCreate(osAccountInfo);
129     if (errCodeForBM != ERR_OK) {
130         if (++counterForStandardCreate_ == MAX_TRY_TIMES) {
131             ACCOUNT_LOGE("failed connect BM to create account");
132         } else {
133             GetEventHandler();
134             OHOS::AppExecFwk::InnerEvent::Callback callback =
135                 std::bind(&IInnerOsAccountManager::CreateBaseStandardAccountSendToOther, this);
136             handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
137         }
138         return;
139     } else {
140         osAccountInfo.SetIsCreateCompleted(true);
141         osAccountControl_->UpdateOsAccount(osAccountInfo);
142         ACCOUNT_LOGI("connect BM to create account ok");
143     }
144 }
145 
ResetActiveStatus(void)146 void IInnerOsAccountManager::ResetActiveStatus(void)
147 {
148     std::vector<OsAccountInfo> osAccountInfos;
149     if (QueryAllCreatedOsAccounts(osAccountInfos) != ERR_OK) {
150         return;
151     }
152     for (size_t i = 0; i < osAccountInfos.size(); ++i) {
153         if (osAccountInfos[i].GetLocalId() == Constants::START_USER_ID) {
154             continue;
155         }
156         osAccountInfos[i].SetIsActived(false);
157         osAccountControl_->UpdateOsAccount(osAccountInfos[i]);
158     }
159 }
160 
StartBaseStandardAccount(void)161 void IInnerOsAccountManager::StartBaseStandardAccount(void)
162 {
163     OsAccountInfo osAccountInfo;
164     osAccountControl_->GetOsAccountInfoById(Constants::START_USER_ID, osAccountInfo);
165     if (!osAccountInfo.GetIsCreateCompleted()) {
166         ++counterForStandard_;
167         GetEventHandler();
168         OHOS::AppExecFwk::InnerEvent::Callback callback =
169             std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this);
170         handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
171         return;
172     }
173     if (!isSendToStorageStart_) {
174         ErrCode errCode = OsAccountStandardInterface::SendToStorageAccountStart(osAccountInfo);
175         if (errCode != ERR_OK) {
176             if (++counterForStandard_ == MAX_TRY_TIMES) {
177                 ACCOUNT_LOGE("failed connect storage to start account");
178             } else {
179                 GetEventHandler();
180                 OHOS::AppExecFwk::InnerEvent::Callback callback =
181                     std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this);
182                 handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
183             }
184             return;
185         }
186         ACCOUNT_LOGI("connect storage to start account ok");
187         counterForStandard_ = 0;
188         isSendToStorageStart_ = true;
189     }
190     ErrCode errCodeForAM = OsAccountStandardInterface::SendToAMSAccountStart(osAccountInfo);
191     if (errCodeForAM != ERR_OK) {
192         if (++counterForStandard_ == MAX_TRY_TIMES) {
193             ACCOUNT_LOGE("failed connect AM to start account");
194         } else {
195             GetEventHandler();
196             OHOS::AppExecFwk::InnerEvent::Callback callback =
197                 std::bind(&IInnerOsAccountManager::StartBaseStandardAccount, this);
198             handler_->PostTask(callback, DELAY_FOR_TIME_INTERVAL);
199         }
200         return;
201     }
202     osAccountInfo.SetIsActived(true);
203     int64_t time = std::chrono::duration_cast<std::chrono::seconds>(
204         std::chrono::system_clock::now().time_since_epoch()).count();
205     osAccountInfo.SetLastLoginTime(time);
206     osAccountControl_->UpdateOsAccount(osAccountInfo);
207     PushIDIntoActiveList(Constants::START_USER_ID);
208     subscribeManagerPtr_->PublishActivatedOsAccount(Constants::START_USER_ID);
209     OsAccountStandardInterface::SendToCESAccountSwitched(osAccountInfo);
210     ACCOUNT_LOGI("connect AM to start account ok");
211 }
212 
PrepareOsAccountInfo(const std::string & name,const OsAccountType & type,const DomainAccountInfo & domainInfo,OsAccountInfo & osAccountInfo)213 ErrCode IInnerOsAccountManager::PrepareOsAccountInfo(const std::string &name, const OsAccountType &type,
214     const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo)
215 {
216     int64_t serialNumber;
217     ErrCode errCode = osAccountControl_->GetSerialNumber(serialNumber);
218     if (errCode != ERR_OK) {
219         ACCOUNT_LOGE("failed to GetSerialNumber");
220         return ERR_OSACCOUNT_SERVICE_INNER_GET_SERIAL_NUMBER_ERROR;
221     }
222     int id = 0;
223     errCode = osAccountControl_->GetAllowCreateId(id);
224     if (errCode != ERR_OK) {
225         ACCOUNT_LOGE("failed to GetAllowCreateId");
226         return ERR_OSACCOUNT_SERVICE_INNER_GET_OSACCOUNT_ID_ERROR;
227     }
228     std::vector<std::string> constraints;
229     constraints.clear();
230     errCode = osAccountControl_->GetConstraintsByType(type, constraints);
231     if (errCode != ERR_OK) {
232         ACCOUNT_LOGE("failed to GetConstraintsByType");
233         return ERR_OSACCOUNT_SERVICE_INNER_GET_TTPE_CONSTRAINTS_ERROR;
234     }
235     osAccountInfo = OsAccountInfo(id, name, type, serialNumber);
236     osAccountInfo.SetConstraints(constraints);
237     int64_t time =
238         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
239     osAccountInfo.SetCreateTime(time);
240     if (!osAccountInfo.SetDomainInfo(domainInfo)) {
241         ACCOUNT_LOGE("failed to SetDomainInfo");
242         return ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR;
243     }
244 
245     errCode = osAccountControl_->InsertOsAccount(osAccountInfo);
246     if (errCode != ERR_OK) {
247         ACCOUNT_LOGE("insert osaccountinfo err");
248         return ERR_OSACCOUNT_SERVICE_INNER_CREATE_ACCOUNT_ERROR;
249     }
250     return ERR_OK;
251 }
252 
SendMsgForAccountCreate(OsAccountInfo & osAccountInfo)253 ErrCode IInnerOsAccountManager::SendMsgForAccountCreate(OsAccountInfo &osAccountInfo)
254 {
255     ErrCode errCode = OsAccountStandardInterface::SendToStorageAccountCreate(osAccountInfo);
256     if (errCode != ERR_OK) {
257         ACCOUNT_LOGE("create os account SendToStorageAccountCreate failed");
258         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_CREATE_ERROR;
259     }
260     errCode = OsAccountStandardInterface::SendToBMSAccountCreate(osAccountInfo);
261     if (errCode != ERR_OK) {
262         ACCOUNT_LOGE("create os account SendToBMSAccountCreate failed");
263         return ERR_OSACCOUNT_SERVICE_INNER_SEND_BM_ACCOUNT_CREATE_ERROR;
264     }
265 
266     osAccountInfo.SetIsCreateCompleted(true);
267     errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
268     if (errCode != ERR_OK) {
269         ACCOUNT_LOGE("create os account when update isCreateComplated");
270         return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
271     }
272 
273     OsAccountStandardInterface::SendToCESAccountCreate(osAccountInfo);
274     ACCOUNT_LOGI("send other subsystem to create os account ok");
275     return ERR_OK;
276 }
277 
CreateOsAccount(const std::string & name,const OsAccountType & type,OsAccountInfo & osAccountInfo)278 ErrCode IInnerOsAccountManager::CreateOsAccount(
279     const std::string &name, const OsAccountType &type, OsAccountInfo &osAccountInfo)
280 {
281     DomainAccountInfo domainInfo;  // default empty domain info
282     ErrCode errCode = PrepareOsAccountInfo(name, type, domainInfo, osAccountInfo);
283     if (errCode != ERR_OK) {
284         return errCode;
285     }
286     return SendMsgForAccountCreate(osAccountInfo);
287 }
288 
CreateOsAccountForDomain(const OsAccountType & type,const DomainAccountInfo & domainInfo,OsAccountInfo & osAccountInfo)289 ErrCode IInnerOsAccountManager::CreateOsAccountForDomain(
290     const OsAccountType &type, const DomainAccountInfo &domainInfo, OsAccountInfo &osAccountInfo)
291 {
292     // check whether if the target domain has already been bound to an os account or not
293     std::vector<OsAccountInfo> osAccountInfos;
294     ErrCode errCode = QueryAllCreatedOsAccounts(osAccountInfos);
295     if (errCode != ERR_OK) {
296         ACCOUNT_LOGE("cannot get current os account list, err %{public}d.", errCode);
297         return errCode;
298     }
299     for (size_t i = 0; i < osAccountInfos.size(); ++i) {
300         DomainAccountInfo curDomainInfo;
301         osAccountInfos[i].GetDomainInfo(curDomainInfo);
302         if (curDomainInfo.accountName_ == domainInfo.accountName_ &&
303             curDomainInfo.domain_ == domainInfo.domain_) {
304             return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ALREADY_BIND_ERROR;
305         }
306     }
307 
308     std::string osAccountName = domainInfo.domain_ + "/" + domainInfo.accountName_;
309     errCode = PrepareOsAccountInfo(osAccountName, type, domainInfo, osAccountInfo);
310     if (errCode != ERR_OK) {
311         return errCode;
312     }
313     return SendMsgForAccountCreate(osAccountInfo);
314 }
315 
RemoveOsAccount(const int id)316 ErrCode IInnerOsAccountManager::RemoveOsAccount(const int id)
317 {
318     ACCOUNT_LOGI("RemoveOsAccount delete id is %{public}d", id);
319     if (IsLocalIdInOperating(id)) {
320         ACCOUNT_LOGE("the %{public}d already in operating", id);
321         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
322     }
323     AddLocalIdToOperating(id);
324     if (IsOsAccountIDInActiveList(id)) {
325         ACCOUNT_LOGI("RemoveOsAccount started account to inactive, account id : %{public}d.", id);
326         ErrCode activeErrCode = ActivateOsAccount(Constants::START_USER_ID);
327         if (activeErrCode != ERR_OK) {
328             RemoveLocalIdToOperating(id);
329             ACCOUNT_LOGE("RemoveOsAccount active base account failed");
330             return ERR_OSACCOUNT_SERVICE_INNER_REMOVE_ACCOUNT_ACTIVED_ERROR;
331         }
332     }
333     OsAccountInfo osAccountInfo;
334     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
335     if (errCode != ERR_OK) {
336         RemoveLocalIdToOperating(id);
337         ACCOUNT_LOGE("RemoveOsAccount cannot find os account info");
338         return ERR_OSACCOUNT_SERVICE_INNER_CANNOT_FIND_OSACCOUNT_ERROR;
339     }
340 
341     // set remove flag first
342     osAccountInfo.SetToBeRemoved(true);
343     osAccountControl_->UpdateOsAccount(osAccountInfo);
344 
345     // stop account first
346     errCode = SendMsgForAccountStop(osAccountInfo);
347     if (errCode != ERR_OK) {
348         RemoveLocalIdToOperating(id);
349         return errCode;
350     }
351 
352     // then remove account
353     errCode = SendMsgForAccountRemove(osAccountInfo);
354     if (errCode != ERR_OK) {
355         RemoveLocalIdToOperating(id);
356         return errCode;
357     }
358     RemoveLocalIdToOperating(id);
359     ACCOUNT_LOGI("IInnerOsAccountManager RemoveOsAccount end");
360     return ERR_OK;
361 }
362 
SendMsgForAccountStop(OsAccountInfo & osAccountInfo)363 ErrCode IInnerOsAccountManager::SendMsgForAccountStop(OsAccountInfo &osAccountInfo)
364 {
365     ErrCode errCode = OsAccountStandardInterface::SendToAMSAccountStop(osAccountInfo);
366     if (errCode != ERR_OK) {
367         ACCOUNT_LOGE("SendToAMSAccountStop failed, id %{public}d, errCode %{public}d",
368             osAccountInfo.GetLocalId(), errCode);
369         return ERR_OSACCOUNT_SERVICE_INNER_SEND_AM_ACCOUNT_STOP_ERROR;
370     }
371     errCode = OsAccountStandardInterface::SendToStorageAccountStop(osAccountInfo);
372     if (errCode != ERR_OK) {
373         ACCOUNT_LOGE("SendToStorageAccountStop failed, id %{public}d, errCode %{public}d",
374             osAccountInfo.GetLocalId(), errCode);
375         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_STOP_ERROR;
376     }
377     return errCode;
378 }
379 
SendMsgForAccountRemove(OsAccountInfo & osAccountInfo)380 ErrCode IInnerOsAccountManager::SendMsgForAccountRemove(OsAccountInfo &osAccountInfo)
381 {
382     ErrCode errCode = OsAccountStandardInterface::SendToBMSAccountDelete(osAccountInfo);
383     if (errCode != ERR_OK) {
384         ACCOUNT_LOGE("SendToBMSAccountDelete failed, id %{public}d, errCode %{public}d",
385             osAccountInfo.GetLocalId(), errCode);
386         return ERR_OSACCOUNT_SERVICE_INNER_SEND_BM_ACCOUNT_DELE_ERROR;
387     }
388     errCode = OsAccountStandardInterface::SendToStorageAccountRemove(osAccountInfo);
389     if (errCode != ERR_OK) {
390         ACCOUNT_LOGE("SendToStorageAccountRemove failed, id %{public}d, errCode %{public}d",
391             osAccountInfo.GetLocalId(), errCode);
392         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_REMOVE_ERROR;
393     }
394     errCode = OsAccountStandardInterface::SendToIAMAccountDelete(osAccountInfo);
395     if (errCode != ERR_OK) {
396         ACCOUNT_LOGE("SendToIAMAccountDelete failed, id %{public}d, errCode %{public}d",
397             osAccountInfo.GetLocalId(), errCode);
398         return ERR_OSACCOUNT_SERVICE_INNER_SEND_IAM_ACCOUNT_DELE_ERROR;
399     }
400     errCode = osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId());
401     if (errCode != ERR_OK) {
402         ACCOUNT_LOGE("remove osaccount info failed, id: %{public}d, errCode %{public}d",
403             osAccountInfo.GetLocalId(), errCode);
404         return ERR_OSACCOUNT_SERVICE_INNER_CANNOT_DELE_OSACCOUNT_ERROR;
405     }
406     OsAccountStandardInterface::SendToCESAccountDelete(osAccountInfo);
407     return errCode;
408 }
409 
Init()410 void IInnerOsAccountManager::Init()
411 {
412     CreateBaseAdminAccount();
413     CreateBaseStandardAccount();
414     StartAccount();
415     CleanGarbageAccounts();
416 }
417 
IsOsAccountExists(const int id,bool & isOsAccountExits)418 ErrCode IInnerOsAccountManager::IsOsAccountExists(const int id, bool &isOsAccountExits)
419 {
420     isOsAccountExits = false;
421     osAccountControl_->IsOsAccountExists(id, isOsAccountExits);
422     return ERR_OK;
423 }
424 
IsOsAccountActived(const int id,bool & isOsAccountActived)425 ErrCode IInnerOsAccountManager::IsOsAccountActived(const int id, bool &isOsAccountActived)
426 {
427     isOsAccountActived = false;
428 
429     // check if os account exists
430     OsAccountInfo osAccountInfo;
431     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
432     if (errCode != ERR_OK) {
433         ACCOUNT_LOGE("get osaccount info error");
434         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
435     }
436     if (id == Constants::ADMIN_LOCAL_ID) {
437         isOsAccountActived = true;
438         return ERR_OK;
439     }
440     isOsAccountActived = IsOsAccountIDInActiveList(id);
441     return ERR_OK;
442 }
443 
IsOsAccountConstraintEnable(const int id,const std::string & constraint,bool & isOsAccountConstraintEnable)444 ErrCode IInnerOsAccountManager::IsOsAccountConstraintEnable(
445     const int id, const std::string &constraint, bool &isOsAccountConstraintEnable)
446 {
447     OsAccountInfo osAccountInfo;
448     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
449     if (errCode != ERR_OK) {
450         ACCOUNT_LOGE("get osaccount info error");
451         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
452     }
453     std::vector<std::string> constraints = osAccountInfo.GetConstraints();
454     if (std::find(constraints.begin(), constraints.end(), constraint) != constraints.end()) {
455         isOsAccountConstraintEnable = true;
456     } else {
457         isOsAccountConstraintEnable = false;
458     }
459     return ERR_OK;
460 }
461 
IsOsAccountVerified(const int id,bool & isVerified)462 ErrCode IInnerOsAccountManager::IsOsAccountVerified(const int id, bool &isVerified)
463 {
464     OsAccountInfo osAccountInfo;
465     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
466     if (errCode != ERR_OK) {
467         ACCOUNT_LOGE("get osaccount info error");
468         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
469     }
470     isVerified = osAccountInfo.GetIsVerified();
471     return ERR_OK;
472 }
473 
GetCreatedOsAccountsCount(unsigned int & createdOsAccountCount)474 ErrCode IInnerOsAccountManager::GetCreatedOsAccountsCount(unsigned int &createdOsAccountCount)
475 {
476     std::vector<OsAccountInfo> osAccountInfos;
477     ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
478     if (errCode != ERR_OK) {
479         ACCOUNT_LOGE("get osaccount info list error");
480         return errCode;
481     }
482     createdOsAccountCount = osAccountInfos.size();
483     return ERR_OK;
484 }
485 
QueryMaxOsAccountNumber(int & maxOsAccountNumber)486 ErrCode IInnerOsAccountManager::QueryMaxOsAccountNumber(int &maxOsAccountNumber)
487 {
488     ErrCode errCode = osAccountControl_->GetMaxCreatedOsAccountNum(maxOsAccountNumber);
489     if (errCode != ERR_OK) {
490         ACCOUNT_LOGE("get max created osaccount num error");
491         return errCode;
492     }
493     return ERR_OK;
494 }
495 
GetOsAccountAllConstraints(const int id,std::vector<std::string> & constraints)496 ErrCode IInnerOsAccountManager::GetOsAccountAllConstraints(const int id, std::vector<std::string> &constraints)
497 {
498     OsAccountInfo osAccountInfo;
499     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
500     if (errCode != ERR_OK) {
501         ACCOUNT_LOGE("get osaccount info error");
502         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
503     }
504     constraints = osAccountInfo.GetConstraints();
505     return ERR_OK;
506 }
507 
QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> & osAccountInfos)508 ErrCode IInnerOsAccountManager::QueryAllCreatedOsAccounts(std::vector<OsAccountInfo> &osAccountInfos)
509 {
510     ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
511     if (errCode != ERR_OK) {
512         ACCOUNT_LOGE("get osaccount info list error");
513         return ERR_OSACCOUNT_SERVICE_INNER_GET_ALL_OSACCOUNTINFO_ERROR;
514     }
515     for (auto osAccountInfosPtr = osAccountInfos.begin(); osAccountInfosPtr != osAccountInfos.end();
516          ++osAccountInfosPtr) {
517         if (IsOsAccountIDInActiveList(osAccountInfosPtr->GetLocalId())) {
518             osAccountInfosPtr->SetIsActived(true);
519         } else {
520             osAccountInfosPtr->SetIsActived(false);
521         }
522     }
523     return ERR_OK;
524 }
525 
CleanGarbageAccounts()526 void IInnerOsAccountManager::CleanGarbageAccounts()
527 {
528     ACCOUNT_LOGI("enter.");
529     std::vector<OsAccountInfo> osAccountInfos;
530     if (QueryAllCreatedOsAccounts(osAccountInfos) != ERR_OK) {
531         ACCOUNT_LOGI("QueryAllCreatedOsAccounts failed.");
532         return;
533     }
534 
535     // check status and remove garbage accounts data
536     for (size_t i = 0; i < osAccountInfos.size(); ++i) {
537         if (!osAccountInfos[i].GetToBeRemoved()) {
538             continue;
539         }
540 
541         if (osAccountInfos[i].GetLocalId() == Constants::START_USER_ID ||
542             osAccountInfos[i].GetLocalId() == Constants::ADMIN_LOCAL_ID) {
543             continue;
544         }
545 
546         ErrCode errCode = SendMsgForAccountRemove(osAccountInfos[i]);
547         if (errCode != ERR_OK) {
548             ACCOUNT_LOGE("remove account %{public}d failed! errCode %{public}d.",
549                 osAccountInfos[i].GetLocalId(), errCode);
550         } else {
551             ACCOUNT_LOGI("remove account %{public}d succeed!", osAccountInfos[i].GetLocalId());
552         }
553     }
554     ACCOUNT_LOGI("finished.");
555 }
556 
GetOsAccountLocalIdFromDomain(const DomainAccountInfo & domainInfo,int & id)557 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdFromDomain(const DomainAccountInfo &domainInfo, int &id)
558 {
559     if (domainInfo.domain_.empty() ||
560         domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) {
561         ACCOUNT_LOGE("invalid domain name length %{public}zu.", domainInfo.domain_.size());
562         return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_NAME_LEN_ERROR;
563     }
564 
565     if (domainInfo.accountName_.empty() ||
566         domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) {
567         ACCOUNT_LOGE("invalid domain account name length %{public}zu.", domainInfo.accountName_.size());
568         return ERR_OSACCOUNT_SERVICE_INNER_DOMAIN_ACCOUNT_NAME_LEN_ERROR;
569     }
570 
571     id = -1;
572     std::vector<OsAccountInfo> osAccountInfos;
573     ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
574     if (errCode != ERR_OK) {
575         return ERR_OSACCOUNT_SERVICE_INNER_GET_ALL_OSACCOUNTINFO_ERROR;
576     }
577 
578     DomainAccountInfo curDomainInfo;
579     for (auto osAccountInfosPtr = osAccountInfos.begin(); osAccountInfosPtr != osAccountInfos.end();
580          ++osAccountInfosPtr) {
581         osAccountInfosPtr->GetDomainInfo(curDomainInfo);
582         if (curDomainInfo.accountName_ == domainInfo.accountName_ &&
583             curDomainInfo.domain_ == domainInfo.domain_) {
584             id = osAccountInfosPtr->GetLocalId();
585             return ERR_OK;
586         }
587     }
588     return ERR_OSACCOUNT_KIT_GET_OS_ACCOUNT_LOCAL_ID_FOR_DOMAIN_ERROR;
589 }
590 
QueryOsAccountById(const int id,OsAccountInfo & osAccountInfo)591 ErrCode IInnerOsAccountManager::QueryOsAccountById(const int id, OsAccountInfo &osAccountInfo)
592 {
593     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
594     if (errCode != ERR_OK) {
595         ACCOUNT_LOGE("get osaccount info error");
596         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
597     }
598 
599     if (IsOsAccountIDInActiveList(id)) {
600         osAccountInfo.SetIsActived(true);
601     } else {
602         osAccountInfo.SetIsActived(false);
603     }
604 
605     if (osAccountInfo.GetPhoto() != "") {
606         std::string photo = osAccountInfo.GetPhoto();
607         errCode = osAccountControl_->GetPhotoById(osAccountInfo.GetLocalId(), photo);
608         if (errCode != ERR_OK) {
609             ACCOUNT_LOGE("get osaccount photo error");
610             return errCode;
611         }
612         osAccountInfo.SetPhoto(photo);
613     }
614     return ERR_OK;
615 }
616 
GetOsAccountType(const int id,OsAccountType & type)617 ErrCode IInnerOsAccountManager::GetOsAccountType(const int id, OsAccountType &type)
618 {
619     OsAccountInfo osAccountInfo;
620     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
621     if (errCode != ERR_OK) {
622         ACCOUNT_LOGE("get osaccount info error");
623         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
624     }
625     type = osAccountInfo.GetType();
626     return ERR_OK;
627 }
628 
GetOsAccountProfilePhoto(const int id,std::string & photo)629 ErrCode IInnerOsAccountManager::GetOsAccountProfilePhoto(const int id, std::string &photo)
630 {
631     OsAccountInfo osAccountInfo;
632     ErrCode errCode = QueryOsAccountById(id, osAccountInfo);
633     if (errCode != ERR_OK) {
634         ACCOUNT_LOGE("QueryOsAccountById return error");
635         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
636     }
637     photo = osAccountInfo.GetPhoto();
638     return ERR_OK;
639 }
640 
IsMultiOsAccountEnable(bool & isMultiOsAccountEnable)641 ErrCode IInnerOsAccountManager::IsMultiOsAccountEnable(bool &isMultiOsAccountEnable)
642 {
643     ErrCode errCode = osAccountControl_->GetIsMultiOsAccountEnable(isMultiOsAccountEnable);
644     if (errCode != ERR_OK) {
645         ACCOUNT_LOGE("GetIsMultiOsAccountEnable error");
646         return errCode;
647     }
648     return ERR_OK;
649 }
650 
SetOsAccountName(const int id,const std::string & name)651 ErrCode IInnerOsAccountManager::SetOsAccountName(const int id, const std::string &name)
652 {
653     OsAccountInfo osAccountInfo;
654     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
655     if (errCode != ERR_OK) {
656         ACCOUNT_LOGE("get osaccount info error");
657         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
658     }
659 
660     // to be removed, cannot change any thing
661     if (osAccountInfo.GetToBeRemoved()) {
662         ACCOUNT_LOGE("account %{public}d will be removed, cannot change name!", id);
663         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
664     }
665 
666     osAccountInfo.SetLocalName(name);
667     errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
668     if (errCode != ERR_OK) {
669         ACCOUNT_LOGE("update osaccount info error,id: %{public}d", osAccountInfo.GetLocalId());
670         return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
671     }
672     return ERR_OK;
673 }
674 
SetOsAccountConstraints(const int id,const std::vector<std::string> & constraints,const bool enable)675 ErrCode IInnerOsAccountManager::SetOsAccountConstraints(
676     const int id, const std::vector<std::string> &constraints, const bool enable)
677 {
678     OsAccountInfo osAccountInfo;
679     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
680     if (errCode != ERR_OK) {
681         ACCOUNT_LOGE("get osaccount info error");
682         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
683     }
684 
685     // to be removed, cannot change any thing
686     if (osAccountInfo.GetToBeRemoved()) {
687         ACCOUNT_LOGE("account %{public}d will be removed, cannot change constraints!", id);
688         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
689     }
690 
691     bool isExists = false;
692     errCode = osAccountControl_->IsConstrarionsInTypeList(constraints, isExists);
693     if (errCode != ERR_OK || !isExists) {
694         ACCOUNT_LOGE("input constraints not in constraints list");
695         return ERR_OSACCOUNT_SERVICE_INNER_SER_CONSTRAINTS_ERROR;
696     }
697     std::vector<std::string> oldconstraints = osAccountInfo.GetConstraints();
698     for (auto it = constraints.begin(); it != constraints.end(); it++) {
699         if (enable) {
700             if (std::find(oldconstraints.begin(), oldconstraints.end(), *it) == oldconstraints.end()) {
701                 oldconstraints.push_back(*it);
702             }
703         } else {
704             oldconstraints.erase(
705                 std::remove(oldconstraints.begin(), oldconstraints.end(), *it), oldconstraints.end());
706         }
707     }
708     osAccountInfo.SetConstraints(oldconstraints);
709     errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
710     if (errCode != ERR_OK) {
711         ACCOUNT_LOGE("update osaccount info error,id: %{public}d", osAccountInfo.GetLocalId());
712         return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
713     }
714     return ERR_OK;
715 }
716 
SetOsAccountProfilePhoto(const int id,const std::string & photo)717 ErrCode IInnerOsAccountManager::SetOsAccountProfilePhoto(const int id, const std::string &photo)
718 {
719     OsAccountInfo osAccountInfo;
720     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
721     if (errCode != ERR_OK) {
722         ACCOUNT_LOGE("get osaccount info error");
723         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
724     }
725 
726     // to be removed, cannot change any thing
727     if (osAccountInfo.GetToBeRemoved()) {
728         ACCOUNT_LOGE("account %{public}d will be removed, cannot change photo!", id);
729         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
730     }
731 
732     errCode = osAccountControl_->SetPhotoById(id, photo);
733     if (errCode != ERR_OK) {
734         ACCOUNT_LOGE("set photo by id error");
735         return errCode;
736     }
737     auto sizeType = photo.find(Constants::USER_PHOTO_BASE_JPG_HEAD);
738     if (sizeType == std::string::npos) {
739         osAccountInfo.SetPhoto(Constants::USER_PHOTO_FILE_PNG_NAME);
740     } else {
741         osAccountInfo.SetPhoto(Constants::USER_PHOTO_FILE_JPG_NAME);
742     }
743     errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
744     if (errCode != ERR_OK) {
745         ACCOUNT_LOGE("update osaccount info error,id: %{public}d", osAccountInfo.GetLocalId());
746         return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
747     }
748     return ERR_OK;
749 }
750 
DeActivateOsAccount(const int id)751 void IInnerOsAccountManager::DeActivateOsAccount(const int id)
752 {
753     if (id == Constants::ADMIN_LOCAL_ID) {
754         return;
755     }
756 
757     OsAccountInfo osAccountInfo;
758     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
759     if (errCode != ERR_OK) {
760         ACCOUNT_LOGE("DeActivateOsAccount cannot get os account %{public}d info. error %{public}d.",
761             id, errCode);
762         return;
763     }
764     osAccountInfo.SetIsActived(false);
765     (void)osAccountControl_->UpdateOsAccount(osAccountInfo);
766 }
767 
ActivateOsAccount(const int id)768 ErrCode IInnerOsAccountManager::ActivateOsAccount(const int id)
769 {
770     if (IsLocalIdInOperating(id)) {
771         ACCOUNT_LOGE("the %{public}d already in operating", id);
772         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
773     }
774     AddLocalIdToOperating(id);
775     if (IsOsAccountIDInActiveList(id)) {
776         RemoveLocalIdToOperating(id);
777         ACCOUNT_LOGE("account is %{public}d already active", id);
778         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_ALREAD_ACTIVE_ERROR;
779     }
780 
781     // get information
782     OsAccountInfo osAccountInfo;
783     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
784     if (errCode != ERR_OK) {
785         RemoveLocalIdToOperating(id);
786         ACCOUNT_LOGE("cannot find os account info by id:%{public}d", id);
787         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
788     }
789 
790     // check complete
791     if (!osAccountInfo.GetIsCreateCompleted()) {
792         RemoveLocalIdToOperating(id);
793         ACCOUNT_LOGE("account %{public}d is not Completed", id);
794         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_IS_UNVERIFIED_ERROR;
795     }
796 
797     // check to be removed
798     if (osAccountInfo.GetToBeRemoved()) {
799         RemoveLocalIdToOperating(id);
800         ACCOUNT_LOGE("account %{public}d will be removed, cannot be activated!", id);
801         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
802     }
803 
804     // activate
805     subscribeManagerPtr_->PublishActivatingOsAccount(id);
806     errCode = SendMsgForAccountActivate(osAccountInfo);
807     if (errCode != ERR_OK) {
808         RemoveLocalIdToOperating(id);
809         ACCOUNT_LOGE("update %{public}d account info failed", id);
810         return errCode;
811     }
812     RemoveLocalIdToOperating(id);
813     subscribeManagerPtr_->PublishActivatedOsAccount(id);
814     ACCOUNT_LOGI("IInnerOsAccountManager ActivateOsAccount end");
815     return ERR_OK;
816 }
817 
SendMsgForAccountActivate(OsAccountInfo & osAccountInfo)818 ErrCode IInnerOsAccountManager::SendMsgForAccountActivate(OsAccountInfo &osAccountInfo)
819 {
820     ErrCode errCode = OsAccountStandardInterface::SendToStorageAccountStart(osAccountInfo);
821     if (errCode != ERR_OK) {
822         ACCOUNT_LOGE("account %{public}d call storage active failed", osAccountInfo.GetLocalId());
823         return ERR_OSACCOUNT_SERVICE_INTERFACE_TO_STORAGE_ACCOUNT_START_ERROR;
824     }
825     errCode = OsAccountStandardInterface::SendToAMSAccountStart(osAccountInfo);
826     if (errCode != ERR_OK) {
827         ACCOUNT_LOGE("account %{public}d call am active failed", osAccountInfo.GetLocalId());
828         return ERR_OSACCOUNT_SERVICE_INNER_SEND_AM_ACCOUNT_SWITCH_ERROR;
829     }
830     // update info
831     osAccountInfo.SetIsActived(true);
832     int64_t time =
833         std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
834     osAccountInfo.SetLastLoginTime(time);
835     errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
836     if (errCode != ERR_OK) {
837         ACCOUNT_LOGE("update %{public}d account info failed", osAccountInfo.GetLocalId());
838         return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
839     }
840     RefreshActiveList(osAccountInfo.GetLocalId());
841     OsAccountStandardInterface::SendToCESAccountSwitched(osAccountInfo);
842     ACCOUNT_LOGI("SendMsgForAccountActivate ok");
843     return errCode;
844 }
845 
StartOsAccount(const int id)846 ErrCode IInnerOsAccountManager::StartOsAccount(const int id)
847 {
848     return ERR_OK;
849 }
850 
StopOsAccount(const int id)851 ErrCode IInnerOsAccountManager::StopOsAccount(const int id)
852 {
853     return ERR_OK;
854 }
855 
GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber,int & id)856 ErrCode IInnerOsAccountManager::GetOsAccountLocalIdBySerialNumber(const int64_t serialNumber, int &id)
857 {
858     if (serialNumber ==
859         Constants::CARRY_NUM * Constants::SERIAL_NUMBER_NUM_START_FOR_ADMIN + Constants::ADMIN_LOCAL_ID) {
860         id = Constants::ADMIN_LOCAL_ID;
861         return ERR_OK;
862     }
863     std::vector<OsAccountInfo> osAccountInfos;
864     id = -1;
865     ErrCode errCode = osAccountControl_->GetOsAccountList(osAccountInfos);
866     if (errCode != ERR_OK) {
867         ACCOUNT_LOGE("get osaccount info list error");
868         return ERR_OSACCOUNT_SERVICE_INNER_GET_ALL_OSACCOUNTINFO_ERROR;
869     }
870     for (auto it = osAccountInfos.begin(); it != osAccountInfos.end(); it++) {
871         if (serialNumber == it->GetSerialNumber()) {
872             id = it->GetLocalId();
873             break;
874         }
875     }
876     if (id == -1) {
877         ACCOUNT_LOGE("cannot find id by serialNumber");
878         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_ERROR;
879     }
880     return ERR_OK;
881 }
882 
GetSerialNumberByOsAccountLocalId(const int & id,int64_t & serialNumber)883 ErrCode IInnerOsAccountManager::GetSerialNumberByOsAccountLocalId(const int &id, int64_t &serialNumber)
884 {
885     OsAccountInfo osAccountInfo;
886     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
887     if (errCode != ERR_OK) {
888         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
889     }
890     serialNumber = osAccountInfo.GetSerialNumber();
891     return ERR_OK;
892 }
893 
SubscribeOsAccount(const OsAccountSubscribeInfo & subscribeInfo,const sptr<IRemoteObject> & eventListener)894 ErrCode IInnerOsAccountManager::SubscribeOsAccount(
895     const OsAccountSubscribeInfo &subscribeInfo, const sptr<IRemoteObject> &eventListener)
896 {
897     ACCOUNT_LOGI("IInnerOsAccountManager SubscribeOsAccount start");
898 
899     if (!subscribeManagerPtr_) {
900         ACCOUNT_LOGE("IInnerOsAccountManager SubscribeOsAccount subscribeManagerPtr_ is nullptr");
901         return ERR_OSACCOUNT_SERVICE_SUBSCRIBE_MANAGER_PTR_IS_NULLPTR;
902     }
903 
904     auto subscribeInfoPtr = std::make_shared<OsAccountSubscribeInfo>(subscribeInfo);
905     if (subscribeInfoPtr == nullptr) {
906         ACCOUNT_LOGE("IInnerOsAccountManager SubscribeOsAccount subscribeInfoPtr is nullptr");
907     }
908     ACCOUNT_LOGI("IInnerOsAccountManager SubscribeOsAccount end");
909     return subscribeManagerPtr_->SubscribeOsAccount(subscribeInfoPtr, eventListener);
910 }
911 
UnsubscribeOsAccount(const sptr<IRemoteObject> & eventListener)912 ErrCode IInnerOsAccountManager::UnsubscribeOsAccount(const sptr<IRemoteObject> &eventListener)
913 {
914     ACCOUNT_LOGI("IInnerOsAccountManager UnsubscribeOsAccount start");
915 
916     if (!subscribeManagerPtr_) {
917         ACCOUNT_LOGE("controlManagerPtr_ is nullptr");
918         return ERR_OSACCOUNT_SERVICE_SUBSCRIBE_MANAGER_PTR_IS_NULLPTR;
919     }
920     ACCOUNT_LOGI("IInnerOsAccountManager UnsubscribeOsAccount end");
921     return subscribeManagerPtr_->UnsubscribeOsAccount(eventListener);
922 }
923 
GetOsAccountSwitchMod()924 OS_ACCOUNT_SWITCH_MOD IInnerOsAccountManager::GetOsAccountSwitchMod()
925 {
926     return Constants::NOW_OS_ACCOUNT_SWITCH_MOD;
927 }
928 
IsOsAccountCompleted(const int id,bool & isOsAccountCompleted)929 ErrCode IInnerOsAccountManager::IsOsAccountCompleted(const int id, bool &isOsAccountCompleted)
930 {
931     OsAccountInfo osAccountInfo;
932     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
933     if (errCode != ERR_OK) {
934         ACCOUNT_LOGE("get osaccount info error");
935         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
936     }
937     isOsAccountCompleted = osAccountInfo.GetIsCreateCompleted();
938     return ERR_OK;
939 }
940 
SetOsAccountIsVerified(const int id,const bool isVerified)941 ErrCode IInnerOsAccountManager::SetOsAccountIsVerified(const int id, const bool isVerified)
942 {
943     OsAccountInfo osAccountInfo;
944     ErrCode errCode = osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
945     if (errCode != ERR_OK) {
946         ACCOUNT_LOGE("get osaccount info error");
947         return ERR_OSACCOUNT_SERVICE_INNER_SELECT_OSACCOUNT_BYID_ERROR;
948     }
949 
950     // to be removed, cannot change any thing
951     if (osAccountInfo.GetToBeRemoved()) {
952         ACCOUNT_LOGE("account %{public}d will be removed, cannot change verify state!", id);
953         return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_TO_BE_REMOVED_ERROR;
954     }
955 
956     osAccountInfo.SetIsVerified(isVerified);
957     errCode = osAccountControl_->UpdateOsAccount(osAccountInfo);
958     if (errCode != ERR_OK) {
959         ACCOUNT_LOGE("update osaccount info error,id: %{public}d", osAccountInfo.GetLocalId());
960         return ERR_OSACCOUNT_SERVICE_INNER_UPDATE_ACCOUNT_ERROR;
961     }
962     return ERR_OK;
963 }
964 
GetEventHandler(void)965 ErrCode IInnerOsAccountManager::GetEventHandler(void)
966 {
967     if (!handler_) {
968         handler_ = std::make_shared<OHOS::AppExecFwk::EventHandler>(OHOS::AppExecFwk::EventRunner::Create());
969         if (handler_ == nullptr) {
970             ACCOUNT_LOGE("failed to create event handler");
971             return ERR_OSACCOUNT_SERVICE_CREATE_EVENT_HANDLER;
972         }
973     }
974 
975     return ERR_OK;
976 }
977 
IsAllowedCreateAdmin(bool & isAllowedCreateAdmin)978 ErrCode IInnerOsAccountManager::IsAllowedCreateAdmin(bool &isAllowedCreateAdmin)
979 {
980     return osAccountControl_->IsAllowedCreateAdmin(isAllowedCreateAdmin);
981 }
982 
GetCreatedOsAccountNumFromDatabase(const std::string & storeID,int & createdOsAccountNum)983 ErrCode IInnerOsAccountManager::GetCreatedOsAccountNumFromDatabase(const std::string& storeID,
984     int &createdOsAccountNum)
985 {
986     return osAccountControl_->GetCreatedOsAccountNumFromDatabase(storeID, createdOsAccountNum);
987 }
988 
GetSerialNumberFromDatabase(const std::string & storeID,int64_t & serialNumber)989 ErrCode IInnerOsAccountManager::GetSerialNumberFromDatabase(const std::string& storeID,
990     int64_t &serialNumber)
991 {
992     return osAccountControl_->GetSerialNumberFromDatabase(storeID, serialNumber);
993 }
994 
GetMaxAllowCreateIdFromDatabase(const std::string & storeID,int & id)995 ErrCode IInnerOsAccountManager::GetMaxAllowCreateIdFromDatabase(const std::string& storeID, int &id)
996 {
997     return osAccountControl_->GetMaxAllowCreateIdFromDatabase(storeID, id);
998 }
999 
GetOsAccountFromDatabase(const std::string & storeID,const int id,OsAccountInfo & osAccountInfo)1000 ErrCode IInnerOsAccountManager::GetOsAccountFromDatabase(const std::string& storeID, const int id,
1001     OsAccountInfo &osAccountInfo)
1002 {
1003     return osAccountControl_->GetOsAccountFromDatabase(storeID, id, osAccountInfo);
1004 }
1005 
GetOsAccountListFromDatabase(const std::string & storeID,std::vector<OsAccountInfo> & osAccountList)1006 ErrCode IInnerOsAccountManager::GetOsAccountListFromDatabase(const std::string& storeID,
1007     std::vector<OsAccountInfo> &osAccountList)
1008 {
1009     return osAccountControl_->GetOsAccountListFromDatabase(storeID, osAccountList);
1010 }
1011 
AddLocalIdToOperating(int32_t localId)1012 void IInnerOsAccountManager::AddLocalIdToOperating(int32_t localId)
1013 {
1014     std::lock_guard<std::mutex> lock(operatingMutex_);
1015     operatingId_.push_back(localId);
1016 }
1017 
RemoveLocalIdToOperating(int32_t localId)1018 void IInnerOsAccountManager::RemoveLocalIdToOperating(int32_t localId)
1019 {
1020     std::lock_guard<std::mutex> lock(operatingMutex_);
1021     auto it = std::find(operatingId_.begin(), operatingId_.end(), localId);
1022     if (it != operatingId_.end()) {
1023         operatingId_.erase(it);
1024     }
1025 }
1026 
IsLocalIdInOperating(int32_t localId)1027 bool IInnerOsAccountManager::IsLocalIdInOperating(int32_t localId)
1028 {
1029     std::lock_guard<std::mutex> lock(operatingMutex_);
1030     return std::find(operatingId_.begin(), operatingId_.end(), localId) != operatingId_.end();
1031 }
1032 
QueryActiveOsAccountIds(std::vector<int32_t> & ids)1033 ErrCode IInnerOsAccountManager::QueryActiveOsAccountIds(std::vector<int32_t>& ids)
1034 {
1035     CopyFromActiveList(ids);
1036     return ERR_OK;
1037 }
1038 
PushIDIntoActiveList(int32_t id)1039 void IInnerOsAccountManager::PushIDIntoActiveList(int32_t id)
1040 {
1041     std::lock_guard<std::mutex> lock(ativeMutex_);
1042     activeAccountId_.push_back(id);
1043 }
1044 
IsOsAccountIDInActiveList(int32_t id)1045 bool IInnerOsAccountManager::IsOsAccountIDInActiveList(int32_t id)
1046 {
1047     std::lock_guard<std::mutex> lock(ativeMutex_);
1048     auto it = std::find(activeAccountId_.begin(), activeAccountId_.end(), id);
1049     return (it != activeAccountId_.end());
1050 }
1051 
CopyFromActiveList(std::vector<int32_t> & idList)1052 void IInnerOsAccountManager::CopyFromActiveList(std::vector<int32_t>& idList)
1053 {
1054     idList.clear();
1055     std::lock_guard<std::mutex> lock(ativeMutex_);
1056     for (auto it = activeAccountId_.begin(); it != activeAccountId_.end(); it++) {
1057         idList.push_back(*it);
1058     }
1059 }
1060 
RefreshActiveList(int32_t newId)1061 void IInnerOsAccountManager::RefreshActiveList(int32_t newId)
1062 {
1063     std::lock_guard<std::mutex> lock(ativeMutex_);
1064 
1065     // deactivate old ids first
1066     for (size_t i = 0; i < activeAccountId_.size(); ++i) {
1067         DeActivateOsAccount(activeAccountId_[i]);
1068     }
1069 
1070     activeAccountId_.clear();
1071     activeAccountId_.push_back(newId);
1072 }
1073 }  // namespace AccountSA
1074 }  // namespace OHOS
1075