]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/users.ts
Add ability to reset our password
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { extname, join } from 'path'
3import * as sharp from 'sharp'
4import * as uuidv4 from 'uuid/v4'
5import { UserCreate, UserRight, UserRole, UserUpdate, UserUpdateMe, UserVideoRate as FormattedUserVideoRate } from '../../../shared'
6import { unlinkPromise } from '../../helpers/core-utils'
7import { retryTransactionWrapper } from '../../helpers/database-utils'
8import { logger } from '../../helpers/logger'
9import { createReqFiles, generateRandomString, getFormattedObjects } from '../../helpers/utils'
10import { AVATAR_MIMETYPE_EXT, AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../../initializers'
11import { updateActorAvatarInstance } from '../../lib/activitypub'
12import { sendUpdateUser } from '../../lib/activitypub/send'
13import { Emailer } from '../../lib/emailer'
14import { EmailPayload } from '../../lib/job-queue/handlers/email'
15import { Redis } from '../../lib/redis'
16import { createUserAccountAndChannel } from '../../lib/user'
17import {
18 asyncMiddleware, authenticate, ensureUserHasRight, ensureUserRegistrationAllowed, paginationValidator, setDefaultSort,
19 setDefaultPagination, token, usersAddValidator, usersGetValidator, usersRegisterValidator, usersRemoveValidator, usersSortValidator,
20 usersUpdateMeValidator, usersUpdateValidator, usersVideoRatingValidator
21} from '../../middlewares'
22import {
23 usersAskResetPasswordValidator, usersResetPasswordValidator, usersUpdateMyAvatarValidator,
24 videosSortValidator
25} from '../../middlewares/validators'
26import { AccountVideoRateModel } from '../../models/account/account-video-rate'
27import { UserModel } from '../../models/account/user'
28import { OAuthTokenModel } from '../../models/oauth/oauth-token'
29import { VideoModel } from '../../models/video/video'
30
31const reqAvatarFile = createReqFiles('avatarfile', CONFIG.STORAGE.AVATARS_DIR, AVATAR_MIMETYPE_EXT)
32
33const usersRouter = express.Router()
34
35usersRouter.get('/me',
36 authenticate,
37 asyncMiddleware(getUserInformation)
38)
39
40usersRouter.get('/me/video-quota-used',
41 authenticate,
42 asyncMiddleware(getUserVideoQuotaUsed)
43)
44
45usersRouter.get('/me/videos',
46 authenticate,
47 paginationValidator,
48 videosSortValidator,
49 setDefaultSort,
50 setDefaultPagination,
51 asyncMiddleware(getUserVideos)
52)
53
54usersRouter.get('/me/videos/:videoId/rating',
55 authenticate,
56 asyncMiddleware(usersVideoRatingValidator),
57 asyncMiddleware(getUserVideoRating)
58)
59
60usersRouter.get('/',
61 authenticate,
62 ensureUserHasRight(UserRight.MANAGE_USERS),
63 paginationValidator,
64 usersSortValidator,
65 setDefaultSort,
66 setDefaultPagination,
67 asyncMiddleware(listUsers)
68)
69
70usersRouter.get('/:id',
71 asyncMiddleware(usersGetValidator),
72 getUser
73)
74
75usersRouter.post('/',
76 authenticate,
77 ensureUserHasRight(UserRight.MANAGE_USERS),
78 asyncMiddleware(usersAddValidator),
79 asyncMiddleware(createUserRetryWrapper)
80)
81
82usersRouter.post('/register',
83 asyncMiddleware(ensureUserRegistrationAllowed),
84 asyncMiddleware(usersRegisterValidator),
85 asyncMiddleware(registerUserRetryWrapper)
86)
87
88usersRouter.put('/me',
89 authenticate,
90 usersUpdateMeValidator,
91 asyncMiddleware(updateMe)
92)
93
94usersRouter.post('/me/avatar/pick',
95 authenticate,
96 reqAvatarFile,
97 usersUpdateMyAvatarValidator,
98 asyncMiddleware(updateMyAvatar)
99)
100
101usersRouter.put('/:id',
102 authenticate,
103 ensureUserHasRight(UserRight.MANAGE_USERS),
104 asyncMiddleware(usersUpdateValidator),
105 asyncMiddleware(updateUser)
106)
107
108usersRouter.delete('/:id',
109 authenticate,
110 ensureUserHasRight(UserRight.MANAGE_USERS),
111 asyncMiddleware(usersRemoveValidator),
112 asyncMiddleware(removeUser)
113)
114
115usersRouter.post('/ask-reset-password',
116 asyncMiddleware(usersAskResetPasswordValidator),
117 asyncMiddleware(askResetUserPassword)
118)
119
120usersRouter.post('/:id/reset-password',
121 asyncMiddleware(usersResetPasswordValidator),
122 asyncMiddleware(resetUserPassword)
123)
124
125usersRouter.post('/token', token, success)
126// TODO: Once https://github.com/oauthjs/node-oauth2-server/pull/289 is merged, implement revoke token route
127
128// ---------------------------------------------------------------------------
129
130export {
131 usersRouter
132}
133
134// ---------------------------------------------------------------------------
135
136async function getUserVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
137 const user = res.locals.oauth.token.User as UserModel
138 const resultList = await VideoModel.listUserVideosForApi(user.id ,req.query.start, req.query.count, req.query.sort)
139
140 return res.json(getFormattedObjects(resultList.data, resultList.total))
141}
142
143async function createUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
144 const options = {
145 arguments: [ req ],
146 errorMessage: 'Cannot insert the user with many retries.'
147 }
148
149 const { user, account } = await retryTransactionWrapper(createUser, options)
150
151 return res.json({
152 user: {
153 id: user.id,
154 uuid: account.uuid
155 }
156 }).end()
157}
158
159async function createUser (req: express.Request) {
160 const body: UserCreate = req.body
161 const userToCreate = new UserModel({
162 username: body.username,
163 password: body.password,
164 email: body.email,
165 displayNSFW: false,
166 autoPlayVideo: true,
167 role: body.role,
168 videoQuota: body.videoQuota
169 })
170
171 const { user, account } = await createUserAccountAndChannel(userToCreate)
172
173 logger.info('User %s with its channel and account created.', body.username)
174
175 return { user, account }
176}
177
178async function registerUserRetryWrapper (req: express.Request, res: express.Response, next: express.NextFunction) {
179 const options = {
180 arguments: [ req ],
181 errorMessage: 'Cannot insert the user with many retries.'
182 }
183
184 await retryTransactionWrapper(registerUser, options)
185
186 return res.type('json').status(204).end()
187}
188
189async function registerUser (req: express.Request) {
190 const body: UserCreate = req.body
191
192 const user = new UserModel({
193 username: body.username,
194 password: body.password,
195 email: body.email,
196 displayNSFW: false,
197 autoPlayVideo: true,
198 role: UserRole.USER,
199 videoQuota: CONFIG.USER.VIDEO_QUOTA
200 })
201
202 await createUserAccountAndChannel(user)
203
204 logger.info('User %s with its channel and account registered.', body.username)
205}
206
207async function getUserInformation (req: express.Request, res: express.Response, next: express.NextFunction) {
208 // We did not load channels in res.locals.user
209 const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
210
211 return res.json(user.toFormattedJSON())
212}
213
214async function getUserVideoQuotaUsed (req: express.Request, res: express.Response, next: express.NextFunction) {
215 // We did not load channels in res.locals.user
216 const user = await UserModel.loadByUsernameAndPopulateChannels(res.locals.oauth.token.user.username)
217 const videoQuotaUsed = await UserModel.getOriginalVideoFileTotalFromUser(user)
218
219 return res.json({
220 videoQuotaUsed
221 })
222}
223
224function getUser (req: express.Request, res: express.Response, next: express.NextFunction) {
225 return res.json((res.locals.user as UserModel).toFormattedJSON())
226}
227
228async function getUserVideoRating (req: express.Request, res: express.Response, next: express.NextFunction) {
229 const videoId = +req.params.videoId
230 const accountId = +res.locals.oauth.token.User.Account.id
231
232 const ratingObj = await AccountVideoRateModel.load(accountId, videoId, null)
233 const rating = ratingObj ? ratingObj.type : 'none'
234
235 const json: FormattedUserVideoRate = {
236 videoId,
237 rating
238 }
239 res.json(json)
240}
241
242async function listUsers (req: express.Request, res: express.Response, next: express.NextFunction) {
243 const resultList = await UserModel.listForApi(req.query.start, req.query.count, req.query.sort)
244
245 return res.json(getFormattedObjects(resultList.data, resultList.total))
246}
247
248async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
249 const user = await UserModel.loadById(req.params.id)
250
251 await user.destroy()
252
253 return res.sendStatus(204)
254}
255
256async function updateMe (req: express.Request, res: express.Response, next: express.NextFunction) {
257 const body: UserUpdateMe = req.body
258
259 const user = res.locals.oauth.token.user
260
261 if (body.password !== undefined) user.password = body.password
262 if (body.email !== undefined) user.email = body.email
263 if (body.displayNSFW !== undefined) user.displayNSFW = body.displayNSFW
264 if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
265
266 await user.save()
267 await sendUpdateUser(user, undefined)
268
269 return res.sendStatus(204)
270}
271
272async function updateMyAvatar (req: express.Request, res: express.Response, next: express.NextFunction) {
273 const avatarPhysicalFile = req.files['avatarfile'][0]
274 const user = res.locals.oauth.token.user
275 const actor = user.Account.Actor
276
277 const avatarDir = CONFIG.STORAGE.AVATARS_DIR
278 const source = join(avatarDir, avatarPhysicalFile.filename)
279 const extension = extname(avatarPhysicalFile.filename)
280 const avatarName = uuidv4() + extension
281 const destination = join(avatarDir, avatarName)
282
283 await sharp(source)
284 .resize(AVATARS_SIZE.width, AVATARS_SIZE.height)
285 .toFile(destination)
286
287 await unlinkPromise(source)
288
289 const avatar = await sequelizeTypescript.transaction(async t => {
290 const updatedActor = await updateActorAvatarInstance(actor, avatarName, t)
291 await updatedActor.save({ transaction: t })
292
293 await sendUpdateUser(user, t)
294
295 return updatedActor.Avatar
296 })
297
298 return res
299 .json({
300 avatar: avatar.toFormattedJSON()
301 })
302 .end()
303}
304
305async function updateUser (req: express.Request, res: express.Response, next: express.NextFunction) {
306 const body: UserUpdate = req.body
307 const user = res.locals.user as UserModel
308 const roleChanged = body.role !== undefined && body.role !== user.role
309
310 if (body.email !== undefined) user.email = body.email
311 if (body.videoQuota !== undefined) user.videoQuota = body.videoQuota
312 if (body.role !== undefined) user.role = body.role
313
314 await user.save()
315
316 // Destroy user token to refresh rights
317 if (roleChanged) {
318 await OAuthTokenModel.deleteUserToken(user.id)
319 }
320
321 // Don't need to send this update to followers, these attributes are not propagated
322
323 return res.sendStatus(204)
324}
325
326async function askResetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
327 const user = res.locals.user as UserModel
328
329 const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id)
330 const url = CONFIG.WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString
331 await Emailer.Instance.addForgetPasswordEmailJob(user.email, url)
332
333 return res.status(204).end()
334}
335
336async function resetUserPassword (req: express.Request, res: express.Response, next: express.NextFunction) {
337 const user = res.locals.user as UserModel
338 user.password = req.body.password
339
340 await user.save()
341
342 return res.status(204).end()
343}
344
345function success (req: express.Request, res: express.Response, next: express.NextFunction) {
346 res.end()
347}