2018-01-30 07:27:07 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-06-10 16:15:25 -04:00
|
|
|
import * as express from 'express'
|
2019-07-25 10:23:44 -04:00
|
|
|
import { body, param } from 'express-validator'
|
2018-01-30 07:27:07 -05:00
|
|
|
import { omit } from 'lodash'
|
2019-08-23 02:56:57 -04:00
|
|
|
import { isIdOrUUIDValid, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
|
2017-09-15 06:17:08 -04:00
|
|
|
import {
|
2019-04-15 04:49:46 -04:00
|
|
|
isUserAdminFlagsValid,
|
2018-12-18 11:18:25 -05:00
|
|
|
isUserAutoPlayVideoValid,
|
|
|
|
isUserBlockedReasonValid,
|
2018-06-29 05:29:23 -04:00
|
|
|
isUserDescriptionValid,
|
|
|
|
isUserDisplayNameValid,
|
2018-04-19 05:01:34 -04:00
|
|
|
isUserNSFWPolicyValid,
|
2018-01-30 07:27:07 -05:00
|
|
|
isUserPasswordValid,
|
|
|
|
isUserRoleValid,
|
2019-07-23 04:40:39 -04:00
|
|
|
isUserUsernameValid,
|
|
|
|
isUserVideoLanguages,
|
2018-12-18 11:18:25 -05:00
|
|
|
isUserVideoQuotaDailyValid,
|
2019-03-19 05:35:15 -04:00
|
|
|
isUserVideoQuotaValid,
|
|
|
|
isUserVideosHistoryEnabledValid
|
2017-12-12 11:53:50 -05:00
|
|
|
} from '../../helpers/custom-validators/users'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-08-14 09:28:30 -04:00
|
|
|
import { isSignupAllowed, isSignupAllowedForCurrentIP } from '../../helpers/signup'
|
2018-01-30 07:27:07 -05:00
|
|
|
import { Redis } from '../../lib/redis'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { UserModel } from '../../models/account/user'
|
2017-11-27 11:30:46 -05:00
|
|
|
import { areValidationErrors } from './utils'
|
2018-06-21 05:54:22 -04:00
|
|
|
import { ActorModel } from '../../models/activitypub/actor'
|
2019-05-28 04:46:32 -04:00
|
|
|
import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
|
|
|
|
import { isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels'
|
|
|
|
import { UserRegister } from '../../../shared/models/users/user-register.model'
|
2019-07-15 09:41:56 -04:00
|
|
|
import { isThemeNameValid } from '../../helpers/custom-validators/plugins'
|
|
|
|
import { isThemeRegistered } from '../../lib/plugins/theme-utils'
|
2019-07-23 04:40:39 -04:00
|
|
|
import { doesVideoExist } from '../../helpers/middlewares'
|
2019-07-30 03:59:19 -04:00
|
|
|
import { UserRole } from '../../../shared/models/users'
|
2019-08-15 05:53:26 -04:00
|
|
|
import { MUserDefault } from '@server/typings/models'
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersAddValidator = [
|
2017-11-04 13:32:38 -04:00
|
|
|
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
|
2017-09-15 06:17:08 -04:00
|
|
|
body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
|
|
|
|
body('email').isEmail().withMessage('Should have a valid email'),
|
|
|
|
body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
|
2018-08-28 03:01:35 -04:00
|
|
|
body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
|
2019-08-22 04:59:14 -04:00
|
|
|
body('role')
|
|
|
|
.customSanitizer(toIntOrNull)
|
|
|
|
.custom(isUserRoleValid).withMessage('Should have a valid role'),
|
2019-04-15 04:49:46 -04:00
|
|
|
body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-01-26 07:55:27 -05:00
|
|
|
logger.debug('Checking usersAdd parameters', { parameters: omit(req.body, 'password') })
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
|
|
|
|
|
2019-07-30 03:59:19 -04:00
|
|
|
const authUser = res.locals.oauth.token.User
|
|
|
|
if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) {
|
|
|
|
return res.status(403)
|
2019-08-22 04:59:14 -04:00
|
|
|
.json({ error: 'You can only create users (and not administrators or moderators)' })
|
2019-07-30 03:59:19 -04:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2017-07-05 07:26:25 -04:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersRegisterValidator = [
|
|
|
|
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username'),
|
|
|
|
body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
|
|
|
|
body('email').isEmail().withMessage('Should have a valid email'),
|
2019-06-07 10:59:53 -04:00
|
|
|
body('displayName')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
|
|
|
|
|
|
|
|
body('channel.name')
|
|
|
|
.optional()
|
|
|
|
.custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
|
|
|
|
body('channel.displayName')
|
|
|
|
.optional()
|
|
|
|
.custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
|
2017-09-06 10:35:40 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-01-26 07:55:27 -05:00
|
|
|
logger.debug('Checking usersRegister parameters', { parameters: omit(req.body, 'password') })
|
2017-09-06 10:35:40 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserNameOrEmailDoesNotAlreadyExist(req.body.username, req.body.email, res)) return
|
|
|
|
|
2019-05-28 04:46:32 -04:00
|
|
|
const body: UserRegister = req.body
|
|
|
|
if (body.channel) {
|
|
|
|
if (!body.channel.name || !body.channel.displayName) {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' })
|
2019-05-28 04:46:32 -04:00
|
|
|
}
|
|
|
|
|
2019-05-29 05:03:01 -04:00
|
|
|
if (body.channel.name === body.username) {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Channel name cannot be the same than user username.' })
|
2019-05-29 05:03:01 -04:00
|
|
|
}
|
|
|
|
|
2019-05-28 04:46:32 -04:00
|
|
|
const existing = await ActorModel.loadLocalByName(body.channel.name)
|
|
|
|
if (existing) {
|
|
|
|
return res.status(409)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: `Channel with name ${body.channel.name} already exists.` })
|
2019-05-28 04:46:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersRemoveValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 06:17:08 -04:00
|
|
|
logger.debug('Checking usersRemove parameters', { parameters: req.params })
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserIdExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const user = res.locals.user
|
|
|
|
if (user.username === 'root') {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Cannot remove the root user' })
|
2017-11-27 11:30:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2017-09-05 15:29:39 -04:00
|
|
|
|
2018-08-08 08:58:21 -04:00
|
|
|
const usersBlockingValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
2018-08-08 11:36:10 -04:00
|
|
|
body('reason').optional().custom(isUserBlockedReasonValid).withMessage('Should have a valid blocking reason'),
|
2018-08-08 08:58:21 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-08-08 11:36:10 -04:00
|
|
|
logger.debug('Checking usersBlocking parameters', { parameters: req.params })
|
2018-08-08 08:58:21 -04:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserIdExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
const user = res.locals.user
|
|
|
|
if (user.username === 'root') {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Cannot block the root user' })
|
2018-08-08 08:58:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-08 04:55:27 -04:00
|
|
|
const deleteMeValidator = [
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-08-08 04:55:27 -04:00
|
|
|
if (user.username === 'root') {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'You cannot delete your root account.' })
|
2018-08-08 04:55:27 -04:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersUpdateValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
2019-02-11 03:30:29 -05:00
|
|
|
body('password').optional().custom(isUserPasswordValid).withMessage('Should have a valid password'),
|
2017-09-15 06:17:08 -04:00
|
|
|
body('email').optional().isEmail().withMessage('Should have a valid email attribute'),
|
2018-11-21 02:48:29 -05:00
|
|
|
body('emailVerified').optional().isBoolean().withMessage('Should have a valid email verified attribute'),
|
2017-09-15 06:17:08 -04:00
|
|
|
body('videoQuota').optional().custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
|
2018-08-28 03:01:35 -04:00
|
|
|
body('videoQuotaDaily').optional().custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
|
2019-08-22 04:59:14 -04:00
|
|
|
body('role')
|
|
|
|
.optional()
|
|
|
|
.customSanitizer(toIntOrNull)
|
|
|
|
.custom(isUserRoleValid).withMessage('Should have a valid role'),
|
2019-04-15 04:49:46 -04:00
|
|
|
body('adminFlags').optional().custom(isUserAdminFlagsValid).withMessage('Should have a valid admin flags'),
|
2017-09-05 15:29:39 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 06:17:08 -04:00
|
|
|
logger.debug('Checking usersUpdate parameters', { parameters: req.body })
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserIdExist(req.params.id, res)) return
|
|
|
|
|
2018-01-23 03:15:36 -05:00
|
|
|
const user = res.locals.user
|
|
|
|
if (user.username === 'root' && req.body.role !== undefined && user.role !== req.body.role) {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Cannot change root role.' })
|
2018-01-23 03:15:36 -05:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersUpdateMeValidator = [
|
2019-06-11 05:54:33 -04:00
|
|
|
body('displayName')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserDisplayNameValid).withMessage('Should have a valid display name'),
|
|
|
|
body('description')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserDescriptionValid).withMessage('Should have a valid description'),
|
|
|
|
body('currentPassword')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserPasswordValid).withMessage('Should have a valid current password'),
|
|
|
|
body('password')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserPasswordValid).withMessage('Should have a valid password'),
|
|
|
|
body('email')
|
|
|
|
.optional()
|
|
|
|
.isEmail().withMessage('Should have a valid email attribute'),
|
|
|
|
body('nsfwPolicy')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserNSFWPolicyValid).withMessage('Should have a valid display Not Safe For Work policy'),
|
|
|
|
body('autoPlayVideo')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
|
2019-06-19 08:55:58 -04:00
|
|
|
body('videoLanguages')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'),
|
2018-12-18 11:18:25 -05:00
|
|
|
body('videosHistoryEnabled')
|
|
|
|
.optional()
|
|
|
|
.custom(isUserVideosHistoryEnabledValid).withMessage('Should have a valid videos history enabled attribute'),
|
2019-07-09 05:45:19 -04:00
|
|
|
body('theme')
|
|
|
|
.optional()
|
2019-07-15 09:41:56 -04:00
|
|
|
.custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'),
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2018-09-26 10:28:15 -04:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-01-26 07:55:27 -05:00
|
|
|
logger.debug('Checking usersUpdateMe parameters', { parameters: omit(req.body, 'password') })
|
2017-09-05 15:29:39 -04:00
|
|
|
|
2019-06-11 08:30:49 -04:00
|
|
|
if (req.body.password || req.body.email) {
|
2018-09-26 10:28:15 -04:00
|
|
|
if (!req.body.currentPassword) {
|
|
|
|
return res.status(400)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'currentPassword parameter is missing.' })
|
2018-09-26 10:28:15 -04:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2019-03-19 11:23:02 -04:00
|
|
|
const user = res.locals.oauth.token.User
|
2018-09-26 10:28:15 -04:00
|
|
|
if (await user.isPasswordMatch(req.body.currentPassword) !== true) {
|
|
|
|
return res.status(401)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'currentPassword is invalid.' })
|
2018-09-26 10:28:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2017-09-05 15:29:39 -04:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersGetValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
2017-03-08 15:35:43 -05:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2018-01-26 07:55:27 -05:00
|
|
|
logger.debug('Checking usersGet parameters', { parameters: req.params })
|
2017-11-27 11:30:46 -05:00
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserIdExist(req.params.id, res)) return
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2017-03-08 15:35:43 -05:00
|
|
|
|
2017-09-15 06:17:08 -04:00
|
|
|
const usersVideoRatingValidator = [
|
2017-10-24 13:41:09 -04:00
|
|
|
param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid video id'),
|
2017-07-11 10:01:56 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
2017-09-15 06:17:08 -04:00
|
|
|
logger.debug('Checking usersVideoRating parameters', { parameters: req.params })
|
2017-07-11 10:01:56 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
if (areValidationErrors(req, res)) return
|
2019-03-19 04:26:50 -04:00
|
|
|
if (!await doesVideoExist(req.params.videoId, res, 'id')) return
|
2017-11-27 11:30:46 -05:00
|
|
|
|
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const ensureUserRegistrationAllowed = [
|
2017-11-27 11:30:46 -05:00
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const allowed = await isSignupAllowed()
|
|
|
|
if (allowed === false) {
|
|
|
|
return res.status(403)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'User registration is not enabled or user limit is reached.' })
|
2017-11-27 11:30:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
2017-09-15 06:17:08 -04:00
|
|
|
}
|
|
|
|
]
|
2017-07-25 14:17:28 -04:00
|
|
|
|
2018-05-22 13:43:13 -04:00
|
|
|
const ensureUserRegistrationAllowedForIP = [
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const allowed = isSignupAllowedForCurrentIP(req.ip)
|
|
|
|
|
|
|
|
if (allowed === false) {
|
|
|
|
return res.status(403)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'You are not on a network authorized for registration.' })
|
2018-05-22 13:43:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-01-30 07:27:07 -05:00
|
|
|
const usersAskResetPasswordValidator = [
|
|
|
|
body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersAskResetPassword parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
2019-02-11 03:30:29 -05:00
|
|
|
|
2018-01-30 07:27:07 -05:00
|
|
|
const exists = await checkUserEmailExist(req.body.email, res, false)
|
|
|
|
if (!exists) {
|
|
|
|
logger.debug('User with email %s does not exist (asking reset password).', req.body.email)
|
|
|
|
// Do not leak our emails
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const usersResetPasswordValidator = [
|
|
|
|
param('id').isInt().not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
body('verificationString').not().isEmpty().withMessage('Should have a valid verification string'),
|
|
|
|
body('password').custom(isUserPasswordValid).withMessage('Should have a valid password'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersResetPassword parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserIdExist(req.params.id, res)) return
|
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.user
|
2018-01-30 07:27:07 -05:00
|
|
|
const redisVerificationString = await Redis.Instance.getResetPasswordLink(user.id)
|
|
|
|
|
|
|
|
if (redisVerificationString !== req.body.verificationString) {
|
|
|
|
return res
|
|
|
|
.status(403)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Invalid verification string.' })
|
2018-01-30 07:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2018-08-31 03:18:19 -04:00
|
|
|
const usersAskSendVerifyEmailValidator = [
|
|
|
|
body('email').isEmail().not().isEmpty().withMessage('Should have a valid email'),
|
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking askUsersSendVerifyEmail parameters', { parameters: req.body })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
const exists = await checkUserEmailExist(req.body.email, res, false)
|
|
|
|
if (!exists) {
|
|
|
|
logger.debug('User with email %s does not exist (asking verify email).', req.body.email)
|
|
|
|
// Do not leak our emails
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
const usersVerifyEmailValidator = [
|
2019-06-11 05:54:33 -04:00
|
|
|
param('id')
|
|
|
|
.isInt().not().isEmpty().withMessage('Should have a valid id'),
|
|
|
|
|
|
|
|
body('verificationString')
|
|
|
|
.not().isEmpty().withMessage('Should have a valid verification string'),
|
|
|
|
body('isPendingEmail')
|
|
|
|
.optional()
|
2019-07-25 11:28:45 -04:00
|
|
|
.customSanitizer(toBooleanOrNull),
|
2018-08-31 03:18:19 -04:00
|
|
|
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
logger.debug('Checking usersVerifyEmail parameters', { parameters: req.params })
|
|
|
|
|
|
|
|
if (areValidationErrors(req, res)) return
|
|
|
|
if (!await checkUserIdExist(req.params.id, res)) return
|
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.user
|
2018-08-31 03:18:19 -04:00
|
|
|
const redisVerificationString = await Redis.Instance.getVerifyEmailLink(user.id)
|
|
|
|
|
|
|
|
if (redisVerificationString !== req.body.verificationString) {
|
|
|
|
return res
|
|
|
|
.status(403)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Invalid verification string.' })
|
2018-08-31 03:18:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
Users can change ownership of their video [#510] (#888)
* [#510] Create a new route to get the list of user names
To be able to transfer ownership to a user,
we need to be able to select him from the list of users.
Because the list could be too big, we add a autocomplete feature.
This commit does the following:
* Add a API endpoint to get a list of user names by searching its name
* [#510] The user can choose the next owner of the video
To be able to transfer ownership to a user,
we need the owner to be able to select the user.
The server can autocomplete the name of the user to give the ownership.
We add a dialog for the user to actually select it.
This commit does the following:
* Create a modal for the owner to select the next one
* Opens this modal with a button into the menu *more*
* Make the dependency injection
* [#510] When the user choose the next owner, create a request in database
For the change of ownership to happen, we need to store the temporary requests.
When the user make the request, save it to database.
This commit does the following:
* Create the model to persist change ownership requests
* Add an API to manage ownership operations
* Add a route to persist an ownership request
* [#510] A user can fetch its ownership requests sent to him
To be able to accept or refuse a change of ownership,
the user must be able to fetch them.
This commit does the following:
* Add an API to list ownership for a user
* Add the query to database model
* [#510] A user can validate an ownership requests sent to him - server
The user can accept or refuse any ownership request that was sent to him.
This commit focus only on the server part.
This commit does the following:
* Add an API for the user to accept or refuse a video ownership
* Add validators to ensure security access
* Add a query to load a specific video change ownership request
* [#510] A user can validate an ownership requests sent to him - web
The user can accept or refuse any ownership request that was sent to him.
This commit focus only on the web part.
This commit does the following:
* Add a page to list user ownership changes
* Add actions to accept or refuse them
* When accepting, show a modal requiring the channel to send the video
* Correct lint - to squash
* [#510] PR reviews - to squash
This commit does the following:
* Search parameter for user autocompletion is required from middleware directly
* [#510] PR reviews - to squash with creation in database commit
This commit does the following:
* Add the status attribute in model
* Set this attribute on instance creation
* Use AccountModel method `loadLocalByName`
* [#510] PR reviews - to squash with fetch ownership
This commit does the following:
* Add the scope `FULL` for database queries with includes
* Add classic pagination middlewares
* [#510] PR reviews - to squash with ownership validation - server
This commit does the following:
* Add a middleware to validate whether a user can validate an ownership
* Change the ownership status instead of deleting the row
* [#510] PR reviews - to squash with ownership validation - client
This commit does the following:
* Correct indentation of html files with two-spaces indentation
* Use event emitter instead of function for accept event
* Update the sort of ownership change table for a decreasing order by creation date
* Add the status in ownership change table
* Use classic method syntax
* code style - to squash
* Add new user right - to squash
* Move the change to my-account instead of video-watch - to squash
As requested in pull-request, move the action to change ownership into my videos page.
The rest of the logic was not really changed.
This commit does the following:
- Move the modal into my video page
- Create the generic component `button` to keep some styles and logic
* [#510] Add tests for the new feature
To avoid regression, we add tests for all api of ownership change.
This commit does the following:
- Create an end-to-end test for ownership change
- Divide it to one test per request
* [#510] Do not send twice the same request to avoid spam
We can send several time the same request to change ownership.
However, it will spam the user.
To avoid this, we do not save a request already existing in database.
This commit does the following:
- Check whether the request exist in database
- Add tests to verify this new condition
* [#510] Change icons
Change icons so they remains logic with the rest of the application.
This commit does the following:
- Add svg for missing icons
- Add icons in `my-button` component
- Use these new icons
* [#510] Add control about the user quota
The user should be able to accept a new video only if his quota allows it.
This commit does the following:
- Update the middleware to control the quota
- Add tests verifying the control
* Correct merge
- Use new modal system
- Move button to new directory `buttons`
* PR reviews - to squash
2018-09-04 02:57:13 -04:00
|
|
|
const userAutocompleteValidator = [
|
|
|
|
param('search').isString().not().isEmpty().withMessage('Should have a search parameter')
|
|
|
|
]
|
|
|
|
|
2019-04-09 05:02:02 -04:00
|
|
|
const ensureAuthUserOwnsAccountValidator = [
|
|
|
|
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const user = res.locals.oauth.token.User
|
|
|
|
|
|
|
|
if (res.locals.account.id !== user.Account.id) {
|
|
|
|
return res.status(403)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Only owner can access ratings list.' })
|
2019-04-09 05:02:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2019-07-30 03:59:19 -04:00
|
|
|
const ensureCanManageUser = [
|
|
|
|
(req: express.Request, res: express.Response, next: express.NextFunction) => {
|
|
|
|
const authUser = res.locals.oauth.token.User
|
|
|
|
const onUser = res.locals.user
|
|
|
|
|
|
|
|
if (authUser.role === UserRole.ADMINISTRATOR) return next()
|
|
|
|
if (authUser.role === UserRole.MODERATOR && onUser.role === UserRole.USER) return next()
|
|
|
|
|
|
|
|
return res.status(403)
|
|
|
|
.json({ error: 'A moderator can only manager users.' })
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2016-08-04 16:32:36 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
usersAddValidator,
|
2018-08-08 04:55:27 -04:00
|
|
|
deleteMeValidator,
|
2017-09-06 10:35:40 -04:00
|
|
|
usersRegisterValidator,
|
2018-08-08 08:58:21 -04:00
|
|
|
usersBlockingValidator,
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRemoveValidator,
|
|
|
|
usersUpdateValidator,
|
2017-09-05 15:29:39 -04:00
|
|
|
usersUpdateMeValidator,
|
2017-07-25 14:17:28 -04:00
|
|
|
usersVideoRatingValidator,
|
2017-09-05 15:29:39 -04:00
|
|
|
ensureUserRegistrationAllowed,
|
2018-05-22 13:43:13 -04:00
|
|
|
ensureUserRegistrationAllowedForIP,
|
2017-12-29 13:10:13 -05:00
|
|
|
usersGetValidator,
|
2018-01-30 07:27:07 -05:00
|
|
|
usersAskResetPasswordValidator,
|
2018-08-31 03:18:19 -04:00
|
|
|
usersResetPasswordValidator,
|
|
|
|
usersAskSendVerifyEmailValidator,
|
Users can change ownership of their video [#510] (#888)
* [#510] Create a new route to get the list of user names
To be able to transfer ownership to a user,
we need to be able to select him from the list of users.
Because the list could be too big, we add a autocomplete feature.
This commit does the following:
* Add a API endpoint to get a list of user names by searching its name
* [#510] The user can choose the next owner of the video
To be able to transfer ownership to a user,
we need the owner to be able to select the user.
The server can autocomplete the name of the user to give the ownership.
We add a dialog for the user to actually select it.
This commit does the following:
* Create a modal for the owner to select the next one
* Opens this modal with a button into the menu *more*
* Make the dependency injection
* [#510] When the user choose the next owner, create a request in database
For the change of ownership to happen, we need to store the temporary requests.
When the user make the request, save it to database.
This commit does the following:
* Create the model to persist change ownership requests
* Add an API to manage ownership operations
* Add a route to persist an ownership request
* [#510] A user can fetch its ownership requests sent to him
To be able to accept or refuse a change of ownership,
the user must be able to fetch them.
This commit does the following:
* Add an API to list ownership for a user
* Add the query to database model
* [#510] A user can validate an ownership requests sent to him - server
The user can accept or refuse any ownership request that was sent to him.
This commit focus only on the server part.
This commit does the following:
* Add an API for the user to accept or refuse a video ownership
* Add validators to ensure security access
* Add a query to load a specific video change ownership request
* [#510] A user can validate an ownership requests sent to him - web
The user can accept or refuse any ownership request that was sent to him.
This commit focus only on the web part.
This commit does the following:
* Add a page to list user ownership changes
* Add actions to accept or refuse them
* When accepting, show a modal requiring the channel to send the video
* Correct lint - to squash
* [#510] PR reviews - to squash
This commit does the following:
* Search parameter for user autocompletion is required from middleware directly
* [#510] PR reviews - to squash with creation in database commit
This commit does the following:
* Add the status attribute in model
* Set this attribute on instance creation
* Use AccountModel method `loadLocalByName`
* [#510] PR reviews - to squash with fetch ownership
This commit does the following:
* Add the scope `FULL` for database queries with includes
* Add classic pagination middlewares
* [#510] PR reviews - to squash with ownership validation - server
This commit does the following:
* Add a middleware to validate whether a user can validate an ownership
* Change the ownership status instead of deleting the row
* [#510] PR reviews - to squash with ownership validation - client
This commit does the following:
* Correct indentation of html files with two-spaces indentation
* Use event emitter instead of function for accept event
* Update the sort of ownership change table for a decreasing order by creation date
* Add the status in ownership change table
* Use classic method syntax
* code style - to squash
* Add new user right - to squash
* Move the change to my-account instead of video-watch - to squash
As requested in pull-request, move the action to change ownership into my videos page.
The rest of the logic was not really changed.
This commit does the following:
- Move the modal into my video page
- Create the generic component `button` to keep some styles and logic
* [#510] Add tests for the new feature
To avoid regression, we add tests for all api of ownership change.
This commit does the following:
- Create an end-to-end test for ownership change
- Divide it to one test per request
* [#510] Do not send twice the same request to avoid spam
We can send several time the same request to change ownership.
However, it will spam the user.
To avoid this, we do not save a request already existing in database.
This commit does the following:
- Check whether the request exist in database
- Add tests to verify this new condition
* [#510] Change icons
Change icons so they remains logic with the rest of the application.
This commit does the following:
- Add svg for missing icons
- Add icons in `my-button` component
- Use these new icons
* [#510] Add control about the user quota
The user should be able to accept a new video only if his quota allows it.
This commit does the following:
- Update the middleware to control the quota
- Add tests verifying the control
* Correct merge
- Use new modal system
- Move button to new directory `buttons`
* PR reviews - to squash
2018-09-04 02:57:13 -04:00
|
|
|
usersVerifyEmailValidator,
|
2019-04-09 05:02:02 -04:00
|
|
|
userAutocompleteValidator,
|
2019-07-30 03:59:19 -04:00
|
|
|
ensureAuthUserOwnsAccountValidator,
|
|
|
|
ensureCanManageUser
|
2017-09-05 15:29:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-01-30 07:27:07 -05:00
|
|
|
function checkUserIdExist (id: number, res: express.Response) {
|
|
|
|
return checkUserExist(() => UserModel.loadById(id), res)
|
|
|
|
}
|
2017-11-27 11:30:46 -05:00
|
|
|
|
2018-01-30 07:27:07 -05:00
|
|
|
function checkUserEmailExist (email: string, res: express.Response, abortResponse = true) {
|
|
|
|
return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse)
|
2017-05-15 16:22:03 -04:00
|
|
|
}
|
2017-09-06 10:35:40 -04:00
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const user = await UserModel.loadByUsernameOrEmail(username, email)
|
2017-11-27 11:30:46 -05:00
|
|
|
|
|
|
|
if (user) {
|
|
|
|
res.status(409)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'User with this username or email already exists.' })
|
2017-11-27 11:30:46 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-06-21 05:54:22 -04:00
|
|
|
const actor = await ActorModel.loadLocalByName(username)
|
|
|
|
if (actor) {
|
|
|
|
res.status(409)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
|
2018-06-21 05:54:22 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-27 11:30:46 -05:00
|
|
|
return true
|
2017-09-06 10:35:40 -04:00
|
|
|
}
|
2018-01-30 07:27:07 -05:00
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
async function checkUserExist (finder: () => Bluebird<MUserDefault>, res: express.Response, abortResponse = true) {
|
2018-01-30 07:27:07 -05:00
|
|
|
const user = await finder()
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
if (abortResponse === true) {
|
|
|
|
res.status(404)
|
2019-07-30 03:59:19 -04:00
|
|
|
.json({ error: 'User not found' })
|
2018-01-30 07:27:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.user = user
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|