From ec1e80c3d29f65b34d3620f22b726e3cc454f309 Mon Sep 17 00:00:00 2001 From: Jacob Mischka Date: Wed, 7 Jun 2023 15:56:29 -0500 Subject: [PATCH] Add role and teams to ctx.user value Closes T-813 --- src/examples/basic/index.ts | 18 +++++++++++------- src/internalRpcSchema.ts | 26 ++++++++++++++++---------- src/types.ts | 11 +++++++++-- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/examples/basic/index.ts b/src/examples/basic/index.ts index 51e8190..0aec454 100644 --- a/src/examples/basic/index.ts +++ b/src/examples/basic/index.ts @@ -224,14 +224,18 @@ const actionLinks: IntervalActionHandler = async () => { } const echoContext = new Action(async () => { + const data = { + organization: ctx.organization, + action: ctx.action, + environment: ctx.environment, + params: ctx.params, + user: ctx.user, + } + + console.log(data) + await io.display.object('Context', { - data: { - organization: ctx.organization, - aciton: ctx.action, - environment: ctx.environment, - params: ctx.params, - user: ctx.user, - }, + data, }) }) diff --git a/src/internalRpcSchema.ts b/src/internalRpcSchema.ts index f59e98d..70d6cd7 100644 --- a/src/internalRpcSchema.ts +++ b/src/internalRpcSchema.ts @@ -78,6 +78,10 @@ export type BackwardCompatibleLoadingState = z.input< typeof BACKWARD_COMPATIBLE_LOADING_STATE > +export const CTX_USER_ROLE = z.enum(['admin', 'developer', 'member']) + +export type CtxUserRole = z.input + export const ACCESS_CONTROL_DEFINITION = z.union([ z.literal('entire-organization'), z.object({ @@ -549,6 +553,16 @@ export const clientSchema = { export type ClientSchema = typeof clientSchema +export const startTransactionUser = z.object({ + email: z.string(), + firstName: z.string().nullable(), + lastName: z.string().nullable(), + role: CTX_USER_ROLE, + teams: z.array(z.string()), +}) + +export type StartTransactionUser = z.input + export const hostSchema = { OPEN_PAGE: { inputs: z.object({ @@ -558,11 +572,7 @@ export const hostSchema = { slug: z.string(), }), environment: actionEnvironment, - user: z.object({ - email: z.string(), - firstName: z.string().nullable(), - lastName: z.string().nullable(), - }), + user: startTransactionUser, params: serializableRecord, paramsMeta: z.any().optional(), }), @@ -598,11 +608,7 @@ export const hostSchema = { url: z.string(), }), environment: actionEnvironment, - user: z.object({ - email: z.string(), - firstName: z.string().nullable(), - lastName: z.string().nullable(), - }), + user: startTransactionUser, params: serializableRecord, paramsMeta: z.any().optional(), }), diff --git a/src/types.ts b/src/types.ts index 6c0acae..caeeb4e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -11,7 +11,6 @@ import type { IOFunctionReturnType, T_IO_DISPLAY_METHOD_NAMES, T_IO_INPUT_METHOD_NAMES, - LinkProps, menuItem, buttonItem, ButtonTheme, @@ -25,7 +24,7 @@ import type { import type { AccessControlDefinition, ActionEnvironment, - HostSchema, + CtxUserRole, } from './internalRpcSchema' import type { IOClient, IOClientRenderValidator } from './classes/IOClient' import type IOComponent from './classes/IOComponent' @@ -66,6 +65,14 @@ export type CtxUser = { * The last name of the user running the action or page, if present. */ lastName: string | null + /** + * The user role within the organization of the user running the action or page. + */ + role: CtxUserRole + /** + * The teams the user running the action or page belongs to within the organization. + */ + teams: string[] } export type CtxOrganization = {