2019-04-05 00:10:19 +02:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const crypto = require('crypto')
|
|
|
|
const { Op } = require('sequelize')
|
2020-01-15 23:37:25 +01:00
|
|
|
const sanitizeHtml = require('sanitize-html')
|
2019-06-21 23:52:18 +02:00
|
|
|
const config = require('config')
|
2019-04-05 00:10:19 +02:00
|
|
|
const mail = require('../mail')
|
2019-12-04 00:50:15 +01:00
|
|
|
const { user: User, event: Event, tag: Tag, place: Place } = require('../models')
|
2019-06-21 23:52:18 +02:00
|
|
|
const settingsController = require('./settings')
|
2019-09-25 14:38:16 +02:00
|
|
|
const debug = require('debug')('user:controller')
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
const userController = {
|
2019-09-11 19:12:24 +02:00
|
|
|
async delEvent (req, res) {
|
2019-04-03 00:25:12 +02:00
|
|
|
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.image_path) {
|
2019-06-11 17:44:11 +02:00
|
|
|
const old_path = path.join(config.upload_path, event.image_path)
|
|
|
|
const old_thumb_path = path.join(config.upload_path, 'thumb', event.image_path)
|
2019-05-30 12:04:14 +02:00
|
|
|
try {
|
2019-10-24 15:20:09 +02:00
|
|
|
fs.unlinkSync(old_thumb_path)
|
|
|
|
fs.unlinkSync(old_path)
|
2019-05-30 12:04:14 +02:00
|
|
|
} catch (e) {
|
2019-10-02 21:04:03 +02:00
|
|
|
debug(e)
|
2019-05-30 12:04:14 +02:00
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
2019-10-02 21:04:03 +02:00
|
|
|
const notifier = require('../../notifier')
|
|
|
|
await notifier.notifyEvent('Delete', event.id)
|
2019-04-03 00:25:12 +02:00
|
|
|
await event.destroy()
|
|
|
|
res.sendStatus(200)
|
|
|
|
} else {
|
|
|
|
res.sendStatus(403)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-09 15:06:25 +01:00
|
|
|
/**
|
|
|
|
* add event
|
|
|
|
*/
|
2019-09-11 19:12:24 +02:00
|
|
|
async addEvent (req, res) {
|
2020-01-29 22:16:11 +01:00
|
|
|
// req.err comes from multer streaming error
|
2020-01-15 23:37:25 +01:00
|
|
|
if (req.err) {
|
|
|
|
debug(req.err)
|
|
|
|
return res.status(400).json(req.err.toString())
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
try {
|
|
|
|
const body = req.body
|
|
|
|
const recurrent = body.recurrent ? JSON.parse(body.recurrent) : null
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
const eventDetails = {
|
|
|
|
title: body.title,
|
|
|
|
// remove html tags
|
|
|
|
description: sanitizeHtml(body.description),
|
|
|
|
multidate: body.multidate,
|
|
|
|
start_datetime: body.start_datetime,
|
|
|
|
end_datetime: body.end_datetime,
|
|
|
|
recurrent,
|
|
|
|
// publish this event only if authenticated
|
|
|
|
is_visible: !!req.user
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
if (req.file) {
|
|
|
|
eventDetails.image_path = req.file.filename
|
|
|
|
}
|
|
|
|
|
|
|
|
const event = await Event.create(eventDetails)
|
|
|
|
|
|
|
|
// create place if needed
|
|
|
|
const place = await Place.findOrCreate({
|
2020-01-15 23:37:25 +01:00
|
|
|
where: { name: body.place_name },
|
|
|
|
defaults: { address: body.place_address }
|
|
|
|
})
|
2019-04-03 00:25:12 +02:00
|
|
|
.spread((place, created) => place)
|
|
|
|
await event.setPlace(place)
|
2019-06-26 14:44:21 +02:00
|
|
|
event.place = place
|
2019-10-02 21:04:03 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
// create/assign tags
|
|
|
|
if (body.tags) {
|
|
|
|
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
|
|
|
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
|
|
|
|
await Promise.all(tags.map(t => t.update({ weigth: Number(t.weigth) + 1 })))
|
|
|
|
await event.addTags(tags)
|
|
|
|
event.tags = tags
|
|
|
|
}
|
2019-06-26 14:44:21 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
// associate user to event and reverse
|
|
|
|
if (req.user) {
|
|
|
|
await req.user.addEvent(event)
|
|
|
|
await event.setUser(req.user)
|
|
|
|
}
|
2019-06-25 01:05:38 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
// return created event to the client
|
|
|
|
res.json(event)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
2020-01-29 22:16:11 +01:00
|
|
|
// send notification (mastodon/email)
|
|
|
|
// only if user is authenticated
|
|
|
|
if (req.user) {
|
|
|
|
const notifier = require('../../notifier')
|
|
|
|
notifier.notifyEvent('Create', event.id)
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
res.sendStatus(400)
|
|
|
|
debug(e.toString())
|
2019-11-06 11:29:00 +01:00
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async updateEvent (req, res) {
|
2020-01-15 23:37:25 +01:00
|
|
|
if (req.err) {
|
|
|
|
return res.status(400).json(req.err.toString())
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
const body = req.body
|
|
|
|
const event = await Event.findByPk(body.id)
|
|
|
|
if (!req.user.is_admin && event.userId !== req.user.id) {
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.file) {
|
|
|
|
if (event.image_path) {
|
2019-06-11 17:44:11 +02:00
|
|
|
const old_path = path.resolve(config.upload_path, event.image_path)
|
|
|
|
const old_thumb_path = path.resolve(config.upload_path, 'thumb', event.image_path)
|
2019-04-03 00:25:12 +02:00
|
|
|
await fs.unlink(old_path, e => console.error(e))
|
|
|
|
await fs.unlink(old_thumb_path, e => console.error(e))
|
|
|
|
}
|
|
|
|
body.image_path = req.file.filename
|
|
|
|
}
|
|
|
|
|
2020-01-15 23:37:25 +01:00
|
|
|
body.description = sanitizeHtml(body.description)
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
await event.update(body)
|
|
|
|
let place
|
|
|
|
try {
|
2020-01-15 23:37:25 +01:00
|
|
|
place = await Place.findOrCreate({
|
|
|
|
where: { name: body.place_name },
|
|
|
|
defaults: { address: body.place_address }
|
|
|
|
}).spread((place, created) => place)
|
2019-04-03 00:25:12 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.log('error', e)
|
|
|
|
}
|
|
|
|
await event.setPlace(place)
|
|
|
|
await event.setTags([])
|
|
|
|
if (body.tags) {
|
|
|
|
await Tag.bulkCreate(body.tags.map(t => ({ tag: t })), { ignoreDuplicates: true })
|
|
|
|
const tags = await Tag.findAll({ where: { tag: { [Op.in]: body.tags } } })
|
|
|
|
await event.addTags(tags)
|
|
|
|
}
|
2019-06-25 01:05:38 +02:00
|
|
|
const newEvent = await Event.findByPk(event.id, { include: [Tag, Place] })
|
|
|
|
res.json(newEvent)
|
2019-10-02 21:04:03 +02:00
|
|
|
const notifier = require('../../notifier')
|
|
|
|
notifier.notifyEvent('Update', event.id)
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async forgotPassword (req, res) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const email = req.body.email
|
|
|
|
const user = await User.findOne({ where: { email: { [Op.eq]: email } } })
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.sendStatus(200) }
|
2019-04-03 00:25:12 +02:00
|
|
|
|
|
|
|
user.recover_code = crypto.randomBytes(16).toString('hex')
|
2019-06-10 00:40:37 +02:00
|
|
|
mail.send(user.email, 'recover', { user, config })
|
2019-05-30 12:04:14 +02:00
|
|
|
|
2019-04-03 00:25:12 +02:00
|
|
|
await user.save()
|
|
|
|
res.sendStatus(200)
|
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async checkRecoverCode (req, res) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const recover_code = req.body.recover_code
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!recover_code) { return res.sendStatus(400) }
|
2019-04-03 00:25:12 +02:00
|
|
|
const user = await User.findOne({ where: { recover_code: { [Op.eq]: recover_code } } })
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.sendStatus(400) }
|
2019-10-02 21:05:15 +02:00
|
|
|
res.sendStatus(200)
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async updatePasswordWithRecoverCode (req, res) {
|
2019-04-03 00:25:12 +02:00
|
|
|
const recover_code = req.body.recover_code
|
|
|
|
const password = req.body.password
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!recover_code || !password) { return res.sendStatus(400) }
|
2019-04-03 00:25:12 +02:00
|
|
|
const user = await User.findOne({ where: { recover_code: { [Op.eq]: recover_code } } })
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.sendStatus(400) }
|
2019-05-30 12:04:14 +02:00
|
|
|
try {
|
2019-10-02 21:04:24 +02:00
|
|
|
await user.update({ recover_code: '', password })
|
2019-05-30 12:04:14 +02:00
|
|
|
res.sendStatus(200)
|
2019-06-07 17:02:33 +02:00
|
|
|
} catch (e) {
|
2019-05-30 12:04:14 +02:00
|
|
|
res.sendStatus(400)
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-09-12 14:59:51 +02:00
|
|
|
async current (req, res) {
|
2019-10-28 17:33:20 +01:00
|
|
|
if (!req.user) { return res.status(400).send('Not logged') }
|
2019-12-04 00:50:15 +01:00
|
|
|
const user = await User.scope('withoutPassword').findByPk(req.user.id)
|
2019-10-02 21:05:15 +02:00
|
|
|
res.json(user)
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async getAll (req, res) {
|
2019-11-09 15:05:33 +01:00
|
|
|
const users = await User.scope('withoutPassword').findAll({
|
|
|
|
order: [['is_admin', 'DESC'], ['createdAt', 'DESC']]
|
2019-04-03 00:25:12 +02:00
|
|
|
})
|
|
|
|
res.json(users)
|
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async update (req, res) {
|
2019-09-11 11:58:42 +02:00
|
|
|
// user to modify
|
2019-10-28 17:33:20 +01:00
|
|
|
const user = await User.findByPk(req.body.id)
|
2019-09-11 11:58:42 +02:00
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!user) { return res.status(404).json({ success: false, message: 'User not found!' }) }
|
2019-09-11 11:58:42 +02:00
|
|
|
|
|
|
|
if (req.body.id !== req.user.id && !req.user.is_admin) {
|
|
|
|
return res.status(400).json({ succes: false, message: 'Not allowed' })
|
|
|
|
}
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
if (!req.body.password) { delete req.body.password }
|
2019-09-11 11:58:42 +02:00
|
|
|
|
|
|
|
if (!user.is_active && req.body.is_active && user.recover_code) {
|
|
|
|
mail.send(user.email, 'confirm', { user, config })
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
2019-10-14 21:49:59 +02:00
|
|
|
|
|
|
|
await user.update(req.body)
|
2019-09-11 11:58:42 +02:00
|
|
|
res.json(user)
|
2019-04-03 00:25:12 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async register (req, res) {
|
|
|
|
if (!settingsController.settings.allow_registration) { return res.sendStatus(404) }
|
2019-04-03 00:25:12 +02:00
|
|
|
const n_users = await User.count()
|
|
|
|
try {
|
2019-05-30 12:04:14 +02:00
|
|
|
// the first registered user will be an active admin
|
2019-04-03 00:25:12 +02:00
|
|
|
if (n_users === 0) {
|
|
|
|
req.body.is_active = req.body.is_admin = true
|
|
|
|
} else {
|
|
|
|
req.body.is_active = false
|
|
|
|
}
|
2019-05-30 12:04:14 +02:00
|
|
|
|
2019-09-06 11:58:11 +02:00
|
|
|
req.body.recover_code = crypto.randomBytes(16).toString('hex')
|
2019-10-20 14:22:55 +02:00
|
|
|
debug('Register user ', req.body.email)
|
2019-04-03 00:25:12 +02:00
|
|
|
const user = await User.create(req.body)
|
|
|
|
try {
|
2019-10-20 14:22:55 +02:00
|
|
|
debug(`Sending registration email to ${user.email}`)
|
2019-09-06 11:58:11 +02:00
|
|
|
mail.send(user.email, 'register', { user, config })
|
2019-09-24 11:46:11 +02:00
|
|
|
mail.send(config.admin_email, 'admin_register', { user, config })
|
2019-04-03 00:25:12 +02:00
|
|
|
} catch (e) {
|
|
|
|
return res.status(400).json(e)
|
|
|
|
}
|
2019-06-06 23:54:32 +02:00
|
|
|
const payload = {
|
|
|
|
id: user.id,
|
|
|
|
email: user.email,
|
|
|
|
scope: [user.is_admin ? 'admin' : 'user']
|
|
|
|
}
|
2019-06-10 00:40:37 +02:00
|
|
|
const token = jwt.sign(payload, config.secret)
|
2019-06-07 17:02:33 +02:00
|
|
|
res.json({ token, user })
|
2019-04-03 00:25:12 +02:00
|
|
|
} catch (e) {
|
|
|
|
res.status(404).json(e)
|
|
|
|
}
|
2019-06-18 14:45:04 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async create (req, res) {
|
2019-06-18 14:45:04 +02:00
|
|
|
try {
|
|
|
|
req.body.is_active = true
|
2019-07-23 01:31:43 +02:00
|
|
|
req.body.recover_code = crypto.randomBytes(16).toString('hex')
|
2019-06-18 14:45:04 +02:00
|
|
|
const user = await User.create(req.body)
|
2019-07-23 01:31:43 +02:00
|
|
|
mail.send(user.email, 'user_confirm', { user, config })
|
2019-06-18 14:45:04 +02:00
|
|
|
res.json(user)
|
|
|
|
} catch (e) {
|
|
|
|
res.status(404).json(e)
|
|
|
|
}
|
2019-06-18 15:13:13 +02:00
|
|
|
},
|
|
|
|
|
2019-09-11 19:12:24 +02:00
|
|
|
async remove (req, res) {
|
2019-06-18 15:13:13 +02:00
|
|
|
try {
|
|
|
|
const user = await User.findByPk(req.params.id)
|
|
|
|
user.destroy()
|
|
|
|
res.sendStatus(200)
|
|
|
|
} catch (e) {
|
|
|
|
res.status(404).json(e)
|
|
|
|
}
|
2019-04-03 00:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = userController
|