]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
edbc9325 C |
2 | import { tokensRouter } from '@server/controllers/api/users/token' |
3 | import { Hooks } from '@server/lib/plugins/hooks' | |
f43db2f4 | 4 | import { OAuthTokenModel } from '@server/models/oauth/oauth-token' |
d3d3deaa C |
5 | import { MUserAccountDefault } from '@server/types/models' |
6 | import { pick } from '@shared/core-utils' | |
e364e31e | 7 | import { HttpStatusCode, UserCreate, UserCreateResult, UserRight, UserUpdate } from '@shared/models' |
edbc9325 | 8 | import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger' |
d03cd8bb | 9 | import { logger } from '../../../helpers/logger' |
45f1bd72 | 10 | import { generateRandomString, getFormattedObjects } from '../../../helpers/utils' |
c1340a6a | 11 | import { WEBSERVER } from '../../../initializers/constants' |
edbc9325 | 12 | import { sequelizeTypescript } from '../../../initializers/database' |
d03cd8bb C |
13 | import { Emailer } from '../../../lib/emailer' |
14 | import { Redis } from '../../../lib/redis' | |
e364e31e | 15 | import { buildUser, createUserAccountAndChannelAndPlaylist } from '../../../lib/user' |
65fcc311 | 16 | import { |
e5a781ec | 17 | adminUsersSortValidator, |
f076daa7 | 18 | asyncMiddleware, |
90d4bb81 | 19 | asyncRetryTransactionMiddleware, |
f076daa7 C |
20 | authenticate, |
21 | ensureUserHasRight, | |
f076daa7 C |
22 | paginationValidator, |
23 | setDefaultPagination, | |
24 | setDefaultSort, | |
74d63469 | 25 | userAutocompleteValidator, |
f076daa7 C |
26 | usersAddValidator, |
27 | usersGetValidator, | |
edbc9325 | 28 | usersListValidator, |
f076daa7 | 29 | usersRemoveValidator, |
d03cd8bb C |
30 | usersUpdateValidator |
31 | } from '../../../middlewares' | |
d9eaee39 | 32 | import { |
d4d9bbc6 | 33 | ensureCanModerateUser, |
993cef4b | 34 | usersAskResetPasswordValidator, |
993cef4b | 35 | usersBlockingValidator, |
e364e31e | 36 | usersResetPasswordValidator |
d9eaee39 | 37 | } from '../../../middlewares/validators' |
7d9ba5c0 | 38 | import { UserModel } from '../../../models/user/user' |
e364e31e | 39 | import { emailVerificationRouter } from './email-verification' |
d03cd8bb | 40 | import { meRouter } from './me' |
edbc9325 | 41 | import { myAbusesRouter } from './my-abuses' |
7ad9b984 | 42 | import { myBlocklistRouter } from './my-blocklist' |
8b9a525a | 43 | import { myVideosHistoryRouter } from './my-history' |
cef534ed | 44 | import { myNotificationsRouter } from './my-notifications' |
cf405589 | 45 | import { mySubscriptionsRouter } from './my-subscriptions' |
edbc9325 | 46 | import { myVideoPlaylistsRouter } from './my-video-playlists' |
e364e31e | 47 | import { registrationsRouter } from './registrations' |
56f47830 | 48 | import { twoFactorRouter } from './two-factor' |
80e36cd9 AB |
49 | |
50 | const auditLogger = auditLoggerFactory('users') | |
65fcc311 C |
51 | |
52 | const usersRouter = express.Router() | |
e364e31e C |
53 | usersRouter.use('/', emailVerificationRouter) |
54 | usersRouter.use('/', registrationsRouter) | |
56f47830 | 55 | usersRouter.use('/', twoFactorRouter) |
e1c55031 | 56 | usersRouter.use('/', tokensRouter) |
cef534ed | 57 | usersRouter.use('/', myNotificationsRouter) |
cf405589 | 58 | usersRouter.use('/', mySubscriptionsRouter) |
7ad9b984 | 59 | usersRouter.use('/', myBlocklistRouter) |
8b9a525a | 60 | usersRouter.use('/', myVideosHistoryRouter) |
f0a39880 | 61 | usersRouter.use('/', myVideoPlaylistsRouter) |
edbc9325 | 62 | usersRouter.use('/', myAbusesRouter) |
06a05d5f | 63 | usersRouter.use('/', meRouter) |
9bd26629 | 64 | |
74d63469 GR |
65 | usersRouter.get('/autocomplete', |
66 | userAutocompleteValidator, | |
67 | asyncMiddleware(autocompleteUsers) | |
68 | ) | |
69 | ||
65fcc311 | 70 | usersRouter.get('/', |
86d13ec2 C |
71 | authenticate, |
72 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
65fcc311 | 73 | paginationValidator, |
87a0cac6 | 74 | adminUsersSortValidator, |
1174a847 | 75 | setDefaultSort, |
f05a1c30 | 76 | setDefaultPagination, |
ea7337cf | 77 | usersListValidator, |
eb080476 | 78 | asyncMiddleware(listUsers) |
5c39adb7 C |
79 | ) |
80 | ||
e6921918 C |
81 | usersRouter.post('/:id/block', |
82 | authenticate, | |
83 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
84 | asyncMiddleware(usersBlockingValidator), | |
d4d9bbc6 | 85 | ensureCanModerateUser, |
e6921918 C |
86 | asyncMiddleware(blockUser) |
87 | ) | |
88 | usersRouter.post('/:id/unblock', | |
89 | authenticate, | |
90 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
91 | asyncMiddleware(usersBlockingValidator), | |
d4d9bbc6 | 92 | ensureCanModerateUser, |
e6921918 C |
93 | asyncMiddleware(unblockUser) |
94 | ) | |
95 | ||
8094a898 | 96 | usersRouter.get('/:id', |
94ff4c23 C |
97 | authenticate, |
98 | ensureUserHasRight(UserRight.MANAGE_USERS), | |
a2431b7d | 99 | asyncMiddleware(usersGetValidator), |
8094a898 C |
100 | getUser |
101 | ) | |
102 | ||
65fcc311 C |
103 | usersRouter.post('/', |
104 | authenticate, | |
954605a8 | 105 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 106 | asyncMiddleware(usersAddValidator), |
90d4bb81 | 107 | asyncRetryTransactionMiddleware(createUser) |
9bd26629 C |
108 | ) |
109 | ||
65fcc311 C |
110 | usersRouter.put('/:id', |
111 | authenticate, | |
954605a8 | 112 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 113 | asyncMiddleware(usersUpdateValidator), |
d4d9bbc6 | 114 | ensureCanModerateUser, |
eb080476 | 115 | asyncMiddleware(updateUser) |
9bd26629 C |
116 | ) |
117 | ||
65fcc311 C |
118 | usersRouter.delete('/:id', |
119 | authenticate, | |
954605a8 | 120 | ensureUserHasRight(UserRight.MANAGE_USERS), |
a2431b7d | 121 | asyncMiddleware(usersRemoveValidator), |
d4d9bbc6 | 122 | ensureCanModerateUser, |
eb080476 | 123 | asyncMiddleware(removeUser) |
9bd26629 | 124 | ) |
6606150c | 125 | |
ecb4e35f C |
126 | usersRouter.post('/ask-reset-password', |
127 | asyncMiddleware(usersAskResetPasswordValidator), | |
128 | asyncMiddleware(askResetUserPassword) | |
129 | ) | |
130 | ||
131 | usersRouter.post('/:id/reset-password', | |
132 | asyncMiddleware(usersResetPasswordValidator), | |
133 | asyncMiddleware(resetUserPassword) | |
134 | ) | |
135 | ||
9457bf88 C |
136 | // --------------------------------------------------------------------------- |
137 | ||
65fcc311 C |
138 | export { |
139 | usersRouter | |
140 | } | |
9457bf88 C |
141 | |
142 | // --------------------------------------------------------------------------- | |
143 | ||
90d4bb81 | 144 | async function createUser (req: express.Request, res: express.Response) { |
4771e000 | 145 | const body: UserCreate = req.body |
3d215dc5 | 146 | |
d3d3deaa C |
147 | const userToCreate = buildUser({ |
148 | ...pick(body, [ 'username', 'password', 'email', 'role', 'videoQuota', 'videoQuotaDaily', 'adminFlags' ]), | |
149 | ||
150 | emailVerified: null | |
151 | }) | |
9bd26629 | 152 | |
45f1bd72 JL |
153 | // NB: due to the validator usersAddValidator, password==='' can only be true if we can send the mail. |
154 | const createPassword = userToCreate.password === '' | |
155 | if (createPassword) { | |
156 | userToCreate.password = await generateRandomString(20) | |
157 | } | |
158 | ||
3d215dc5 | 159 | const { user, account, videoChannel } = await createUserAccountAndChannelAndPlaylist({ |
160 | userToCreate, | |
69db1470 | 161 | channelNames: body.channelName && { name: body.channelName, displayName: body.channelName } |
3d215dc5 | 162 | }) |
eb080476 | 163 | |
993cef4b | 164 | auditLogger.create(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) |
38fa2065 | 165 | logger.info('User %s with its channel and account created.', body.username) |
f05a1c30 | 166 | |
45f1bd72 JL |
167 | if (createPassword) { |
168 | // this will send an email for newly created users, so then can set their first password. | |
169 | logger.info('Sending to user %s a create password email', body.username) | |
170 | const verificationString = await Redis.Instance.setCreatePasswordVerificationString(user.id) | |
171 | const url = WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString | |
d17c7b4e | 172 | Emailer.Instance.addPasswordCreateEmailJob(userToCreate.username, user.email, url) |
45f1bd72 JL |
173 | } |
174 | ||
7226e90f | 175 | Hooks.runAction('action:api.user.created', { body, user, account, videoChannel, req, res }) |
6f3fe96f | 176 | |
1e904cde | 177 | return res.json({ |
90d4bb81 C |
178 | user: { |
179 | id: user.id, | |
180 | account: { | |
57cfff78 | 181 | id: account.id |
90d4bb81 | 182 | } |
7926c5f9 | 183 | } as UserCreateResult |
8795d6f2 | 184 | }) |
47e0652b C |
185 | } |
186 | ||
dae86118 C |
187 | async function unblockUser (req: express.Request, res: express.Response) { |
188 | const user = res.locals.user | |
e6921918 C |
189 | |
190 | await changeUserBlock(res, user, false) | |
191 | ||
7226e90f | 192 | Hooks.runAction('action:api.user.unblocked', { user, req, res }) |
6f3fe96f | 193 | |
2d53be02 | 194 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
e6921918 C |
195 | } |
196 | ||
b426edd4 | 197 | async function blockUser (req: express.Request, res: express.Response) { |
dae86118 | 198 | const user = res.locals.user |
eacb25c4 | 199 | const reason = req.body.reason |
e6921918 | 200 | |
eacb25c4 | 201 | await changeUserBlock(res, user, true, reason) |
e6921918 | 202 | |
7226e90f | 203 | Hooks.runAction('action:api.user.blocked', { user, req, res }) |
6f3fe96f | 204 | |
2d53be02 | 205 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
e6921918 C |
206 | } |
207 | ||
b426edd4 | 208 | function getUser (req: express.Request, res: express.Response) { |
1eddc9a7 | 209 | return res.json(res.locals.user.toFormattedJSON({ withAdminFlags: true })) |
8094a898 C |
210 | } |
211 | ||
b426edd4 | 212 | async function autocompleteUsers (req: express.Request, res: express.Response) { |
5cf84858 | 213 | const resultList = await UserModel.autoComplete(req.query.search as string) |
74d63469 GR |
214 | |
215 | return res.json(resultList) | |
216 | } | |
217 | ||
b426edd4 | 218 | async function listUsers (req: express.Request, res: express.Response) { |
87a0cac6 | 219 | const resultList = await UserModel.listForAdminApi({ |
8491293b RK |
220 | start: req.query.start, |
221 | count: req.query.count, | |
222 | sort: req.query.sort, | |
223 | search: req.query.search, | |
224 | blocked: req.query.blocked | |
225 | }) | |
eb080476 | 226 | |
1eddc9a7 | 227 | return res.json(getFormattedObjects(resultList.data, resultList.total, { withAdminFlags: true })) |
9bd26629 C |
228 | } |
229 | ||
b426edd4 | 230 | async function removeUser (req: express.Request, res: express.Response) { |
dae86118 | 231 | const user = res.locals.user |
eb080476 | 232 | |
993cef4b | 233 | auditLogger.delete(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON())) |
80e36cd9 | 234 | |
fae6e4da C |
235 | await sequelizeTypescript.transaction(async t => { |
236 | // Use a transaction to avoid inconsistencies with hooks (account/channel deletion & federation) | |
237 | await user.destroy({ transaction: t }) | |
238 | }) | |
2dbc170d | 239 | |
7226e90f | 240 | Hooks.runAction('action:api.user.deleted', { user, req, res }) |
6f3fe96f | 241 | |
76148b27 | 242 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
9bd26629 C |
243 | } |
244 | ||
b426edd4 | 245 | async function updateUser (req: express.Request, res: express.Response) { |
8094a898 | 246 | const body: UserUpdate = req.body |
dae86118 | 247 | const userToUpdate = res.locals.user |
80e36cd9 AB |
248 | const oldUserAuditView = new UserAuditView(userToUpdate.toFormattedJSON()) |
249 | const roleChanged = body.role !== undefined && body.role !== userToUpdate.role | |
8094a898 | 250 | |
16c016e8 C |
251 | const keysToUpdate: (keyof UserUpdate)[] = [ |
252 | 'password', | |
253 | 'email', | |
254 | 'emailVerified', | |
255 | 'videoQuota', | |
256 | 'videoQuotaDaily', | |
257 | 'role', | |
258 | 'adminFlags', | |
259 | 'pluginAuth' | |
260 | ] | |
261 | ||
262 | for (const key of keysToUpdate) { | |
263 | if (body[key] !== undefined) userToUpdate.set(key, body[key]) | |
264 | } | |
8094a898 | 265 | |
80e36cd9 | 266 | const user = await userToUpdate.save() |
eb080476 | 267 | |
f8b8c36b | 268 | // Destroy user token to refresh rights |
f43db2f4 | 269 | if (roleChanged || body.password !== undefined) await OAuthTokenModel.deleteUserToken(userToUpdate.id) |
f8b8c36b | 270 | |
91411dba | 271 | auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView) |
80e36cd9 | 272 | |
7226e90f | 273 | Hooks.runAction('action:api.user.updated', { user, req, res }) |
6f3fe96f | 274 | |
b426edd4 | 275 | // Don't need to send this update to followers, these attributes are not federated |
265ba139 | 276 | |
76148b27 | 277 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
8094a898 C |
278 | } |
279 | ||
dae86118 C |
280 | async function askResetUserPassword (req: express.Request, res: express.Response) { |
281 | const user = res.locals.user | |
ecb4e35f C |
282 | |
283 | const verificationString = await Redis.Instance.setResetPasswordVerificationString(user.id) | |
6dd9de95 | 284 | const url = WEBSERVER.URL + '/reset-password?userId=' + user.id + '&verificationString=' + verificationString |
c5f3ff39 | 285 | Emailer.Instance.addPasswordResetEmailJob(user.username, user.email, url) |
ecb4e35f | 286 | |
2d53be02 | 287 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
ecb4e35f C |
288 | } |
289 | ||
dae86118 C |
290 | async function resetUserPassword (req: express.Request, res: express.Response) { |
291 | const user = res.locals.user | |
ecb4e35f C |
292 | user.password = req.body.password |
293 | ||
294 | await user.save() | |
e9c5f123 | 295 | await Redis.Instance.removePasswordVerificationString(user.id) |
ecb4e35f | 296 | |
2d53be02 | 297 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
ecb4e35f C |
298 | } |
299 | ||
453e83ea | 300 | async function changeUserBlock (res: express.Response, user: MUserAccountDefault, block: boolean, reason?: string) { |
e6921918 C |
301 | const oldUserAuditView = new UserAuditView(user.toFormattedJSON()) |
302 | ||
303 | user.blocked = block | |
eacb25c4 | 304 | user.blockedReason = reason || null |
e6921918 C |
305 | |
306 | await sequelizeTypescript.transaction(async t => { | |
f43db2f4 | 307 | await OAuthTokenModel.deleteUserToken(user.id, t) |
e6921918 C |
308 | |
309 | await user.save({ transaction: t }) | |
310 | }) | |
311 | ||
eacb25c4 C |
312 | await Emailer.Instance.addUserBlockJob(user, block, reason) |
313 | ||
91411dba | 314 | auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()), oldUserAuditView) |
e6921918 | 315 | } |