The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add usePermissions hook
Beng Eudad4d3d93d14
diff --git a/studio/hooks/queries/index.ts b/studio/hooks/queries/index.tsindex 648566e9c58..a42c88933e6 100644--- a/studio/hooks/queries/index.ts+++ b/studio/hooks/queries/index.ts@@ -1,5 +1,6 @@export * from './useJwtSecretUpdateStatus'export * from './useOrganizationDetail'+export * from './usePermissions'export * from './useProfile'export * from './useProjectAuthConfig'export * from './useProjectPaygStatistics'diff --git a/studio/hooks/queries/usePermissions.ts b/studio/hooks/queries/usePermissions.tsnew file mode 100644index 00000000000..715628bd6a4--- /dev/null+++ b/studio/hooks/queries/usePermissions.ts@@ -0,0 +1,47 @@+import jsonLogic from 'json-logic-js'+import { find } from 'lodash'+import { useRouter } from 'next/router'+import useSWR from 'swr'+import { useStore } from 'hooks'+import { get } from 'lib/common/fetch'+import { API_URL, IS_PLATFORM } from 'lib/constants'+import { Organization, Project } from 'types'++export function usePermissions(action: string, resource: string, data?: object) {+ if (!IS_PLATFORM) return true++ const { app } = useStore()+ const router = useRouter()+ const url = `${API_URL}/profile/permissions`+ const { data: permissions, error } = useSWR<any>(url, get)+ if (error || !permissions || permissions.error) return false++ 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+ }++ return permissions+ .filter(+ (permission: {+ actions: string[]+ condition: jsonLogic.RulesLogic+ organization_id: number+ resources: string[]+ }) =>+ permission.actions.some((act) => action.match(act.replace('.', '.').replace('%', '.*'))) &&+ permission.resources.some((res) =>+ resource.match(res.replace('.', '.').replace('%', '.*'))+ ) &&+ permission.organization_id === organization_id+ )+ .some(+ ({ condition }: { condition: jsonLogic.RulesLogic }) =>+ condition === null || jsonLogic.apply(condition, data)+ )+}diff --git a/studio/package-lock.json b/studio/package-lock.jsonindex 41b7da46e70..1d1b046734d 100644--- a/studio/package-lock.json+++ b/studio/package-lock.json@@ -35499,6 +35499,12 @@"@types/tough-cookie": "*"}},+ "@types/json-logic-js": {+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/@types/json-logic-js/-/json-logic-js-1.2.1.tgz",+ "integrity": "sha512-g/g+wj/7sgazpiCHiyAtndoNiy/LodLkNG4I9MILAl0UinKKwv3GiPKbtvcE1hIoezQqgDamXfx8Lht62/hHqw==",+ "dev": true+ },"@types/json-schema": {"version": "7.0.11","resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.11.tgz",@@ -43945,6 +43951,11 @@"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-2.5.2.tgz","integrity": "sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA=="},+ "json-logic-js": {+ "version": "2.0.2",+ "resolved": "https://registry.npmjs.org/json-logic-js/-/json-logic-js-2.0.2.tgz",+ "integrity": "sha512-ZBtBdMJieqQcH7IX/LaBsr5pX+Y5JIW+EhejtM3Ffg2jdN9Iwf+Ht6TbHnvAZ/YtwyuhPaCBlnvzrwVeWdvGDQ=="+ },"json-parse-better-errors": {"version": "1.0.2","resolved": "https://registry.npmjs.org/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz",diff --git a/studio/package.json b/studio/package.jsonindex a7375799d99..f3bb1ca73fd 100644--- a/studio/package.json+++ b/studio/package.json@@ -33,6 +33,7 @@"configcat-js": "^5.6.0","crypto-js": "^4.1.1","dayjs": "^1.11.0",+ "json-logic-js": "^2.0.2","lodash": "^4.17.21","lottie-react": "^2.1.0","markdown-table": "=2.0.0",@@ -79,6 +80,7 @@"@testing-library/react": "^12.1.2","@testing-library/user-event": "^13.5.0","@types/crypto-js": "^4.0.2",+ "@types/json-logic-js": "^1.2.1","@types/lodash": "^4.14.172","@types/markdown-table": "^3.0.0","@types/papaparse": "^5.3.1",