Fix export button visibility by normalizing team role for RBAC checks.
Sync team_role from the API into session storage and resolve export permissions using canonical role keys so production sessions match localhost behavior. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
c53e95ff1e
commit
8294b3fcf4
|
|
@ -1,4 +1,5 @@
|
||||||
import http from "./http"
|
import http from "./http"
|
||||||
|
import { syncSessionTeamRole } from "../lib/teamRole"
|
||||||
import type {
|
import type {
|
||||||
AcceptInvitationRequest,
|
AcceptInvitationRequest,
|
||||||
AcceptInvitationResponse,
|
AcceptInvitationResponse,
|
||||||
|
|
@ -106,7 +107,10 @@ export async function fetchCurrentTeamMemberPermissions(): Promise<string[]> {
|
||||||
.map((entry) => normalizeTeamMember(entry))
|
.map((entry) => normalizeTeamMember(entry))
|
||||||
.filter((entry): entry is TeamMember => entry != null)
|
.filter((entry): entry is TeamMember => entry != null)
|
||||||
const self = members.find((member) => member.id === memberId)
|
const self = members.find((member) => member.id === memberId)
|
||||||
if (self) return self.permissions ?? []
|
if (self) {
|
||||||
|
syncSessionTeamRole(self.team_role)
|
||||||
|
return self.permissions ?? []
|
||||||
|
}
|
||||||
page++
|
page++
|
||||||
} while (page <= totalPages)
|
} while (page <= totalPages)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { useCallback, useEffect, useState } from "react"
|
import { useCallback, useEffect, useState } from "react"
|
||||||
import { fetchCurrentTeamMemberPermissions } from "../api/team.api"
|
import { fetchCurrentTeamMemberPermissions } from "../api/team.api"
|
||||||
import { getMyProfile } from "../api/users.api"
|
import { getMyProfile } from "../api/users.api"
|
||||||
|
import { syncSessionTeamRole } from "../lib/teamRole"
|
||||||
import type { TeamMeProfile } from "../types/team.types"
|
import type { TeamMeProfile } from "../types/team.types"
|
||||||
|
|
||||||
export function useTeamPermissions() {
|
export function useTeamPermissions() {
|
||||||
|
|
@ -18,6 +19,7 @@ export function useTeamPermissions() {
|
||||||
|
|
||||||
const res = await getMyProfile()
|
const res = await getMyProfile()
|
||||||
const profile = res.data?.data as TeamMeProfile & { permissions?: string[] }
|
const profile = res.data?.data as TeamMeProfile & { permissions?: string[] }
|
||||||
|
syncSessionTeamRole(profile?.team_role)
|
||||||
setPermissions(Array.isArray(profile?.permissions) ? profile.permissions : [])
|
setPermissions(Array.isArray(profile?.permissions) ? profile.permissions : [])
|
||||||
} catch {
|
} catch {
|
||||||
setPermissions([])
|
setPermissions([])
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { getSessionTeamRole } from "./teamRole"
|
import { getNormalizedSessionTeamRole } from "./teamRole"
|
||||||
|
|
||||||
const ALL_EXPORT_PERMISSIONS = [
|
const ALL_EXPORT_PERMISSIONS = [
|
||||||
"payments.export",
|
"payments.export",
|
||||||
|
|
@ -18,6 +18,6 @@ const EXPORT_ROLE_PERMISSIONS: Record<string, readonly string[]> = {
|
||||||
|
|
||||||
export function hasExportPermission(permission: string, permissions: string[]): boolean {
|
export function hasExportPermission(permission: string, permissions: string[]): boolean {
|
||||||
if (permissions.includes(permission)) return true
|
if (permissions.includes(permission)) return true
|
||||||
const rolePerms = EXPORT_ROLE_PERMISSIONS[getSessionTeamRole().toUpperCase()]
|
const rolePerms = EXPORT_ROLE_PERMISSIONS[getNormalizedSessionTeamRole()]
|
||||||
return rolePerms?.includes(permission) ?? false
|
return rolePerms?.includes(permission) ?? false
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,41 @@
|
||||||
|
import { getAccessToken } from "./teamAuthStorage"
|
||||||
|
import { rbacRoleNameToTeamRole } from "./teamRoles"
|
||||||
|
|
||||||
|
function decodeJwtPayload(token: string): Record<string, unknown> | null {
|
||||||
|
try {
|
||||||
|
const payloadPart = token.split(".")[1]
|
||||||
|
if (!payloadPart) return null
|
||||||
|
const normalized = payloadPart.replace(/-/g, "+").replace(/_/g, "/")
|
||||||
|
const padded = normalized.padEnd(Math.ceil(normalized.length / 4) * 4, "=")
|
||||||
|
return JSON.parse(atob(padded)) as Record<string, unknown>
|
||||||
|
} catch {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function getSessionTeamRole(): string {
|
export function getSessionTeamRole(): string {
|
||||||
return localStorage.getItem("role")?.trim() ?? ""
|
return localStorage.getItem("role")?.trim() ?? ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Canonical team role key (e.g. ADMIN) for RBAC checks. */
|
||||||
|
export function getNormalizedSessionTeamRole(): string {
|
||||||
|
const stored = getSessionTeamRole()
|
||||||
|
if (stored) return rbacRoleNameToTeamRole(stored)
|
||||||
|
|
||||||
|
const token = getAccessToken()
|
||||||
|
if (!token) return ""
|
||||||
|
|
||||||
|
const payload = decodeJwtPayload(token)
|
||||||
|
const fromJwt = payload?.team_role ?? payload?.role
|
||||||
|
if (typeof fromJwt === "string" && fromJwt.trim()) {
|
||||||
|
return rbacRoleNameToTeamRole(fromJwt)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
export function syncSessionTeamRole(teamRole: string | undefined | null): void {
|
||||||
|
const next = teamRole?.trim()
|
||||||
|
if (!next) return
|
||||||
|
localStorage.setItem("role", next)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user