Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

VAR-325 | Change createIsStaffSelector to respect manager role #1118

Merged
merged 1 commit into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/state/selectors/__tests__/authSelectors.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import User from '../../../utils/fixtures/User';
import Resource from '../../../utils/fixtures/Resource';
import { getState } from '../../../utils/testUtils';
import {
currentUserSelector,
isAdminSelector,
isLoggedInSelector,
createIsStaffSelector,
} from '../authSelectors';

describe('state/selectors/authSelectors', () => {
Expand Down Expand Up @@ -82,4 +84,24 @@ describe('state/selectors/authSelectors', () => {
expect(getSelected({ token: 'mock-token', userId: 'u-1' })).toBe(true);
});
});

describe('createIsStaffSelector', () => {
const getMockResource = overrides => ({
...Resource.build(),
...overrides,
});
const mockResourceSelector = overrides => () => getMockResource(overrides);

test('returns true when the user has unit admin permission for resource', () => {
const selector = createIsStaffSelector(mockResourceSelector({ userPermissions: { isAdmin: true } }));

expect(selector()).toEqual(true);
});

test('returns true when the user has unit manager permission for resource', () => {
const selector = createIsStaffSelector(mockResourceSelector({ userPermissions: { isManager: true } }));

expect(selector()).toEqual(true);
});
});
});
36 changes: 33 additions & 3 deletions app/state/selectors/authSelectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,46 @@ function isLoggedInSelector(state) {
}

/**
* Check if a user has admin permission for a unit.
* TODO: Find a better name for this.
* Check if the user has admin level permissions for the resource. I.e.
* if they are a unit admin for the resource in question.
*/
function createIsStaffSelector(resourceSelector) {
function createIsUnitAdminSelector(resourceSelector) {
return createSelector(
resourceSelector,
resource => Boolean(get(resource, 'userPermissions.isAdmin', false)),
);
}

/**
* Check if the user has manager level permissions for the resource.
* I.e. if they are a unit manager for the resource in question.
*/
function createIsUnitManagerSelector(resourceSelector) {
return createSelector(
resourceSelector,
resource => Boolean(get(resource, 'userPermissions.isManager', false)),
);
}

/**
* Check if a user has admin or manager permission for a unit.
* TODO: Find a better name for this.
*
* 2020/02/07
* I added support for the manager role. According to the spec, the
* unit manager should have the exact same permissions as the unit admin
* has. I couldn't get a good sense of the permission management
* architecture, but it seems like this selector is the easiest way to
* add support for this role
*/
function createIsStaffSelector(resourceSelector) {
return createSelector(
createIsUnitAdminSelector(resourceSelector),
createIsUnitManagerSelector(resourceSelector),
(isAdmin, isManager) => isAdmin || isManager,
);
}

export {
createIsStaffSelector,
currentUserSelector,
Expand Down