Skip to content

Centralize role comparison logic #3301

Description

@david-crespo

roleOrder gives each role a number where lower = stronger (admin is 0). That makes direct comparisons on it read backwards: roleOrder[a] <= roleOrder[b] means "a is at least as strong as b", the opposite of what the <= looks like it's saying.

console/app/api/roles.ts

Lines 31 to 38 in 1b740d3

// This is a record only to ensure that all RoleKey are covered. weird order
// on purpose so allRoles test can confirm sorting works
export const roleOrder: Record<RoleKey, number> = {
collaborator: 1,
admin: 0,
viewer: 3,
limited_collaborator: 2,
}

We do role strength comparison using roleOrder inline at several call sites. I don't think call sites should be doing the order comparison directly. We already have getEffectiveRole (strongest of a list); the boolean comparisons want a helper with a name clear enough that it would be hard to use backwards — roleIsAtLeast(a, b) or a compareRoles comparator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions