Skip to main content

assignRoleToGroup

The assignRoleToGroup function allows you to assign a specific role to a group within a permission management system. This function returns a JSON object with role and group details.

Parameters:

  • groupId (required): the unique identifier of the group to which the role will be assigned.

  • roleId (required): unique identifier of the role that will be assigned to the group.

  • address (optional): address of the group. This parameter is optional and depends on whether the address is required for the assignment.

  • seed (optional): transaction signing seed of the group. This parameter is optional and depends on whether the seed is required for the assignment.

Code example


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

/**
* Assigns a specific role to a group in a permission management system.
* @param {string} groupId - The unique identifier of the group to which the role will be assigned.
* @param {string} roleId - The unique identifier of the role that will be assigned to the group.
* @param {string} [address] - The address of the group. Optional if address is not required.
* @param {string} [seed] - The transaction signing seed of the group. Optional if seed is not required.
* @returns {Promise<object>} - A promise that resolves when the role is successfully assign to group.
*/
async function assignRoleToGroup(groupId, roleId, seed) {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed
});

try {
// Perform the role assignment operation using the SDK's rbac.assignRoleToGroup method
const result = await sdkInstance.rbac.assignRoleToGroup({
groupId,
roleId,
});

return result
} catch (error) {
console.error(`Error assigning role to group: ${error}`);
} finally {
await sdkInstance.disconnect();
}
}

// Example usage
const groupId = "02709409-kd01-4444-8222-b5zz8512";
const roleId = "78709009-cf06-4224-8632-b5ee8512";
const seed = "your-seed-phrase";

const response = assignRoleToGroup(groupId, roleId, seed);

console.log(response.message);

Response object


{
message: "Successfully assign role 78709009-cf06-4224-8632-b5ee8512 to group 02709409-kd01-4444-8222-b5zz8512"
}