Skip to content

Commit d13288a

Browse files
refactor: useGetAccess now uses the canAll endpoint to massively reduce traffic through acquisitions and our module -- ERM-3967 (#765)
* refactor: useGetAccess now uses the canAll endpoint to massively reduce traffic through acquisitions and our module * test(useGetAccess): Fixed broken test
1 parent d404f2d commit d13288a

3 files changed

Lines changed: 28 additions & 103 deletions

File tree

lib/AccessControl/constants/restrictions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export const UPDATE = 'update';
55
export const CLAIM = 'claim';
66
export const APPLY_POLICIES = 'apply_policies';
77

8+
export const ALL = 'ALL'; // Technically not a real restriction, but we surface a "canAll" endpoint now.
9+
10+
// Don't include ALL here
811
export const RESTRICTIONS = [
912
READ,
1013
CREATE,
@@ -35,6 +38,8 @@ export const CAN_CREATE = 'canCreate';
3538
export const CAN_DELETE = 'canDelete';
3639
export const CAN_UPDATE = 'canUpdate';
3740
export const CAN_APPLY_POLICIES = 'canApplyPolicies';
41+
export const CAN_ALL = 'canAll';
42+
3843

3944
// Helper methods to go from restriction -> canAccess or restrictionPolicies. Potentially slightly overkill
4045
export const getCanAccessFromRestriction = (restriction) => {
@@ -49,6 +54,8 @@ export const getCanAccessFromRestriction = (restriction) => {
4954
return CAN_UPDATE;
5055
case APPLY_POLICIES:
5156
return CAN_APPLY_POLICIES;
57+
case ALL: // Technically not a real restriction, but we surface a "canAll" endpoint now.
58+
return CAN_ALL;
5259
default:
5360
return null;
5461
}

lib/AccessControl/hooks/useGetAccess/useGetAccess.js

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,41 @@
1-
// Currently this is handled via 5 different calls to our API (CLAIM is a different thing)
2-
// This isn't the most ideal, so bring together in "nice" single hook
31
import useCanAccess from '../useCanAccess';
42
import {
5-
APPLY_POLICIES,
6-
CREATE,
7-
DELETE,
3+
ALL,
84
getCanAccessFromRestriction,
9-
READ,
10-
UPDATE
115
} from '../../constants';
126

137
const useGetAccess = ({
148
resourceEndpoint,
159
resourceId,
16-
restrictions, // Array of restrictions to fetch,
1710
queryNamespaceGenerator,
1811
queryOptions // Shared between the queries
1912
}) => {
20-
// ---------- EDIT ----------
21-
const canEditQuery = useCanAccess({
13+
const canAccessQuery = useCanAccess({
2214
resourceEndpoint,
2315
resourceId,
24-
restriction: UPDATE,
25-
queryNamespaceGenerator: () => queryNamespaceGenerator(UPDATE, getCanAccessFromRestriction(UPDATE)),
26-
queryOptions: {
27-
enabled: restrictions.includes(UPDATE),
28-
...queryOptions
29-
}
16+
restriction: ALL,
17+
queryNamespaceGenerator: () => queryNamespaceGenerator(ALL, getCanAccessFromRestriction(ALL)),
18+
queryOptions
3019
});
31-
const { canAccess: canEdit, isLoading: canEditLoading } = canEditQuery;
32-
33-
// ---------- CREATE ----------
34-
const canCreateQuery = useCanAccess({
35-
resourceEndpoint,
36-
resourceId,
37-
restriction: CREATE,
38-
queryNamespaceGenerator: () => queryNamespaceGenerator(CREATE, getCanAccessFromRestriction(CREATE)),
39-
queryOptions: {
40-
enabled: restrictions.includes(CREATE),
41-
}
42-
});
43-
const { canAccess: canCreate, isLoading: canCreateLoading } = canCreateQuery;
44-
45-
// ---------- APPLY_POLICIES ----------
46-
const canApplyPoliciesQuery = useCanAccess({
47-
resourceEndpoint,
48-
resourceId,
49-
restriction: APPLY_POLICIES,
50-
queryNamespaceGenerator: () => queryNamespaceGenerator(APPLY_POLICIES, getCanAccessFromRestriction(APPLY_POLICIES)),
51-
queryOptions: {
52-
enabled: restrictions.includes(APPLY_POLICIES),
53-
}
54-
});
55-
const { canAccess: canApplyPolicies, isLoading: canApplyPoliciesLoading } = canApplyPoliciesQuery;
56-
57-
// ---------- READ ----------
58-
const canReadQuery = useCanAccess({
59-
resourceEndpoint,
60-
resourceId,
61-
restriction: READ,
62-
queryNamespaceGenerator: () => queryNamespaceGenerator(READ, getCanAccessFromRestriction(READ)),
63-
queryOptions: {
64-
enabled: restrictions.includes(READ),
65-
}
66-
});
67-
const { canAccess: canRead, isLoading: canReadLoading } = canReadQuery;
68-
69-
// ---------- DELETE ----------
70-
const canDeleteQuery = useCanAccess({
71-
resourceEndpoint,
72-
resourceId,
73-
restriction: DELETE,
74-
queryNamespaceGenerator: () => queryNamespaceGenerator(DELETE, getCanAccessFromRestriction(DELETE)),
75-
queryOptions: {
76-
enabled: restrictions.includes(DELETE),
77-
}
78-
});
79-
const { canAccess: canDelete, isLoading: canDeleteLoading } = canDeleteQuery;
20+
const {
21+
canAccessObj: {
22+
canApplyPolicies,
23+
canCreate,
24+
canDelete,
25+
canRead,
26+
canUpdate: canEdit
27+
} = {},
28+
isLoading
29+
} = canAccessQuery;
8030

8131
return ({
32+
isLoading,
33+
canAccessQuery,
8234
canCreate,
83-
canCreateLoading,
84-
canCreateQuery,
8535
canRead,
86-
canReadLoading,
87-
canReadQuery,
8836
canEdit,
89-
canEditLoading,
90-
canEditQuery,
9137
canDelete,
92-
canDeleteLoading,
93-
canDeleteQuery,
9438
canApplyPolicies,
95-
canApplyPoliciesLoading,
96-
canApplyPoliciesQuery
9739
});
9840
};
9941

lib/AccessControl/hooks/useGetAccess/useGetAccess.test.js

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,38 @@ describe('useGetAccess', () => {
2626
queryNamespaceGenerator,
2727
resourceEndpoint: '/wibble',
2828
resourceId: '123',
29-
restrictions: [READ, UPDATE]
3029
}))?.result;
3130
});
3231

3332
test('useCanAccess called the correct number of times', () => {
34-
expect(useCanAccess).toHaveBeenCalledTimes(5);
33+
expect(useCanAccess).toHaveBeenCalledTimes(1);
3534
});
3635

3736
let mockCall;
3837
describe.each([
3938
{
4039
restriction: UPDATE,
41-
expectedQueryOptions: { enabled: true },
42-
expectedQueryNamespace: ['update', 'canUpdate'],
43-
callIndex: 0
4440
},
4541
{
4642
restriction: CREATE,
47-
expectedQueryOptions: { enabled: false },
48-
expectedQueryNamespace: ['create', 'canCreate'],
49-
callIndex: 1
5043
},
5144
{
5245
restriction: APPLY_POLICIES,
53-
expectedQueryOptions: { enabled: false },
54-
expectedQueryNamespace: ['apply_policies', 'canApplyPolicies'],
55-
callIndex: 2
5646
},
5747
{
5848
restriction: READ,
59-
expectedQueryOptions: { enabled: true },
60-
expectedQueryNamespace: ['read', 'canRead'],
61-
callIndex: 3
6249
},
6350
{
6451
restriction: DELETE,
65-
expectedQueryOptions: { enabled: false },
66-
expectedQueryNamespace: ['delete', 'canDelete'],
67-
callIndex: 4
6852
}
69-
])('useGetAccess $restriction', ({
70-
callIndex,
71-
expectedQueryNamespace,
72-
expectedQueryOptions,
73-
}) => {
53+
])('useGetAccess $restriction', () => {
7454
beforeEach(() => {
75-
mockCall = useCanAccess.mock.calls[callIndex][0];
76-
});
77-
78-
test('queryOptions are as expected', () => {
79-
expect(mockCall).toHaveProperty('queryOptions', expectedQueryOptions);
55+
mockCall = useCanAccess.mock.calls[0][0];
8056
});
8157

8258
test('queryNamespaceGenerator returns expected namespace', () => {
8359
const queryNamespace = mockCall.queryNamespaceGenerator();
84-
expect(queryNamespace).toEqual(expectedQueryNamespace);
60+
expect(queryNamespace).toEqual(['ALL', 'canAll']);
8561
});
8662
});
8763
});

0 commit comments

Comments
 (0)