Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
do not show app links to users without workspace update access
  • Loading branch information
AbhineetJain committed Jun 10, 2022
commit edf985b6a40e143a557c194b58454fd826b40c8e
47 changes: 25 additions & 22 deletions site/src/components/Resources/Resources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ interface ResourcesProps {
resources?: WorkspaceResource[]
getResourcesError?: Error
workspace: Workspace
canUpdateWorkspace: boolean
}

export const Resources: FC<ResourcesProps> = ({ resources, getResourcesError, workspace }) => {
export const Resources: FC<ResourcesProps> = ({ resources, getResourcesError, workspace, canUpdateWorkspace }) => {
const styles = useStyles()
const theme: Theme = useTheme()

Expand All @@ -89,7 +90,7 @@ export const Resources: FC<ResourcesProps> = ({ resources, getResourcesError, wo
<AgentHelpTooltip />
</Stack>
</TableCell>
<TableCell>{Language.accessLabel}</TableCell>
{canUpdateWorkspace && <TableCell>{Language.accessLabel}</TableCell>}
<TableCell>{Language.statusLabel}</TableCell>
</TableHeaderRow>
</TableHead>
Expand Down Expand Up @@ -130,28 +131,30 @@ export const Resources: FC<ResourcesProps> = ({ resources, getResourcesError, wo
{agent.name}
<span className={styles.operatingSystem}>{agent.operating_system}</span>
</TableCell>
<TableCell>
<Stack>
{agent.status === "connected" && (
<TerminalLink
className={styles.accessLink}
workspaceName={workspace.name}
agentName={agent.name}
userName={workspace.owner_name}
/>
)}
{agent.status === "connected" &&
agent.apps.map((app) => (
<AppLink
key={app.name}
appIcon={app.icon}
appName={app.name}
userName={workspace.owner_name}
{canUpdateWorkspace && (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see this living in its own component given the length of the file.

<TableCell>
<Stack>
{agent.status === "connected" && (
<TerminalLink
className={styles.accessLink}
workspaceName={workspace.name}
agentName={agent.name}
userName={workspace.owner_name}
/>
))}
</Stack>
</TableCell>
)}
{agent.status === "connected" &&
agent.apps.map((app) => (
<AppLink
key={app.name}
appIcon={app.icon}
appName={app.name}
userName={workspace.owner_name}
workspaceName={workspace.name}
/>
))}
</Stack>
</TableCell>
)}
<TableCell>
<span style={{ color: getDisplayAgentStatus(theme, agent).color }}>
{getDisplayAgentStatus(theme, agent).status}
Expand Down
7 changes: 7 additions & 0 deletions site/src/components/Workspace/Workspace.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ Started.args = {
handleStop: action("stop"),
resources: [Mocks.MockWorkspaceResource, Mocks.MockWorkspaceResource2],
builds: [Mocks.MockWorkspaceBuild],
canUpdateWorkspace: true,
}

export const WithoutUpdateAccess = Template.bind({})
WithoutUpdateAccess.args = {
...Started.args,
canUpdateWorkspace: false,
}

export const Starting = Template.bind({})
Expand Down
9 changes: 8 additions & 1 deletion site/src/components/Workspace/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface WorkspaceProps {
resources?: TypesGen.WorkspaceResource[]
getResourcesError?: Error
builds?: TypesGen.WorkspaceBuild[]
canUpdateWorkspace: boolean
}

/**
Expand All @@ -44,6 +45,7 @@ export const Workspace: FC<WorkspaceProps> = ({
resources,
getResourcesError,
builds,
canUpdateWorkspace,
}) => {
const styles = useStyles()
const navigate = useNavigate()
Expand Down Expand Up @@ -80,7 +82,12 @@ export const Workspace: FC<WorkspaceProps> = ({
<WorkspaceStats workspace={workspace} />

{!!resources && !!resources.length && (
<Resources resources={resources} getResourcesError={getResourcesError} workspace={workspace} />
<Resources
resources={resources}
getResourcesError={getResourcesError}
workspace={workspace}
canUpdateWorkspace={canUpdateWorkspace}
/>
)}

<WorkspaceSection title="Timeline" contentsProps={{ className: styles.timelineContents }}>
Expand Down
16 changes: 12 additions & 4 deletions site/src/pages/WorkspacePage/WorkspacePage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMachine } from "@xstate/react"
import React, { useEffect } from "react"
import { useMachine, useSelector } from "@xstate/react"
import React, { useContext, useEffect } from "react"
import { Helmet } from "react-helmet"
import { useParams } from "react-router-dom"
import { DeleteWorkspaceDialog } from "../../components/DeleteWorkspaceDialog/DeleteWorkspaceDialog"
Expand All @@ -8,6 +8,8 @@ import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
import { Workspace } from "../../components/Workspace/Workspace"
import { firstOrItem } from "../../util/array"
import { pageTitle } from "../../util/page"
import { selectUser } from "../../xServices/auth/authSelectors"
import { XServiceContext } from "../../xServices/StateContext"
import { workspaceMachine } from "../../xServices/workspace/workspaceXService"
import { workspaceScheduleBannerMachine } from "../../xServices/workspaceSchedule/workspaceScheduleBannerXService"

Expand All @@ -16,8 +18,13 @@ export const WorkspacePage: React.FC = () => {
const username = firstOrItem(usernameQueryParam, null)
const workspaceName = firstOrItem(workspaceQueryParam, null)

const [workspaceState, workspaceSend] = useMachine(workspaceMachine)
const { workspace, resources, getWorkspaceError, getResourcesError, builds } = workspaceState.context
const xServices = useContext(XServiceContext)
const me = useSelector(xServices.authXService, selectUser)

const [workspaceState, workspaceSend] = useMachine(workspaceMachine.withContext({ userId: me?.id }))
const { workspace, resources, getWorkspaceError, getResourcesError, builds, permissions } = workspaceState.context

const canUpdateWorkspace = !!permissions?.updateWorkspace

const [bannerState, bannerSend] = useMachine(workspaceScheduleBannerMachine)

Expand Down Expand Up @@ -56,6 +63,7 @@ export const WorkspacePage: React.FC = () => {
resources={resources}
getResourcesError={getResourcesError instanceof Error ? getResourcesError : undefined}
builds={builds}
canUpdateWorkspace={canUpdateWorkspace}
/>
<DeleteWorkspaceDialog
isOpen={workspaceState.matches({ ready: { build: "askingDelete" } })}
Expand Down
4 changes: 4 additions & 0 deletions site/src/xServices/auth/authSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ export const selectOrgId = (state: AuthState): string | undefined => {
export const selectPermissions = (state: AuthState): AuthContext["permissions"] => {
return state.context.permissions
}

export const selectUser = (state: AuthState): AuthContext["me"] => {
return state.context.me
}
89 changes: 81 additions & 8 deletions site/src/xServices/workspace/workspaceXService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const Language = {
buildError: "Workspace action failed.",
}

type Permissions = Record<keyof ReturnType<typeof permissionsToCheck>, boolean>

export interface WorkspaceContext {
workspace?: TypesGen.Workspace
template?: TypesGen.Template
Expand All @@ -26,14 +28,18 @@ export interface WorkspaceContext {
// error creating a new WorkspaceBuild
buildError?: Error | unknown
// these are separate from getX errors because they don't make the page unusable
refreshWorkspaceError: Error | unknown
refreshTemplateError: Error | unknown
getResourcesError: Error | unknown
refreshWorkspaceError?: Error | unknown
refreshTemplateError?: Error | unknown
getResourcesError?: Error | unknown
// Builds
builds?: TypesGen.WorkspaceBuild[]
getBuildsError?: Error | unknown
loadMoreBuildsError?: Error | unknown
cancellationMessage: string
cancellationMessage?: string
// permissions
permissions?: Permissions
checkPermissionsError?: Error | unknown
userId?: string
}

export type WorkspaceEvent =
Expand All @@ -48,6 +54,30 @@ export type WorkspaceEvent =
| { type: "LOAD_MORE_BUILDS" }
| { type: "REFRESH_TIMELINE" }

export const checks = {
readWorkspace: "readWorkspace",
updateWorkspace: "updateWorkspace",
} as const

const permissionsToCheck = (workspace: TypesGen.Workspace) => ({
[checks.readWorkspace]: {
object: {
resource_type: "workspace",
resource_id: workspace.id,
owner_id: workspace.owner_id,
},
action: "read",
},
[checks.updateWorkspace]: {
object: {
resource_type: "workspace",
resource_id: workspace.id,
owner_id: workspace.owner_id,
},
action: "update",
},
})

export const workspaceMachine = createMachine(
{
tsTypes: {} as import("./workspaceXService.typegen").Typegen0,
Expand Down Expand Up @@ -82,6 +112,9 @@ export const workspaceMachine = createMachine(
loadMoreBuilds: {
data: TypesGen.WorkspaceBuild[]
}
checkPermissions: {
data: TypesGen.UserAuthorizationResponse
}
},
},
id: "workspaceState",
Expand All @@ -99,7 +132,7 @@ export const workspaceMachine = createMachine(
src: "getWorkspace",
id: "getWorkspace",
onDone: {
target: "ready",
target: "gettingPermissions",
actions: ["assignWorkspace"],
},
onError: {
Expand All @@ -109,6 +142,25 @@ export const workspaceMachine = createMachine(
},
tags: "loading",
},
gettingPermissions: {
entry: "clearGetPermissionsError",
invoke: {
src: "checkPermissions",
id: "checkPermissions",
onDone: [
{
actions: ["assignPermissions"],
target: "ready",
},
],
onError: [
{
actions: "assignGetPermissionsError",
target: "error",
},
],
},
},
ready: {
type: "parallel",
states: {
Expand Down Expand Up @@ -312,6 +364,7 @@ export const workspaceMachine = createMachine(
workspace: undefined,
template: undefined,
build: undefined,
permissions: undefined,
}),
assignWorkspace: assign({
workspace: (_, event) => event.data,
Expand All @@ -323,6 +376,17 @@ export const workspaceMachine = createMachine(
assignTemplate: assign({
template: (_, event) => event.data,
}),
assignPermissions: assign({
// Setting event.data as Permissions to be more stricted. So we know
// what permissions we asked for.
permissions: (_, event) => event.data as Permissions,
}),
assignGetPermissionsError: assign({
checkPermissionsError: (_, event) => event.data,
}),
clearGetPermissionsError: assign({
checkPermissionsError: (_) => undefined,
}),
assignBuild: (_, event) =>
assign({
build: event.data,
Expand All @@ -347,7 +411,7 @@ export const workspaceMachine = createMachine(
cancellationMessage: undefined,
}),
displayCancellationError: (context) => {
displayError(context.cancellationMessage)
displayError(context.cancellationMessage || "Cancellation failed")
},
assignRefreshWorkspaceError: (_, event) =>
assign({
Expand Down Expand Up @@ -489,14 +553,23 @@ export const workspaceMachine = createMachine(
if (context.workspace) {
return await API.getWorkspaceBuilds(context.workspace.id)
} else {
throw Error("Cannot refresh workspace without id")
throw Error("Cannot get builds without id")
}
},
loadMoreBuilds: async (context) => {
if (context.workspace) {
return await API.getWorkspaceBuilds(context.workspace.id)
} else {
throw Error("Cannot refresh workspace without id")
throw Error("Cannot load more builds without id")
}
},
checkPermissions: async (context) => {
if (context.workspace && context.userId) {
return await API.checkUserPermissions(context.userId, {
checks: permissionsToCheck(context.workspace),
})
} else {
throw Error("Cannot check permissions without both workspace and user id")
}
},
},
Expand Down