2018-01-23 20:06:30 +00:00
|
|
|
const bcrypt = require('bcrypt')
|
2019-10-06 23:11:07 +00:00
|
|
|
const path = require('path')
|
2018-01-23 20:06:30 +00:00
|
|
|
const randomstring = require('randomstring')
|
2019-10-06 23:11:07 +00:00
|
|
|
const paths = require('./pathsController')
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
const perms = require('./permissionController')
|
2019-06-18 21:04:14 +00:00
|
|
|
const tokens = require('./tokenController')
|
2018-04-13 16:20:57 +00:00
|
|
|
const utils = require('./utilsController')
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
const config = require('./../config')
|
|
|
|
const logger = require('./../logger')
|
|
|
|
const db = require('knex')(config.database)
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-17 04:13:41 +00:00
|
|
|
// Don't forget to update min/max length of text inputs in auth.njk
|
|
|
|
// when changing these values.
|
2019-09-08 01:56:29 +00:00
|
|
|
const self = {
|
2019-09-17 04:13:41 +00:00
|
|
|
user: {
|
|
|
|
min: 4,
|
|
|
|
max: 32
|
|
|
|
},
|
|
|
|
pass: {
|
|
|
|
min: 6,
|
|
|
|
// Should not be more than 72 characters
|
|
|
|
// https://github.com/kelektiv/node.bcrypt.js#security-issues-and-concerns
|
|
|
|
max: 64,
|
|
|
|
// Length of randomized password
|
|
|
|
// when resetting passwordthrough Dashboard's Manage Users.
|
|
|
|
rand: 16
|
|
|
|
}
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-17 04:13:41 +00:00
|
|
|
// https://github.com/kelektiv/node.bcrypt.js#a-note-on-rounds
|
|
|
|
const saltRounds = 10
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.verify = async (req, res, next) => {
|
|
|
|
const username = typeof req.body.username === 'string'
|
|
|
|
? req.body.username.trim()
|
|
|
|
: ''
|
|
|
|
if (!username)
|
2019-08-26 22:00:57 +00:00
|
|
|
return res.json({ success: false, description: 'No username provided.' })
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const password = typeof req.body.password === 'string'
|
|
|
|
? req.body.password.trim()
|
|
|
|
: ''
|
|
|
|
if (!password)
|
|
|
|
return res.json({ success: false, description: 'No password provided.' })
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
try {
|
|
|
|
const user = await db.table('users')
|
|
|
|
.where('username', username)
|
|
|
|
.first()
|
|
|
|
|
|
|
|
if (!user)
|
|
|
|
return res.json({ success: false, description: 'Username does not exist.' })
|
|
|
|
|
|
|
|
if (user.enabled === false || user.enabled === 0)
|
|
|
|
return res.json({ success: false, description: 'This account has been disabled.' })
|
|
|
|
|
2019-09-17 04:13:41 +00:00
|
|
|
const result = await bcrypt.compare(password, user.password)
|
2019-09-08 01:56:29 +00:00
|
|
|
if (result === false)
|
|
|
|
return res.json({ success: false, description: 'Wrong password.' })
|
|
|
|
else
|
|
|
|
return res.json({ success: true, token: user.token })
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
|
|
|
return res.status(500).json({ success: false, description: 'An unexpected error occurred. Try again?' })
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.register = async (req, res, next) => {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (config.enableUserAccounts === false)
|
2019-09-08 01:56:29 +00:00
|
|
|
return res.json({ success: false, description: 'Registration is currently disabled.' })
|
2019-08-26 22:00:57 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const username = typeof req.body.username === 'string'
|
|
|
|
? req.body.username.trim()
|
|
|
|
: ''
|
2019-09-17 04:13:41 +00:00
|
|
|
if (username.length < self.user.min || username.length > self.user.max)
|
|
|
|
return res.json({ success: false, description: `Username must have ${self.user.min}-${self.user.max} characters.` })
|
2018-12-18 17:01:28 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const password = typeof req.body.password === 'string'
|
|
|
|
? req.body.password.trim()
|
|
|
|
: ''
|
2019-09-17 04:13:41 +00:00
|
|
|
if (password.length < self.pass.min || password.length > self.pass.max)
|
|
|
|
return res.json({ success: false, description: `Password must have ${self.pass.min}-${self.pass.max} characters.` })
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
try {
|
|
|
|
const user = await db.table('users')
|
|
|
|
.where('username', username)
|
|
|
|
.first()
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
if (user)
|
|
|
|
return res.json({ success: false, description: 'Username already exists.' })
|
|
|
|
|
2019-09-17 04:13:41 +00:00
|
|
|
const hash = await bcrypt.hash(password, saltRounds)
|
2019-06-18 21:04:14 +00:00
|
|
|
|
|
|
|
const token = await tokens.generateUniqueToken()
|
|
|
|
if (!token)
|
2019-09-08 01:56:29 +00:00
|
|
|
return res.json({ success: false, description: 'Sorry, we could not allocate a unique token. Try again?' })
|
2019-06-18 21:04:14 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
await db.table('users')
|
|
|
|
.insert({
|
|
|
|
username,
|
|
|
|
password: hash,
|
|
|
|
token,
|
|
|
|
enabled: 1,
|
|
|
|
permission: perms.permissions.user
|
|
|
|
})
|
2019-04-12 00:45:33 +00:00
|
|
|
utils.invalidateStatsCache('users')
|
2019-09-08 18:33:07 +00:00
|
|
|
tokens.onHold.delete(token)
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
return res.json({ success: true, token })
|
2019-09-08 01:56:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
|
|
|
return res.status(500).json({ success: false, description: 'An unexpected error occurred. Try again?' })
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.changePassword = async (req, res, next) => {
|
2018-01-23 20:06:30 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user) return
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const password = typeof req.body.password === 'string'
|
|
|
|
? req.body.password.trim()
|
|
|
|
: ''
|
2019-09-17 04:13:41 +00:00
|
|
|
if (password.length < self.pass.min || password.length > self.pass.max)
|
|
|
|
return res.json({ success: false, description: `Password must have ${self.pass.min}-${self.pass.max} characters.` })
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
try {
|
2019-09-17 04:13:41 +00:00
|
|
|
const hash = await bcrypt.hash(password, saltRounds)
|
2017-10-04 00:13:38 +00:00
|
|
|
|
2018-04-28 17:26:39 +00:00
|
|
|
await db.table('users')
|
|
|
|
.where('id', user.id)
|
|
|
|
.update('password', hash)
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
return res.json({ success: true })
|
2019-09-08 01:56:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
|
|
|
return res.status(500).json({ success: false, description: 'An unexpected error occurred. Try again?' })
|
|
|
|
}
|
2018-03-24 13:52:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 23:11:07 +00:00
|
|
|
self.assertPermission = (user, target) => {
|
|
|
|
if (!target)
|
|
|
|
throw new Error('Could not get user with the specified ID.')
|
|
|
|
else if (!perms.higher(user, target))
|
|
|
|
throw new Error('The user is in the same or higher group as you.')
|
|
|
|
else if (target.username === 'root')
|
|
|
|
throw new Error('Root user may not be tampered with.')
|
|
|
|
}
|
|
|
|
|
2020-04-17 07:25:18 +00:00
|
|
|
self.createUser = async (req, res, next) => {
|
|
|
|
const user = await utils.authorize(req, res)
|
|
|
|
if (!user) return
|
|
|
|
|
|
|
|
const isadmin = perms.is(user, 'admin')
|
|
|
|
if (!isadmin)
|
|
|
|
return res.status(403).end()
|
|
|
|
|
|
|
|
const username = typeof req.body.username === 'string'
|
|
|
|
? req.body.username.trim()
|
|
|
|
: ''
|
|
|
|
if (username.length < self.user.min || username.length > self.user.max)
|
|
|
|
return res.json({ success: false, description: `Username must have ${self.user.min}-${self.user.max} characters.` })
|
|
|
|
|
|
|
|
let password = typeof req.body.password === 'string'
|
|
|
|
? req.body.password.trim()
|
|
|
|
: ''
|
|
|
|
if (password.length) {
|
|
|
|
if (password.length < self.pass.min || password.length > self.pass.max)
|
|
|
|
return res.json({ success: false, description: `Password must have ${self.pass.min}-${self.pass.max} characters.` })
|
|
|
|
} else {
|
|
|
|
password = randomstring.generate(self.pass.rand)
|
|
|
|
}
|
|
|
|
|
|
|
|
let group = req.body.group
|
|
|
|
let permission
|
|
|
|
if (group !== undefined) {
|
|
|
|
permission = perms.permissions[group]
|
|
|
|
if (typeof permission !== 'number' || permission < 0) {
|
|
|
|
group = 'user'
|
|
|
|
permission = perms.permissions.user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const user = await db.table('users')
|
|
|
|
.where('username', username)
|
|
|
|
.first()
|
|
|
|
|
|
|
|
if (user)
|
|
|
|
return res.json({ success: false, description: 'Username already exists.' })
|
|
|
|
|
|
|
|
const hash = await bcrypt.hash(password, saltRounds)
|
|
|
|
|
|
|
|
const token = await tokens.generateUniqueToken()
|
|
|
|
if (!token)
|
|
|
|
return res.json({ success: false, description: 'Sorry, we could not allocate a unique token. Try again?' })
|
|
|
|
|
|
|
|
await db.table('users')
|
|
|
|
.insert({
|
|
|
|
username,
|
|
|
|
password: hash,
|
|
|
|
token,
|
|
|
|
enabled: 1,
|
|
|
|
permission
|
|
|
|
})
|
|
|
|
utils.invalidateStatsCache('users')
|
|
|
|
tokens.onHold.delete(token)
|
|
|
|
|
|
|
|
return res.json({ success: true, username, password, group })
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
|
|
|
return res.status(500).json({ success: false, description: 'An unexpected error occurred. Try again?' })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.editUser = async (req, res, next) => {
|
2018-10-09 19:52:41 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user) return
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-10-06 23:11:07 +00:00
|
|
|
const isadmin = perms.is(user, 'admin')
|
|
|
|
if (!isadmin)
|
|
|
|
return res.status(403).end()
|
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
const id = parseInt(req.body.id)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (isNaN(id))
|
2018-10-09 19:52:41 +00:00
|
|
|
return res.json({ success: false, description: 'No user specified.' })
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
try {
|
|
|
|
const target = await db.table('users')
|
|
|
|
.where('id', id)
|
|
|
|
.first()
|
2019-10-06 23:11:07 +00:00
|
|
|
self.assertPermission(user, target)
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const update = {}
|
2019-01-01 19:39:08 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
if (req.body.username !== undefined) {
|
|
|
|
update.username = String(req.body.username).trim()
|
2019-09-17 04:13:41 +00:00
|
|
|
if (update.username.length < self.user.min || update.username.length > self.user.max)
|
2019-10-06 23:11:07 +00:00
|
|
|
throw new Error(`Username must have ${self.user.min}-${self.user.max} characters.`)
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
2018-10-10 17:33:11 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
if (req.body.enabled !== undefined)
|
|
|
|
update.enabled = Boolean(req.body.enabled)
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
if (req.body.group !== undefined) {
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
update.permission = perms.permissions[req.body.group]
|
2019-09-08 01:56:29 +00:00
|
|
|
if (typeof update.permission !== 'number' || update.permission < 0)
|
|
|
|
update.permission = target.permission
|
|
|
|
}
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
let password
|
|
|
|
if (req.body.resetPassword) {
|
2019-09-17 04:13:41 +00:00
|
|
|
password = randomstring.generate(self.pass.rand)
|
|
|
|
update.password = await bcrypt.hash(password, saltRounds)
|
2018-10-09 19:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await db.table('users')
|
|
|
|
.where('id', id)
|
2019-09-08 01:56:29 +00:00
|
|
|
.update(update)
|
|
|
|
utils.invalidateStatsCache('users')
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const response = { success: true, update }
|
2020-04-18 19:52:11 +00:00
|
|
|
if (password) response.update.password = password
|
2019-09-08 01:56:29 +00:00
|
|
|
return res.json(response)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
2019-10-06 23:11:07 +00:00
|
|
|
return res.status(500).json({
|
|
|
|
success: false,
|
|
|
|
description: error.message || 'An unexpected error occurred. Try again?'
|
|
|
|
})
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
2018-10-09 19:52:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.disableUser = async (req, res, next) => {
|
|
|
|
req.body = { id: req.body.id, enabled: false }
|
|
|
|
return self.editUser(req, res, next)
|
2019-01-01 19:39:08 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 23:11:07 +00:00
|
|
|
self.deleteUser = async (req, res, next) => {
|
|
|
|
const user = await utils.authorize(req, res)
|
|
|
|
if (!user) return
|
|
|
|
|
|
|
|
const isadmin = perms.is(user, 'admin')
|
|
|
|
if (!isadmin)
|
|
|
|
return res.status(403).end()
|
|
|
|
|
|
|
|
const id = parseInt(req.body.id)
|
|
|
|
const purge = req.body.purge
|
|
|
|
if (isNaN(id))
|
|
|
|
return res.json({ success: false, description: 'No user specified.' })
|
|
|
|
|
|
|
|
try {
|
|
|
|
const target = await db.table('users')
|
|
|
|
.where('id', id)
|
|
|
|
.first()
|
|
|
|
self.assertPermission(user, target)
|
|
|
|
|
|
|
|
const files = await db.table('files')
|
|
|
|
.where('userid', id)
|
|
|
|
.select('id')
|
|
|
|
|
|
|
|
if (files.length) {
|
|
|
|
const fileids = files.map(file => file.id)
|
|
|
|
if (purge) {
|
|
|
|
const failed = await utils.bulkDeleteFromDb('id', fileids, user)
|
|
|
|
if (failed.length)
|
|
|
|
return res.json({ success: false, failed })
|
|
|
|
utils.invalidateStatsCache('uploads')
|
|
|
|
} else {
|
|
|
|
// Clear out userid attribute from the files
|
|
|
|
await db.table('files')
|
|
|
|
.whereIn('id', fileids)
|
|
|
|
.update('userid', null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Figure out obstacles of just deleting the albums
|
|
|
|
const albums = await db.table('albums')
|
|
|
|
.where('userid', id)
|
|
|
|
.where('enabled', 1)
|
|
|
|
.select('id', 'identifier')
|
|
|
|
|
|
|
|
if (albums.length) {
|
|
|
|
const albumids = albums.map(album => album.id)
|
|
|
|
await db.table('albums')
|
|
|
|
.whereIn('id', albumids)
|
|
|
|
.del()
|
|
|
|
utils.invalidateAlbumsCache(albumids)
|
|
|
|
|
|
|
|
// Unlink their archives
|
|
|
|
await Promise.all(albums.map(async album => {
|
|
|
|
try {
|
|
|
|
await paths.unlink(path.join(paths.zips, `${album.identifier}.zip`))
|
|
|
|
} catch (error) {
|
|
|
|
if (error.code !== 'ENOENT')
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
await db.table('users')
|
|
|
|
.where('id', id)
|
|
|
|
.del()
|
|
|
|
utils.invalidateStatsCache('users')
|
|
|
|
|
|
|
|
return res.json({ success: true })
|
|
|
|
} catch (error) {
|
|
|
|
return res.status(500).json({
|
|
|
|
success: false,
|
|
|
|
description: error.message || 'An unexpected error occurred. Try again?'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.bulkDeleteUsers = async (req, res, next) => {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
self.listUsers = async (req, res, next) => {
|
2018-10-09 19:52:41 +00:00
|
|
|
const user = await utils.authorize(req, res)
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user) return
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2018-10-13 11:06:58 +00:00
|
|
|
const isadmin = perms.is(user, 'admin')
|
2019-06-18 21:04:14 +00:00
|
|
|
if (!isadmin)
|
|
|
|
return res.status(403).end()
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
try {
|
|
|
|
const count = await db.table('users')
|
|
|
|
.count('id as count')
|
|
|
|
.then(rows => rows[0].count)
|
|
|
|
if (!count)
|
|
|
|
return res.json({ success: true, users: [], count })
|
2019-01-01 19:39:08 +00:00
|
|
|
|
2020-05-02 15:42:23 +00:00
|
|
|
let offset = Number(req.params.page)
|
|
|
|
if (isNaN(offset)) offset = 0
|
|
|
|
else if (offset < 0) offset = Math.max(0, Math.ceil(count / 25) + offset)
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const users = await db.table('users')
|
|
|
|
.limit(25)
|
|
|
|
.offset(25 * offset)
|
|
|
|
.select('id', 'username', 'enabled', 'permission')
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-10 16:31:27 +00:00
|
|
|
const pointers = {}
|
2019-09-08 01:56:29 +00:00
|
|
|
for (const user of users) {
|
|
|
|
user.groups = perms.mapPermissions(user)
|
|
|
|
delete user.permission
|
2019-09-10 16:31:27 +00:00
|
|
|
user.uploads = 0
|
|
|
|
user.usage = 0
|
|
|
|
pointers[user.id] = user
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
2018-10-12 09:42:16 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
const uploads = await db.table('files')
|
2019-09-10 16:31:27 +00:00
|
|
|
.whereIn('userid', Object.keys(pointers))
|
|
|
|
.select('userid', 'size')
|
2018-10-12 10:19:14 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
for (const upload of uploads) {
|
2019-09-10 16:31:27 +00:00
|
|
|
pointers[upload.userid].uploads++
|
|
|
|
pointers[upload.userid].usage += parseInt(upload.size)
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
2018-10-09 19:52:41 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
return res.json({ success: true, users, count })
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
|
|
|
return res.status(500).json({ success: false, description: 'An unexpected error occurred. Try again?' })
|
|
|
|
}
|
2018-10-09 19:52:41 +00:00
|
|
|
}
|
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
module.exports = self
|