The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Ensure that is_owner checks are correct
Joshen Lim3270551167c1
diff --git a/studio/components/interfaces/Billing/Subscription/Subscription.tsx b/studio/components/interfaces/Billing/Subscription/Subscription.tsxindex bd2293490ca..c18eabca6ce 100644--- a/studio/components/interfaces/Billing/Subscription/Subscription.tsx+++ b/studio/components/interfaces/Billing/Subscription/Subscription.tsx@@ -5,7 +5,7 @@ import { useRouter } from 'next/router'import { Button, Loading } from '@supabase/ui'import { PermissionAction } from '@supabase/shared-types/out/constants'-import { checkPermissions, useStore, useFlag } from 'hooks'+import { checkPermissions, useFlag, checkIsOwner } from 'hooks'import { STRIPE_PRODUCT_IDS } from 'lib/constants'import { formatBytes } from 'lib/helpers'@@ -36,8 +36,7 @@ const Subscription: FC<Props> = ({currentPeriodEnd,}) => {const router = useRouter()- const { ui } = useStore()- const isOrgOwner = ui.selectedOrganization?.is_owner+ const isOrgOwner = checkIsOwner()const projectUpdateDisabled = useFlag('disableProjectCreationAndUpdate')const canChangeSubscription = checkPermissions(diff --git a/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx b/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsxindex 9d8032534c6..bf0bedd485d 100644--- a/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx+++ b/studio/components/interfaces/Organization/BillingSettings/PaymentMethods.tsx@@ -14,7 +14,7 @@ import {} from '@supabase/ui'import * as Tooltip from '@radix-ui/react-tooltip'-import { useStore } from 'hooks'+import { useStore, checkIsOwner } from 'hooks'import { delete_, patch } from 'lib/common/fetch'import { getURL } from 'lib/helpers'import { API_URL } from 'lib/constants'@@ -37,13 +37,12 @@ const PaymentMethods: FC<Props> = ({onPaymentMethodsDeleted,}) => {const { ui } = useStore()- const isOwner = ui.selectedOrganization?.is_owner ?? falseconst orgSlug = ui.selectedOrganization?.slug ?? ''+ const isOwner = checkIsOwner()const [selectedMethodForDefault, setSelectedMethodForDefault] = useState<any>()const [selectedMethodToDelete, setSelectedMethodToDelete] = useState<any>()const [showAddPaymentMethodModal, setShowAddPaymentMethodModal] = useState(false)-const [isUpdatingPaymentMethod, setIsUpdatingPaymentMethod] = useState(false)const onConfirmMakeDefaultPaymentMethod = async () => {diff --git a/studio/hooks/queries/usePermissions.ts b/studio/hooks/queries/usePermissions.tsindex 68c1bf05bac..c445649536f 100644--- a/studio/hooks/queries/usePermissions.ts+++ b/studio/hooks/queries/usePermissions.ts@@ -3,7 +3,7 @@ import jsonLogic from 'json-logic-js'import { find } from 'lodash'import { useRouter } from 'next/router'-import { useFlag, useStore } from 'hooks'+import { useFlag, useOrganizationDetail, useOrganizationRoles, useStore } from 'hooks'import { get } from 'lib/common/fetch'import { API_URL, IS_PLATFORM } from 'lib/constants'import { Organization, Project } from 'types'@@ -66,3 +66,31 @@ export function checkPermissions(action: string, resource: string, data?: objectcondition === null || jsonLogic.apply(condition, data))}++// [Joshen TODO] This method is just to support legacy permissions where+// a couple of the UI was checking only if the user is an owner or not+// Will deprecate once ABAC is fully rolled out.+export function checkIsOwner() {+ const { app, ui } = useStore()+ const router = useRouter()++ let organization_id: number | undefined+ const { ref, slug } = router.query++ if (ref) {+ const project = find(app.projects.list(), { ref }) as Project | undefined+ organization_id = project?.organization_id+ } else if (slug) {+ const organization = find(app.projects.list(), { ref }) as Organization | undefined+ organization_id = organization?.id+ }++ const { roles } = useOrganizationRoles(router.query.slug as string)+ const { members } = useOrganizationDetail(router.query.slug as string)++ const userMember = (members || []).find((member) => member.gotrue_id === ui?.profile?.gotrue_id)+ const [memberRoleId] = userMember?.role_ids ?? []+ const memberRole = (roles || []).find((role) => role.id === memberRoleId)++ return memberRole?.name === 'Owner'+}diff --git a/studio/pages/new/[slug].tsx b/studio/pages/new/[slug].tsxindex 1b6cb8b97a4..287209f9900 100644--- a/studio/pages/new/[slug].tsx+++ b/studio/pages/new/[slug].tsx@@ -27,6 +27,7 @@ import {useFlag,withAuth,useSubscriptionStats,+ checkIsOwner,checkPermissions,useOrganizationDetail,useOrganizationRoles,@@ -51,16 +52,6 @@ const Wizard: NextPageWithLayout = () => {const projectCreationDisabled = useFlag('disableProjectCreationAndUpdate')const subscriptionStats = useSubscriptionStats()- const { roles } = useOrganizationRoles(router.query.slug as string)- const { members } = useOrganizationDetail(router.query.slug as string)-- const isOrganizationOwner = () => {- const userMember = (members || []).find((member) => member.gotrue_id === ui?.profile?.gotrue_id)- const [memberRoleId] = userMember?.role_ids ?? []- const memberRole = (roles || []).find((role) => role.id === memberRoleId)- return memberRole?.name === 'Owner'- }-const [projectName, setProjectName] = useState('')const [dbPass, setDbPass] = useState('')const [dbRegion, setDbRegion] = useState(REGIONS_DEFAULT)@@ -75,13 +66,14 @@ const Wizard: NextPageWithLayout = () => {const currentOrg = organizations.find((o: any) => o.slug === slug)const stripeCustomerId = currentOrg?.stripe_customer_id+ const isOrganizationOwner = checkIsOwner()const totalFreeProjects = subscriptionStats.total_active_free_projectsconst freeProjectsLimit = ui.profile?.free_project_limit ?? DEFAULT_FREE_PROJECTS_LIMIT// [Joshen TODO] This is a fallback to original behaviour if permissions is not enabledconst isAdmin = enablePermissions? checkPermissions(PermissionAction.SQL_INSERT, 'postgres.public.projects')- : isOrganizationOwner()+ : isOrganizationOwnerconst isEmptyOrganizations = organizations.length <= 0 && app.organizations.isInitializedconst isEmptyPaymentMethod = paymentMethods ? !paymentMethods.length : falsediff --git a/studio/stores/RootStore.ts b/studio/stores/RootStore.tsindex 3fb309abcbe..de105b7e1fb 100644--- a/studio/stores/RootStore.ts+++ b/studio/stores/RootStore.ts@@ -39,10 +39,7 @@ export class RootStore implements IRootStore {constructor() {this.app = new AppStore(this)this.ui = new UiStore(this)- this.meta = new MetaStore(this, {- projectRef: '',- connectionString: '',- })+ this.meta = new MetaStore(this, { projectRef: '', connectionString: '' })// @ts-ignorethis.content = new ProjectContentStore(this, { projectRef: '' })diff --git a/studio/types/base.ts b/studio/types/base.tsindex 8068ca7cbb7..986b07ac4a8 100644--- a/studio/types/base.ts+++ b/studio/types/base.ts@@ -5,7 +5,7 @@ export interface Organization {slug: stringname: stringbilling_email: string- is_owner?: boolean+ is_owner?: boolean // [Joshen] This will be deprecated after ABAC full roll outstripe_customer_id?: string}