Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Scaffold UI for editing roles

Joshen Lim1291d6d18af3
diff --git a/studio/components/interfaces/Database/Roles/RoleRow.tsx b/studio/components/interfaces/Database/Roles/RoleRow.tsx
index f1b65d052c7..5b949af8dd6 100644
--- a/studio/components/interfaces/Database/Roles/RoleRow.tsx
+++ b/studio/components/interfaces/Database/Roles/RoleRow.tsx
@@ -1,18 +1,81 @@
-import { FC } from 'react'
+import { FC, useState, useRef } from 'react'
import { PostgresRole } from '@supabase/postgres-meta'
-import { IconChevronUp, IconCheck, IconX, Collapsible } from 'ui'
+import {
+ Button,
+ Form,
+ Dropdown,
+ IconChevronUp,
+ IconCheck,
+ IconX,
+ Collapsible,
+ IconEdit3,
+ IconTrash,
+ IconMoreVertical,
+ Checkbox,
+} from 'ui'
interface Props {
role: PostgresRole
- isExpanded: boolean
- onClick: () => void
}
-const RoleRow: FC<Props> = ({ role, isExpanded, onClick }) => {
+const ROLE_PERMISSIONS: any = {
+ is_superuser: 'User is a Superuser',
+ can_login: 'User can login',
+ can_create_role: 'User can create roles',
+ can_create_db: 'User can create databases',
+ is_replication_role:
+ 'User can initiate streaming replication and put the system in and out of backup mode',
+ can_bypass_rls: 'User bypasses every row level security policy',
+}
+
+const RoleRow: FC<Props> = ({ role }) => {
+ const [isExpanded, setIsExpanded] = useState(false)
+ const [isEditing, setIsEditing] = useState(false)
+ const [isSaving, setIsSaving] = useState(false)
+ const [isDeleting, setIsDeleting] = useState(false)
+
+ const isEditingRef = useRef<boolean>()
+
+ const {
+ is_superuser,
+ can_login,
+ can_create_role,
+ can_create_db,
+ is_replication_role,
+ can_bypass_rls,
+ } = role
+
+ const onSelectEdit = (event: any) => {
+ event.stopPropagation()
+ setIsExpanded(true)
+ setIsEditing(true)
+ isEditingRef.current = true
+ }
+
+ const onCancelEdit = (event: any) => {
+ event.stopPropagation()
+ setIsEditing(false)
+ isEditingRef.current = false
+ }
+
+ const onSaveChanges = async (values: any) => {
+ setIsSaving(true)
+ console.log(values)
+ setIsSaving(false)
+ }
+
return (
- <Collapsible
- open={isExpanded}
- onOpenChange={onClick}
+ <Form
+ name="role-update-form"
+ initialValues={{
+ is_superuser,
+ can_login,
+ can_create_role,
+ can_create_db,
+ is_replication_role,
+ can_bypass_rls,
+ }}
+ onSubmit={onSaveChanges}
className={[
'bg-scale-100 dark:bg-scale-300',
'hover:bg-scale-200 dark:hover:bg-scale-500',
@@ -21,93 +84,118 @@ const RoleRow: FC<Props> = ({ role, isExpanded, onClick }) => {
'dark:hover:border-scale-700 data-open:border-scale-700',
'data-open:pb-px col-span-12 mx-auto',
'-space-y-px overflow-hidden',
- 'border border-b-0 last:border-b shadow transition hover:z-50',
+ 'border border-t-0 first:border-t first:!mt-0 hover:border-t hover:-mt-[1px] shadow transition hover:z-50',
'first:rounded-tl first:rounded-tr',
'last:rounded-bl last:rounded-br',
].join(' ')}
>
- <Collapsible.Trigger asChild>
- <button
- type="button"
- className="group flex w-full items-center justify-between rounded py-3 px-6 text-scale-1200"
+ {({ handleReset }: any) => (
+ <Collapsible
+ open={isExpanded}
+ onOpenChange={() => {
+ if (!isEditingRef.current) setIsExpanded(!isExpanded)
+ }}
>
- <div className="flex items-start space-x-3">
- <IconChevronUp
- className="text-scale-800 transition data-open-parent:rotate-0 data-closed-parent:rotate-180"
- strokeWidth={2}
- width={14}
- />
- <div className="space-x-2 flex items-center">
- <p className="text-left text-sm">{role.name}</p>
- <p className="text-left text-sm text-scale-1000">(ID: {role.id})</p>
- </div>
- </div>
- <p
- className={`text-sm ${
- role.active_connections > 0 ? 'text-scale-1100' : 'text-scale-1000'
- }`}
- >
- {role.active_connections} connections
- </p>
- </button>
- </Collapsible.Trigger>
- <Collapsible.Content>
- <div className="group border-t border-scale-500 bg-scale-100 py-6 px-6 text-scale-1200 dark:bg-scale-300">
- <div className="mx-auto py-4 px-6 space-y-2">
- <div className="flex items-center space-x-2">
- {role.is_superuser ? (
- <IconCheck className="text-brand-900" strokeWidth={2} />
- ) : (
- <IconX className="text-scale-1000" strokeWidth={2} />
- )}
- <p className="text-sm">User is a Superuser</p>
- </div>
- <div className="flex items-center space-x-2">
- {role.can_login ? (
- <IconCheck className="text-brand-900" strokeWidth={2} />
- ) : (
- <IconX className="text-scale-1000" strokeWidth={2} />
- )}
- <p className="text-sm">User can login</p>
- </div>
- <div className="flex items-center space-x-2">
- {role.can_create_role ? (
- <IconCheck className="text-brand-900" strokeWidth={2} />
- ) : (
- <IconX className="text-scale-1000" strokeWidth={2} />
- )}
- <p className="text-sm">User can create roles</p>
- </div>
- <div className="flex items-center space-x-2">
- {role.can_create_db ? (
- <IconCheck className="text-brand-900" strokeWidth={2} />
- ) : (
- <IconX className="text-scale-1000" strokeWidth={2} />
- )}
- <p className="text-sm">User can create databases</p>
- </div>
- <div className="flex items-center space-x-2">
- {role.is_replication_role ? (
- <IconCheck className="text-brand-900" strokeWidth={2} />
- ) : (
- <IconX className="text-scale-1000" strokeWidth={2} />
- )}
- <p className="text-sm">
- User can initiate streaming replication and put the system in and out of backup mode
- </p>
- </div>
- <div className="flex items-center space-x-2">
- {role.can_bypass_rls ? (
- <IconCheck className="text-brand-900" strokeWidth={2} />
+ <Collapsible.Trigger asChild>
+ <button
+ type="button"
+ className="group flex w-full items-center justify-between rounded py-3 px-6 text-scale-1200"
+ >
+ <div className="flex items-start space-x-3">
+ <IconChevronUp
+ className="text-scale-800 transition data-open-parent:rotate-0 data-closed-parent:rotate-180"
+ strokeWidth={2}
+ width={14}
+ />
+ <div className="space-x-2 flex items-center">
+ <p className="text-left text-sm">{role.name}</p>
+ <p className="text-left text-sm text-scale-1000">(ID: {role.id})</p>
+ </div>
+ </div>
+ <div className="flex items-center space-x-4">
+ <p
+ className={`text-sm ${
+ role.active_connections > 0 ? 'text-scale-1100' : 'text-scale-1000'
+ }`}
+ >
+ {role.active_connections} connections
+ </p>
+ {isEditing ? (
+ <div className="flex items-center space-x-2">
+ <Button
+ type="default"
+ disabled={isSaving}
+ onClick={(event) => {
+ handleReset()
+ onCancelEdit(event)
+ }}
+ >
+ Cancel
+ </Button>
+ <Button type="primary" disabled={isSaving} loading={isSaving} htmlType="submit">
+ Save changes
+ </Button>
+ </div>
+ ) : (
+ <Dropdown
+ side="bottom"
+ className="w-[120px]"
+ overlay={
+ <>
+ <Dropdown.Item
+ icon={<IconEdit3 size="tiny" strokeWidth={2} />}
+ onClick={onSelectEdit}
+ >
+ Edit
+ </Dropdown.Item>
+ <Dropdown.Separator />
+ <Dropdown.Item
+ icon={<IconTrash className="text-red-800" size="tiny" strokeWidth={2} />}
+ onClick={() => setIsDeleting(true)}
+ >
+ Delete
+ </Dropdown.Item>
+ </>
+ }
+ >
+ <Button as="span" type="default" className="px-1" icon={<IconMoreVertical />} />
+ </Dropdown>
+ )}
+ </div>
+ </button>
+ </Collapsible.Trigger>
+ <Collapsible.Content>
+ <div className="group border-t border-scale-500 bg-scale-100 py-6 px-6 text-scale-1200 dark:bg-scale-300">
+ {isEditing ? (
+ <div className="mx-auto py-4 px-6 space-y-[9px]">
+ {Object.keys(ROLE_PERMISSIONS).map((permission) => (
+ <Checkbox
+ key={permission}
+ id={permission}
+ name={permission}
+ label={ROLE_PERMISSIONS[permission]}
+ />
+ ))}
+ </div>
) : (
- <IconX className="text-scale-1000" strokeWidth={2} />
+ <div className="mx-auto py-4 px-6 space-y-2">
+ {Object.keys(ROLE_PERMISSIONS).map((permission) => (
+ <div key={permission} className="flex items-center space-x-2">
+ {(role as any)[permission] ? (
+ <IconCheck className="text-brand-900" strokeWidth={2} />
+ ) : (
+ <IconX className="text-scale-1000" strokeWidth={2} />
+ )}
+ <p className="text-sm">{ROLE_PERMISSIONS[permission]}</p>
+ </div>
+ ))}
+ </div>
)}
- <p className="text-sm">User bypasses every row level security policy</p>
</div>
- </div>
- </div>
- </Collapsible.Content>
- </Collapsible>
+ </Collapsible.Content>
+ </Collapsible>
+ )}
+ </Form>
)
}
diff --git a/studio/components/interfaces/Database/Roles/RolesList.tsx b/studio/components/interfaces/Database/Roles/RolesList.tsx
index 9fd1ba80944..e7eac075fce 100644
--- a/studio/components/interfaces/Database/Roles/RolesList.tsx
+++ b/studio/components/interfaces/Database/Roles/RolesList.tsx
@@ -1,11 +1,12 @@
-import { FC, useState } from 'react'
+import { FC } from 'react'
import { observer } from 'mobx-react-lite'
import * as Tooltip from '@radix-ui/react-tooltip'
+import { PostgresRole } from '@supabase/postgres-meta'
+import { Button, IconPlus } from 'ui'
import { useStore } from 'hooks'
import SparkBar from 'components/ui/SparkBar'
import { FormHeader } from 'components/ui/Forms'
-import { PostgresRole } from '@supabase/postgres-meta'
import RoleRow from './RoleRow'
interface Props {
@@ -15,7 +16,6 @@ interface Props {
const RolesList: FC<Props> = ({ onSelectRole = () => {} }) => {
const { meta } = useStore()
const roles = meta.roles.list()
- const [expandedRoleId, setExpandedRoleId] = useState<number>()
const connectionLimit = Math.max(...roles.map((role: PostgresRole) => role.connection_limit))
const totalActiveConnections = roles
@@ -32,7 +32,7 @@ const RolesList: FC<Props> = ({ onSelectRole = () => {} }) => {
title="Database Roles"
description="Manage access control to your database through users, groups, and permissions"
/>
- <div className="mb-6">
+ <div className="flex items-center mb-6 space-x-6">
<Tooltip.Root delayDuration={0}>
<Tooltip.Trigger>
<div className="w-42">
@@ -60,7 +60,7 @@ const RolesList: FC<Props> = ({ onSelectRole = () => {} }) => {
'border border-scale-200 space-y-1',
].join(' ')}
>
- <p className="text-xs text-scale-1100 pr-2">Connection breakdown:</p>
+ <p className="text-xs text-scale-1100 pr-2">Connections by roles:</p>
{rolesWithActiveConnections.map((role: PostgresRole) => (
<div key={role.id} className="text-xs text-scale-1200">
{role.name}: {role.active_connections}
@@ -69,20 +69,15 @@ const RolesList: FC<Props> = ({ onSelectRole = () => {} }) => {
</div>
</Tooltip.Content>
</Tooltip.Root>
+ <Button type="primary" icon={<IconPlus size="tiny" />}>
+ Add role
+ </Button>
</div>
</div>
<div>
{roles.map((role: PostgresRole, i: number) => (
- <RoleRow
- key={role.id}
- role={role}
- isExpanded={expandedRoleId === role.id}
- onClick={() => {
- if (expandedRoleId === role.id) setExpandedRoleId(undefined)
- else setExpandedRoleId(role.id)
- }}
- />
+ <RoleRow key={role.id} role={role} />
))}
</div>
</div>