Skip to content

Commit 5280daa

Browse files
committed
Prevent enrolment and responsibility upsert if a user is not in the DB
1 parent 4d1165d commit 5280daa

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

src/server/chatInstances/access.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import { EXAMPLE_COURSE_ID, TEST_COURSE_ID, accessIams } from '../util/config'
2-
import { ChatInstance, Enrolment, Responsibility } from '../db/models'
1+
import { EXAMPLE_COURSE_ID, TEST_COURSE_ID, employeeIam } from '../util/config'
2+
import {
3+
ChatInstance,
4+
Enrolment,
5+
Responsibility,
6+
User as UserModel,
7+
} from '../db/models'
38
import { User } from '../types'
49

5-
export const checkIamAccess = (iamGroups: string[]) =>
6-
accessIams.some((iam) => iamGroups.includes(iam))
10+
const getUserById = async (id: string) => UserModel.findByPk(id)
711

812
/**
913
* Gets the chat instance ids of the courses the user is enrolled in
1014
*/
1115
export const getEnrolledCourses = async (user: User) => {
12-
if (checkIamAccess(user.iamGroups)) {
16+
// Only do the example/test course upserts if the user is an employee
17+
// students have no reason to be in these courses.
18+
// We also want to check if the user exists in the database
19+
// before we try to upsert the enrolments.
20+
if (user.iamGroups.includes(employeeIam) && getUserById(user.id)) {
1321
await Enrolment.upsert(
1422
{
1523
userId: user.id,
@@ -47,7 +55,9 @@ export const getEnrolledCourses = async (user: User) => {
4755
}
4856

4957
export const getOwnCourses = async (user: User) => {
50-
if (user.isAdmin) {
58+
// We want to check if the user exists in the database
59+
// before we try to upsert the enrolments
60+
if (user.isAdmin && getUserById(user.id)) {
5161
await Responsibility.upsert(
5262
{
5363
userId: user.id,

src/server/routes/user.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ import express from 'express'
22

33
import { ChatRequest } from '../types'
44
import logger from '../util/logger'
5-
import {
6-
checkIamAccess,
7-
getEnrolledCourses,
8-
getOwnCourses,
9-
} from '../chatInstances/access'
5+
import { getEnrolledCourses, getOwnCourses } from '../chatInstances/access'
106
import { User } from '../db/models'
117
import { getUserStatus, getUsage } from '../chatInstances/usage'
128
import { DEFAULT_TOKEN_LIMIT } from '../../config'
139
import { getLastRestart } from '../util/lastRestart'
10+
import { accessIams } from '../util/config'
11+
12+
export const checkIamAccess = (iamGroups: string[]) =>
13+
accessIams.some((iam) => iamGroups.includes(iam))
1414

1515
const userRouter = express.Router()
1616

@@ -20,7 +20,7 @@ userRouter.get('/login', async (req, res) => {
2020
const { id, isAdmin, iamGroups } = user
2121

2222
if (!id) return res.status(401).send('Unauthorized')
23-
23+
const lastRestart = await getLastRestart()
2424
const hasIamAccess = checkIamAccess(iamGroups)
2525

2626
const enrolledCourses = await getEnrolledCourses(user)

0 commit comments

Comments
 (0)