harmony 鸿蒙Applying Constraints for System Accounts
Applying Constraints for System Accounts
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.
Before You Start
Request the ohos.permission.MANAGE_LOCAL_ACCOUNTS permission. For details, see Requesting Permissions for system_basic Applications.
Import the osAccount module.
import { osAccount } from '@kit.BasicServicesKit';
- 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
- Specify the system account ID and the constraints.
let localId: number = 100;
let constraint: string[] = [ 'constraint.wifi.set' ];
- Use setOsAccountConstraints to enable the constraints for the system account.
try {
accountManager.setOsAccountConstraints(localId, constraint, true);
console.log('setOsAccountConstraints successfully');
} catch (err) {
console.log('setOsAccountConstraints failed, error: ' + JSON.stringify(err));
}
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
- Set the system account ID and constraint.
let localId: number = 100;
let constraint: string = 'constraint.wifi.set';
- Use isOsAccountConstraintEnabled to check whether the constraint can be enabled for the system account.
let isEnabled: boolean = await accountManager.isOsAccountConstraintEnabled(localId, constraint);
if (isEnabled) {
// Your business logic
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Account Management Overview
harmony 鸿蒙Authenticating a Domain Account (for System Applications Only)
harmony 鸿蒙Managing Application Accounts
harmony 鸿蒙Managing Distributed Accounts (for System Applications Only)
harmony 鸿蒙Managing Domain Accounts (for System Applications Only)
harmony 鸿蒙Managing Domain Account Plugins (for System Applications Only)
harmony 鸿蒙Managing System Account Credentials (for System Application Only)
harmony 鸿蒙Managing System Accounts (for System Applications Only)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