Add Check during creation and deletion

Signed-off-by: cc_ggboy <chenweimin8@huawei.com>
This commit is contained in:
cc_ggboy 2024-09-06 02:46:01 +00:00
parent b646260d2d
commit 57188df34c
2 changed files with 43 additions and 23 deletions

View File

@ -264,7 +264,10 @@ ErrCode IInnerOsAccountManager::PrepareOsAccountInfo(const std::string &localNam
ACCOUNT_LOGE("Check type number failed.");
return errCode;
}
if (!CheckAndAddLocalIdOperating(osAccountInfo.GetLocalId())) {
ACCOUNT_LOGW("Account id = %{public}d already in operating", osAccountInfo.GetLocalId());
return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
}
errCode = osAccountControl_->InsertOsAccount(osAccountInfo);
if (errCode != ERR_OK) {
ACCOUNT_LOGE("insert os account info err, errCode %{public}d.", errCode);
@ -423,9 +426,11 @@ ErrCode IInnerOsAccountManager::CreateOsAccount(
DomainAccountInfo domainInfo; // default empty domain info
ErrCode errCode = PrepareOsAccountInfo(name, type, domainInfo, osAccountInfo);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
return errCode;
}
errCode = SendMsgForAccountCreate(osAccountInfo);
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
if (errCode != ERR_OK) {
(void)osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId());
}
@ -466,9 +471,11 @@ ErrCode IInnerOsAccountManager::CreateOsAccount(const std::string &localName, co
DomainAccountInfo domainInfo; // default empty domain info
ErrCode errCode = PrepareOsAccountInfo(localName, shortName, type, domainInfo, osAccountInfo);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
return errCode;
}
errCode = SendMsgForAccountCreate(osAccountInfo, options);
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
if (errCode != ERR_OK) {
(void)osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId());
}
@ -503,11 +510,17 @@ ErrCode IInnerOsAccountManager::CreateOsAccountWithFullInfo(OsAccountInfo &osAcc
ACCOUNT_LOGI("Not allow creation account.");
return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_PLUGIN_NOT_ALLOWED_CREATION_ERROR;
}
if (!CheckAndAddLocalIdOperating(osAccountInfo.GetLocalId())) {
ACCOUNT_LOGW("Account id = %{public}d already in operating", osAccountInfo.GetLocalId());
return ERR_OSACCOUNT_SERVICE_INNER_ACCOUNT_OPERATING_ERROR;
}
ErrCode errCode = PrepareOsAccountInfoWithFullInfo(osAccountInfo);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
return errCode;
}
errCode = SendMsgForAccountCreate(osAccountInfo);
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
if (errCode != ERR_OK) {
(void)osAccountControl_->DelOsAccount(osAccountInfo.GetLocalId());
}
@ -609,8 +622,10 @@ ErrCode IInnerOsAccountManager::BindDomainAccount(const OsAccountType &type, con
ErrCode errCode = PrepareOsAccountInfo(domainAccountInfo.accountName_, options.shortName,
type, domainAccountInfo, osAccountInfo);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
return errCode;
}
RemoveLocalIdToOperating(osAccountInfo.GetLocalId());
#else
ACCOUNT_LOGW("multiple os accounts feature not enabled");
return ERR_OSACCOUNT_SERVICE_MANAGER_NOT_ENABLE_MULTI_ERROR;
@ -695,7 +710,6 @@ ErrCode IInnerOsAccountManager::PrepareRemoveOsAccount(OsAccountInfo &osAccountI
if (osAccountInfo.GetIsForeground()) {
ACCOUNT_LOGI("Remove foreground account id=%{public}d.", id);
if (ActivateOsAccount(Constants::START_USER_ID) != ERR_OK) {
RemoveLocalIdToOperating(id);
ACCOUNT_LOGE("RemoveOsAccount active base account failed");
return ERR_OSACCOUNT_SERVICE_INNER_REMOVE_ACCOUNT_ACTIVED_ERROR;
}
@ -706,7 +720,6 @@ ErrCode IInnerOsAccountManager::PrepareRemoveOsAccount(OsAccountInfo &osAccountI
// stop account
errCode = SendMsgForAccountStop(osAccountInfo);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(id);
ReportOsAccountOperationFail(id, "stop", errCode, "stop os account failed");
return errCode;
}
@ -721,7 +734,6 @@ ErrCode IInnerOsAccountManager::RemoveOsAccountOperate(const int id, OsAccountIn
}
ErrCode errCode = PrepareRemoveOsAccount(osAccountInfo, isCleanGarbage);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(id);
ACCOUNT_LOGE("PrepareRemoveOsAccount failed, errCode %{public}d.", errCode);
return errCode;
}
@ -744,10 +756,8 @@ ErrCode IInnerOsAccountManager::RemoveOsAccountOperate(const int id, OsAccountIn
}
errCode = SendMsgForAccountRemove(osAccountInfo);
if (errCode != ERR_OK) {
RemoveLocalIdToOperating(id);
return errCode;
}
RemoveLocalIdToOperating(id);
errCode = osAccountControl_->RemoveOAConstraintsInfo(id);
if (errCode != ERR_OK) {
@ -780,7 +790,9 @@ ErrCode IInnerOsAccountManager::RemoveOsAccount(const int id)
osAccountControl_->UpdateOsAccount(osAccountInfo);
// then remove account
return RemoveOsAccountOperate(id, osAccountInfo);
errCode = RemoveOsAccountOperate(id, osAccountInfo);
RemoveLocalIdToOperating(id);
return errCode;
}
ErrCode IInnerOsAccountManager::SendMsgForAccountStop(OsAccountInfo &osAccountInfo)
@ -1216,30 +1228,32 @@ ErrCode IInnerOsAccountManager::DealWithDeviceOwnerId(const bool isDeviceOwner,
void IInnerOsAccountManager::CleanGarbageOsAccounts()
{
ACCOUNT_LOGD("enter.");
std::vector<OsAccountInfo> osAccountInfos;
if (osAccountControl_->GetOsAccountList(osAccountInfos) != ERR_OK) {
ACCOUNT_LOGI("QueryAllCreatedOsAccounts failed.");
ACCOUNT_LOGI("enter");
std::vector<int32_t> idList;
if (osAccountControl_->GetOsAccountIdList(idList) != ERR_OK) {
ACCOUNT_LOGI("GetOsAccountIdList failed.");
return;
}
// check status and remove garbage accounts data
for (size_t i = 0; i < osAccountInfos.size(); ++i) {
if (!osAccountInfos[i].GetToBeRemoved() && osAccountInfos[i].GetIsCreateCompleted()) {
for (auto id : idList) {
if (id == Constants::START_USER_ID || id == Constants::ADMIN_LOCAL_ID) {
continue;
}
if (osAccountInfos[i].GetLocalId() == Constants::START_USER_ID ||
osAccountInfos[i].GetLocalId() == Constants::ADMIN_LOCAL_ID) {
if (!CheckAndAddLocalIdOperating(id)) {
ACCOUNT_LOGI("Account id = %{public}d already in operating", id);
continue;
}
ErrCode errCode = RemoveOsAccountOperate(osAccountInfos[i].GetLocalId(), osAccountInfos[i], true);
OsAccountInfo osAccountInfo;
osAccountControl_->GetOsAccountInfoById(id, osAccountInfo);
if (!osAccountInfo.GetToBeRemoved() && osAccountInfo.GetIsCreateCompleted()) {
RemoveLocalIdToOperating(id);
continue;
}
ErrCode errCode = RemoveOsAccountOperate(id, osAccountInfo, true);
RemoveLocalIdToOperating(id);
if (errCode != ERR_OK) {
ACCOUNT_LOGE("remove account %{public}d failed! errCode %{public}d.",
osAccountInfos[i].GetLocalId(), errCode);
ACCOUNT_LOGE("remove account %{public}d failed! errCode %{public}d.", id, errCode);
} else {
ACCOUNT_LOGI("remove account %{public}d succeed!", osAccountInfos[i].GetLocalId());
ACCOUNT_LOGI("remove account %{public}d succeed!", id);
}
}
ACCOUNT_LOGI("finished.");

View File

@ -932,20 +932,24 @@ HWTEST_F(OsAccountInnerAccmgrMockTest, OsAccountInnerAccmgrMockTest009, TestSize
DomainAccountInfo domainInfo2(STRING_DOMAIN_NAME_OUT_OF_RANGE, STRING_DOMAIN_ACCOUNT_NAME_VALID);
OsAccountInfo accountInfo;
int ret = innerMgrService_->PrepareOsAccountInfo(name, type, domainInfo1, accountInfo);
innerMgrService_->RemoveLocalIdToOperating(accountInfo.GetLocalId());
EXPECT_EQ(ret, -1);
EXPECT_CALL(*ptr, GetAllowCreateId(::testing::_))
.WillRepeatedly(testing::Return(0));
EXPECT_CALL(*ptr, GetConstraintsByType(::testing::_, ::testing::_))
.WillRepeatedly(testing::Return(-1));
ret = innerMgrService_->PrepareOsAccountInfo(name, type, domainInfo1, accountInfo);
innerMgrService_->RemoveLocalIdToOperating(accountInfo.GetLocalId());
EXPECT_NE(ret, ERR_OK);
EXPECT_CALL(*ptr, GetConstraintsByType(::testing::_, ::testing::_))
.WillRepeatedly(testing::Return(0));
ret = innerMgrService_->PrepareOsAccountInfo(name, type, domainInfo2, accountInfo);
innerMgrService_->RemoveLocalIdToOperating(accountInfo.GetLocalId());
EXPECT_EQ(ret, ERR_OSACCOUNT_KIT_CREATE_OS_ACCOUNT_FOR_DOMAIN_ERROR);
EXPECT_CALL(*ptr, InsertOsAccount(::testing::_))
.WillRepeatedly(testing::Return(-1));
ret = innerMgrService_->PrepareOsAccountInfo(name, type, domainInfo1, accountInfo);
innerMgrService_->RemoveLocalIdToOperating(accountInfo.GetLocalId());
EXPECT_NE(ret, ERR_OK);
EXPECT_CALL(*ptr, InsertOsAccount(::testing::_))
.WillRepeatedly(testing::Return(0));
@ -953,9 +957,11 @@ HWTEST_F(OsAccountInnerAccmgrMockTest, OsAccountInnerAccmgrMockTest009, TestSize
.WillRepeatedly(testing::Return(-1));
ret = innerMgrService_->PrepareOsAccountInfo(name, type, domainInfo1, accountInfo);
EXPECT_NE(ret, ERR_OK);
innerMgrService_->RemoveLocalIdToOperating(accountInfo.GetLocalId());
EXPECT_CALL(*ptr, UpdateBaseOAConstraints(::testing::_, ::testing::_, ::testing::_))
.WillRepeatedly(testing::Return(0));
ret = innerMgrService_->PrepareOsAccountInfo(name, type, domainInfo1, accountInfo);
innerMgrService_->RemoveLocalIdToOperating(accountInfo.GetLocalId());
EXPECT_EQ(ret, ERR_OK);
innerMgrService_->RemoveOsAccount(accountInfo.GetLocalId());
}