Skip to main content

assignPermissionToRole

assignPermissionToRole function assigns a permission to a role within the RBAC (Role-Based Access Control) system on the PEAQ network. This function returns permission and role IDs.

Parameters:

  • permissionId (string, required): ID of the permission to be assigned to the role.

  • roleId (string, required): ID of the role to which the permission will be assigned.

  • address (Address, optional): address associated with the operation (not required).

  • seed (string, optional): mnemonic seed used for authentication and connection (not required).

Code example


import { Sdk } from "@peaq-network/sdk";

/**
* Assigns a permission to a role within the RBAC system.
* @param {string} permissionId - The ID of the permission to be assigned (required).
* @param {string} roleId - The ID of the role to which the permission will be assigned (required).
* @param {Address} [address] - The address associated with the operation (not required).
* @param {string} [seed] - The mnemonic seed (not required).
* @returns {Promise<object>} - An object with a 'message' field indicating the success of the operation.
*/
const assignPermissionToRole = async (permissionId, roleId, seed) => {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed,
});

try {
await sdkInstance.connect();
const result = await sdkInstance.rbac.assignPermissionToRole({
permissionId: permissionId,
roleId: roleId,
});

return result;
} finally {
await sdkInstance.disconnect();
}
};

// Example usage
const permissionId = "18709409-bc06-4444-2222-b4zz0000"; // Replace with the actual permission ID
const roleId = "78709009-cf06-4224-8632-b5ee8512"; // Replace with the actual role ID
const seed = "your-seed-phrase";

assignPermissionToRole(permissionId, roleId, seed)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error("Error assigning permission to role:", error);
});


Response object


{
message: "Successfully assign permission 18709409-bc06-4444-2222-b4zz0000 to role 78709009-cf06-4224-8632-b5ee8512"
}