openharmony 鸿蒙 js-apis-enterprise-accountManager-sys

2025-06-12 浏览 (1)

@ohos.enterprise.accountManager (Account Management) (System API)

The accountManager module provides APIs for account management of enterprise devices.

NOTE

  • The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.

  • The APIs of this module can be used only in the stage model.

  • The APIs of this module can be called only by a device administrator application that is enabled.

  • This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.enterprise.accountManager.

Modules to Import

import { accountManager } from '@kit.MDMKit';

accountManager.disallowAddLocalAccount

disallowAddLocalAccount(admin: Want, disallow: boolean, callback: AsyncCallback<void>): void

Disallows a device to create local user accounts. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
disallowbooleanYesWhether to forbid the creation of local user accounts. The value true means the creation of local user accounts is forbidden, and the value false means the opposite.
callbackAsyncCallback<void>YesCallback invoked to return the result. If the operation is successful, err is null. Otherwise, err is an error object.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

accountManager.disallowAddLocalAccount(wantTemp, true, (err) => {
  if (err) {
    console.error(`Failed to disallow add local account. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info('Succeeded in disallowing add local account');
});

accountManager.disallowAddLocalAccount

disallowAddLocalAccount(admin: Want, disallow: boolean): Promise<void>

Disallows a device to create local user accounts. This API uses a promise to return the result.

Required permissions: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
disallowbooleanYesWhether to forbid the creation of local user accounts. The value true means the creation of local user accounts is forbidden, and the value false means the opposite.

Return value

TypeDescription
Promise<void>Promise that returns no value. An error object will be thrown if the operation fails.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { Want } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

accountManager.disallowAddLocalAccount(wantTemp, true).then(() => {
  console.info('Succeeded in disallowing add local account');
}).catch((err: BusinessError) => {
  console.error(`Failed to disallow add local account. Code: ${err.code}, message: ${err.message}`);
});

accountManager.disallowAddOsAccountByUser11+

disallowAddOsAccountByUser(admin: Want, userId: number, disallow: boolean): void

Disallows a user to add accounts.

Required permissions: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
userIdnumberYesUser ID, which must be greater than or equal to 0.
disallowbooleanYesWhether to disallow the user to add system accounts. The value true means to disallow the user to add system accounts; the value false means the opposite.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  accountManager.disallowAddOsAccountByUser(wantTemp, 100, true);
  console.info(`Succeeded in disallowing user add os account`);
} catch (err) {
  console.error(`Failed to disallow user add os account. Code: ${err.code}, message: ${err.message}`);
}

accountManager.isAddOsAccountByUserDisallowed11+

isAddOsAccountByUserDisallowed(admin: Want, userId: number): boolean

Queries whether to disallow a user to add accounts.

Required permissions: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
userIdnumberYesUser ID, which must be greater than or equal to 0.

Return value

TypeDescription
booleanReturns true if the user is not allowed to add system accounts; returns false otherwise.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { Want } from '@kit.AbilityKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let isDisallowed: boolean = accountManager.isAddOsAccountByUserDisallowed(wantTemp, 100);
  console.info(`Succeeded in querying the user can add os account or not: ${isDisallowed}`);
} catch (err) {
  console.error(`Failed to query the user can add os account or not. Code: ${err.code}, message: ${err.message}`);
}

accountManager.addOsAccount11+

addOsAccount(admin: Want, name: string, type: osAccount.OsAccountType): osAccount.OsAccountInfo

Adds an account in the background.

Required permissions: ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY

System capability: SystemCapability.Customization.EnterpriseDeviceManager

Parameters

NameTypeMandatoryDescription
adminWantYesEnterpriseAdminExtensionAbility.
namestringYesUser ID, which must be greater than or equal to 0.
typeosAccount.OsAccountTypeYesType of the account to add.
The value can be any of the following:
· ADMIN: administrator account.
· NORMAL: normal account.
· GUEST: guest account.

Return value

TypeDescription
osAccount.OsAccountInfoInformation about the account added.

Error codes

For details about the error codes, see Enterprise Device Management Error Codes and Universal Error Codes.

IDError Message
9200001The application is not an administrator application of the device.
9200002The administrator application does not have permission to manage the device.
9201003Failed to add an OS account.
201Permission verification failed. The application does not have the permission required to call the API.
202Permission verification failed. A non-system application calls a system API.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

Example

import { Want } from '@kit.AbilityKit';
import { osAccount } from '@kit.BasicServicesKit';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let info: osAccount.OsAccountInfo = accountManager.addOsAccount(wantTemp, "TestAccountName", osAccount.OsAccountType.NORMAL);
  console.info(`Succeeded in creating os account: ${JSON.stringify(info)}`);
} catch (err) {
  console.error(`Failed to creating os account. Code: ${err.code}, message: ${err.message}`);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙MDM Kit

harmony 鸿蒙Enterprise Device Management Error Codes

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (EnterpriseAdminExtensionAbility)

harmony 鸿蒙@ohos.enterprise.accountManager (Account Management)

harmony 鸿蒙@ohos.enterprise.adminManager (Enterprise Device Management) (System API)

harmony 鸿蒙@ohos.enterprise.adminManager (Enterprise Device Management)

harmony 鸿蒙@ohos.enterprise.applicationManager (Application Management (System API)

harmony 鸿蒙@ohos.enterprise.applicationManager (Application Management)

harmony 鸿蒙@ohos.enterprise.bluetoothManager (Bluetooth Management) (System API)

harmony 鸿蒙@ohos.enterprise.bluetoothManager (Bluetooth Management)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/6AaqWc