aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/users
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-06-11 11:54:33 +0200
committerChocobozzz <me@florianbigard.com>2019-06-11 14:31:11 +0200
commitd1ab89deb79f70c439b58750d044d9cadf1194e5 (patch)
tree3cd18a0a8a3bf7497aec9d803c759bf54656115c /server/controllers/api/users
parentfff77ba23191f9bca959d5989a1f1df331dbac0b (diff)
downloadPeerTube-d1ab89deb79f70c439b58750d044d9cadf1194e5.tar.gz
PeerTube-d1ab89deb79f70c439b58750d044d9cadf1194e5.tar.zst
PeerTube-d1ab89deb79f70c439b58750d044d9cadf1194e5.zip
Handle email update on server
Diffstat (limited to 'server/controllers/api/users')
-rw-r--r--server/controllers/api/users/index.ts18
-rw-r--r--server/controllers/api/users/me.ts16
2 files changed, 23 insertions, 11 deletions
diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts
index 99f51a648..c1d72087c 100644
--- a/server/controllers/api/users/index.ts
+++ b/server/controllers/api/users/index.ts
@@ -6,7 +6,7 @@ import { getFormattedObjects } from '../../../helpers/utils'
6import { RATES_LIMIT, WEBSERVER } from '../../../initializers/constants' 6import { RATES_LIMIT, WEBSERVER } from '../../../initializers/constants'
7import { Emailer } from '../../../lib/emailer' 7import { Emailer } from '../../../lib/emailer'
8import { Redis } from '../../../lib/redis' 8import { Redis } from '../../../lib/redis'
9import { createUserAccountAndChannelAndPlaylist } from '../../../lib/user' 9import { createUserAccountAndChannelAndPlaylist, sendVerifyUserEmail } from '../../../lib/user'
10import { 10import {
11 asyncMiddleware, 11 asyncMiddleware,
12 asyncRetryTransactionMiddleware, 12 asyncRetryTransactionMiddleware,
@@ -147,7 +147,7 @@ usersRouter.post('/:id/reset-password',
147usersRouter.post('/ask-send-verify-email', 147usersRouter.post('/ask-send-verify-email',
148 askSendEmailLimiter, 148 askSendEmailLimiter,
149 asyncMiddleware(usersAskSendVerifyEmailValidator), 149 asyncMiddleware(usersAskSendVerifyEmailValidator),
150 asyncMiddleware(askSendVerifyUserEmail) 150 asyncMiddleware(reSendVerifyUserEmail)
151) 151)
152 152
153usersRouter.post('/:id/verify-email', 153usersRouter.post('/:id/verify-email',
@@ -320,14 +320,7 @@ async function resetUserPassword (req: express.Request, res: express.Response) {
320 return res.status(204).end() 320 return res.status(204).end()
321} 321}
322 322
323async function sendVerifyUserEmail (user: UserModel) { 323async function reSendVerifyUserEmail (req: express.Request, res: express.Response) {
324 const verificationString = await Redis.Instance.setVerifyEmailVerificationString(user.id)
325 const url = WEBSERVER.URL + '/verify-account/email?userId=' + user.id + '&verificationString=' + verificationString
326 await Emailer.Instance.addVerifyEmailJob(user.email, url)
327 return
328}
329
330async function askSendVerifyUserEmail (req: express.Request, res: express.Response) {
331 const user = res.locals.user 324 const user = res.locals.user
332 325
333 await sendVerifyUserEmail(user) 326 await sendVerifyUserEmail(user)
@@ -339,6 +332,11 @@ async function verifyUserEmail (req: express.Request, res: express.Response) {
339 const user = res.locals.user 332 const user = res.locals.user
340 user.emailVerified = true 333 user.emailVerified = true
341 334
335 if (req.body.isPendingEmail === true) {
336 user.email = user.pendingEmail
337 user.pendingEmail = null
338 }
339
342 await user.save() 340 await user.save()
343 341
344 return res.status(204).end() 342 return res.status(204).end()
diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts
index ddb239e7b..1750a02e9 100644
--- a/server/controllers/api/users/me.ts
+++ b/server/controllers/api/users/me.ts
@@ -28,6 +28,7 @@ import { VideoImportModel } from '../../../models/video/video-import'
28import { AccountModel } from '../../../models/account/account' 28import { AccountModel } from '../../../models/account/account'
29import { CONFIG } from '../../../initializers/config' 29import { CONFIG } from '../../../initializers/config'
30import { sequelizeTypescript } from '../../../initializers/database' 30import { sequelizeTypescript } from '../../../initializers/database'
31import { sendVerifyUserEmail } from '../../../lib/user'
31 32
32const auditLogger = auditLoggerFactory('users-me') 33const auditLogger = auditLoggerFactory('users-me')
33 34
@@ -171,17 +172,26 @@ async function deleteMe (req: express.Request, res: express.Response) {
171 172
172async function updateMe (req: express.Request, res: express.Response) { 173async function updateMe (req: express.Request, res: express.Response) {
173 const body: UserUpdateMe = req.body 174 const body: UserUpdateMe = req.body
175 let sendVerificationEmail = false
174 176
175 const user = res.locals.oauth.token.user 177 const user = res.locals.oauth.token.user
176 const oldUserAuditView = new UserAuditView(user.toFormattedJSON({})) 178 const oldUserAuditView = new UserAuditView(user.toFormattedJSON({}))
177 179
178 if (body.password !== undefined) user.password = body.password 180 if (body.password !== undefined) user.password = body.password
179 if (body.email !== undefined) user.email = body.email
180 if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy 181 if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy
181 if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled 182 if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled
182 if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo 183 if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo
183 if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled 184 if (body.videosHistoryEnabled !== undefined) user.videosHistoryEnabled = body.videosHistoryEnabled
184 185
186 if (body.email !== undefined) {
187 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
188 user.pendingEmail = body.email
189 sendVerificationEmail = true
190 } else {
191 user.email = body.email
192 }
193 }
194
185 await sequelizeTypescript.transaction(async t => { 195 await sequelizeTypescript.transaction(async t => {
186 const userAccount = await AccountModel.load(user.Account.id) 196 const userAccount = await AccountModel.load(user.Account.id)
187 197
@@ -196,6 +206,10 @@ async function updateMe (req: express.Request, res: express.Response) {
196 auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON({})), oldUserAuditView) 206 auditLogger.update(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON({})), oldUserAuditView)
197 }) 207 })
198 208
209 if (sendVerificationEmail === true) {
210 await sendVerifyUserEmail(user, true)
211 }
212
199 return res.sendStatus(204) 213 return res.sendStatus(204)
200} 214}
201 215