Skip to content

Commit

Permalink
fix: update permissions with tasks (#3362)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 authored Oct 9, 2024
1 parent b01a680 commit 6443b1b
Show file tree
Hide file tree
Showing 3 changed files with 1,259 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export const [DialogProvider, useDialogActions, useDialogContext] =

const permissions = useComputedPermissions(
scopesLoadable.data,
keyExists,
translationsLoadable?.data?._embedded?.keys?.[0],
languagesLoadable.data?._embedded?.languages
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import { isAuthorizedTo, isLanguagePermitted } from '../../tools/permissions';

type ApiKeyPermissionsModel = components['schemas']['ApiKeyPermissionsModel'];
type LanguageModel = components['schemas']['LanguageModel'];
type KeyWithTranslationsModel =
components['schemas']['KeyWithTranslationsModel'];

export const getComputedPermissions = (
permissions: ApiKeyPermissionsModel | undefined,
keyExists: boolean | undefined,
keyData: KeyWithTranslationsModel | undefined,
availableLanguages: LanguageModel[] | undefined
) => {
const has = (scope: string) => isAuthorizedTo(scope, permissions?.scopes);
const keyExists = Boolean(keyData);

const canCreateKey = has('keys.create');

Expand All @@ -26,35 +29,41 @@ export const getComputedPermissions = (
const canUploadScreenshots = has('screenshots.upload') && keyCreationOk;
const canDeleteScreenshots = has('screenshots.delete') && keyCreationOk;
const canSendBigMeta = has('translations.edit');
const isAssignedToTranslation = keyData?.tasks?.find((i) => i?.userAssigned);

const canSubmitForm =
canEditTranslations ||
canEditStates ||
canEditTags ||
canUploadScreenshots ||
canDeleteScreenshots;
canDeleteScreenshots ||
isAssignedToTranslation;

const getLanguageId = (language: string) => {
return availableLanguages?.find((l) => l.tag === language)?.id;
};

const canEditTranslation = (language: string) => {
const firstTask = keyData?.tasks?.find((t) => t.languageTag === language);
return (
canEditTranslations &&
isLanguagePermitted(
getLanguageId(language),
permissions?.translateLanguageIds
)
(canEditTranslations &&
isLanguagePermitted(
getLanguageId(language),
permissions?.translateLanguageIds
)) ||
(firstTask?.userAssigned && firstTask.type === 'TRANSLATE')
);
};

const canEditState = (language: string) => {
const firstTask = keyData?.tasks?.find((t) => t.languageTag === language);
return (
canEditStates &&
isLanguagePermitted(
getLanguageId(language),
permissions?.stateChangeLanguageIds
)
(canEditStates &&
isLanguagePermitted(
getLanguageId(language),
permissions?.stateChangeLanguageIds
)) ||
(firstTask?.userAssigned && firstTask.type === 'REVIEW')
);
};

Expand All @@ -73,11 +82,11 @@ export const getComputedPermissions = (

export const useComputedPermissions = (
permissions: ApiKeyPermissionsModel | undefined,
keyExists: boolean | undefined,
keyData: KeyWithTranslationsModel | undefined,
availableLanguages: LanguageModel[] | undefined
) => {
return useMemo(
() => getComputedPermissions(permissions, keyExists, availableLanguages),
[permissions, keyExists, availableLanguages]
() => getComputedPermissions(permissions, keyData, availableLanguages),
[permissions, keyData, availableLanguages]
);
};
Loading

0 comments on commit 6443b1b

Please sign in to comment.