openharmony 鸿蒙 control-os-account-by-constraints-sys

2026-08-25 浏览 (1)

Applying Constraints for System Accounts (for System Applications Only)

The account module provides a role-based access control mechanism. You can set constraints for system accounts to restrict their behaviors.

Constraints

For details about the predefined account constraints, see Constraints.

Getting Started

  1. Request the ohos.permission.MANAGE_LOCAL_ACCOUNTS permission. For details, see Requesting Permissions for system_basic Applications.

  2. Import the osAccount module.

import { osAccount, BusinessError } from '@kit.BasicServicesKit';
  1. Obtain an AccountManager instance.

let accountManager = osAccount.getAccountManager();

Setting Constraints for a System Account

The user can set constraints to restrict the system account behaviors. For example, the user can enable mobile guardian for children to prevent the kids to use Wi-Fi or install applications.

Procedure

  1. Specify the system account ID and the constraints.

    let localId: number = 100;
    let constraint: string[] = [ 'constraint.wifi.set' ];
  1. Use setOsAccountConstraints to enable the constraints for the system account.

    try {
      accountManager.setOsAccountConstraints(localId, constraint, true);
      console.info('setOsAccountConstraints successfully');
	// ···
    } catch (e) {
      const err = e as BusinessError;
      console.error(`setOsAccountConstraints failed, error: code is ${err.code}, message is ${err.message}`);
	// ···
    }

Checking Whether a Constraint Can be Enabled for a System Account

Before a constraint is enabled for a system account, the application needs to check whether the constraint can be enabled. You can use isOsAccountConstraintEnabled to implement this operation.

Procedure

  1. Set the system account ID and constraint.

    let localId: number = 100;
    let constraint: string = 'constraint.wifi.set';
  1. Use isOsAccountConstraintEnabled to check whether the constraint can be enabled for the system account.

    accountManager.isOsAccountConstraintEnabled(localId, constraint).then((isEnabled: boolean) => {
      if (isEnabled) {
        // your business logic
		// ···
      }
    });

你可能感兴趣的鸿蒙文章

openharmony 鸿蒙 manage-distributed-account-sys

openharmony 鸿蒙 account-overview-sys

openharmony 鸿蒙 manage-domain-account-sys

openharmony 鸿蒙 auth-domain-account-sys

openharmony 鸿蒙 Readme-EN

openharmony 鸿蒙 manage-os-account-credential-sys

openharmony 鸿蒙 manage-os-account-sys

openharmony 鸿蒙 manage-application-account

openharmony 鸿蒙 manage-domain-plugin-sys

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