1# @ohos.enterprise.accountManager(账户管理)(系统接口) 2<!--Kit: MDM Kit--> 3<!--Subsystem: Customization--> 4<!--Owner: @huanleima--> 5<!--Designer: @liuzuming--> 6<!--Tester: @lpw_work--> 7<!--Adviser: @Brilliantry_Rui--> 8 9本模块提供设备帐户管理能力,包括禁止创建本地用户等。 10 11> **说明:** 12> 13> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> 本模块接口仅可在Stage模型下使用。 16> 17> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-term.md#mdm应用设备管理应用)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin-2)后调用。 18> 19> 当前页面仅包含本模块的系统接口,其他公开接口参见。其他公开接口参见[@ohos.enterprise.accountManager](js-apis-enterprise-accountManager.md)。 20 21## 导入模块 22 23```ts 24import { accountManager } from '@kit.MDMKit'; 25``` 26 27## accountManager.disallowAddLocalAccount 28 29disallowAddLocalAccount(admin: Want, disallow: boolean, callback: AsyncCallback<void>): void 30 31禁止设备创建本地用户。使用callback异步回调。 32 33**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY 34 35**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 36 37**模型约束:** 此接口仅可在Stage模型下使用。 38 39**系统接口:** 此接口为系统接口。 40 41**参数:** 42 43| 参数名 | 类型 | 必填 | 说明 | 44| -------- | ---------------------------------------- | ---- | ------------------------------- | 45| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 46| disallow | boolean | 是 | 是否禁止创建本地用户,true表示禁止创建,false表示允许创建。 | 47| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则为错误对象。 | 48 49**错误码**: 50 51以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 52 53| 错误码ID | 错误信息 | 54| ------- | ---------------------------------------------------------------------------- | 55| 9200001 | The application is not an administrator application of the device. | 56| 9200002 | The administrator application does not have permission to manage the device. | 57| 201 | Permission verification failed. The application does not have the permission required to call the API. | 58| 202 | Permission verification failed. A non-system application calls a system API. | 59| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 60 61**示例:** 62 63```ts 64import { accountManager } from '@kit.MDMKit'; 65import { Want } from '@kit.AbilityKit'; 66 67let wantTemp: Want = { 68 // 需根据实际情况进行替换 69 bundleName: 'com.example.myapplication', 70 abilityName: 'EntryAbility' 71}; 72 73accountManager.disallowAddLocalAccount(wantTemp, true, (err) => { 74 if (err) { 75 console.error(`Failed to disallow add local account. Code: ${err.code}, message: ${err.message}`); 76 return; 77 } 78 console.info('Succeeded in disallowing add local account'); 79}); 80``` 81 82## accountManager.disallowAddLocalAccount 83 84disallowAddLocalAccount(admin: Want, disallow: boolean): Promise<void> 85 86禁止设备创建本地用户。使用promise异步回调。 87 88**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY 89 90**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 91 92**模型约束:** 此接口仅可在Stage模型下使用。 93 94**系统接口:** 此接口为系统接口。 95 96**参数:** 97 98| 参数名 | 类型 | 必填 | 说明 | 99| ----- | ----------------------------------- | ---- | ------- | 100| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 101| disallow | boolean | 是 | 是否禁止创建本地用户,true表示禁止创建,false表示允许创建。 | 102 103**返回值:** 104 105| 类型 | 说明 | 106| --------------------- | ------------------------- | 107| Promise<void> | 无返回结果的Promise对象。当禁止创建本地用户失败时,抛出错误对象。 | 108 109**错误码**: 110 111以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 112 113| 错误码ID | 错误信息 | 114| ------- | ---------------------------------------------------------------------------- | 115| 9200001 | The application is not an administrator application of the device. | 116| 9200002 | The administrator application does not have permission to manage the device. | 117| 201 | Permission verification failed. The application does not have the permission required to call the API. | 118| 202 | Permission verification failed. A non-system application calls a system API. | 119| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 120 121**示例:** 122 123```ts 124import { accountManager } from '@kit.MDMKit'; 125import { Want } from '@kit.AbilityKit'; 126import { BusinessError } from '@kit.BasicServicesKit'; 127 128let wantTemp: Want = { 129 // 需根据实际情况进行替换 130 bundleName: 'com.example.myapplication', 131 abilityName: 'EntryAbility' 132}; 133 134accountManager.disallowAddLocalAccount(wantTemp, true).then(() => { 135 console.info('Succeeded in disallowing add local account'); 136}).catch((err: BusinessError) => { 137 console.error(`Failed to disallow add local account. Code: ${err.code}, message: ${err.message}`); 138}); 139``` 140 141## accountManager.disallowAddOsAccountByUser<sup>11+</sup> 142 143disallowAddOsAccountByUser(admin: Want, userId: number, disallow: boolean): void 144 145禁止用户添加账号。 146 147**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY 148 149**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 150 151**模型约束:** 此接口仅可在Stage模型下使用。 152 153**系统接口:** 此接口为系统接口。 154 155**参数:** 156 157| 参数名 | 类型 | 必填 | 说明 | 158| -------- | ------------------------------------------------------- | ---- | ----------------------------------------------------------- | 159| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 160| userId | number | 是 | 用户ID,指定具体用户,取值范围:大于等于0。 | 161| disallow | boolean | 是 | 是否禁止用户添加账号,true表示禁止添加,false表示允许添加。 | 162 163**错误码**: 164 165以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 166 167| 错误码ID | 错误信息 | 168| -------- | ------------------------------------------------------------ | 169| 9200001 | The application is not an administrator application of the device. | 170| 9200002 | The administrator application does not have permission to manage the device. | 171| 201 | Permission verification failed. The application does not have the permission required to call the API. | 172| 202 | Permission verification failed. A non-system application calls a system API. | 173| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 174 175**示例:** 176 177```ts 178import { accountManager } from '@kit.MDMKit'; 179import { Want } from '@kit.AbilityKit'; 180 181let wantTemp: Want = { 182 // 需根据实际情况进行替换 183 bundleName: 'com.example.myapplication', 184 abilityName: 'EntryAbility' 185}; 186 187try { 188 // 参数需根据实际情况进行替换 189 accountManager.disallowAddOsAccountByUser(wantTemp, 100, true); 190 console.info(`Succeeded in disallowing user add os account`); 191} catch (err) { 192 console.error(`Failed to disallow user add os account. Code: ${err.code}, message: ${err.message}`); 193} 194``` 195 196## accountManager.isAddOsAccountByUserDisallowed<sup>11+</sup> 197 198isAddOsAccountByUserDisallowed(admin: Want, userId: number): boolean 199 200查询是否禁止某用户添加账号。 201 202**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY 203 204**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 205 206**模型约束:** 此接口仅可在Stage模型下使用。 207 208**系统接口:** 此接口为系统接口。 209 210**参数:** 211 212| 参数名 | 类型 | 必填 | 说明 | 213| ------ | ------------------------------------------------------- | ---- | ------------------------------------------- | 214| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 215| userId | number | 是 | 用户ID,指定具体用户,取值范围:大于等于0。 | 216 217**返回值:** 218 219| 类型 | 说明 | 220| ------- | ------------------------------------------------------------ | 221| boolean | 返回true表示该用户禁止添加账号。<br/>返回false表示该用户允许添加账号。 | 222 223**错误码**: 224 225以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 226 227| 错误码ID | 错误信息 | 228| -------- | ------------------------------------------------------------ | 229| 9200001 | The application is not an administrator application of the device. | 230| 9200002 | The administrator application does not have permission to manage the device. | 231| 201 | Permission verification failed. The application does not have the permission required to call the API. | 232| 202 | Permission verification failed. A non-system application calls a system API. | 233| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 234 235**示例:** 236 237```ts 238import { accountManager } from '@kit.MDMKit'; 239import { Want } from '@kit.AbilityKit'; 240 241let wantTemp: Want = { 242 // 需根据实际情况进行替换 243 bundleName: 'com.example.myapplication', 244 abilityName: 'EntryAbility' 245}; 246 247try { 248 // 参数需根据实际情况进行替换 249 let isDisallowed: boolean = accountManager.isAddOsAccountByUserDisallowed(wantTemp, 100); 250 console.info(`Succeeded in querying the user can add os account or not: ${isDisallowed}`); 251} catch (err) { 252 console.error(`Failed to query the user can add os account or not. Code: ${err.code}, message: ${err.message}`); 253} 254``` 255 256## accountManager.addOsAccount<sup>11+</sup> 257 258addOsAccount(admin: Want, name: string, type: osAccount.OsAccountType): osAccount.OsAccountInfo 259 260后台添加账号。 261 262**需要权限:** ohos.permission.ENTERPRISE_SET_ACCOUNT_POLICY 263 264**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 265 266**模型约束:** 此接口仅可在Stage模型下使用。 267 268**系统接口:** 此接口为系统接口。 269 270**参数:** 271 272| 参数名 | 类型 | 必填 | 说明 | 273| ------ | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 274| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 企业设备管理扩展组件。 | 275| name | string | 是 | 用户ID,指定具体用户,取值范围:大于等于0。 | 276| type | [osAccount.OsAccountType](../apis-basic-services-kit/js-apis-osAccount.md#osaccounttype) | 是 | 要添加的账号的类型。<br/>取值范围:ADMIN、NORMAL、GUEST。<br/>· ADMIN:管理员帐号。<br/>· NORMAL:普通账号。<br/>· GUEST:访客账号。 | 277 278**返回值:** 279 280| 类型 | 说明 | 281| ------------------------------------------------------------ | -------------------- | 282| [osAccount.OsAccountInfo](../apis-basic-services-kit/js-apis-osAccount.md#osaccounttype) | 返回添加的账号信息。 | 283 284**错误码**: 285 286以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 287 288| 错误码ID | 错误信息 | 289| -------- | ------------------------------------------------------------ | 290| 9200001 | The application is not an administrator application of the device. | 291| 9200002 | The administrator application does not have permission to manage the device. | 292| 9201003 | Failed to add an OS account. | 293| 201 | Permission verification failed. The application does not have the permission required to call the API. | 294| 202 | Permission verification failed. A non-system application calls a system API. | 295| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 296 297**示例:** 298 299```ts 300import { accountManager } from '@kit.MDMKit'; 301import { Want } from '@kit.AbilityKit'; 302import { osAccount } from '@kit.BasicServicesKit'; 303 304let wantTemp: Want = { 305 // 需根据实际情况进行替换 306 bundleName: 'com.example.myapplication', 307 abilityName: 'EntryAbility' 308}; 309 310try { 311 // 参数需根据实际情况进行替换 312 let info: osAccount.OsAccountInfo = accountManager.addOsAccount(wantTemp, "TestAccountName", osAccount.OsAccountType.NORMAL); 313 console.info(`Succeeded in creating os account: ${JSON.stringify(info)}`); 314} catch (err) { 315 console.error(`Failed to creating os account. Code: ${err.code}, message: ${err.message}`); 316} 317``` 318 319