Skip to content

Commit c268a44

Browse files
committed
fix
1 parent 638f5e5 commit c268a44

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/client/components/Admin/Statistics.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import useStatistics from '../../hooks/useStatistics'
2020
import { Statistic } from '../../types'
2121
import programme from '../../locales/programme.json'
2222
import faculties from '../../locales/faculties.json'
23+
import useCurrentUser from '../../hooks/useCurrentUser'
2324

2425
const Statistics = () => {
2526
const [from, setFrom] = useState(1)
@@ -28,8 +29,9 @@ const Statistics = () => {
2829
const { statistics, isLoading } = useStatistics()
2930
const { t, i18n } = useTranslation()
3031
const { language } = i18n
32+
const { user, isLoading: isUserLoading } = useCurrentUser()
3133

32-
if (isLoading) return null
34+
if (isLoading || isUserLoading) return null
3335

3436
const namesOf = (codes: string[]) => {
3537
if (!codes || codes.length === 0) return ''
@@ -157,9 +159,13 @@ const Statistics = () => {
157159
<Typography>{chat.codes.join(', ')}</Typography>
158160
</TableCell>
159161
<TableCell align="left">
160-
<Link to={`/courses/${chat.id}`} component={RouterLink}>
162+
{user.isAdmin ? (
163+
<Link to={`/courses/${chat.id}`} component={RouterLink}>
164+
<Typography>{chat.name[language]}</Typography>
165+
</Link>
166+
) : (
161167
<Typography>{chat.name[language]}</Typography>
162-
</Link>
168+
)}
163169
</TableCell>
164170
<TableCell align="left">
165171
<Typography>

src/server/routes/course.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,20 @@ courseRouter.get('/statistics/:id', async (req, res) => {
9494
return res.send({ average, usagePercentage, usages: normalizedUsage })
9595
})
9696

97+
interface AcualResponsibility {
98+
id: string
99+
user: {
100+
id: string
101+
username: string
102+
last_name: string
103+
first_names: string
104+
}
105+
}
106+
97107
courseRouter.get('/:id', async (req, res) => {
98108
const { id } = req.params
109+
const request = req as unknown as RequestWithUser
110+
const { user } = request
99111

100112
const include = [
101113
{
@@ -134,10 +146,16 @@ courseRouter.get('/:id', async (req, res) => {
134146
},
135147
]
136148

137-
const chatInstance = await ChatInstance.findOne({
149+
const chatInstance = (await ChatInstance.findOne({
138150
where: { courseId: id },
139151
include,
140-
})
152+
})) as ChatInstance & { responsibilities: AcualResponsibility[] }
153+
154+
const canAccess =
155+
user.isAdmin ||
156+
chatInstance.responsibilities.map((r) => r.user.id).includes(user.id)
157+
158+
if (!canAccess) throw new Error('Unauthorized')
141159

142160
return res.send(chatInstance)
143161
})

0 commit comments

Comments
 (0)