Skip to main content

assignRoleToUser

The assignRoleToUser function allows to assign a specific role to a user in a permission management system. This function returns a JSON object with role and user details.

Parameters:

  • userId (required): the unique identifier of the user to whom the role will be assigned.

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

  • address (optional): address of the user. This can be useful if the user's blockchain address is required for the assignment process.

  • seed (optional): another optional parameter that can be used if the user's transaction signing seed is needed to carry out the assignment.

Code example


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

/**
* Assigns a role to a user in the peaq network's RBAC system.
* @param {string} userId - The ID of the user to whom the role is being assigned.
* @param {string} roleId - The ID of the role to be assigned.
* @param {string} [address] - The address of the user (optional).
* @param {string} [seed] - The seed for signing transactions (optional).
* @returns {Promise<object>} - A promise that resolves when the role is successfully assigned.
*/
const assignRoleToUser = async (userId, roleId, seed) => {
const sdkInstance = await Sdk.createInstance({
baseUrl: "wss://wsspc1-qa.agung.peaq.network",
seed
});
try {
await sdkInstance.connect();
const result = await sdkInstance.rbac.assignRoleToUser({
roleId: roleId,
userId: userId
});

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

// Example usage
const userId = "44759999-az06-2112-7130-b5kk4321";
const roleId = "78709009-cf06-4224-8632-b5ee8512";
const seed = "your-seed-phrase";

assignRoleToUser(userId, roleId, seed)
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error("Error assigning role to user:", error);
});


Response object


{
message: "Successfully assign role 78709009-cf06-4224-8632-b5ee8512 to user 44759999-az06-2112-7130-b5kk4321"
}