2017-02-09 01:52:37 +00:00
|
|
|
'use strict'
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
/* global wiki */
|
2017-05-13 19:29:00 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
var express = require('express')
|
|
|
|
var router = express.Router()
|
|
|
|
const Promise = require('bluebird')
|
|
|
|
const validator = require('validator')
|
|
|
|
const _ = require('lodash')
|
2017-02-13 04:17:14 +00:00
|
|
|
const axios = require('axios')
|
2017-02-13 05:59:52 +00:00
|
|
|
const path = require('path')
|
2017-02-19 21:13:51 +00:00
|
|
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
2017-07-02 18:03:49 +00:00
|
|
|
const os = require('os')
|
|
|
|
const filesize = require('filesize.js')
|
2016-08-17 03:56:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Admin
|
|
|
|
*/
|
|
|
|
router.get('/', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
res.redirect('/admin/profile')
|
|
|
|
})
|
2016-11-02 03:02:11 +00:00
|
|
|
|
|
|
|
router.get('/profile', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (res.locals.isGuest) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
2017-01-03 04:32:16 +00:00
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
res.render('pages/admin/profile', { adminTab: 'profile' })
|
|
|
|
})
|
2016-11-21 01:09:50 +00:00
|
|
|
|
2017-01-31 00:43:37 +00:00
|
|
|
router.post('/profile', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (res.locals.isGuest) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
return wiki.db.User.findById(req.user.id).then((usr) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
usr.name = _.trim(req.body.name)
|
|
|
|
if (usr.provider === 'local' && req.body.password !== '********') {
|
|
|
|
let nPwd = _.trim(req.body.password)
|
|
|
|
if (nPwd.length < 6) {
|
|
|
|
return Promise.reject(new Error('New Password too short!'))
|
|
|
|
} else {
|
2017-10-09 04:16:08 +00:00
|
|
|
return wiki.db.User.hashPassword(nPwd).then((pwd) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
usr.password = pwd
|
|
|
|
return usr.save()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return usr.save()
|
|
|
|
}
|
|
|
|
}).then(() => {
|
|
|
|
return res.json({ msg: 'OK' })
|
|
|
|
}).catch((err) => {
|
|
|
|
res.status(400).json({ msg: err.message })
|
|
|
|
})
|
|
|
|
})
|
2017-01-31 00:43:37 +00:00
|
|
|
|
2016-11-21 01:09:50 +00:00
|
|
|
router.get('/stats', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (res.locals.isGuest) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise.all([
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.db.Entry.count(),
|
|
|
|
wiki.db.UplFile.count(),
|
|
|
|
wiki.db.User.count()
|
2017-02-09 01:52:37 +00:00
|
|
|
]).spread((totalEntries, totalUploads, totalUsers) => {
|
|
|
|
return res.render('pages/admin/stats', {
|
|
|
|
totalEntries, totalUploads, totalUsers, adminTab: 'stats'
|
|
|
|
}) || true
|
|
|
|
}).catch((err) => {
|
|
|
|
throw err
|
|
|
|
})
|
|
|
|
})
|
2016-11-21 01:09:50 +00:00
|
|
|
|
|
|
|
router.get('/users', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.db.User.find({})
|
2017-02-09 01:52:37 +00:00
|
|
|
.select('-password -rights')
|
|
|
|
.sort('name email')
|
|
|
|
.exec().then((usrs) => {
|
|
|
|
res.render('pages/admin/users', { adminTab: 'users', usrs })
|
|
|
|
})
|
|
|
|
})
|
2017-01-04 05:07:15 +00:00
|
|
|
|
|
|
|
router.get('/users/:id', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isMongoId(req.params.id)) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.db.User.findById(req.params.id)
|
2017-02-09 01:52:37 +00:00
|
|
|
.select('-password -providerId')
|
|
|
|
.exec().then((usr) => {
|
|
|
|
let usrOpts = {
|
|
|
|
canChangeEmail: (usr.email !== 'guest' && usr.provider === 'local' && usr.email !== req.app.locals.appconfig.admin),
|
|
|
|
canChangeName: (usr.email !== 'guest'),
|
|
|
|
canChangePassword: (usr.email !== 'guest' && usr.provider === 'local'),
|
|
|
|
canChangeRole: (usr.email !== 'guest' && !(usr.provider === 'local' && usr.email === req.app.locals.appconfig.admin)),
|
|
|
|
canBeDeleted: (usr.email !== 'guest' && !(usr.provider === 'local' && usr.email === req.app.locals.appconfig.admin))
|
|
|
|
}
|
|
|
|
|
|
|
|
res.render('pages/admin/users-edit', { adminTab: 'users', usr, usrOpts })
|
2017-06-10 14:41:15 +00:00
|
|
|
}).catch(err => { // eslint-disable-line handle-callback-err
|
|
|
|
return res.status(404).end() || true
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
|
|
|
})
|
2016-11-21 01:09:50 +00:00
|
|
|
|
2017-02-10 06:43:26 +00:00
|
|
|
/**
|
|
|
|
* Create / Authorize a new user
|
|
|
|
*/
|
|
|
|
router.post('/users/create', (req, res) => {
|
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.status(401).json({ msg: 'Unauthorized' })
|
|
|
|
}
|
|
|
|
|
|
|
|
let nUsr = {
|
2017-03-12 16:15:29 +00:00
|
|
|
email: _.toLower(_.trim(req.body.email)),
|
2017-02-10 06:43:26 +00:00
|
|
|
provider: _.trim(req.body.provider),
|
|
|
|
password: req.body.password,
|
|
|
|
name: _.trim(req.body.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isEmail(nUsr.email)) {
|
|
|
|
return res.status(400).json({ msg: 'Invalid email address' })
|
2017-02-24 21:50:04 +00:00
|
|
|
} else if (!validator.isIn(nUsr.provider, ['local', 'google', 'windowslive', 'facebook', 'github', 'slack'])) {
|
2017-02-10 06:43:26 +00:00
|
|
|
return res.status(400).json({ msg: 'Invalid provider' })
|
|
|
|
} else if (nUsr.provider === 'local' && !validator.isLength(nUsr.password, { min: 6 })) {
|
|
|
|
return res.status(400).json({ msg: 'Password too short or missing' })
|
|
|
|
} else if (nUsr.provider === 'local' && !validator.isLength(nUsr.name, { min: 2 })) {
|
|
|
|
return res.status(400).json({ msg: 'Name is missing' })
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.db.User.findOne({ email: nUsr.email, provider: nUsr.provider }).then(exUsr => {
|
2017-02-10 06:43:26 +00:00
|
|
|
if (exUsr) {
|
|
|
|
return res.status(400).json({ msg: 'User already exists!' }) || true
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
let pwdGen = (nUsr.provider === 'local') ? wiki.db.User.hashPassword(nUsr.password) : Promise.resolve(true)
|
2017-02-10 06:43:26 +00:00
|
|
|
return pwdGen.then(nPwd => {
|
|
|
|
if (nUsr.provider !== 'local') {
|
|
|
|
nUsr.password = ''
|
|
|
|
nUsr.name = '-- pending --'
|
|
|
|
} else {
|
|
|
|
nUsr.password = nPwd
|
|
|
|
}
|
|
|
|
|
|
|
|
nUsr.rights = [{
|
|
|
|
role: 'read',
|
|
|
|
path: '/',
|
|
|
|
exact: false,
|
|
|
|
deny: false
|
|
|
|
}]
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
return wiki.db.User.create(nUsr).then(() => {
|
2017-02-10 06:43:26 +00:00
|
|
|
return res.json({ ok: true })
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.logger.warn(err)
|
2017-02-10 06:43:26 +00:00
|
|
|
return res.status(500).json({ msg: err })
|
|
|
|
})
|
|
|
|
}).catch(err => {
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.logger.warn(err)
|
2017-02-10 06:43:26 +00:00
|
|
|
return res.status(500).json({ msg: err })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-01-28 00:12:25 +00:00
|
|
|
router.post('/users/:id', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.manage) {
|
2017-10-09 04:16:08 +00:00
|
|
|
return res.status(401).json({ msg: wiki.lang.t('errors:unauthorized') })
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isMongoId(req.params.id)) {
|
2017-10-09 04:16:08 +00:00
|
|
|
return res.status(400).json({ msg: wiki.lang.t('errors:invaliduserid') })
|
2017-02-09 01:52:37 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
return wiki.db.User.findById(req.params.id).then((usr) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
usr.name = _.trim(req.body.name)
|
|
|
|
usr.rights = JSON.parse(req.body.rights)
|
|
|
|
if (usr.provider === 'local' && req.body.password !== '********') {
|
|
|
|
let nPwd = _.trim(req.body.password)
|
|
|
|
if (nPwd.length < 6) {
|
2017-10-09 04:16:08 +00:00
|
|
|
return Promise.reject(new Error(wiki.lang.t('errors:newpasswordtooshort')))
|
2017-02-09 01:52:37 +00:00
|
|
|
} else {
|
2017-10-09 04:16:08 +00:00
|
|
|
return wiki.db.User.hashPassword(nPwd).then((pwd) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
usr.password = pwd
|
|
|
|
return usr.save()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return usr.save()
|
|
|
|
}
|
2017-04-29 15:20:46 +00:00
|
|
|
}).then((usr) => {
|
|
|
|
// Update guest rights for future requests
|
|
|
|
if (usr.provider === 'local' && usr.email === 'guest') {
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.rights.guest = usr
|
2017-04-29 15:20:46 +00:00
|
|
|
}
|
|
|
|
return usr
|
2017-02-09 01:52:37 +00:00
|
|
|
}).then(() => {
|
|
|
|
return res.json({ msg: 'OK' })
|
|
|
|
}).catch((err) => {
|
|
|
|
res.status(400).json({ msg: err.message })
|
|
|
|
})
|
|
|
|
})
|
2017-01-28 00:12:25 +00:00
|
|
|
|
2017-02-10 18:13:40 +00:00
|
|
|
/**
|
|
|
|
* Delete / Deauthorize a user
|
|
|
|
*/
|
|
|
|
router.delete('/users/:id', (req, res) => {
|
|
|
|
if (!res.locals.rights.manage) {
|
2017-10-09 04:16:08 +00:00
|
|
|
return res.status(401).json({ msg: wiki.lang.t('errors:unauthorized') })
|
2017-02-10 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!validator.isMongoId(req.params.id)) {
|
2017-10-09 04:16:08 +00:00
|
|
|
return res.status(400).json({ msg: wiki.lang.t('errors:invaliduserid') })
|
2017-02-10 18:13:40 +00:00
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
return wiki.db.User.findByIdAndRemove(req.params.id).then(() => {
|
2017-06-10 14:41:15 +00:00
|
|
|
return res.json({ ok: true })
|
2017-02-10 18:13:40 +00:00
|
|
|
}).catch((err) => {
|
2017-06-10 14:41:15 +00:00
|
|
|
res.status(500).json({ ok: false, msg: err.message })
|
2017-02-10 18:13:40 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-11-21 01:09:50 +00:00
|
|
|
router.get('/settings', (req, res) => {
|
2017-02-09 01:52:37 +00:00
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
2017-06-24 20:26:33 +00:00
|
|
|
res.render('pages/admin/settings', { adminTab: 'settings' })
|
|
|
|
})
|
|
|
|
|
|
|
|
router.get('/system', (req, res) => {
|
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
2017-01-03 04:32:16 +00:00
|
|
|
|
2017-07-02 18:03:49 +00:00
|
|
|
let hostInfo = {
|
|
|
|
cpus: os.cpus(),
|
|
|
|
hostname: os.hostname(),
|
|
|
|
nodeversion: process.version,
|
|
|
|
os: `${os.type()} (${os.platform()}) ${os.release()} ${os.arch()}`,
|
|
|
|
totalmem: filesize(os.totalmem()),
|
|
|
|
cwd: process.cwd()
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
fs.readJsonAsync(path.join(wiki.ROOTPATH, 'package.json')).then(packageObj => {
|
2017-02-19 21:13:51 +00:00
|
|
|
axios.get('https://api.github.com/repos/Requarks/wiki/releases/latest').then(resp => {
|
|
|
|
let sysversion = {
|
|
|
|
current: 'v' + packageObj.version,
|
|
|
|
latest: resp.data.tag_name,
|
|
|
|
latestPublishedAt: resp.data.published_at
|
|
|
|
}
|
2017-02-13 04:17:14 +00:00
|
|
|
|
2017-07-02 18:03:49 +00:00
|
|
|
res.render('pages/admin/system', { adminTab: 'system', hostInfo, sysversion })
|
2017-02-19 21:13:51 +00:00
|
|
|
}).catch(err => {
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.logger.warn(err)
|
2017-07-02 18:03:49 +00:00
|
|
|
res.render('pages/admin/system', { adminTab: 'system', hostInfo, sysversion: { current: 'v' + packageObj.version } })
|
2017-02-19 21:13:51 +00:00
|
|
|
})
|
2017-02-13 04:17:14 +00:00
|
|
|
})
|
2017-02-09 01:52:37 +00:00
|
|
|
})
|
2016-08-17 03:56:08 +00:00
|
|
|
|
2017-06-24 20:26:33 +00:00
|
|
|
router.post('/system/install', (req, res) => {
|
2017-02-13 05:59:52 +00:00
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-02-19 21:13:51 +00:00
|
|
|
// let sysLib = require(path.join(ROOTPATH, 'libs/system.js'))
|
|
|
|
// sysLib.install('v1.0-beta.7')
|
|
|
|
res.status(400).send('Sorry, Upgrade/Re-Install via the web UI is not yet ready. You must use the npm upgrade method in the meantime.').end()
|
2017-02-13 05:59:52 +00:00
|
|
|
})
|
|
|
|
|
2017-05-21 03:21:16 +00:00
|
|
|
router.get('/theme', (req, res) => {
|
|
|
|
if (!res.locals.rights.manage) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
res.render('pages/admin/theme', { adminTab: 'theme' })
|
|
|
|
})
|
|
|
|
|
2017-07-02 16:51:39 +00:00
|
|
|
router.post('/theme', (req, res) => {
|
|
|
|
if (res.locals.isGuest) {
|
|
|
|
return res.render('error-forbidden')
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
if (!validator.isIn(req.body.primary, wiki.data.colors)) {
|
2017-07-02 16:51:39 +00:00
|
|
|
return res.status(406).json({ msg: 'Primary color is invalid.' })
|
2017-10-09 04:16:08 +00:00
|
|
|
} else if (!validator.isIn(req.body.alt, wiki.data.colors)) {
|
2017-07-02 16:51:39 +00:00
|
|
|
return res.status(406).json({ msg: 'Alternate color is invalid.' })
|
2017-10-09 04:16:08 +00:00
|
|
|
} else if (!validator.isIn(req.body.footer, wiki.data.colors)) {
|
2017-07-02 16:51:39 +00:00
|
|
|
return res.status(406).json({ msg: 'Footer color is invalid.' })
|
|
|
|
}
|
|
|
|
|
2017-10-09 04:16:08 +00:00
|
|
|
wiki.config.theme.primary = req.body.primary
|
|
|
|
wiki.config.theme.alt = req.body.alt
|
|
|
|
wiki.config.theme.footer = req.body.footer
|
|
|
|
wiki.config.theme.code.dark = req.body.codedark === 'true'
|
|
|
|
wiki.config.theme.code.colorize = req.body.codecolorize === 'true'
|
2017-07-02 16:51:39 +00:00
|
|
|
|
|
|
|
return res.json({ msg: 'OK' })
|
|
|
|
})
|
|
|
|
|
2017-02-09 01:52:37 +00:00
|
|
|
module.exports = router
|