Skip to content
Open
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
1 change: 1 addition & 0 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ jobs:
FBS_EMAIL: FacilitiesBusinessSystem@stfc.ac.uk
PROFILE_PAGE_LINK: http://localhost:9003/auth/ManageDetails.aspx
EXTERNAL_AUTH_HOMEPAGE_URL: http://localhost:9003/auth/Menu.aspx
TRANSLATION_PATH: /config/locales/
run: |
REPO_DIR_NAME=$(basename $GITHUB_WORKSPACE)

Expand Down
20 changes: 20 additions & 0 deletions apps/backend/i18next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import path from 'path';

import i18next from 'i18next';
import Backend from 'i18next-fs-backend';

i18next.use(Backend).init({
initAsync: false,
lng: 'override',
fallbackLng: 'override',
backend: {
loadPath: path.resolve(
process.env.TRANSLATION_PATH || '',
'{{lng}}/translation.json'
),
},
});

i18next.languages = ['override'];

export default i18next;
46 changes: 45 additions & 1 deletion apps/backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"graphql": "^16.13.2",
"graphql-request": "^7.4.0",
"graphql-tag": "^2.12.6",
"i18next": "^26.3.4",
"i18next-fs-backend": "^2.6.6",
"jsonpath": "^1.2.1",
"jsonwebtoken": "^9.0.1",
"knex": "^3.1.0",
Expand Down Expand Up @@ -101,6 +103,7 @@
"@types/email-templates": "^8.0.4",
"@types/express": "^4.17.13",
"@types/express-jwt": "^6.0.4",
"@types/i18n": "^0.13.12",
"@types/jest": "^29.5.6",
"@types/jsonpath": "^0.2.0",
"@types/jsonwebtoken": "^9.0.2",
Expand Down
11 changes: 11 additions & 0 deletions apps/backend/src/auth/UserAuthorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Tokens } from '../config/Tokens';
import { FapDataSource } from '../datasources/FapDataSource';
import { InternalReviewDataSource } from '../datasources/InternalReviewDataSource';
import { ProposalDataSource } from '../datasources/ProposalDataSource';
import { RoleDataSource } from '../datasources/RoleDataSource';
import { UserDataSource } from '../datasources/UserDataSource';
import { VisitDataSource } from '../datasources/VisitDataSource';
import { Institution } from '../models/Institution';
Expand Down Expand Up @@ -36,6 +37,10 @@ export abstract class UserAuthorization {
Tokens.AdminDataSource
);

protected roleDataSource: RoleDataSource = container.resolve(
Tokens.RoleDataSource
);

protected getUniqueId(user: ValidUserInfo) {
return user.sub;
}
Expand Down Expand Up @@ -197,6 +202,12 @@ export abstract class UserAuthorization {
return readableUsers.includes(id);
}

async getCurrentRoleTags(agent: UserWithRole | null) {
return agent?.currentRole?.id == null
? []
: this.roleDataSource.getTagsByRoleId(agent.currentRole.id);
}

abstract externalTokenLogin(
token: string,
redirectUri: string,
Expand Down
37 changes: 27 additions & 10 deletions apps/backend/src/datasources/stfc/StfcUserDataSource.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StfcUserDataSource } from './StfcUserDataSource';
import { Roles, createRole } from '../../models/Role';
import { dummyUser } from '../mockups/UserDataSource';
import RoleDataSource from '../postgres/RoleDataSource';
import PostgresUserDataSource from '../postgres/UserDataSource';

jest.mock('../postgres/UserDataSource.ts');
Expand Down Expand Up @@ -164,6 +165,12 @@ describe('Role tests', () => {
);
mockGetUserRoles.mockImplementation(() => Promise.resolve([]));

const mockGetTagsByRoleId = jest.spyOn(
RoleDataSource.prototype,
'getTagsByRoleId'
);
mockGetTagsByRoleId.mockImplementation(() => Promise.resolve([]));

const mockEnsureDummyUserExists = jest.spyOn(
StfcUserDataSource.prototype,
'ensureDummyUserExists'
Expand All @@ -187,6 +194,7 @@ describe('Role tests', () => {
title: 'User',
description: '',
isRootRole: true,
tags: [],
})
);
});
Expand All @@ -198,16 +206,25 @@ describe('Role tests', () => {
userdataSource.getUserRoles(dummyUserNumber)
).resolves.toEqual(
expect.arrayContaining([
createRole(1, Roles.USER, 'User', '', { note: '' }, true),
createRole(2, Roles.USER_OFFICER, 'User Officer', '', {}, true),
createRole(
3,
Roles.INSTRUMENT_SCIENTIST,
'Instrument Scientist',
'',
{},
true
),
{
...createRole(1, Roles.USER, 'User', '', { note: '' }, true),
tags: [],
},
{
...createRole(2, Roles.USER_OFFICER, 'User Officer', '', {}, true),
tags: [],
},
{
...createRole(
3,
Roles.INSTRUMENT_SCIENTIST,
'Instrument Scientist',
'',
{},
true
),
tags: [],
},
])
);
});
Expand Down
13 changes: 12 additions & 1 deletion apps/backend/src/datasources/stfc/StfcUserDataSource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '@user-office-software/duo-logger';

