gancio-upstream/server/api/controller/user.js

141 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-04-05 00:10:19 +02:00
const crypto = require('crypto')
const { Op } = require('sequelize')
2021-09-27 10:42:17 +02:00
const config = require('../../config')
2019-04-05 00:10:19 +02:00
const mail = require('../mail')
const { User } = require('../models/models')
const settingsController = require('./settings')
2021-03-05 14:17:10 +01:00
const log = require('../../log')
2020-02-15 15:42:41 +01:00
const linkify = require('linkifyjs')
2019-04-03 00:25:12 +02:00
const userController = {
2023-11-21 01:39:18 +01: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
2023-10-25 09:58:30 +02:00
const user = await User.findOne({ where: { email, is_active: true } })
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')
2022-02-26 21:27:40 +01:00
mail.send(user.email, 'recover', { user, config }, res.locals.locale)
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) }
res.json({ email: user.email })
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) {
2022-11-04 12:22:21 +01:00
if (!req.user) { return res.status(400).send('Not logged') }
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) {
2022-11-04 12:22:21 +01:00
const users = await User.scope(req.user.is_admin ? 'withRecover' : 'withoutPassword').findAll({
2019-11-09 15:05:33 +01:00
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) {
// user to modify
2019-10-28 17:33:20 +01:00
const user = await User.findByPk(req.body.id)
2019-09-11 19:12:24 +02:00
if (!user) { return res.status(404).json({ success: false, message: 'User not found!' }) }
2019-09-11 19:12:24 +02:00
if (!req.body.password) { delete req.body.password }
2024-02-10 11:53:05 +01:00
if ((!user.is_active && req.body.is_active)) {
2022-02-26 21:27:40 +01:00
mail.send(user.email, 'confirm', { user, config }, res.locals.settings.locale)
2019-04-03 00:25:12 +02:00
}
2019-10-14 21:49:59 +02:00
await user.update(req.body)
2021-05-19 16:26:01 +02:00
res.status(200).send()
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 {
2020-02-15 15:42:41 +01:00
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
2024-04-01 10:29:18 +02:00
req.body.role = 'admin'
2020-02-15 15:42:41 +01:00
const user = await User.create(req.body)
return res.json(user)
}
req.body.is_active = false
2023-10-25 09:50:20 +02:00
req.body.is_admin = false
2020-02-15 15:42:41 +01:00
// check email
if (!linkify.test(req.body.email, 'email')) {
return res.status(404).json('Invalid email')
2019-04-03 00:25:12 +02:00
}
2019-05-30 12:04:14 +02:00
2021-04-28 12:44:26 +02:00
log.info('Register user ', req.body.email)
2019-04-03 00:25:12 +02:00
const user = await User.create(req.body)
2021-04-28 12:44:26 +02:00
log.info(`Sending registration email to ${user.email}`)
2022-03-07 17:47:31 +01:00
mail.send(user.email, 'register', { user, config }, res.locals.locale)
2021-10-20 14:14:26 +02:00
mail.send(settingsController.settings.admin_email, 'admin_register', { user, config })
res.sendStatus(200)
2019-04-03 00:25:12 +02:00
} catch (e) {
2021-07-08 20:41:56 +02:00
log.error('Registration error:', e)
res.status(400).json(e)
2019-04-03 00:25:12 +02:00
}
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')
const user = await User.scope('withRecover').create(req.body)
2022-03-07 17:47:31 +01:00
mail.send(user.email, 'user_confirm', { user, config }, res.locals.locale)
2019-06-18 14:45:04 +02:00
res.json(user)
} catch (e) {
2021-07-08 20:41:56 +02:00
log.error('User creation error:', e)
2019-06-18 14:45:04 +02:00
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 {
2022-05-03 11:42:59 +02:00
let user
// TODO: has to unset events first!
2022-11-04 12:22:21 +01:00
if (req.user.is_admin && req.params.id) {
2022-05-03 11:42:59 +02:00
user = await User.findByPk(req.params.id)
} else {
2022-11-04 12:22:21 +01:00
user = await User.findByPk(req.user.id)
2022-05-03 11:42:59 +02:00
}
2021-11-10 10:59:47 +01:00
await user.destroy()
log.warn(`User ${user.email} removed!`)
2019-06-18 15:13:13 +02:00
res.sendStatus(200)
} catch (e) {
2021-07-08 20:41:56 +02:00
log.error('User removal error:"', e)
2019-06-18 15:13:13 +02:00
res.status(404).json(e)
}
2019-04-03 00:25:12 +02:00
}
}
module.exports = userController