Skip to content

Commit

Permalink
fix: check permissions even for pats
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Dec 6, 2023
1 parent 4478476 commit 7cf05d3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
19 changes: 8 additions & 11 deletions packages/web/src/ui/KeyDialog/dialogContext/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export const [DialogProvider, useDialogActions, useDialogContext] =

const [selectedNs, setSelectedNs] = useState<string>(props.namespace);
const [tags, setTags] = useState<string[]>([]);
const isPat = getApiKeyType(props.uiProps.apiKey) === 'tgpat';

const {
screenshots,
Expand All @@ -98,8 +97,8 @@ export const [DialogProvider, useDialogActions, useDialogContext] =
const scopesLoadable = useApiQuery({
url: '/v2/api-keys/current-permissions',
method: 'get',
options: {
enabled: !isPat,
query: {
projectId: props.uiProps.projectId,
},
});

Expand Down Expand Up @@ -426,16 +425,15 @@ export const [DialogProvider, useDialogActions, useDialogContext] =
const scopes = scopesLoadable.data?.scopes;

const canEditSomething =
isAuthorizedTo('screenshots.upload', scopes, isPat) ||
isAuthorizedTo('screenshots.delete', scopes, isPat) ||
isAuthorizedTo('screenshots.upload', scopes) ||
isAuthorizedTo('screenshots.delete', scopes) ||
(translationsLoadable.data?._embedded?.keys?.length
? isAuthorizedTo('translations.edit', scopes, isPat)
: isAuthorizedTo('keys.edit', scopes, isPat));
? isAuthorizedTo('translations.edit', scopes)
: isAuthorizedTo('keys.edit', scopes));

const formDisabled = !isPat && (loading || !canEditSomething);
const formDisabled = loading || !canEditSomething;

const canEditTags =
!formDisabled && isAuthorizedTo('keys.edit', scopes, isPat);
const canEditTags = !formDisabled && isAuthorizedTo('keys.edit', scopes);

const keyExists = Boolean(
translationsLoadable.data?._embedded?.keys?.length
Expand Down Expand Up @@ -469,7 +467,6 @@ export const [DialogProvider, useDialogActions, useDialogContext] =
stateChangableLanguageIds,
tags,
canEditTags,
isPat,
} as const;

const actions = {
Expand Down
8 changes: 2 additions & 6 deletions packages/web/src/ui/KeyDialog/dialogContext/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ export function getImgSize(url: string) {
});
}

export function isAuthorizedTo(
scope: string,
scopes: string[] | undefined,
isPat: boolean
) {
return Boolean(scopes?.includes(scope)) || isPat;
export function isAuthorizedTo(scope: string, scopes: string[] | undefined) {
return Boolean(scopes?.includes(scope));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { isAuthorizedTo } from './tools';

export const usePermissions = () => {
const scopes = useDialogContext((c) => c.scopes);
const isPat = useDialogContext((c) => c.isPat);

return (scope: string) => isAuthorizedTo(scope, scopes, isPat);
return (scope: string) => isAuthorizedTo(scope, scopes);
};

0 comments on commit 7cf05d3

Please sign in to comment.