mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem Migrating Studio's permission checks from the old RLS json logic to openfga backed permissions. ### New hooks and helpers - `usePermissionsQueryV2` fetches the new profile openfga permisisons - `useAsyncCheckPermissionsV2` the replacement for the old check hook; takes an FGA permission string (or a list) - `FGA_PERMISSIONS` permission constants derived from `@supabase/shared-types`. At the moment, the hook also accepts plain strings for permissions not yet published in the package - `useAsyncCheckUserContentPermissions` new hook for permission checks on user content (SQL snippets, reports, log snippets). This wraps ``useAsyncCheckPermissionsV2` and adds existing content level rules on top of the fga permission check ### Notes - ~~Need api to support new permissions: project_operations_write, billing_read/write, audit_logs_read~~ [DONE] - ~~Still need to support: organization_config_read/write, auth hooks and users permissions~~ [DONE] --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
91 lines
3.4 KiB
TypeScript
91 lines
3.4 KiB
TypeScript
import { ChevronDownIcon, MailIcon, UserPlusIcon } from 'lucide-react'
|
|
import { parseAsBoolean, useQueryState } from 'nuqs'
|
|
import { Button, DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from 'ui'
|
|
|
|
import CreateUserModal from './CreateUserModal'
|
|
import InviteUserModal from './InviteUserModal'
|
|
import { DropdownMenuItemTooltip } from '@/components/ui/DropdownMenuItemTooltip'
|
|
import { ShortcutBadge } from '@/components/ui/ShortcutBadge'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
|
|
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
|
|
|
|
export const AddUserDropdown = () => {
|
|
const showSendInvitation = useIsFeatureEnabled('authentication:show_send_invitation')
|
|
|
|
const { can: canInviteUsers } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.AUTH_USERS_WRITE
|
|
)
|
|
|
|
const { can: canCreateUsers } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.AUTH_USERS_WRITE
|
|
)
|
|
|
|
const [inviteVisible, setInviteVisible] = useQueryState(
|
|
'invite',
|
|
parseAsBoolean.withDefault(false).withOptions({ history: 'push', clearOnDefault: true })
|
|
)
|
|
const [createVisible, setCreateVisible] = useQueryState(
|
|
'new',
|
|
parseAsBoolean.withDefault(false).withOptions({ history: 'push', clearOnDefault: true })
|
|
)
|
|
|
|
return (
|
|
<>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="primary" iconRight={<ChevronDownIcon size={14} strokeWidth={1.5} />}>
|
|
Add user
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent side="bottom" align="end" className="w-52">
|
|
{showSendInvitation && (
|
|
<DropdownMenuItemTooltip
|
|
className="gap-x-2"
|
|
disabled={!canInviteUsers}
|
|
onClick={() => {
|
|
if (canInviteUsers) setInviteVisible(true)
|
|
}}
|
|
tooltip={{
|
|
content: { side: 'left', text: 'You need additional permissions to invite users' },
|
|
}}
|
|
>
|
|
<MailIcon size={14} className="shrink-0" />
|
|
<p className="flex-1 min-w-0">Send invitation</p>
|
|
{canInviteUsers && (
|
|
<ShortcutBadge
|
|
shortcutId={SHORTCUT_IDS.AUTH_USERS_INVITE_USER}
|
|
className="shrink-0"
|
|
/>
|
|
)}
|
|
</DropdownMenuItemTooltip>
|
|
)}
|
|
|
|
<DropdownMenuItemTooltip
|
|
className="gap-x-2 pointer-events-auto!"
|
|
disabled={!canCreateUsers}
|
|
onClick={() => {
|
|
if (canCreateUsers) setCreateVisible(true)
|
|
}}
|
|
tooltip={{
|
|
content: { side: 'left', text: 'You need additional permissions to create users' },
|
|
}}
|
|
>
|
|
<UserPlusIcon size={14} className="shrink-0" />
|
|
<p className="flex-1 min-w-0">Create new user</p>
|
|
{canCreateUsers && (
|
|
<ShortcutBadge
|
|
shortcutId={SHORTCUT_IDS.AUTH_USERS_CREATE_USER}
|
|
className="shrink-0"
|
|
/>
|
|
)}
|
|
</DropdownMenuItemTooltip>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
|
|
<InviteUserModal visible={inviteVisible} setVisible={setInviteVisible} />
|
|
<CreateUserModal visible={createVisible} setVisible={setCreateVisible} />
|
|
</>
|
|
)
|
|
}
|