You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.1 KiB

8 years ago
  1. 'use strict'
  2. var express = require('express')
  3. var router = express.Router()
  4. const Promise = require('bluebird')
  5. const validator = require('validator')
  6. const _ = require('lodash')
  7. /**
  8. * Admin
  9. */
  10. router.get('/', (req, res) => {
  11. res.redirect('/admin/profile')
  12. })
  13. router.get('/profile', (req, res) => {
  14. if (res.locals.isGuest) {
  15. return res.render('error-forbidden')
  16. }
  17. res.render('pages/admin/profile', { adminTab: 'profile' })
  18. })
  19. router.post('/profile', (req, res) => {
  20. if (res.locals.isGuest) {
  21. return res.render('error-forbidden')
  22. }
  23. return db.User.findById(req.user.id).then((usr) => {
  24. usr.name = _.trim(req.body.name)
  25. if (usr.provider === 'local' && req.body.password !== '********') {
  26. let nPwd = _.trim(req.body.password)
  27. if (nPwd.length < 6) {
  28. return Promise.reject(new Error('New Password too short!'))
  29. } else {
  30. return db.User.hashPassword(nPwd).then((pwd) => {
  31. usr.password = pwd
  32. return usr.save()
  33. })
  34. }
  35. } else {
  36. return usr.save()
  37. }
  38. }).then(() => {
  39. return res.json({ msg: 'OK' })
  40. }).catch((err) => {
  41. res.status(400).json({ msg: err.message })
  42. })
  43. })
  44. router.get('/stats', (req, res) => {
  45. if (res.locals.isGuest) {
  46. return res.render('error-forbidden')
  47. }
  48. Promise.all([
  49. db.Entry.count(),
  50. db.UplFile.count(),
  51. db.User.count()
  52. ]).spread((totalEntries, totalUploads, totalUsers) => {
  53. return res.render('pages/admin/stats', {
  54. totalEntries, totalUploads, totalUsers, adminTab: 'stats'
  55. }) || true
  56. }).catch((err) => {
  57. throw err
  58. })
  59. })
  60. router.get('/users', (req, res) => {
  61. if (!res.locals.rights.manage) {
  62. return res.render('error-forbidden')
  63. }
  64. db.User.find({})
  65. .select('-password -rights')
  66. .sort('name email')
  67. .exec().then((usrs) => {
  68. res.render('pages/admin/users', { adminTab: 'users', usrs })
  69. })
  70. })
  71. router.get('/users/:id', (req, res) => {
  72. if (!res.locals.rights.manage) {
  73. return res.render('error-forbidden')
  74. }
  75. if (!validator.isMongoId(req.params.id)) {
  76. return res.render('error-forbidden')
  77. }
  78. db.User.findById(req.params.id)
  79. .select('-password -providerId')
  80. .exec().then((usr) => {
  81. let usrOpts = {
  82. canChangeEmail: (usr.email !== 'guest' && usr.provider === 'local' && usr.email !== req.app.locals.appconfig.admin),
  83. canChangeName: (usr.email !== 'guest'),
  84. canChangePassword: (usr.email !== 'guest' && usr.provider === 'local'),
  85. canChangeRole: (usr.email !== 'guest' && !(usr.provider === 'local' && usr.email === req.app.locals.appconfig.admin)),
  86. canBeDeleted: (usr.email !== 'guest' && !(usr.provider === 'local' && usr.email === req.app.locals.appconfig.admin))
  87. }
  88. res.render('pages/admin/users-edit', { adminTab: 'users', usr, usrOpts })
  89. })
  90. })
  91. /**
  92. * Create / Authorize a new user
  93. */
  94. router.post('/users/create', (req, res) => {
  95. if (!res.locals.rights.manage) {
  96. return res.status(401).json({ msg: 'Unauthorized' })
  97. }
  98. let nUsr = {
  99. email: _.trim(req.body.email),
  100. provider: _.trim(req.body.provider),
  101. password: req.body.password,
  102. name: _.trim(req.body.name)
  103. }
  104. if (!validator.isEmail(nUsr.email)) {
  105. return res.status(400).json({ msg: 'Invalid email address' })
  106. } else if (!validator.isIn(nUsr.provider, ['local', 'google', 'windowslive', 'facebook'])) {
  107. return res.status(400).json({ msg: 'Invalid provider' })
  108. } else if (nUsr.provider === 'local' && !validator.isLength(nUsr.password, { min: 6 })) {
  109. return res.status(400).json({ msg: 'Password too short or missing' })
  110. } else if (nUsr.provider === 'local' && !validator.isLength(nUsr.name, { min: 2 })) {
  111. return res.status(400).json({ msg: 'Name is missing' })
  112. }
  113. db.User.findOne({ email: nUsr.email, provider: nUsr.provider }).then(exUsr => {
  114. if (exUsr) {
  115. return res.status(400).json({ msg: 'User already exists!' }) || true
  116. }
  117. let pwdGen = (nUsr.provider === 'local') ? db.User.hashPassword(nUsr.password) : Promise.resolve(true)
  118. return pwdGen.then(nPwd => {
  119. if (nUsr.provider !== 'local') {
  120. nUsr.password = ''
  121. nUsr.name = '-- pending --'
  122. } else {
  123. nUsr.password = nPwd
  124. }
  125. nUsr.rights = [{
  126. role: 'read',
  127. path: '/',
  128. exact: false,
  129. deny: false
  130. }]
  131. return db.User.create(nUsr).then(() => {
  132. return res.json({ ok: true })
  133. })
  134. }).catch(err => {
  135. winston.warn(err)
  136. return res.status(500).json({ msg: err })
  137. })
  138. }).catch(err => {
  139. winston.warn(err)
  140. return res.status(500).json({ msg: err })
  141. })
  142. })
  143. router.post('/users/:id', (req, res) => {
  144. if (!res.locals.rights.manage) {
  145. return res.status(401).json({ msg: 'Unauthorized' })
  146. }
  147. if (!validator.isMongoId(req.params.id)) {
  148. return res.status(400).json({ msg: 'Invalid User ID' })
  149. }
  150. return db.User.findById(req.params.id).then((usr) => {
  151. usr.name = _.trim(req.body.name)
  152. usr.rights = JSON.parse(req.body.rights)
  153. if (usr.provider === 'local' && req.body.password !== '********') {
  154. let nPwd = _.trim(req.body.password)
  155. if (nPwd.length < 6) {
  156. return Promise.reject(new Error('New Password too short!'))
  157. } else {
  158. return db.User.hashPassword(nPwd).then((pwd) => {
  159. usr.password = pwd
  160. return usr.save()
  161. })
  162. }
  163. } else {
  164. return usr.save()
  165. }
  166. }).then(() => {
  167. return res.json({ msg: 'OK' })
  168. }).catch((err) => {
  169. res.status(400).json({ msg: err.message })
  170. })
  171. })
  172. /**
  173. * Delete / Deauthorize a user
  174. */
  175. router.delete('/users/:id', (req, res) => {
  176. if (!res.locals.rights.manage) {
  177. return res.status(401).json({ msg: 'Unauthorized' })
  178. }
  179. if (!validator.isMongoId(req.params.id)) {
  180. return res.status(400).json({ msg: 'Invalid User ID' })
  181. }
  182. return db.User.findByIdAndRemove(req.params.id).then(() => {
  183. return res.json({ msg: 'OK' })
  184. }).catch((err) => {
  185. res.status(500).json({ msg: err.message })
  186. })
  187. })
  188. router.get('/settings', (req, res) => {
  189. if (!res.locals.rights.manage) {
  190. return res.render('error-forbidden')
  191. }
  192. res.render('pages/admin/settings', { adminTab: 'settings' })
  193. })
  194. module.exports = router