mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +00:00
## Context Building on top of https://github.com/supabase/supabase/pull/54192, there was a missing piece to the JIT DB access UI which was showing the assigned access to the user if any (On staging it just shows an error, on the previous PR we show a "Contact your project admin" CTA). So this PR addresses that - there's a couple of cases to consider and I'd love for design to feedback / adjust as well as I'm not convinced this is the best way to do things. Am deliberately leaving out the "copyable connection strings" part for now as Danny's investigating into that one ### If you're a non-owner If not assigned a role: <img width="500" alt="image" src="https://github.com/user-attachments/assets/111d7c4f-0083-4bd5-88d5-ec45974ccf4d" /> If assigned a single role: <img width="500" alt="image" src="https://github.com/user-attachments/assets/1b4b1f3c-9a20-4010-bff8-7bf68a455e26" /> If assigned multiple roles: <img width="500" alt="image" src="https://github.com/user-attachments/assets/42cc66f6-4604-42eb-ab30-f58b1396ca6b" /> <img width="500" alt="image" src="https://github.com/user-attachments/assets/0914e1a3-9c7d-463e-851d-65f6b9793bb7" /> If (all) access expired: <img width="500" alt="image" src="https://github.com/user-attachments/assets/bda0fc65-e22a-49d9-b444-fc5d58a4745a" /> ### If you're an owner If not assigned a role: Status quo <img width="500" alt="image" src="https://github.com/user-attachments/assets/435abe70-ae88-49bb-86b9-7a03963b5b66" /> If yourself is assigned a role: <img width="500" alt="image" src="https://github.com/user-attachments/assets/9fafc74a-1a33-489e-a7da-70f57356a034" /> If yourself is assigned multiple roles: <img width="500" alt="image" src="https://github.com/user-attachments/assets/f4af901b-a24e-4272-b573-9ccb474bb7e2" /> If (all) access expired: Same as not assigned a role, just refer back to the table
179 lines
5.9 KiB
TypeScript
179 lines
5.9 KiB
TypeScript
import { useParams } from 'common'
|
|
import dayjs from 'dayjs'
|
|
import { ChevronDownIcon } from 'lucide-react'
|
|
import { cn, Collapsible, CollapsibleContent, CollapsibleTrigger } from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
import { TimestampInfo } from 'ui-patterns/TimestampInfo'
|
|
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import {
|
|
JitDbAccessStatusData,
|
|
useJitDbAccessStatusQuery,
|
|
} from '@/data/jit-db-access/jit-db-access-status-query'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
|
|
const ACCESS_TITLE = "You have access to the project's database"
|
|
|
|
export const JitDbAccessInfo = () => {
|
|
const { ref: projectRef } = useParams()
|
|
const { data: jitDbAccess, error, isError } = useJitDbAccessStatusQuery({ projectRef })
|
|
|
|
const { can: canManageJitDbAccess } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.DATABASE_JIT_WRITE
|
|
)
|
|
|
|
if (!jitDbAccess) return null
|
|
|
|
if (isError) {
|
|
return (
|
|
<AlertError
|
|
className="border-0 rounded-none"
|
|
subject="Failed to retrieve access status"
|
|
error={error}
|
|
/>
|
|
)
|
|
} else if (jitDbAccess.user_id === undefined) {
|
|
if (!canManageJitDbAccess) return <NoAccess />
|
|
else return null
|
|
} else if (jitDbAccess.user_roles.length === 1) {
|
|
return <SingleRoleAccess role={jitDbAccess.user_roles[0]} />
|
|
} else {
|
|
return <MultiRoleAccess roles={jitDbAccess.user_roles} />
|
|
}
|
|
}
|
|
|
|
type JitRole = JitDbAccessStatusData['user_roles'][number]
|
|
|
|
// A role without `expires_at` never expires, so it keeps access alive. Units in unix seconds.
|
|
const isRoleExpired = (role: JitRole) =>
|
|
role.expires_at !== undefined && dayjs.unix(role.expires_at).isBefore(dayjs())
|
|
|
|
const SingleRoleAccess = ({ role }: { role: JitRole }) => {
|
|
const isExpired = isRoleExpired(role)
|
|
|
|
const { can: canManageJitDbAccess } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.DATABASE_JIT_WRITE
|
|
)
|
|
|
|
if (isExpired) {
|
|
if (!canManageJitDbAccess) return <ExpiredAccess />
|
|
else return null
|
|
}
|
|
|
|
return (
|
|
<Admonition
|
|
type="default"
|
|
showIcon={false}
|
|
className={cn('rounded-none', canManageJitDbAccess ? 'border-x-0 border-t-0' : 'border-none')}
|
|
title={ACCESS_TITLE}
|
|
description={
|
|
<div className="flex items-center gap-x-2">
|
|
<span>
|
|
<span>As</span> <code>{role.role}</code>
|
|
</span>
|
|
{role.expires_at && (
|
|
<>
|
|
<span>·</span>
|
|
<span>
|
|
Expires on{' '}
|
|
<TimestampInfo className="text-sm" utcTimestamp={role.expires_at * 1000} />
|
|
</span>
|
|
</>
|
|
)}
|
|
</div>
|
|
}
|
|
/>
|
|
)
|
|
}
|
|
|
|
const MultiRoleAccess = ({ roles }: { roles: JitDbAccessStatusData['user_roles'] }) => {
|
|
// A role without `expires_at` never expires, so it keeps access alive
|
|
const isExpired = roles.length > 0 && roles.every(isRoleExpired)
|
|
|
|
// Earliest expiry among roles that are still active (unix seconds)
|
|
const upcomingExpiries = roles.flatMap((role) =>
|
|
role.expires_at !== undefined && !isRoleExpired(role) ? [role.expires_at] : []
|
|
)
|
|
const nextExpiry = upcomingExpiries.length > 0 ? Math.min(...upcomingExpiries) : undefined
|
|
|
|
const { can: canManageJitDbAccess } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.DATABASE_JIT_WRITE
|
|
)
|
|
|
|
if (isExpired) {
|
|
if (!canManageJitDbAccess) return <ExpiredAccess />
|
|
else return null
|
|
}
|
|
|
|
return (
|
|
<Collapsible className={canManageJitDbAccess ? 'border-b' : ''}>
|
|
<CollapsibleTrigger
|
|
type="button"
|
|
className="flex items-center justify-between gap-x-2 w-full py-4 px-4 text-left cursor-pointer [&[data-state=open]>svg]:-rotate-180!"
|
|
>
|
|
<span className="block text-sm">
|
|
<span className="block mb-0.5">{ACCESS_TITLE}</span>
|
|
<span className="flex items-center gap-x-2 text-foreground-light">
|
|
<span>Assigned {roles.length} roles</span>
|
|
{nextExpiry !== undefined && (
|
|
<>
|
|
<span>·</span>
|
|
<span>
|
|
<span>Next expiry on</span>{' '}
|
|
<TimestampInfo className="text-sm" utcTimestamp={nextExpiry * 1000} />
|
|
</span>
|
|
</>
|
|
)}
|
|
</span>
|
|
</span>
|
|
<ChevronDownIcon className="transition-transform" size={14} />
|
|
</CollapsibleTrigger>
|
|
<CollapsibleContent className="data-closed:animate-collapsible-up-clipped data-open:animate-collapsible-down-clipped motion-reduce:animate-none">
|
|
{roles.map((role) => {
|
|
return (
|
|
<div key={role.role} className="py-2 px-4 first:pt-0 text-foreground-light">
|
|
<p className="text-sm flex items-center gap-x-2">
|
|
<code>{role.role}</code>
|
|
<span>·</span>
|
|
{role.expires_at && (
|
|
<span>
|
|
Expires on{' '}
|
|
<TimestampInfo className="text-sm" utcTimestamp={role.expires_at * 1000} />
|
|
</span>
|
|
)}
|
|
</p>
|
|
</div>
|
|
)
|
|
})}
|
|
</CollapsibleContent>
|
|
</Collapsible>
|
|
)
|
|
}
|
|
|
|
const ExpiredAccess = () => {
|
|
const { can: canManageJitDbAccess } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.DATABASE_JIT_WRITE
|
|
)
|
|
|
|
return (
|
|
<Admonition
|
|
type="default"
|
|
className={cn('rounded-none', canManageJitDbAccess ? 'border-x-0 border-t-0' : 'border-none')}
|
|
title="Your access to the project's database is expired"
|
|
description="Contact your project's owner or administrator for renewed access if required."
|
|
/>
|
|
)
|
|
}
|
|
|
|
const NoAccess = () => {
|
|
return (
|
|
<Admonition
|
|
type="default"
|
|
showIcon={false}
|
|
className="rounded-none border-none"
|
|
title="You need additional permissions to manage temporary database access"
|
|
description="Contact your project's owner or administrator for assistance."
|
|
/>
|
|
)
|
|
}
|