mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 21:04:59 +00:00
* feat(project): add recent viewed files section to project folder tree * feat(webui): disable toolbar buttons and context menu in recents page * test(core): optimize recents cap test execution time * feat(webui): show hint text in recents page top toolbar * feat(webui): rename recents section to Recently Viewed * refactor(core): consolidate recents methods into AssetService * fix(api): forward pagination parameters in folder children endpoint (#349) * fix(project-recents): scope views and disable recents mutations * fix(api): allow recent views in demo mode * fix(api): limit demo recent view exception to post
55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
import type { Prisma } from '@shumai/db'
|
|
import { auth } from '@shumai/core/src/auth/auth'
|
|
import { userService } from '@shumai/core/src/user/user'
|
|
import { teamService } from '@shumai/core/src/team/team'
|
|
import { createMiddleware } from 'hono/factory'
|
|
|
|
type User = Prisma.UserGetPayload<Record<string, never>>
|
|
|
|
export const authMiddleware = createMiddleware<{
|
|
Variables: {
|
|
user: User
|
|
}
|
|
}>(async (c, next) => {
|
|
if (c.get('user')) {
|
|
await next()
|
|
return
|
|
}
|
|
|
|
const session = await auth.api.getSession({
|
|
headers: c.req.raw.headers,
|
|
})
|
|
|
|
if (!session) {
|
|
return c.json({ error: 'Unauthorized' }, 401)
|
|
}
|
|
|
|
try {
|
|
const user = await userService.getUserById(session.user.id)
|
|
|
|
if (!user) {
|
|
return c.json({ error: 'User not found' }, 401)
|
|
}
|
|
|
|
c.set('user', user)
|
|
|
|
if (process.env.SHUMAI_DEMO_MODE === '1') {
|
|
const method = c.req.method.toUpperCase()
|
|
const path = c.req.path
|
|
const isAllowedDemoWrite =
|
|
method === 'POST' && (path.endsWith('/search') || path.endsWith('/recents/view'))
|
|
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && !isAllowedDemoWrite) {
|
|
const hasWritable = await teamService.hasWritableRoleInAnyTeam(user.id)
|
|
|
|
if (!hasWritable) {
|
|
return c.json({ error: 'System is in read-only mode' }, 403)
|
|
}
|
|
}
|
|
}
|
|
|
|
await next()
|
|
} catch {
|
|
return c.json({ error: 'Authentication failed' }, 401)
|
|
}
|
|
})
|