Skip to content

Commit f90222c

Browse files
mathjazzeemeliflodolo
authored
Check translator rights per string in All Projects (#4412)
Co-authored-by: Eemeli Aro <eemeli@gmail.com> Co-authored-by: Francesco Lodolo <flod@lodolo.net>
1 parent b8f14cd commit f90222c

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎translate/src/hooks/useTranslator.test.js‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,28 @@ import { useTranslator } from './useTranslator';
66
import { vi } from 'vitest';
77

88
vi.spyOn(Hooks, 'useAppSelector');
9+
10+
const ctx = vi.hoisted(() => ({ code: 'mylocale', entity: { project: {} } }));
911
vi.mock('react', async (importOriginal) => {
1012
const actual = await importOriginal();
11-
return { ...actual, useContext: () => ({ code: 'mylocale' }) };
13+
return { ...actual, useContext: () => ctx };
14+
});
15+
16+
beforeEach(() => {
17+
ctx.entity = { project: {} };
1218
});
1319

1420
afterAll(() => {
1521
vi.restoreAllMocks();
1622
});
1723

18-
const fakeSelector = (user) => (sel) =>
19-
sel({
20-
[PROJECT]: { slug: 'myproject' },
21-
[USER]: user,
22-
});
24+
const fakeSelector =
25+
(user, slug = 'myproject') =>
26+
(sel) =>
27+
sel({
28+
[PROJECT]: { slug },
29+
[USER]: user,
30+
});
2331

2432
describe('useTranslator', () => {
2533
it('returns false for non-authenticated users', () => {
@@ -64,4 +72,51 @@ describe('useTranslator', () => {
6472
);
6573
expect(useTranslator()).toBeTruthy();
6674
});
75+
76+
it('returns true for a project the user translates, in the All Projects view', () => {
77+
ctx.entity = { project: { slug: 'myproject' } };
78+
Hooks.useAppSelector.mockImplementation(
79+
fakeSelector(
80+
{
81+
isAuthenticated: true,
82+
canManageLocales: [],
83+
canTranslateLocales: [],
84+
translatorForProjects: { 'mylocale-myproject': true },
85+
},
86+
'all-projects',
87+
),
88+
);
89+
expect(useTranslator()).toBeTruthy();
90+
});
91+
92+
it('uses the given entity rather than the selected one', () => {
93+
ctx.entity = { project: { slug: 'myproject' } };
94+
Hooks.useAppSelector.mockImplementation(
95+
fakeSelector(
96+
{
97+
isAuthenticated: true,
98+
canManageLocales: [],
99+
canTranslateLocales: [],
100+
translatorForProjects: { 'mylocale-myproject': true },
101+
},
102+
'all-projects',
103+
),
104+
);
105+
expect(useTranslator({ project: { slug: 'otherproject' } })).toBeFalsy();
106+
});
107+
108+
it('falls back to the locale permission with no string in hand', () => {
109+
Hooks.useAppSelector.mockImplementation(
110+
fakeSelector(
111+
{
112+
isAuthenticated: true,
113+
canManageLocales: [],
114+
canTranslateLocales: ['mylocale'],
115+
translatorForProjects: { 'mylocale-myproject': false },
116+
},
117+
'all-projects',
118+
),
119+
);
120+
expect(useTranslator()).toBeTruthy();
121+
});
67122
});

‎translate/src/hooks/useTranslator.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { useContext } from 'react';
22

3+
import type { Entity } from '~/api/entity';
4+
import { EntityView } from '~/context/EntityView';
35
import { Locale } from '~/context/Locale';
46
import { useProject } from '~/modules/project';
57
import { USER } from '~/modules/user';
@@ -8,10 +10,13 @@ import { useAppSelector } from '~/hooks';
810
/**
911
* Return true if the user has translator permission for the current project
1012
* and locale.
13+
*
14+
* @param entity Used in the All Projects view to check the permission.
1115
*/
12-
export function useTranslator(): boolean {
16+
export function useTranslator(entity?: Entity): boolean {
1317
const { code } = useContext(Locale);
1418
const { slug } = useProject();
19+
const selected = useContext(EntityView).entity;
1520
const {
1621
isAuthenticated,
1722
canManageLocales,
@@ -27,7 +32,10 @@ export function useTranslator(): boolean {
2732
return true;
2833
}
2934

30-
const localeProject = `${code}-${slug}`;
35+
const projectSlug =
36+
slug === 'all-projects' ? (entity ?? selected).project.slug : slug;
37+
38+
const localeProject = `${code}-${projectSlug}`;
3139
if (Object.hasOwnProperty.call(translatorForProjects, localeProject)) {
3240
return translatorForProjects[localeProject];
3341
}

‎translate/src/modules/entitieslist/components/Entity.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function Entity({
5454
selectEntity,
5555
toggleForBatchEditing,
5656
}: Props): React.ReactElement<'li'> {
57-
const isTranslator = useTranslator();
57+
const isTranslator = useTranslator(entity);
5858
const [areSiblingsActive, setSiblingsActive] = useState(false);
5959
const entitiesList = useContext(EntitiesList);
6060

0 commit comments

Comments
 (0)