Skip to content

Commit

Permalink
Align target access on UI with backend
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Oct 10, 2024
1 parent 81e35a9 commit a0c8fc2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export class SchemaManager {
this.logger.debug('Updating base schema (selector=%o)', selector);
await this.authManager.ensureTargetAccess({
...selector,
scope: TargetAccessScope.REGISTRY_READ,
scope: TargetAccessScope.REGISTRY_WRITE,
});
await this.storage.updateBaseSchema(selector, newBaseSchema);
}
Expand Down
18 changes: 14 additions & 4 deletions packages/web/app/src/components/layouts/target.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,24 @@ export const TargetLayout = ({

useLastVisitedOrganizationWriter(currentOrganization?.cleanId);

const canAccessSchema = canAccessTarget(
const hasRegistryReadAccess = canAccessTarget(
TargetAccessScope.RegistryRead,
currentOrganization?.me ?? null,
);
const canAccessSettings = canAccessTarget(
const hasSettingsAccess = canAccessTarget(
TargetAccessScope.Settings,
currentOrganization?.me ?? null,
);
const hasRegistryWriteAccess = canAccessTarget(
TargetAccessScope.RegistryWrite,
currentOrganization?.me ?? null,
);
const hasTokensWriteAccess = canAccessTarget(
TargetAccessScope.TokensWrite,
currentOrganization?.me ?? null,
);

const canAccessSettingsPage = hasSettingsAccess || hasRegistryWriteAccess || hasTokensWriteAccess;

return (
<>
Expand Down Expand Up @@ -169,7 +179,7 @@ export const TargetLayout = ({
{currentOrganization && currentProject && currentTarget ? (
<Tabs className="flex h-full grow flex-col" value={page}>
<TabsList variant="menu">
{canAccessSchema && (
{hasRegistryReadAccess && (
<>
<TabsTrigger variant="menu" value={Page.Schema} asChild>
<Link
Expand Down Expand Up @@ -259,7 +269,7 @@ export const TargetLayout = ({
</TabsTrigger>
</>
)}
{canAccessSettings && (
{canAccessSettingsPage && (
<TabsTrigger variant="menu" value={Page.Settings} asChild>
<Link
to="/$organizationId/$projectId/$targetId/settings"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,21 +425,23 @@ export function CDNAccessTokens(props: {
created <TimeAgo date={node.createdAt} />
</Td>
<Td align="right">
<Button
className="hover:text-red-500"
variant="ghost"
onClick={() => {
void router.navigate({
search: {
page: 'cdn',
cdn: 'delete',
id: node.id,
},
});
}}
>
<TrashIcon />
</Button>
{canManage ? (
<Button
className="hover:text-red-500"
variant="ghost"
onClick={() => {
void router.navigate({
search: {
page: 'cdn',
cdn: 'delete',
id: node.id,
},
});
}}
>
<TrashIcon />
</Button>
) : null}
</Td>
</Tr>
);
Expand Down
35 changes: 25 additions & 10 deletions packages/web/app/src/pages/target-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1136,11 +1136,26 @@ function TargetSettingsContent(props: {

const targetForSettings = useFragment(TargetSettingsPage_TargetFragment, currentTarget);

const canAccessTokens = canAccessTarget(
TargetAccessScope.TokensRead,
const hasTokensWriteAccess = canAccessTarget(
TargetAccessScope.TokensWrite,
organizationForSettings?.me ?? null,
);
const hasReadAccess = canAccessTarget(
TargetAccessScope.Read,
organizationForSettings?.me ?? null,
);
const hasDeleteAccess = canAccessTarget(
TargetAccessScope.Delete,
organizationForSettings?.me ?? null,
);
const hasSettingsAccess = canAccessTarget(
TargetAccessScope.Settings,
organizationForSettings?.me ?? null,
);
const hasRegistryWriteAccess = canAccessTarget(
TargetAccessScope.RegistryWrite,
organizationForSettings?.me ?? null,
);
const canDelete = canAccessTarget(TargetAccessScope.Delete, organizationForSettings?.me ?? null);

if (query.error) {
return <QueryError organizationId={props.organizationId} error={query.error} />;
Expand Down Expand Up @@ -1189,7 +1204,7 @@ function TargetSettingsContent(props: {
<PageLayoutContent>
{currentOrganization && currentProject && currentTarget && organizationForSettings ? (
<div className="space-y-12">
{props.page === 'general' ? (
{props.page === 'general' && hasSettingsAccess ? (
<>
<TargetName
targetName={currentTarget.name}
Expand All @@ -1203,7 +1218,7 @@ function TargetSettingsContent(props: {
organizationId={currentOrganization.cleanId}
graphqlEndpointUrl={currentTarget.graphqlEndpointUrl ?? null}
/>
{canDelete && (
{hasDeleteAccess && (
<TargetDelete
targetId={currentTarget.cleanId}
projectId={currentProject.cleanId}
Expand All @@ -1212,38 +1227,38 @@ function TargetSettingsContent(props: {
)}
</>
) : null}
{props.page === 'cdn' && canAccessTokens ? (
{props.page === 'cdn' && hasReadAccess ? (
<CDNAccessTokens
me={organizationForSettings.me}
organizationId={props.organizationId}
projectId={props.projectId}
targetId={props.targetId}
/>
) : null}
{props.page === 'registry-token' && canAccessTokens ? (
{props.page === 'registry-token' && hasTokensWriteAccess ? (
<RegistryAccessTokens
me={organizationForSettings.me}
organizationId={props.organizationId}
projectId={props.projectId}
targetId={props.targetId}
/>
) : null}
{props.page === 'breaking-changes' ? (
{props.page === 'breaking-changes' && hasSettingsAccess ? (
<ConditionalBreakingChanges
organizationId={props.organizationId}
projectId={props.projectId}
targetId={props.targetId}
/>
) : null}
{props.page === 'base-schema' ? (
{props.page === 'base-schema' && hasRegistryWriteAccess ? (
<ExtendBaseSchema
baseSchema={targetForSettings?.baseSchema ?? ''}
organizationId={props.organizationId}
projectId={props.projectId}
targetId={props.targetId}
/>
) : null}
{props.page === 'schema-contracts' ? (
{props.page === 'schema-contracts' && hasSettingsAccess ? (
<SchemaContracts
organizationId={props.organizationId}
projectId={props.projectId}
Expand Down

0 comments on commit a0c8fc2

Please sign in to comment.