Skip to content

Commit

Permalink
Use user's iam groups to prefill faculty selection when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Keskimaki committed Feb 24, 2023
1 parent e9d6785 commit 561cde0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
4 changes: 3 additions & 1 deletion src/client/components/InteractiveForm/SelectFaculty.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import { useTranslation } from 'react-i18next'
import styles from './styles'
import { InputProps, Faculty, Locales } from '../../types'
import useFaculties from '../../hooks/useFaculties'
import useUserFaculties from '../../hooks/useUserFaculties'

const SelectFaculty: React.FC<InputProps> = ({ control }) => {
const { t, i18n } = useTranslation()
const { language } = i18n
const [faculty, setFaculty] = useState('')
const { faculties, isLoading } = useFaculties()
const { userFaculties = [] } = useUserFaculties()

const handleChange = (event: SelectChangeEvent) => {
setFaculty(event.target.value)
Expand All @@ -41,7 +43,7 @@ const SelectFaculty: React.FC<InputProps> = ({ control }) => {
<Controller
control={control}
name="faculty"
defaultValue=""
defaultValue={userFaculties[0]?.code || ''}
render={({ field }) => (
<FormControl sx={{ width: '100%' }}>
<InputLabel>{t('facultySelect:inputLabel')}</InputLabel>
Expand Down
1 change: 1 addition & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,5 @@ export interface User {
email?: string
language?: string
isAdmin: boolean
iamGroups: string[]
}
10 changes: 9 additions & 1 deletion src/server/middeware/user.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { inDevelopment } from '../../config'

const parseIamGroups = (iamGroups: string) =>
iamGroups?.split(';').filter(Boolean) ?? []

const mockHeaders = {
uid: 'testuser',
givenname: 'Testi',
sn: 'Kayttaja',
mail: '[email protected]',
preferredlanguage: 'fi',
hypersonsisuid: 'hy-hlo-123',
hygroupcn: 'grp-toska;hy-mltdk-employees',
}

const userMiddleware = (req, _, next) => {
Expand All @@ -19,15 +23,19 @@ const userMiddleware = (req, _, next) => {
mail: email,
preferredlanguage: language,
hypersonsisuid: id,
hygroupcn,
} = headers

const iamGroups = parseIamGroups(hygroupcn)

const user = {
id,
username,
firstName,
lastName,
email,
language,
id,
iamGroups,
}

req.user = user
Expand Down
13 changes: 5 additions & 8 deletions src/server/routes/faculty.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import express from 'express'

import { inE2EMode } from '../../config'
import { RequestWithUser } from '../types'
import { getOrganisationData, getUserOrganisations } from '../util/jami'
import { OrganisationData, RequestWithUser } from '../types'

const parseIamGroups = (iamGroups: string) =>
iamGroups?.split(';').filter(Boolean) ?? []

const mockFaculty = [
{
Expand All @@ -23,19 +20,19 @@ const facultyRouter = express.Router()
facultyRouter.get('/', async (req, res) => {
if (inE2EMode) return res.send(mockFaculty)

const organisationData: Array<OrganisationData> = await getOrganisationData()
const organisationData = await getOrganisationData()

const faculties = organisationData.map(({ code, name }) => ({ code, name }))

return res.send(faculties)
})

facultyRouter.get('/user', async (req: RequestWithUser, res) => {
const { id } = req.user
const { id, iamGroups } = req.user

const iamGroups = parseIamGroups(req.headers.hygroupcn as string)
const organisationData = await getUserOrganisations(id, iamGroups)

const faculties = await getUserOrganisations(id, iamGroups)
const faculties = organisationData.map(({ code, name }) => ({ code, name }))

return res.send(faculties)
})
Expand Down
1 change: 1 addition & 0 deletions src/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface User {
email?: string
language?: string
isAdmin: boolean
iamGroups: string[]
}

export interface RequestWithUser extends Request {
Expand Down

0 comments on commit 561cde0

Please sign in to comment.