Skip to content

Commit 8ef5a11

Browse files
h-kanazawaryasmi
authored andcommitted
fix(Users Page): Prevents errors when users without "manage roles" scope expand users. (#1451 - [LL-143](https://learningpool.atlassian.net/browse/LL-143))
1 parent b46e4f3 commit 8ef5a11

27 files changed

+133
-130
lines changed

lib/services/auth/filters/getAdminModelFilter.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import includes from 'lodash/includes';
2-
import intersection from 'lodash/intersection';
31
import getScopesFromAuthInfo from 'lib/services/auth/authInfoSelectors/getScopesFromAuthInfo';
42
import getOrgFilter from 'lib/services/auth/filters/getOrgFilter';
53
import NoAccessError from 'lib/errors/NoAccessError';
@@ -10,18 +8,24 @@ import getModelsFilter,
108
checkAllScope
119
} from 'lib/services/auth/filters/utils/getModelsFilter';
1210

13-
const adminModelFilter = ({ viewAllScope, editAllScope }) =>
11+
/**
12+
* @param {string[]} _.viewAllScopes
13+
* @param {string[]} _.editAllScopes
14+
* @return {({ actionName, authInfo }) => Promise}
15+
*/
16+
const adminModelFilter = ({ viewAllScopes, editAllScopes }) =>
1417
async ({ actionName, authInfo }) => {
1518
const scopes = getScopesFromAuthInfo(authInfo);
1619

1720
switch (actionName) {
1821
case 'view': {
19-
const validScopes = intersection(scopes, [viewAllScope, editAllScope]);
20-
if (validScopes.length > 0) return getOrgFilter(authInfo);
22+
const hasValidViewScopes = [...viewAllScopes, ...editAllScopes].some(s => scopes.includes(s));
23+
if (hasValidViewScopes) return getOrgFilter(authInfo);
2124
throw new NoAccessError();
2225
}
2326
default: {
24-
if (includes(scopes, editAllScope)) return getOrgFilter(authInfo);
27+
const hasValidEditScopes = editAllScopes.some(s => scopes.includes(s));
28+
if (hasValidEditScopes) return getOrgFilter(authInfo);
2529
throw new NoAccessError();
2630
}
2731
}

lib/services/auth/filters/getGlobalModelFilter.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import includes from 'lodash/includes';
21
import getScopesFromAuthInfo from 'lib/services/auth/authInfoSelectors/getScopesFromAuthInfo';
32
import getOrgFilter from 'lib/services/auth/filters/getOrgFilter';
43
import NoAccessError from 'lib/errors/NoAccessError';
@@ -9,7 +8,7 @@ import getModelsFilter,
98
checkAllScope
109
} from 'lib/services/auth/filters/utils/getModelsFilter';
1110

12-
const globalModelFilter = ({ editAllScope }) =>
11+
const globalModelFilter = ({ editAllScopes }) =>
1312
async ({ actionName, authInfo }) => {
1413
const scopes = getScopesFromAuthInfo(authInfo);
1514

@@ -20,13 +19,15 @@ const globalModelFilter = ({ editAllScope }) =>
2019
case 'organisation':
2120
return getOrgFilter(authInfo);
2221
default: {
23-
if (includes(scopes, editAllScope)) return getOrgFilter(authInfo);
22+
const isValid = editAllScopes.some(s => scopes.includes(s));
23+
if (isValid) return getOrgFilter(authInfo);
2424
throw new NoAccessError();
2525
}
2626
}
2727
}
2828
default: {
29-
if (includes(scopes, editAllScope)) return getOrgFilter(authInfo);
29+
const isValid = editAllScopes.some(s => scopes.includes(s));
30+
if (isValid) return getOrgFilter(authInfo);
3031
throw new NoAccessError();
3132
}
3233
}

lib/services/auth/filters/getShareableModelFilter.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import includes from 'lodash/includes';
2-
import intersection from 'lodash/intersection';
31
import getScopesFromAuthInfo
42
from 'lib/services/auth/authInfoSelectors/getScopesFromAuthInfo';
53
import getPublicOrgFilter from 'lib/services/auth/filters/getPublicOrgFilter';
@@ -15,22 +13,22 @@ import getModelsFilter,
1513

1614

