import { unlinkPromise } from '../../helpers/core-utils'
import { retryTransactionWrapper } from '../../helpers/database-utils'
import { logger } from '../../helpers/logger'
-import { createReqFiles, getFormattedObjects } from '../../helpers/utils'
+import { createReqFiles, generateRandomString, getFormattedObjects } from '../../helpers/utils'
import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers'
import { updateActorAvatarInstance } from '../../lib/activitypub'
import { sendUpdateUser } from '../../lib/activitypub/send'
+import { Emailer } from '../../lib/emailer'
+import { EmailPayload } from '../../lib/job-queue/handlers/email'
+import { Redis } from '../../lib/redis'
import { createUserAccountAndChannel } from '../../lib/user'
import {
asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort,
- setPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
+ setDefaultPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
} from '../../middlewares'
-import { usersUpdateMyAvatarValidator, videosSortValidator } from '../../middlewares/validators'
+import {
+ usersAskResetPasswordValidator, usersResetPasswordValidator, usersUpdateMyAvatarValidator,
+ videosSortValidator
+} from '../../middlewares/validators'
import { AccountVideoRateModel } from '../../models/account/account-video-rate'
import { UserModel } from '../../models/account/user'
+import { OAuthTokenModel } from '../../models/oauth/oauth-token'
import { VideoModel } from '../../models/video/video'
const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT)
paginationValidator,
videosSortValidator,
setDefaultSort,
- setPagination,
+ setDefaultPagination,
asyncMiddleware(getUserVideos)
)
paginationValidator,
usersSortValidator,
setDefaultSort,
- setPagination,
+ setDefaultPagination,
asyncMiddleware(listUsers)
)
asyncMiddleware(removeUser)
)
+usersRouter.post('/ask-reset-password',
+ asyncMiddleware(usersAskResetPasswordValidator),
+ asyncMiddleware(askResetUserPassword)
+)
+
+usersRouter.post('/:id/reset-password',
+ asyncMiddleware(usersResetPasswordValidator),
+ asyncMiddleware(resetUserPassword)
+)
+
usersRouter.post('/token', token, success)
// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
errorMessage: 'Cannot insert the user with many retries.'
}
- await retryTransactionWrapper(createUser, options)
+ const { user, account } = await retryTransactionWrapper(createUser, options)
- // TODO : include Location of the new user -> 201
- return res.type('json').status(204).end()
+ return res.json({
+ user: {
+ id: user.id,
+ uuid: account.uuid
+ }
+ }).end()
}
async function createUser (req: express.Request) {
const body: UserCreate = req.body
- const user = new UserModel({
+ const userToCreate = new UserModel({
username: body.username,
password: body.password,
email: body.email,
videoQuota: body.videoQuota
})
- await createUserAccountAndChannel(user)
+ const { user, account } = await createUserAccountAndChannel(userToCreate)
logger.info('User %s with its channel and account created.', body.username)
+
+ return { user, account }
}
async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
const body: UserUpdate = req.body
const user = res.locals.user as UserModel
+ const roleChanged = body.role !== undefined && body.role !== user.role
if (body.email !== undefined) user.email = body.email
if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
await user.save()
+ // Destroy user token to refresh rights
+ if (roleChanged) {
+ await OAuthTokenModel.deleteUserToken(user.id)
+ }
+
// Don't need to send this update to followers, these attributes are not propagated
return res.sendStatus(204)
}
+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()
+}
+
function success (req: express.Request, res: express.Response, next: express.NextFunction) {
res.end()
}