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

134 lines
4 KiB
JavaScript
Raw Normal View History

2019-04-05 00:10:19 +02:00
const crypto = require('crypto')
const { Op } = require('sequelize')
const config = require('config')
2019-04-05 00:10:19 +02:00
const mail = require('../mail')
const { user: User } = require('../models')
const settingsController = require('./settings')
2019-09-25 14:38:16 +02:00
const debug = require('debug')('user:controller')
2020-02-15 15:42:41 +01:00
const linkify = require('linkifyjs')
2019-04-03 00:25:12 +02:00
const userController = {
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')
mail.send(user.email, 'recover', { user, config }, req.settings.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) }
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') }
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) {
// 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!' }) }
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 }
if (!user.is_active && req.body.is_active && user.recover_code) {
mail.send(user.email, 'confirm', { user, config }, req.settings.locale)
2019-04-03 00:25:12 +02:00
}
2019-10-14 21:49:59 +02:00
await user.update(req.body)
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 {
2020-02-15 15:42:41 +01:00
req.body.recover_code = crypto.randomBytes(16).toString('hex')
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
2020-02-15 15:42:41 +01:00
const user = await User.create(req.body)
return res.json(user)
}
req.body.is_active = false
// 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
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)
debug(`Sending registration email to ${user.email}`)
mail.send(user.email, 'register', { user, config }, req.settings.locale)
2020-02-20 18:37:10 +01:00
mail.send(config.admin_email, 'admin_register', { user, config })
res.sendStatus(200)
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)
mail.send(user.email, 'user_confirm', { user, config }, req.settings.locale)
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