2017-06-05 15:53:49 -04:00
|
|
|
import * as express from 'express'
|
2018-02-13 12:17:05 -05:00
|
|
|
import 'multer'
|
2017-12-29 13:10:13 -05:00
|
|
|
import { extname, join } from 'path'
|
|
|
|
import * as uuidv4 from 'uuid/v4'
|
2018-03-29 04:58:24 -04:00
|
|
|
import * as RateLimit from 'express-rate-limit'
|
2017-11-10 11:27:49 -05:00
|
|
|
import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { retryTransactionWrapper } from '../../helpers/database-utils'
|
2018-02-13 12:17:05 -05:00
|
|
|
import { processImage } from '../../helpers/image-utils'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2018-04-24 09:10:54 -04:00
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
2018-03-29 04:58:24 -04:00
|
|
|
import { AVATARS_SIZE, CONFIG, IMAGE_MIMETYPE_EXT, RATES_LIMIT, sequelizeTypescript } from '../../initializers'
|
2018-01-04 08:04:02 -05:00
|
|
|
import { updateActorAvatarInstance } from '../../lib/activitypub'
|
2018-02-15 08:46:26 -05:00
|
|
|
import { sendUpdateActor } from '../../lib/activitypub/send'
|
2018-01-30 07:27:07 -05:00
|
|
|
import { Emailer } from '../../lib/emailer'
|
|
|
|
import { Redis } from '../../lib/redis'
|
2017-12-14 11:38:41 -05:00
|
|
|
import { createUserAccountAndChannel } from '../../lib/user'
|
2017-05-15 16:22:03 -04:00
|
|
|
import {
|
2018-01-30 09:16:24 -05:00
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
ensureUserRegistrationAllowed,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort,
|
|
|
|
token,
|
|
|
|
usersAddValidator,
|
|
|
|
usersGetValidator,
|
|
|
|
usersRegisterValidator,
|
|
|
|
usersRemoveValidator,
|
|
|
|
usersSortValidator,
|
|
|
|
usersUpdateMeValidator,
|
|
|
|
usersUpdateValidator,
|
|
|
|
usersVideoRatingValidator
|
2017-05-15 16:22:03 -04:00
|
|
|
} from '../../middlewares'
|
2018-01-30 07:27:07 -05:00
|
|
|
import {
|
2018-01-30 09:16:24 -05:00
|
|
|
usersAskResetPasswordValidator,
|
|
|
|
usersResetPasswordValidator,
|
|
|
|
usersUpdateMyAvatarValidator,
|
2018-01-30 07:27:07 -05:00
|
|
|
videosSortValidator
|
|
|
|
} from '../../middlewares/validators'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
|
|
|
|
import { UserModel } from '../../models/account/user'
|
2018-01-23 03:15:36 -05:00
|
|
|
import { OAuthTokenModel } from '../../models/oauth/oauth-token'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { VideoModel } from '../../models/video/video'
|
2018-04-19 05:01:34 -04:00
|
|
|
import { VideoSortField } from '../../../client/src/app/shared/video/sort-field.type'
|
2018-04-24 09:10:54 -04:00
|
|
|
import { createReqFiles } from '../../helpers/express-utils'
|
2017-05-15 16:22:03 -04:00
|
|
|
|
2018-02-13 12:17:05 -05:00
|
|
|
const reqAvatarFile = createReqFiles([ 'avatarfile' ], IMAGE_MIMETYPE_EXT, { avatarfile: CONFIG.STORAGE.AVATARS_DIR })
|
2018-03-29 04:58:24 -04:00
|
|
|
const loginRateLimiter = new RateLimit({
|
|
|
|
windowMs: RATES_LIMIT.LOGIN.WINDOW_MS,
|
|
|
|
max: RATES_LIMIT.LOGIN.MAX,
|
|
|
|
delayMs: 0
|
|
|
|
})
|
2017-12-29 13:10:13 -05:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
const usersRouter = express.Router()
|
|
|
|
|
|
|
|
usersRouter.get('/me',
|
|
|
|
authenticate,
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(getUserInformation)
|
2017-03-08 15:35:43 -05:00
|
|
|
)
|
|
|
|
|
2018-01-08 06:53:09 -05:00
|
|
|
usersRouter.get('/me/video-quota-used',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(getUserVideoQuotaUsed)
|
|
|
|
)
|
|
|
|
|
2017-10-31 06:52:52 -04:00
|
|
|
usersRouter.get('/me/videos',
|
|
|
|
authenticate,
|
|
|
|
paginationValidator,
|
|
|
|
videosSortValidator,
|
2018-01-17 04:50:33 -05:00
|
|
|
setDefaultSort,
|
2018-01-18 04:53:54 -05:00
|
|
|
setDefaultPagination,
|
2017-10-31 06:52:52 -04:00
|
|
|
asyncMiddleware(getUserVideos)
|
|
|
|
)
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRouter.get('/me/videos/:videoId/rating',
|
|
|
|
authenticate,
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(usersVideoRatingValidator),
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(getUserVideoRating)
|
2017-03-08 15:35:43 -05:00
|
|
|
)
|
2016-08-04 16:32:36 -04:00
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRouter.get('/',
|
2017-11-29 07:18:05 -05:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-05-15 16:22:03 -04:00
|
|
|
paginationValidator,
|
|
|
|
usersSortValidator,
|
2018-01-17 04:50:33 -05:00
|
|
|
setDefaultSort,
|
2018-01-18 04:53:54 -05:00
|
|
|
setDefaultPagination,
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(listUsers)
|
2016-08-16 16:31:45 -04:00
|
|
|
)
|
|
|
|
|
2017-09-05 15:29:39 -04:00
|
|
|
usersRouter.get('/:id',
|
2018-04-16 04:48:17 -04:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(usersGetValidator),
|
2017-09-05 15:29:39 -04:00
|
|
|
getUser
|
|
|
|
)
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRouter.post('/',
|
|
|
|
authenticate,
|
2017-10-27 10:55:03 -04:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(usersAddValidator),
|
|
|
|
asyncMiddleware(createUserRetryWrapper)
|
2016-08-04 16:32:36 -04:00
|
|
|
)
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRouter.post('/register',
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(ensureUserRegistrationAllowed),
|
|
|
|
asyncMiddleware(usersRegisterValidator),
|
2017-11-16 12:40:50 -05:00
|
|
|
asyncMiddleware(registerUserRetryWrapper)
|
2017-04-09 06:08:36 -04:00
|
|
|
)
|
|
|
|
|
2017-09-05 15:29:39 -04:00
|
|
|
usersRouter.put('/me',
|
|
|
|
authenticate,
|
|
|
|
usersUpdateMeValidator,
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(updateMe)
|
2017-09-05 15:29:39 -04:00
|
|
|
)
|
|
|
|
|
2017-12-29 13:10:13 -05:00
|
|
|
usersRouter.post('/me/avatar/pick',
|
|
|
|
authenticate,
|
|
|
|
reqAvatarFile,
|
|
|
|
usersUpdateMyAvatarValidator,
|
|
|
|
asyncMiddleware(updateMyAvatar)
|
|
|
|
)
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRouter.put('/:id',
|
|
|
|
authenticate,
|
2017-10-27 10:55:03 -04:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(usersUpdateValidator),
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(updateUser)
|
2016-08-04 16:32:36 -04:00
|
|
|
)
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
usersRouter.delete('/:id',
|
|
|
|
authenticate,
|
2017-10-27 10:55:03 -04:00
|
|
|
ensureUserHasRight(UserRight.MANAGE_USERS),
|
2017-11-27 11:30:46 -05:00
|
|
|
asyncMiddleware(usersRemoveValidator),
|
2017-10-25 05:55:06 -04:00
|
|
|
asyncMiddleware(removeUser)
|
2016-08-04 16:32:36 -04:00
|
|
|
)
|
2016-08-05 10:09:39 -04:00
|
|
|
|
2018-01-30 07:27:07 -05:00
|
|
|
usersRouter.post('/ask-reset-password',
|
|
|
|
asyncMiddleware(usersAskResetPasswordValidator),
|
|
|
|
asyncMiddleware(askResetUserPassword)
|
|
|
|
)
|
|
|
|
|
|
|
|
usersRouter.post('/:id/reset-password',
|
|
|
|
asyncMiddleware(usersResetPasswordValidator),
|
|
|
|
asyncMiddleware(resetUserPassword)
|
|
|
|
)
|
|
|
|
|
2018-03-29 04:58:24 -04:00
|
|
|
usersRouter.post('/token',
|
|
|
|
loginRateLimiter,
|
|
|
|
token,
|
|
|
|
success
|
|
|
|
)
|
2016-08-04 16:32:36 -04:00
|
|
|
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
|
2016-03-21 06:56:33 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 16:22:03 -04:00
|
|
|
export {
|
|
|
|
usersRouter
|
|
|
|
}
|
2016-03-21 06:56:33 -04:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-10-31 06:52:52 -04:00
|
|
|
async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const user = res.locals.oauth.token.User as UserModel
|
2018-04-19 05:01:34 -04:00
|
|
|
const resultList = await VideoModel.listAccountVideosForApi(
|
|
|
|
user.Account.id,
|
|
|
|
req.query.start as number,
|
|
|
|
req.query.count as number,
|
|
|
|
req.query.sort as VideoSortField,
|
|
|
|
false // Display my NSFW videos
|
|
|
|
)
|
2017-10-31 06:52:52 -04:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-24 13:41:09 -04:00
|
|
|
const options = {
|
2017-11-16 12:40:50 -05:00
|
|
|
arguments: [ req ],
|
2017-10-24 13:41:09 -04:00
|
|
|
errorMessage: 'Cannot insert the user with many retries.'
|
|
|
|
}
|
|
|
|
|
2018-01-18 04:53:54 -05:00
|
|
|
const { user, account } = await retryTransactionWrapper(createUser, options)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2018-01-18 04:53:54 -05:00
|
|
|
return res.json({
|
|
|
|
user: {
|
|
|
|
id: user.id,
|
2018-04-25 04:21:38 -04:00
|
|
|
account: {
|
|
|
|
id: account.id,
|
|
|
|
uuid: account.Actor.uuid
|
|
|
|
}
|
2018-01-18 04:53:54 -05:00
|
|
|
}
|
|
|
|
}).end()
|
2017-10-24 13:41:09 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 12:40:50 -05:00
|
|
|
async function createUser (req: express.Request) {
|
2017-07-10 13:43:21 -04:00
|
|
|
const body: UserCreate = req.body
|
2018-01-18 04:53:54 -05:00
|
|
|
const userToCreate = new UserModel({
|
2017-07-10 13:43:21 -04:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
2018-04-19 05:01:34 -04:00
|
|
|
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
2017-12-19 04:45:49 -05:00
|
|
|
autoPlayVideo: true,
|
2017-10-27 10:55:03 -04:00
|
|
|
role: body.role,
|
2017-09-04 14:07:54 -04:00
|
|
|
videoQuota: body.videoQuota
|
2016-08-04 16:32:36 -04:00
|
|
|
})
|
|
|
|
|
2018-01-18 04:53:54 -05:00
|
|
|
const { user, account } = await createUserAccountAndChannel(userToCreate)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2017-11-10 08:48:08 -05:00
|
|
|
logger.info('User %s with its channel and account created.', body.username)
|
2018-01-18 04:53:54 -05:00
|
|
|
|
|
|
|
return { user, account }
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
2017-11-16 12:40:50 -05:00
|
|
|
async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ req ],
|
|
|
|
errorMessage: 'Cannot insert the user with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
await retryTransactionWrapper(registerUser, options)
|
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function registerUser (req: express.Request) {
|
2017-09-06 10:35:40 -04:00
|
|
|
const body: UserCreate = req.body
|
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const user = new UserModel({
|
2017-09-06 10:35:40 -04:00
|
|
|
username: body.username,
|
|
|
|
password: body.password,
|
|
|
|
email: body.email,
|
2018-04-19 05:01:34 -04:00
|
|
|
nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
|
2017-12-19 04:45:49 -05:00
|
|
|
autoPlayVideo: true,
|
2017-10-27 10:55:03 -04:00
|
|
|
role: UserRole.USER,
|
2017-09-06 10:35:40 -04:00
|
|
|
videoQuota: CONFIG.USER.VIDEO_QUOTA
|
|
|
|
})
|
|
|
|
|
2017-11-10 08:48:08 -05:00
|
|
|
await createUserAccountAndChannel(user)
|
2017-11-16 12:40:50 -05:00
|
|
|
|
|
|
|
logger.info('User %s with its channel and account registered.', body.username)
|
2017-09-06 10:35:40 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-10-31 06:52:52 -04:00
|
|
|
// We did not load channels in res.locals.user
|
2017-12-12 11:53:50 -05:00
|
|
|
const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
return res.json(user.toFormattedJSON())
|
2016-08-05 11:19:08 -04:00
|
|
|
}
|
|
|
|
|
2018-01-08 06:53:09 -05:00
|
|
|
async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
// We did not load channels in res.locals.user
|
|
|
|
const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
|
|
|
|
const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
videoQuotaUsed
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-05 15:29:39 -04:00
|
|
|
function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2018-01-08 06:53:09 -05:00
|
|
|
return res.json((res.locals.user as UserModel).toFormattedJSON())
|
2017-09-05 15:29:39 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-11 10:01:56 -04:00
|
|
|
const videoId = +req.params.videoId
|
2017-11-10 11:27:49 -05:00
|
|
|
const accountId = +res.locals.oauth.token.User.Account.id
|
2017-03-08 15:35:43 -05:00
|
|
|
|
2017-12-12 11:53:50 -05:00
|
|
|
const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
|
2017-10-26 02:51:11 -04:00
|
|
|
const rating = ratingObj ? ratingObj.type : 'none'
|
|
|
|
|
|
|
|
const json: FormattedUserVideoRate = {
|
|
|
|
videoId,
|
|
|
|
rating
|
|
|
|
}
|
|
|
|
res.json(json)
|
2017-03-08 15:35:43 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const user = await UserModel.loadById(req.params.id)
|
2017-10-25 05:55:06 -04:00
|
|
|
|
|
|
|
await user.destroy()
|
|
|
|
|
|
|
|
return res.sendStatus(204)
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-09-05 15:29:39 -04:00
|
|
|
const body: UserUpdateMe = req.body
|
2017-07-10 13:43:21 -04:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
const user: UserModel = res.locals.oauth.token.user
|
2017-04-03 15:24:36 -04:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
if (body.password !== undefined) user.password = body.password
|
|
|
|
if (body.email !== undefined) user.email = body.email
|
2018-04-19 05:01:34 -04:00
|
|
|
if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
|
2017-12-19 04:45:49 -05:00
|
|
|
if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
await user.save({ transaction: t })
|
|
|
|
|
|
|
|
if (body.description !== undefined) user.Account.description = body.description
|
|
|
|
await user.Account.save({ transaction: t })
|
|
|
|
|
|
|
|
await sendUpdateActor(user.Account, t)
|
|
|
|
})
|
2017-10-25 05:55:06 -04:00
|
|
|
|
2017-10-25 10:52:01 -04:00
|
|
|
return res.sendStatus(204)
|
2016-08-04 16:32:36 -04:00
|
|
|
}
|
|
|
|
|
2017-12-29 13:10:13 -05:00
|
|
|
async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const avatarPhysicalFile = req.files['avatarfile'][0]
|
2018-01-03 10:38:50 -05:00
|
|
|
const user = res.locals.oauth.token.user
|
|
|
|
const actor = user.Account.Actor
|
2017-12-29 13:10:13 -05:00
|
|
|
|
|
|
|
const extension = extname(avatarPhysicalFile.filename)
|
|
|
|
const avatarName = uuidv4() + extension
|
2018-02-13 12:17:05 -05:00
|
|
|
const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
|
|
|
|
await processImage(avatarPhysicalFile, destination, AVATARS_SIZE)
|
2017-12-29 13:10:13 -05:00
|
|
|
|
2018-01-04 08:04:02 -05:00
|
|
|
const avatar = await sequelizeTypescript.transaction(async t => {
|
2018-01-04 08:53:25 -05:00
|
|
|
const updatedActor = await updateActorAvatarInstance(actor, avatarName, t)
|
|
|
|
await updatedActor.save({ transaction: t })
|
2017-12-29 13:10:13 -05:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
await sendUpdateActor(user.Account, t)
|
2017-12-29 13:10:13 -05:00
|
|
|
|
2018-01-04 08:53:25 -05:00
|
|
|
return updatedActor.Avatar
|
2017-12-29 13:10:13 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return res
|
|
|
|
.json({
|
|
|
|
avatar: avatar.toFormattedJSON()
|
|
|
|
})
|
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-09-05 15:29:39 -04:00
|
|
|
const body: UserUpdate = req.body
|
2017-12-12 11:53:50 -05:00
|
|
|
const user = res.locals.user as UserModel
|
2018-01-23 03:15:36 -05:00
|
|
|
const roleChanged = body.role !== undefined && body.role !== user.role
|
2017-09-05 15:29:39 -04:00
|
|
|
|
|
|
|
if (body.email !== undefined) user.email = body.email
|
|
|
|
if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
|
2017-10-27 10:55:03 -04:00
|
|
|
if (body.role !== undefined) user.role = body.role
|
2017-09-05 15:29:39 -04:00
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
await user.save()
|
|
|
|
|
2018-01-23 03:15:36 -05:00
|
|
|
// Destroy user token to refresh rights
|
|
|
|
if (roleChanged) {
|
|
|
|
await OAuthTokenModel.deleteUserToken(user.id)
|
|
|
|
}
|
|
|
|
|
2018-01-03 10:38:50 -05:00
|
|
|
// Don't need to send this update to followers, these attributes are not propagated
|
|
|
|
|
2017-10-25 05:55:06 -04:00
|
|
|
return res.sendStatus(204)
|
2017-09-05 15:29:39 -04:00
|
|
|
}
|
|
|
|
|
2018-01-30 07:27:07 -05:00
|
|
|
async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user = res.locals.user as UserModel
|
|
|
|
|
|
|
|
const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id)
|
|
|
|
const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString
|
|
|
|
await Emailer.Instance.addForgetPasswordEmailJob(user.email, url)
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
|
|
|
|
const user = res.locals.user as UserModel
|
|
|
|
user.password = req.body.password
|
|
|
|
|
|
|
|
await user.save()
|
|
|
|
|
|
|
|
return res.status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-06-10 16:15:25 -04:00
|
|
|
function success (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2016-03-21 06:56:33 -04:00
|
|
|
res.end()
|
|
|
|
}
|