refactor res.locals and settings

This commit is contained in:
lesion
2022-02-26 21:27:40 +01:00
parent 9e2ed063b6
commit cf3e1c69fa
20 changed files with 133 additions and 102 deletions

View File

@@ -100,7 +100,7 @@ const eventController = {
async get (req, res) {
const format = req.params.format || 'json'
const is_admin = req.user && req.user.is_admin
const is_admin = res.locals.user && res.locals.user.is_admin
const slug = req.params.event_slug
// retrocompatibility, old events URL does not use slug, use id as fallback
@@ -206,7 +206,7 @@ const eventController = {
log.warn(`Trying to confirm a unknown event, id: ${id}`)
return res.sendStatus(404)
}
if (!req.user.is_admin && req.user.id !== event.userId) {
if (!res.locals.user.is_admin && res.locals.user.id !== event.userId) {
log.warn(`Someone unallowed is trying to confirm -> "${event.title} `)
return res.sendStatus(403)
}
@@ -232,7 +232,7 @@ const eventController = {
const id = Number(req.params.event_id)
const event = await Event.findByPk(id)
if (!event) { return req.sendStatus(404) }
if (!req.user.is_admin && req.user.id !== event.userId) {
if (!res.locals.user.is_admin && res.locals.user.id !== event.userId) {
return res.sendStatus(403)
}
@@ -315,7 +315,7 @@ const eventController = {
end_datetime: body.end_datetime,
recurrent,
// publish this event only if authenticated
is_visible: !!req.user
is_visible: !!res.locals.user
}
if (req.file || body.image_url) {
@@ -358,9 +358,9 @@ const eventController = {
}
// associate user to event and reverse
if (req.user) {
await req.user.addEvent(event)
await event.setUser(req.user)
if (res.locals.user) {
await res.locals.user.addEvent(event)
await event.setUser(res.locals.user)
}
// return created event to the client
@@ -382,15 +382,15 @@ const eventController = {
},
async update (req, res) {
if (req.err) {
return res.status(400).json(req.err.toString())
if (res.locals.err) {
return res.status(400).json(res.locals.err.toString())
}
try {
const body = req.body
const event = await Event.findByPk(body.id)
if (!event) { return res.sendStatus(404) }
if (!req.user.is_admin && event.userId !== req.user.id) {
if (!res.locals.user.is_admin && event.userId !== res.locals.user.id) {
return res.sendStatus(403)
}
@@ -470,7 +470,7 @@ const eventController = {
async remove (req, res) {
const event = await Event.findByPk(req.params.id)
// check if event is mine (or user is admin)
if (event && (req.user.is_admin || req.user.id === event.userId)) {
if (event && (res.locals.user.is_admin || res.locals.user.id === event.userId)) {
if (event.media && event.media.length && !event.recurrent) {
const old_path = path.join(config.upload_path, event.media[0].url)
const old_thumb_path = path.join(config.upload_path, 'thumb', event.media[0].url)