import { createUOWSClient } from './UOWSClient';
import { BasicPersonDetailsDTO } from '../../../generated/models/BasicPersonDetailsDTO';
import { PermissionUserGroupDTO } from '../../../generated/models/PermissionUserGroupDTO';
import { RoleDTO } from '../../../generated/models/RoleDTO';
Expand All @@ -12,11 +13,12 @@ import { UpdateUserByIdArgs } from '../../resolvers/mutations/UpdateUserMutation
import { UsersArgs } from '../../resolvers/queries/UsersQuery';
import { Cache } from '../../utils/Cache';
import { PaginationSortDirection } from '../../utils/pagination';
import RoleDataSource from '../postgres/RoleDataSource';
import PostgresUserDataSource from '../postgres/UserDataSource';
import { UserDataSource } from '../UserDataSource';
import { createUOWSClient } from './UOWSClient';

const postgresUserDataSource = new PostgresUserDataSource();
const roleDataSource = new RoleDataSource();

const UOWSClient = createUOWSClient();

Expand Down Expand Up @@ -454,6 +456,7 @@ export class StfcUserDataSource implements UserDataSource {

return stfcRawRolesRequest!;
}

async getUserRoles(id: number): Promise<Role[]> {
const cachedRoles = this.uopRolesCache.get(String(id));
if (cachedRoles) {
Expand All @@ -477,6 +480,8 @@ export class StfcUserDataSource implements UserDataSource {
return [];
}

userRole.tags = await roleDataSource.getTagsByRoleId(userRole.id);

if (!stfcRoles || stfcRoles.length == 0) {
return [userRole];
}
Expand All @@ -490,6 +495,12 @@ export class StfcUserDataSource implements UserDataSource {

const uniqueRoles: Role[] = [...new Set(combinedUserRoles)];

for (let i = 0; i < uniqueRoles.length; i++) {
uniqueRoles[i].tags = await roleDataSource.getTagsByRoleId(
uniqueRoles[i].id
);
}

uniqueRoles.sort((a, b) => a.id - b.id);

const userRoles = [userRole, ...uniqueRoles];
Expand Down
24 changes: 15 additions & 9 deletions apps/backend/src/middlewares/factory/xlsx.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import { container } from 'tsyringe';

import i18next from '../../../i18next';
import { UserAuthorization } from '../../auth/UserAuthorization';
import { Tokens } from '../../config/Tokens';
import callFactoryService, {
Expand Down Expand Up @@ -165,13 +166,26 @@ router.get(`/${XLSXType.CALL_FAP}/:call_id`, async (req, res, next) => {
});

router.get(`/${XLSXType.TECHNIQUE}/:proposal_pks`, async (req, res, next) => {
const userAuthorization = container.resolve<UserAuthorization>(
Tokens.UserAuthorization
);

const userWithRole = {
...res.locals.agent,
};

const roleTags = await userAuthorization.getCurrentRoleTags(userWithRole);
const translationForFirstRoleTag = !roleTags.length
? ''
: roleTags[0].name + '.';

const techniqueProposalDataColumns = [
'Proposal ID',
'Title',
'Principal Investigator',
'PI Email',
'Date Submitted',
'Technique',
i18next.t(`${translationForFirstRoleTag}Technique`),
'Instrument',
'Status',
];
Expand All @@ -181,19 +195,11 @@ router.get(`/${XLSXType.TECHNIQUE}/:proposal_pks`, async (req, res, next) => {
throw new Error('Not authorized');
}

const userWithRole = {
...res.locals.agent,
};

const proposalPks: number[] = req.params.proposal_pks
.split(',')
.map((n: string) => parseInt(n))
.filter((id: number) => !isNaN(id));

const userAuthorization = container.resolve<UserAuthorization>(
Tokens.UserAuthorization
);

if (
!userAuthorization.isUserOfficer(userWithRole) &&
!userAuthorization.isInstrumentScientist(userWithRole)
Expand Down
3 changes: 3 additions & 0 deletions apps/backend/src/models/Role.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Tag } from './Tag';

export enum Roles {
USER = 'user',
USER_OFFICER = 'user_officer',
Expand All @@ -23,6 +25,7 @@ type RoleBase = {
title: string;
description: string;
isRootRole: boolean;
tags?: Tag[];
};

export type Role =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import InputLabel from '@mui/material/InputLabel';
import ListSubheader from '@mui/material/ListSubheader';
import MenuItem from '@mui/material/MenuItem';
import Select from '@mui/material/Select';
import React, { Dispatch } from 'react';
import React, { useContext, Dispatch } from 'react';
import { useTranslation } from 'react-i18next';
import { useSearchParams } from 'react-router-dom';

import { UserContext } from 'context/UserContextProvider';
import { TechniqueMinimalFragment, TechniqueFilterInput } from 'generated/sdk';

export enum TechniqueFilterEnum {
Expand All @@ -35,18 +36,24 @@ const TechniqueFilter = ({
shouldShowMultiple,
showMultiTechniqueProposals,
}: TechniqueFilterProps) => {
const { roles, currentRoleId } = useContext(UserContext);
const [, setSearchParams] = useSearchParams();
const { t } = useTranslation();

if (techniques === undefined) {
return null;
}

const currentRoleTags = roles.find((r) => currentRoleId === r.id)!.tags;
const currentRoleFirstTagName = currentRoleTags?.length
? currentRoleTags[0].name
: '';

return (
<>
<FormControl fullWidth>
<InputLabel id="technique-select-label" shrink>
{t('Technique')}
{t(`${currentRoleFirstTagName}.Technique`)}
</InputLabel>
{isLoading ? (
<Box sx={{ minHeight: '32px', marginTop: '16px' }}>Loading...</Box>
Expand Down
Loading
Loading