export function assignPrincipalToRole()

in package/src/rbac.ts [107:127]


export function assignPrincipalToRole(roleId: string, principalId: string): IdentityResponse {
    if (roleExists(roleId) && principalExists(principalId)) {
        if (!roleAssignments.has(principalId)) {
            roleAssignments.set(principalId, JSON.parse(`{"roles": [ "${roleId}" ]}`));
        } else {
            let assignment = roleAssignments.get(principalId);
            if (assignment !== undefined) {
                assignment?.roles.push(roleId);
                roleAssignments.set(principalId, assignment);
            }
        }
        return { message: "Role assigned successfully" };
    } else {
        if (!roleExists(roleId)) {
            return { message: "Role assignment failed, role id passed does not exist." };
        } else if (!principalExists(principalId)) {
            return { message: "Role assignment failed, principal id passed does not exist." };
        }
    }
    return { message: "Role assignment failed" };
}