diff --git a/frameworks/domain_account/src/domain_account_common.cpp b/frameworks/domain_account/src/domain_account_common.cpp index 9717b0e7b..cdc25da2f 100644 --- a/frameworks/domain_account/src/domain_account_common.cpp +++ b/frameworks/domain_account/src/domain_account_common.cpp @@ -350,5 +350,42 @@ DomainAuthResult *DomainAuthResult::Unmarshalling(Parcel &parcel) } return result; } + +bool CreateOsAccountForDomainOptions::Marshalling(Parcel &parcel) const +{ + if (!parcel.WriteString(shortName)) { + ACCOUNT_LOGE("Failed to write shortName"); + return false; + } + if (!parcel.WriteBool(hasShortName)) { + ACCOUNT_LOGE("Failed to write hasShortName"); + return false; + } + return true; +} + +CreateOsAccountForDomainOptions *CreateOsAccountForDomainOptions::Unmarshalling(Parcel &parcel) +{ + CreateOsAccountForDomainOptions *options = new (std::nothrow) CreateOsAccountForDomainOptions(); + if ((options != nullptr) && (!options->ReadFromParcel(parcel))) { + ACCOUNT_LOGE("Failed to read from parcel"); + delete options; + options = nullptr; + } + return options; +} + +bool CreateOsAccountForDomainOptions::ReadFromParcel(Parcel &parcel) +{ + if (!parcel.ReadString(shortName)) { + ACCOUNT_LOGE("Failed to read shortName."); + return false; + } + if (!parcel.ReadBool(hasShortName)) { + ACCOUNT_LOGE("Failed to read hasShortName."); + return false; + } + return true; +} } // namespace AccountSA } // namespace OHOS \ No newline at end of file diff --git a/frameworks/domain_account/test/moduletest/src/domain_account_client_mock_plugin_so_module_test.cpp b/frameworks/domain_account/test/moduletest/src/domain_account_client_mock_plugin_so_module_test.cpp index 261d30c7a..9f17f5dfd 100644 --- a/frameworks/domain_account/test/moduletest/src/domain_account_client_mock_plugin_so_module_test.cpp +++ b/frameworks/domain_account/test/moduletest/src/domain_account_client_mock_plugin_so_module_test.cpp @@ -67,6 +67,7 @@ const std::vector DEFAULT_TOKEN = {49, 50, 51, 52, 53}; static uint64_t g_selfTokenID; #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS const int32_t WAIT_TIME = 2; +const std::string STRING_SHORT_NAME_OUT_OF_RANGE(256, '1'); #endif const std::map PLUGIN_METHOD_MAP = { {PluginMethodEnum::AUTH, reinterpret_cast(Auth)}, @@ -697,7 +698,69 @@ HWTEST_F(DomainAccountClientMockPluginSoModuleTest, DomainAccountClientModuleTes EXPECT_EQ(OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, userId), ERR_OK); EXPECT_EQ(OsAccountManager::RemoveOsAccount(userId), ERR_OK); } -#endif + +/** + * @tc.name: DomainAccountClientModuleTest_IsAuthenticationExpired_005 + * @tc.desc: IsAuthenticationExpired success. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DomainAccountClientMockPluginSoModuleTest, DomainAccountClientModuleTest_IsAuthenticationExpired_007, + TestSize.Level0) +{ + DomainAccountInfo domainInfo; + domainInfo.accountName_ = "testaccount"; + domainInfo.domain_ = "test.example.com"; + domainInfo.accountId_ = "testid"; + + CreateOsAccountForDomainOptions options; + options.hasShortName = true; + + OsAccountInfo osAccountInfo; + ErrCode code = OsAccountManager::CreateOsAccount("domain007", "shortExist", OsAccountType::NORMAL, osAccountInfo); + EXPECT_EQ(code, ERR_OK); + + auto testCallback1 = nullptr; + options.shortName = "TEST1*"; + EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback1, options), ERR_OK); + + options.shortName = ".."; + EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback1, options), ERR_OK); + + options.shortName = ""; + EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback1, options), ERR_OK); + + options.shortName = STRING_SHORT_NAME_OUT_OF_RANGE; + EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback1, options), ERR_OK); + + options.shortName = "shortExist"; + EXPECT_NE(OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback1, options), ERR_OK); + + LoadPluginMethods(); + auto callback = std::make_shared(); + ASSERT_NE(callback, nullptr); + auto testCallback = std::make_shared(callback); + EXPECT_CALL(*callback, OnResult(ERR_OK, "testaccount", "test.example.com", _)).Times(Exactly(1)); + ASSERT_NE(testCallback, nullptr); + options.shortName = "shortNameTest"; + options.hasShortName = false; + ErrCode errCode = OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback, options); + std::unique_lock lock(testCallback->mutex); + testCallback->cv.wait_for(lock, std::chrono::seconds(WAIT_TIME), + [lockCallback = testCallback]() { return lockCallback->isReady; }); + EXPECT_EQ(errCode, ERR_OK); + int32_t userId = -1; + EXPECT_EQ(OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, userId), ERR_OK); + EXPECT_EQ(OsAccountManager::RemoveOsAccount(userId), ERR_OK); + EXPECT_EQ(OsAccountManager::RemoveOsAccount(osAccountInfo.GetLocalId()), ERR_OK); +} +#endif // ENABLE_MULTIPLE_OS_ACCOUNTS /** * @tc.name: DomainAccountClientModuleTest_IsAuthenticationExpired_006 diff --git a/frameworks/domain_account/test/moduletest/src/domain_account_client_module_test.cpp b/frameworks/domain_account/test/moduletest/src/domain_account_client_module_test.cpp index 1125c8378..57541899d 100644 --- a/frameworks/domain_account/test/moduletest/src/domain_account_client_module_test.cpp +++ b/frameworks/domain_account/test/moduletest/src/domain_account_client_module_test.cpp @@ -719,7 +719,10 @@ HWTEST_F(DomainAccountClientModuleTest, DomainAccountClientModuleTest_CreateOsAc EXPECT_CALL(*callback, OnResult(ERR_OK, STRING_NAME, INVALID_STRING_DOMAIN, STRING_ACCOUNTID_NEW)).Times(Exactly(1)); ASSERT_NE(testCallback, nullptr); - ErrCode errCode = OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, testCallback); + CreateOsAccountForDomainOptions options; + options.shortName = "shortName"; + ErrCode errCode = OsAccountManager::CreateOsAccountForDomain(OsAccountType::NORMAL, domainInfo, + testCallback, options); std::unique_lock lock(testCallback->mutex); testCallback->cv.wait_for( lock, std::chrono::seconds(WAIT_TIME), [lockCallback = testCallback]() { return lockCallback->isReady; }); @@ -729,6 +732,9 @@ HWTEST_F(DomainAccountClientModuleTest, DomainAccountClientModuleTest_CreateOsAc int32_t userId = -1; errCode = OsAccountManager::GetOsAccountLocalIdFromDomain(domainInfo, userId); EXPECT_EQ(errCode, ERR_OK); + OsAccountInfo osAccountInfo; + EXPECT_EQ(OsAccountManager::QueryOsAccountById(userId, osAccountInfo), ERR_OK); + EXPECT_EQ(osAccountInfo.GetShortName(), options.shortName); EXPECT_EQ(OsAccountManager::RemoveOsAccount(userId), ERR_OK); } #endif // ENABLE_MULTIPLE_OS_ACCOUNTS diff --git a/frameworks/osaccount/core/include/ios_account.h b/frameworks/osaccount/core/include/ios_account.h index f81f0a840..647c76c47 100644 --- a/frameworks/osaccount/core/include/ios_account.h +++ b/frameworks/osaccount/core/include/ios_account.h @@ -39,7 +39,7 @@ public: virtual ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) = 0; virtual ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) = 0; virtual ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const sptr &callback) = 0; + const sptr &callback, const CreateOsAccountForDomainOptions& options = {}) = 0; virtual ErrCode RemoveOsAccount(const int id) = 0; virtual ErrCode IsOsAccountExists(const int id, bool &isOsAccountExists) = 0; virtual ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) = 0; diff --git a/frameworks/osaccount/core/include/os_account.h b/frameworks/osaccount/core/include/os_account.h index e87c17e46..1a7e06f54 100644 --- a/frameworks/osaccount/core/include/os_account.h +++ b/frameworks/osaccount/core/include/os_account.h @@ -32,7 +32,7 @@ public: ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo); ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo); ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const std::shared_ptr &callback); + const std::shared_ptr &callback, const CreateOsAccountForDomainOptions& options = {}); ErrCode RemoveOsAccount(const int id); ErrCode IsOsAccountExists(const int id, bool &isOsAccountExists); ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived); diff --git a/frameworks/osaccount/core/include/os_account_proxy.h b/frameworks/osaccount/core/include/os_account_proxy.h index baa426353..86572d947 100644 --- a/frameworks/osaccount/core/include/os_account_proxy.h +++ b/frameworks/osaccount/core/include/os_account_proxy.h @@ -33,7 +33,7 @@ public: ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override; ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override; ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const sptr &callback) override; + const sptr &callback, const CreateOsAccountForDomainOptions& options) override; ErrCode RemoveOsAccount(const int id) override; ErrCode IsOsAccountExists(const int id, bool &isOsAccountExists) override; ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) override; diff --git a/frameworks/osaccount/core/src/os_account.cpp b/frameworks/osaccount/core/src/os_account.cpp index 956092e70..d2ee4fac8 100644 --- a/frameworks/osaccount/core/src/os_account.cpp +++ b/frameworks/osaccount/core/src/os_account.cpp @@ -114,17 +114,23 @@ ErrCode OsAccount::UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) } ErrCode OsAccount::CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const std::shared_ptr &callback) + const std::shared_ptr &callback, const CreateOsAccountForDomainOptions& options) { if (domainInfo.domain_.empty() || domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) { - ACCOUNT_LOGE("domain is empty or too long, len %{public}zu.", domainInfo.domain_.size()); + ACCOUNT_LOGE("Domain is empty or too long, len=%{public}zu.", domainInfo.domain_.size()); return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; } if (domainInfo.accountName_.empty() || domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE) { - ACCOUNT_LOGE("account name is empty or too long, len %{public}zu.", domainInfo.accountName_.size()); + ACCOUNT_LOGE("Account name is empty or too long, len=%{public}zu.", domainInfo.accountName_.size()); + return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; + } + + if (options.hasShortName && ((options.shortName.size() > Constants::SHORT_NAME_MAX_SIZE) || + (options.shortName == ""))) { + ACCOUNT_LOGE("Account short name is empty or too long, len=%{public}zu.", options.shortName.size()); return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; } @@ -133,7 +139,7 @@ ErrCode OsAccount::CreateOsAccountForDomain(const OsAccountType &type, const Dom return ERR_ACCOUNT_COMMON_GET_PROXY; } sptr callbackService = new (std::nothrow) DomainAccountCallbackService(callback); - return proxy->CreateOsAccountForDomain(type, domainInfo, callbackService); + return proxy->CreateOsAccountForDomain(type, domainInfo, callbackService, options); } ErrCode OsAccount::RemoveOsAccount(const int id) diff --git a/frameworks/osaccount/core/src/os_account_proxy.cpp b/frameworks/osaccount/core/src/os_account_proxy.cpp index 9a4a4fe4e..07ccdc284 100644 --- a/frameworks/osaccount/core/src/os_account_proxy.cpp +++ b/frameworks/osaccount/core/src/os_account_proxy.cpp @@ -174,40 +174,45 @@ ErrCode OsAccountProxy::UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo } ErrCode OsAccountProxy::CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const sptr &callback) + const sptr &callback, const CreateOsAccountForDomainOptions& options) { MessageParcel data; MessageParcel reply; if (!data.WriteInterfaceToken(GetDescriptor())) { - ACCOUNT_LOGE("failed to write descriptor!"); + ACCOUNT_LOGE("Failed to write descriptor!"); return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR; } if (!data.WriteInt32(static_cast(type))) { - ACCOUNT_LOGE("failed to write type "); + ACCOUNT_LOGE("Failed to write type "); return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR; } if (!data.WriteParcelable(&domainInfo)) { - ACCOUNT_LOGE("fail to write name"); + ACCOUNT_LOGE("Fail to write domainInfo"); return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR; } if ((callback == nullptr) || (!data.WriteRemoteObject(callback->AsObject()))) { - ACCOUNT_LOGE("fail to write callback"); + ACCOUNT_LOGE("Fail to write callback"); + return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR; + } + + if (!data.WriteParcelable(&options)) { + ACCOUNT_LOGE("Failed to write options"); return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR; } ErrCode result = SendRequest(OsAccountInterfaceCode::CREATE_OS_ACCOUNT_FOR_DOMAIN, data, reply); if (result != ERR_OK) { - ACCOUNT_LOGE("failed to send request, result %{public}d.", result); + ACCOUNT_LOGE("Failed to send request, result %{public}d.", result); return result; } result = reply.ReadInt32(); if (result != ERR_OK) { - ACCOUNT_LOGE("failed to read reply for create os account for domain, result %{public}d.", result); + ACCOUNT_LOGE("Failed to read reply for create os account for domain, result %{public}d.", result); return result; } return ERR_OK; diff --git a/frameworks/osaccount/native/libos_account_innerkits.map b/frameworks/osaccount/native/libos_account_innerkits.map index f82ffe494..4142a30e5 100644 --- a/frameworks/osaccount/native/libos_account_innerkits.map +++ b/frameworks/osaccount/native/libos_account_innerkits.map @@ -18,9 +18,11 @@ "VTT for OHOS::AccountSA::OsAccountInfo"; "VTT for OHOS::AccountSA::OsAccountSubscribeInfo"; "VTT for OHOS::AccountSA::CreateOsAccountOptions"; + "VTT for OHOS::AccountSA::CreateOsAccountForDomainOptions"; "vtable for OHOS::AccountSA::OsAccountInfo"; "vtable for OHOS::AccountSA::OsAccountSubscribeInfo"; "vtable for OHOS::AccountSA::CreateOsAccountOptions"; + "vtable for OHOS::AccountSA::CreateOsAccountForDomainOptions"; "OHOS::AccountSA::OsAccount::GetInstance()"; "OHOS::AccountSA::OsAccount::CreateOsAccountEventListener(std::__h::shared_ptr const&, OHOS::sptr&)"; "OHOS::AccountSA::OsAccount::CreateOsAccount(std::__h::basic_string, std::__h::allocator> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)"; @@ -84,7 +86,6 @@ "OHOS::AccountSA::OsAccountManager::CreateOsAccount(std::__h::basic_string, std::__h::allocator> const&, std::__h::basic_string, std::__h::allocator> const&, OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::OsAccountInfo&)"; "OHOS::AccountSA::OsAccountManager::CreateOsAccountWithFullInfo(OHOS::AccountSA::OsAccountInfo&)"; "OHOS::AccountSA::OsAccountManager::UpdateOsAccountWithFullInfo(OHOS::AccountSA::OsAccountInfo&)"; - "OHOS::AccountSA::OsAccountManager::CreateOsAccountForDomain(OHOS::AccountSA::OsAccountType const&, OHOS::AccountSA::DomainAccountInfo const&, OHOS::AccountSA::OsAccountInfo&)"; "OHOS::AccountSA::OsAccountManager::RemoveOsAccount(int)"; "OHOS::AccountSA::OsAccountManager::IsOsAccountExists(int, bool&)"; "OHOS::AccountSA::OsAccountManager::IsOsAccountActived(int, bool&)"; @@ -224,6 +225,7 @@ "OHOS::AccountSA::OsAccountSubscribeInfo::SetOsAccountSubscribeType(OHOS::AccountSA::OS_ACCOUNT_SUBSCRIBE_TYPE const&)"; "OHOS::AccountSA::OsAccountSubscribeInfo::SetName(std::__h::basic_string, std::__h::allocator> const&)"; *OHOS::AccountSA::CreateOsAccountOptions*; + *OHOS::AccountSA::CreateOsAccountForDomainOptions*; "vtable for OHOS::AccountSA::DomainAccountInfo"; "OHOS::AccountSA::DomainAccountInfo::DomainAccountInfo()"; "OHOS::AccountSA::DomainAccountClient::GetInstance()"; diff --git a/frameworks/osaccount/native/src/os_account_manager.cpp b/frameworks/osaccount/native/src/os_account_manager.cpp index 9445e8809..1cb9fbb6f 100644 --- a/frameworks/osaccount/native/src/os_account_manager.cpp +++ b/frameworks/osaccount/native/src/os_account_manager.cpp @@ -50,9 +50,9 @@ ErrCode OsAccountManager::UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountIn } ErrCode OsAccountManager::CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const std::shared_ptr &callback) + const std::shared_ptr &callback, const CreateOsAccountForDomainOptions &options) { - return OsAccount::GetInstance().CreateOsAccountForDomain(type, domainInfo, callback); + return OsAccount::GetInstance().CreateOsAccountForDomain(type, domainInfo, callback, options); } ErrCode OsAccountManager::RemoveOsAccount(const int id) diff --git a/interfaces/innerkits/domain_account/native/include/domain_account_common.h b/interfaces/innerkits/domain_account/native/include/domain_account_common.h index 0ecd6197a..2fda96e58 100644 --- a/interfaces/innerkits/domain_account/native/include/domain_account_common.h +++ b/interfaces/innerkits/domain_account/native/include/domain_account_common.h @@ -45,6 +45,14 @@ typedef enum { LOG_OUT, } DomainAccountEvent; +struct CreateOsAccountForDomainOptions: public Parcelable { + bool ReadFromParcel(Parcel &parcel); + bool Marshalling(Parcel &parcel) const override; + static CreateOsAccountForDomainOptions *Unmarshalling(Parcel &parcel); + std::string shortName; + bool hasShortName = false; +}; + class DomainAccountInfo : public Parcelable { public: DomainAccountInfo(); diff --git a/interfaces/innerkits/osaccount/native/include/os_account_manager.h b/interfaces/innerkits/osaccount/native/include/os_account_manager.h index f678e24e7..e0ef7f824 100644 --- a/interfaces/innerkits/osaccount/native/include/os_account_manager.h +++ b/interfaces/innerkits/osaccount/native/include/os_account_manager.h @@ -108,7 +108,7 @@ public: * @return error code, see account_error_no.h */ static ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const std::shared_ptr &callback); + const std::shared_ptr &callback, const CreateOsAccountForDomainOptions& options = {}); /** * @brief Removes an OS account based on its local ID. diff --git a/interfaces/kits/napi/osaccount/include/napi_os_account.h b/interfaces/kits/napi/osaccount/include/napi_os_account.h index 8861fd6f3..81aca231b 100644 --- a/interfaces/kits/napi/osaccount/include/napi_os_account.h +++ b/interfaces/kits/napi/osaccount/include/napi_os_account.h @@ -85,6 +85,7 @@ struct CreateOAForDomainAsyncContext : public CommonAsyncContext { OsAccountType type; DomainAccountInfo domainInfo; OsAccountInfo osAccountInfos; + CreateOsAccountForDomainOptions domainOptions; ThreadLockInfo *lockInfo; }; diff --git a/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp b/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp index 7f4ee3c26..9087f6f58 100644 --- a/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp +++ b/interfaces/kits/napi/osaccount/src/napi_os_account_common.cpp @@ -533,6 +533,16 @@ static bool ParseDomainAccountInfo(napi_env env, napi_value object, DomainAccoun return true; } +static bool ParseDomainOptionInfo(napi_env env, napi_value object, CreateOsAccountForDomainOptions &domainOptions) +{ + if (!GetStringPropertyByKey(env, object, "shortName", domainOptions.shortName)) { + ACCOUNT_LOGE("Failed to get options's shortName"); + return false; + } + domainOptions.hasShortName = true; + return true; +} + bool ParseParaCreateOAForDomain(napi_env env, napi_callback_info cbInfo, CreateOAForDomainAsyncContext *asyncContext) { @@ -540,12 +550,16 @@ bool ParseParaCreateOAForDomain(napi_env env, napi_callback_info cbInfo, napi_value argv[ARGS_SIZE_THREE] = {0}; napi_get_cb_info(env, cbInfo, &argc, argv, nullptr, nullptr); + napi_valuetype valueType = napi_undefined; if (argc == ARGS_SIZE_THREE) { - if (!GetCallbackProperty(env, argv[argc - 1], asyncContext->callbackRef, 1)) { - ACCOUNT_LOGE("Get CreateOAForDomain callbackRef failed"); - std::string errMsg = "The type of arg " + std::to_string(argc) + " must be function"; - AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr); - return false; + napi_typeof(env, argv[ARGS_SIZE_TWO], &valueType); + if (!GetCallbackProperty(env, argv[PARAMTWO], asyncContext->callbackRef, 1)) { + if (!ParseDomainOptionInfo(env, argv[PARAMTWO], asyncContext->domainOptions)) { + ACCOUNT_LOGE("Failed to get domainOptions"); + std::string errMsg = "The type of arg 3 must contains shortName"; + AccountNapiThrow(env, ERR_JS_PARAMETER_ERROR, errMsg, asyncContext->throwErr); + return false; + } } } @@ -592,7 +606,8 @@ void CreateOAForDomainExecuteCB(napi_env env, void *data) CreateOAForDomainAsyncContext *asyncContext = reinterpret_cast(data); auto callback = std::make_shared(env, asyncContext->callbackRef, asyncContext->deferred); asyncContext->errCode = - OsAccountManager::CreateOsAccountForDomain(asyncContext->type, asyncContext->domainInfo, callback); + OsAccountManager::CreateOsAccountForDomain(asyncContext->type, asyncContext->domainInfo, + callback, asyncContext->domainOptions); if (asyncContext->errCode != ERR_OK) { Parcel emptyParcel; callback->OnResult(asyncContext->errCode, emptyParcel); diff --git a/services/accountmgr/include/osaccount/iinner_os_account.h b/services/accountmgr/include/osaccount/iinner_os_account.h index 00f44c9de..b89d12d91 100644 --- a/services/accountmgr/include/osaccount/iinner_os_account.h +++ b/services/accountmgr/include/osaccount/iinner_os_account.h @@ -35,7 +35,7 @@ public: virtual ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) = 0; virtual ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) = 0; virtual ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const sptr &callback) = 0; + const sptr &callback, const CreateOsAccountForDomainOptions &options = {}) = 0; virtual ErrCode RemoveOsAccount(const int id) = 0; virtual ErrCode IsOsAccountExists(const int id, bool &isOsAccountExits) = 0; virtual ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) = 0; diff --git a/services/accountmgr/include/osaccount/iinner_os_account_manager.h b/services/accountmgr/include/osaccount/iinner_os_account_manager.h index e8970efbb..d167167f4 100644 --- a/services/accountmgr/include/osaccount/iinner_os_account_manager.h +++ b/services/accountmgr/include/osaccount/iinner_os_account_manager.h @@ -40,7 +40,7 @@ public: ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override; ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override; ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const sptr &callback) override; + const sptr &callback, const CreateOsAccountForDomainOptions &options = {}) override; ErrCode RemoveOsAccount(const int id) override; ErrCode IsOsAccountExists(const int id, bool &isOsAccountExits) override; ErrCode IsOsAccountActived(const int id, bool &isOsAccountActived) override; @@ -108,7 +108,7 @@ public: ErrCode GetBackgroundOsAccountLocalIds(std::vector &localIds) override; ErrCode SetOsAccountToBeRemoved(int32_t localId, bool toBeRemoved) override; ErrCode BindDomainAccount(const OsAccountType &type, const DomainAccountInfo &domainAccountInfo, - const sptr &callback); + const sptr &callback, const CreateOsAccountForDomainOptions &options = {}); ErrCode SendMsgForAccountCreate(OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {}); ErrCode GetOsAccountInfoById(const int id, OsAccountInfo &osAccountInfo); ErrCode UpdateAccountStatusForDomain(const int id, DomainAccountStatus status); diff --git a/services/accountmgr/include/osaccount/os_account_domain_account_callback.h b/services/accountmgr/include/osaccount/os_account_domain_account_callback.h index 5c4d3e8c8..d3a9dbe96 100644 --- a/services/accountmgr/include/osaccount/os_account_domain_account_callback.h +++ b/services/accountmgr/include/osaccount/os_account_domain_account_callback.h @@ -28,12 +28,13 @@ namespace AccountSA { class CheckAndCreateDomainAccountCallback final : public DomainAccountCallbackStub { public: CheckAndCreateDomainAccountCallback(const OsAccountType &type, const DomainAccountInfo &domainAccountInfo_, - const sptr &callback); + const sptr &callback, const CreateOsAccountForDomainOptions &accountOptions); void OnResult(const int32_t errCode, Parcel &parcel) override; private: OsAccountType type_; DomainAccountInfo domainAccountInfo_; + CreateOsAccountForDomainOptions accountOptions_; sptr innerCallback_ = nullptr; }; diff --git a/services/accountmgr/include/osaccount/os_account_manager_service.h b/services/accountmgr/include/osaccount/os_account_manager_service.h index baa20a460..2cfc40552 100644 --- a/services/accountmgr/include/osaccount/os_account_manager_service.h +++ b/services/accountmgr/include/osaccount/os_account_manager_service.h @@ -36,7 +36,7 @@ public: ErrCode CreateOsAccount(const std::string &localName, const std::string &shortName, const OsAccountType &type, OsAccountInfo &osAccountInfo, const CreateOsAccountOptions &options = {}) override; ErrCode CreateOsAccountForDomain(const OsAccountType &type, const DomainAccountInfo &domainInfo, - const sptr &callback) override; + const sptr &callback, const CreateOsAccountForDomainOptions &options = {}) override; ErrCode CreateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override; ErrCode UpdateOsAccountWithFullInfo(OsAccountInfo &osAccountInfo) override; diff --git a/services/accountmgr/src/osaccount/inner_os_account_manager.cpp b/services/accountmgr/src/osaccount/inner_os_account_manager.cpp index 78fd0a52a..eaad311c6 100644 --- a/services/accountmgr/src/osaccount/inner_os_account_manager.cpp +++ b/services/accountmgr/src/osaccount/inner_os_account_manager.cpp @@ -540,7 +540,7 @@ bool IInnerOsAccountManager::CheckDomainAccountBound( } ErrCode IInnerOsAccountManager::BindDomainAccount(const OsAccountType &type, const DomainAccountInfo &domainAccountInfo, - const sptr &callback) + const sptr &callback, const CreateOsAccountForDomainOptions &options) { std::vector osAccountInfos; (void)QueryAllCreatedOsAccounts(osAccountInfos); @@ -567,7 +567,7 @@ ErrCode IInnerOsAccountManager::BindDomainAccount(const OsAccountType &type, con } if (osAccountInfo.GetLocalId() != Constants::START_USER_ID) { #ifdef ENABLE_MULTIPLE_OS_ACCOUNTS - ErrCode errCode = PrepareOsAccountInfo(osAccountName, domainAccountInfo.accountName_, + ErrCode errCode = PrepareOsAccountInfo(osAccountName, options.shortName, type, domainAccountInfo, osAccountInfo); if (errCode != ERR_OK) { return errCode; @@ -588,7 +588,8 @@ ErrCode IInnerOsAccountManager::BindDomainAccount(const OsAccountType &type, con } ErrCode IInnerOsAccountManager::CreateOsAccountForDomain( - const OsAccountType &type, const DomainAccountInfo &domainInfo, const sptr &callback) + const OsAccountType &type, const DomainAccountInfo &domainInfo, const sptr &callback, + const CreateOsAccountForDomainOptions &options) { if (!pluginManager_.IsCreationAllowed()) { ACCOUNT_LOGI("Not allow creation account."); @@ -609,7 +610,7 @@ ErrCode IInnerOsAccountManager::CreateOsAccountForDomain( return ERR_DOMAIN_ACCOUNT_SERVICE_PLUGIN_NOT_EXIST; } sptr callbackWrapper = - new (std::nothrow) CheckAndCreateDomainAccountCallback(type, domainInfo, callback); + new (std::nothrow) CheckAndCreateDomainAccountCallback(type, domainInfo, callback, options); if (callbackWrapper == nullptr) { ACCOUNT_LOGE("new DomainCreateDomainCallback failed"); return ERR_ACCOUNT_COMMON_INSUFFICIENT_MEMORY_ERROR; diff --git a/services/accountmgr/src/osaccount/os_account_domain_account_callback.cpp b/services/accountmgr/src/osaccount/os_account_domain_account_callback.cpp index 8666a7285..97604d2b2 100644 --- a/services/accountmgr/src/osaccount/os_account_domain_account_callback.cpp +++ b/services/accountmgr/src/osaccount/os_account_domain_account_callback.cpp @@ -29,8 +29,9 @@ namespace OHOS { namespace AccountSA { CheckAndCreateDomainAccountCallback::CheckAndCreateDomainAccountCallback( - const OsAccountType &type, const DomainAccountInfo &domainAccountInfo, const sptr &callback) - : type_(type), domainAccountInfo_(domainAccountInfo), innerCallback_(callback) + const OsAccountType &type, const DomainAccountInfo &domainAccountInfo, + const sptr &callback, const CreateOsAccountForDomainOptions &accountOptions) + : type_(type), domainAccountInfo_(domainAccountInfo), accountOptions_(accountOptions), innerCallback_(callback) {} void CheckAndCreateDomainAccountCallback::OnResult(int32_t errCode, Parcel &parcel) @@ -60,7 +61,8 @@ void CheckAndCreateDomainAccountCallback::OnResult(int32_t errCode, Parcel &parc ACCOUNT_LOGE("domain account not found"); return innerCallback_->OnResult(ERR_JS_ACCOUNT_NOT_FOUND, resultParcel); } - errCode = IInnerOsAccountManager::GetInstance().BindDomainAccount(type_, domainAccountInfo, innerCallback_); + errCode = IInnerOsAccountManager::GetInstance().BindDomainAccount(type_, domainAccountInfo, + innerCallback_, accountOptions_); if (errCode != ERR_OK) { return innerCallback_->OnResult(errCode, resultParcel); } diff --git a/services/accountmgr/src/osaccount/os_account_manager_service.cpp b/services/accountmgr/src/osaccount/os_account_manager_service.cpp index e48f53815..367900489 100644 --- a/services/accountmgr/src/osaccount/os_account_manager_service.cpp +++ b/services/accountmgr/src/osaccount/os_account_manager_service.cpp @@ -209,7 +209,8 @@ ErrCode OsAccountManagerService::UpdateOsAccountWithFullInfo(OsAccountInfo &osAc } ErrCode OsAccountManagerService::CreateOsAccountForDomain(const OsAccountType &type, - const DomainAccountInfo &domainInfo, const sptr &callback) + const DomainAccountInfo &domainInfo, const sptr &callback, + const CreateOsAccountForDomainOptions &options) { ACCOUNT_LOGI("start"); // permission check @@ -224,22 +225,35 @@ ErrCode OsAccountManagerService::CreateOsAccountForDomain(const OsAccountType &t return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; } if (domainInfo.accountName_.empty() || domainInfo.domain_.empty()) { + ACCOUNT_LOGE("Domain account name is empty or domain is empty"); return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; } if (domainInfo.accountName_.size() > Constants::DOMAIN_ACCOUNT_NAME_MAX_SIZE || domainInfo.domain_.size() > Constants::DOMAIN_NAME_MAX_SIZE) { + ACCOUNT_LOGE("Domain account name is overlength or domain is overlength"); return ERR_ACCOUNT_COMMON_INVALID_PARAMETER; } + if (options.hasShortName || (options.shortName != "")) { + ErrCode code = innerManager_.ValidateShortName(options.shortName); + if (code != ERR_OK) { + ACCOUNT_LOGE("Failed to create os account for domain, shortName=%{public}s is invalid!", + options.shortName.c_str()); + return code; + } + } + bool isAllowedCreateAdmin = false; ErrCode errCode = innerManager_.IsAllowedCreateAdmin(isAllowedCreateAdmin); if (errCode != ERR_OK) { + ACCOUNT_LOGE("Failed to get allowed create admin permission, code=%{public}d.", errCode); return errCode; } if (!isAllowedCreateAdmin && type == OsAccountType::ADMIN) { + ACCOUNT_LOGE("Do not allowed create admin."); return ERR_OSACCOUNT_SERVICE_MANAGER_CREATE_OSACCOUNT_TYPE_ERROR; } - return innerManager_.CreateOsAccountForDomain(type, domainInfo, callback); + return innerManager_.CreateOsAccountForDomain(type, domainInfo, callback, options); } ErrCode OsAccountManagerService::RemoveOsAccount(const int id) diff --git a/services/accountmgr/src/osaccount/os_account_stub.cpp b/services/accountmgr/src/osaccount/os_account_stub.cpp index 124153ef4..d3386ef49 100644 --- a/services/accountmgr/src/osaccount/os_account_stub.cpp +++ b/services/accountmgr/src/osaccount/os_account_stub.cpp @@ -622,7 +622,13 @@ ErrCode OsAccountStub::ProcCreateOsAccountForDomain(MessageParcel &data, Message ACCOUNT_LOGE("failed to read parcel"); return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR; } - ErrCode result = CreateOsAccountForDomain(type, *info, callback); + + sptr options = data.ReadParcelable(); + if (options == nullptr) { + ACCOUNT_LOGE("Read options failed"); + return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR; + } + ErrCode result = CreateOsAccountForDomain(type, *info, callback, *options); return WriteResultWithOsAccountInfo(reply, result, osAccountInfo); } diff --git a/services/accountmgr/test/unittest/os_account/os_account_domain_account_callback_test.cpp b/services/accountmgr/test/unittest/os_account/os_account_domain_account_callback_test.cpp index b5a492504..7c515d05f 100644 --- a/services/accountmgr/test/unittest/os_account/os_account_domain_account_callback_test.cpp +++ b/services/accountmgr/test/unittest/os_account/os_account_domain_account_callback_test.cpp @@ -66,7 +66,9 @@ void DomainAccountCallbackTest::TearDown(void) HWTEST_F(DomainAccountCallbackTest, DomainAccountCallbackTest_OnResult_001, TestSize.Level0) { DomainAccountInfo info; - auto callbackPtr = std::make_shared(OsAccountType::NORMAL, info, nullptr); + CreateOsAccountForDomainOptions accountOptions; + auto callbackPtr = std::make_shared(OsAccountType::NORMAL, + info, nullptr, accountOptions); Parcel parcel; callbackPtr->OnResult(0, parcel); EXPECT_EQ(callbackPtr->innerCallback_, nullptr);