Skip to content

Commit

Permalink
Merge pull request #1285 from interval/ctx-user-roles-teams
Browse files Browse the repository at this point in the history
Add role and teams to ctx.user value
  • Loading branch information
jacobmischka authored Jul 13, 2023
2 parents 26a5f64 + ec1e80c commit 56792dd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
18 changes: 11 additions & 7 deletions src/examples/basic/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
})

Expand Down
26 changes: 16 additions & 10 deletions src/internalRpcSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof CTX_USER_ROLE>

export const ACCESS_CONTROL_DEFINITION = z.union([
z.literal('entire-organization'),
z.object({
Expand Down Expand Up @@ -550,6 +554,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<typeof startTransactionUser>

export const hostSchema = {
OPEN_PAGE: {
inputs: z.object({
Expand All @@ -559,11 +573,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(),
}),
Expand Down Expand Up @@ -599,11 +609,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(),
}),
Expand Down
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type {
IOFunctionReturnType,
T_IO_DISPLAY_METHOD_NAMES,
T_IO_INPUT_METHOD_NAMES,
LinkProps,
menuItem,
buttonItem,
ButtonTheme,
Expand All @@ -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'
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 56792dd

Please sign in to comment.