mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
150 lines
5.1 KiB
TypeScript
150 lines
5.1 KiB
TypeScript
import { Fragment, ReactNode } from 'react'
|
||
import { Tooltip, TooltipContent, TooltipTrigger } from 'ui'
|
||
import { Admonition } from 'ui-patterns/Admonition'
|
||
|
||
import { formatPermissionName, groupScopesByLevel } from './Consent/OAuthAppsAuthorizeScreen.utils'
|
||
import { OAuthGrantAccordionItem } from './OAuthGrantAccordionItem'
|
||
import { TOOLTIP_UNDERLINE_CLASS_NAME } from '@/components/ui/tooltip-styles'
|
||
import {
|
||
DATABASE_TOKEN_ACCESS_PERMISSION,
|
||
getOAuthDatabaseTokenAccess,
|
||
} from '@/data/oauth-apps/types'
|
||
import { pluralize } from '@/lib/helpers'
|
||
|
||
export function OAuthGrantAccess({
|
||
id,
|
||
title,
|
||
scopes,
|
||
projects,
|
||
icon,
|
||
expanded = false,
|
||
}: {
|
||
id: string
|
||
title: string
|
||
scopes: string[]
|
||
projects?: { name: string; ref: string }[]
|
||
expanded?: boolean
|
||
icon: ReactNode
|
||
}) {
|
||
const databaseAccess = getOAuthDatabaseTokenAccess(scopes)
|
||
const groups = groupScopesByLevel(
|
||
databaseAccess === 'write' ? [...scopes, `${DATABASE_TOKEN_ACCESS_PERMISSION}:read`] : scopes
|
||
)
|
||
const canRunSqlAsPostgres = scopes.includes('database:write')
|
||
const levels = [
|
||
{ label: 'Read and write', permissions: groups['read-write'] },
|
||
{ label: 'Write', permissions: groups.write },
|
||
{ label: 'Read only', permissions: groups.read },
|
||
]
|
||
const count = levels.reduce((total, { permissions }) => total + permissions.length, 0)
|
||
const projectSummary =
|
||
projects == null
|
||
? 'All projects'
|
||
: `${projects.length} ${pluralize(projects.length, 'project')}`
|
||
|
||
const details = (
|
||
<div className="space-y-6 text-sm">
|
||
<section className="space-y-1">
|
||
<h3 className="text-sm font-medium">Projects</h3>
|
||
{projects && projects.length > 0 ? (
|
||
<dl className="mt-3 divide-y rounded-md border bg-surface-75 px-4">
|
||
{projects.map((project) => (
|
||
<div key={project.ref} className="space-y-1 py-3">
|
||
<dt>{project.name}</dt>
|
||
<dd>
|
||
<code>{project.ref}</code>
|
||
</dd>
|
||
</div>
|
||
))}
|
||
</dl>
|
||
) : (
|
||
<p className="mt-3 text-foreground-light">
|
||
{projects ? 'No projects included.' : 'All current and future projects.'}
|
||
</p>
|
||
)}
|
||
</section>
|
||
<section className="space-y-3">
|
||
<h3 className="text-sm font-medium">Permissions</h3>
|
||
{count > 0 && (
|
||
<div className="mt-3 divide-y rounded-md border bg-surface-75 px-4">
|
||
{levels
|
||
.filter(({ permissions }) => permissions.length > 0)
|
||
.map(({ label, permissions }) => (
|
||
<div key={label} className="space-y-1.5 py-3">
|
||
<h4 className="font-mono text-xs uppercase text-foreground-light">{label}</h4>
|
||
<p className="text-sm text-foreground">
|
||
{permissions.map((permission, index) => (
|
||
<Fragment key={permission}>
|
||
{index > 0 && <span>, </span>}
|
||
<PermissionLabel permission={permission} />
|
||
</Fragment>
|
||
))}
|
||
</p>
|
||
</div>
|
||
))}
|
||
</div>
|
||
)}
|
||
{(databaseAccess || canRunSqlAsPostgres) && (
|
||
<Admonition
|
||
type="warning"
|
||
title="Database permissions"
|
||
description={
|
||
<div className="space-y-1">
|
||
<p>These permissions allow this app to:</p>
|
||
<ul>
|
||
{databaseAccess && <li>Read rows that your access rules normally hide</li>}
|
||
{databaseAccess === 'write' && <li>Change data and database structure</li>}
|
||
{canRunSqlAsPostgres && (
|
||
<li>Run SQL as postgres and reset the database password</li>
|
||
)}
|
||
</ul>
|
||
</div>
|
||
}
|
||
/>
|
||
)}
|
||
|
||
{count === 0 && <p className="text-foreground-light">No permissions granted.</p>}
|
||
</section>
|
||
</div>
|
||
)
|
||
if (expanded)
|
||
return (
|
||
<div className="-mx-5 mb-8 space-y-4 border-t px-5 pt-6">
|
||
<h2 className="text-base font-medium">{title}</h2>
|
||
{details}
|
||
</div>
|
||
)
|
||
return (
|
||
<OAuthGrantAccordionItem
|
||
value={id}
|
||
icon={icon}
|
||
title={title}
|
||
description={`${projectSummary} · ${count} ${pluralize(count, 'permission')}`}
|
||
>
|
||
{details}
|
||
</OAuthGrantAccordionItem>
|
||
)
|
||
}
|
||
|
||
function PermissionLabel({ permission }: { permission: string }) {
|
||
const descriptions: Record<string, string> = {
|
||
database: 'Access database settings. Write access also allows the app to run SQL.',
|
||
environment: 'Access project branches.',
|
||
[DATABASE_TOKEN_ACCESS_PERMISSION]:
|
||
'Connect to Postgres as the app’s database role with an access token.',
|
||
}
|
||
const label =
|
||
permission === DATABASE_TOKEN_ACCESS_PERMISSION
|
||
? 'Database token access'
|
||
: formatPermissionName(permission)
|
||
if (!descriptions[permission]) return label
|
||
return (
|
||
<Tooltip>
|
||
<TooltipTrigger className={TOOLTIP_UNDERLINE_CLASS_NAME}>{label}</TooltipTrigger>
|
||
<TooltipContent side="top" className="max-w-60">
|
||
{descriptions[permission]}
|
||
</TooltipContent>
|
||
</Tooltip>
|
||
)
|
||
}
|