1715
export const shareableModelFilter = ({
18-
viewAllScope,
19-
viewPublicScope,
20-
editAllScope,
21-
editPublicScope
16+
viewAllScopes,
17+
viewPublicScopes,
18+
editAllScopes,
19+
editPublicScopes,
2220
}) => async ({ actionName, authInfo }) => {
2321
const scopes = getScopesFromAuthInfo(authInfo);
2422

2523
switch (actionName) {
2624
case 'view': {
27-
const validAllScopes = intersection(scopes, [viewAllScope, editAllScope]);
28-
if (validAllScopes.length > 0) {
25+
const hasValidAllScopes = [...viewAllScopes, ...editAllScopes].some(s => scopes.includes(s));
26+
if (hasValidAllScopes) {
2927
return getOrgFilter(authInfo);
3028
}
3129

32-
const validPublicScopes = intersection(scopes, [viewPublicScope, editPublicScope]);
33-
if (validPublicScopes.length > 0) {
30+
const hasValidPublicScopes = [...viewPublicScopes, ...editPublicScopes].some(s => scopes.includes(s));
31+
if (hasValidPublicScopes) {
3432
return getPublicOrgFilter(authInfo);
3533
}
3634

@@ -42,10 +40,10 @@ export const shareableModelFilter = ({
4240
return privateFilter;
4341
}
4442
default: {
45-
if (includes(scopes, editAllScope)) {
43+
if (editAllScopes.some(s => scopes.includes(s))) {
4644
return getOrgFilter(authInfo);
4745
}
48-
if (includes(scopes, editPublicScope)) {
46+
if (editPublicScopes.some(s => scopes.includes(s))) {
4947
return getPublicOrgFilter(authInfo);
5048
}
5149
const privateFilter = getPrivateOrgFilter(authInfo);

lib/services/auth/modelFilters/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import getAdminModelFilter
33
from 'lib/services/auth/filters/getAdminModelFilter';
44

55
export default getAdminModelFilter({
6-
viewAllScope: MANAGE_ALL_CLIENTS,
7-
editAllScope: MANAGE_ALL_CLIENTS,
6+
viewAllScopes: [MANAGE_ALL_CLIENTS],
7+
editAllScopes: [MANAGE_ALL_CLIENTS],
88
});

lib/services/auth/modelFilters/dashboard.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ export const filters = [
4848
];
4949

5050
export default getModelsFilter({
51-
viewAllScope: VIEW_ALL_DASHBOARDS,
52-
editAllScope: EDIT_ALL_DASHBOARDS,
53-
viewPublicScope: VIEW_PUBLIC_DASHBOARDS,
54-
editPublicScope: EDIT_PUBLIC_DASHBOARDS,
51+
viewAllScopes: [VIEW_ALL_DASHBOARDS],
52+
editAllScopes: [EDIT_ALL_DASHBOARDS],
53+
viewPublicScopes: [VIEW_PUBLIC_DASHBOARDS],
54+
editPublicScopes: [EDIT_PUBLIC_DASHBOARDS],
5555
allowedTokenTypes: ['organisation', 'dashboard', 'client'],
5656
filters
5757
});

lib/services/auth/modelFilters/download.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import getShareableModelFilter
88
from 'lib/services/auth/filters/getShareableModelFilter';
99

1010
export default getShareableModelFilter({
11-
viewAllScope: VIEW_ALL_DOWNLOADS,
12-
editAllScope: EDIT_ALL_DOWNLOADS,
13-
viewPublicScope: VIEW_PUBLIC_DOWNLOADS,
14-
editPublicScope: EDIT_PUBLIC_DOWNLOADS
11+
viewAllScopes: [VIEW_ALL_DOWNLOADS],
12+
editAllScopes: [EDIT_ALL_DOWNLOADS],
13+
viewPublicScopes: [VIEW_PUBLIC_DOWNLOADS],
14+
editPublicScopes: [EDIT_PUBLIC_DOWNLOADS],
1515
});

lib/services/auth/modelFilters/export.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import getShareableModelFilter
88
from 'lib/services/auth/filters/getShareableModelFilter';
99

1010
export default getShareableModelFilter({
11-
viewAllScope: VIEW_ALL_EXPORTS,
12-
editAllScope: EDIT_ALL_EXPORTS,
13-
viewPublicScope: VIEW_PUBLIC_EXPORTS,
14-
editPublicScope: EDIT_PUBLIC_EXPORTS
11+
viewAllScopes: [VIEW_ALL_EXPORTS],
12+
editAllScopes: [EDIT_ALL_EXPORTS],
13+
viewPublicScopes: [VIEW_PUBLIC_EXPORTS],
14+
editPublicScopes: [EDIT_PUBLIC_EXPORTS],
1515
});

lib/services/auth/modelFilters/lrs.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import getGlobalModelFilter
33
from 'lib/services/auth/filters/getGlobalModelFilter';
44

55
export default getGlobalModelFilter({
6-
editAllScope: MANAGE_ALL_STORES,
6+
editAllScopes: [MANAGE_ALL_STORES],
77
});

lib/services/auth/modelFilters/persona.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import getGlobalModelFilter
33
from 'lib/services/auth/filters/getGlobalModelFilter';
44

55
export default getGlobalModelFilter({
6-
editAllScope: MANAGE_ALL_PERSONAS,
6+
editAllScopes: [MANAGE_ALL_PERSONAS],
77
});

lib/services/auth/modelFilters/query.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import getShareableModelFilter
88
from 'lib/services/auth/filters/getShareableModelFilter';
99

1010
export default getShareableModelFilter({
11-
viewAllScope: VIEW_ALL_QUERIES,
12-
editAllScope: EDIT_ALL_QUERIES,
13-
viewPublicScope: VIEW_PUBLIC_QUERIES,
14-
editPublicScope: EDIT_PUBLIC_QUERIES
11+
viewAllScopes: [VIEW_ALL_QUERIES],
12+
editAllScopes: [EDIT_ALL_QUERIES],
13+
viewPublicScopes: [VIEW_PUBLIC_QUERIES],
14+
editPublicScopes: [EDIT_PUBLIC_QUERIES],
1515
});

0 commit comments

Comments
 (0)