aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/users/registrations.ts23
-rw-r--r--server/middlewares/validators/user-registrations.ts7
-rw-r--r--server/tests/api/users/registrations.ts36
3 files changed, 60 insertions, 6 deletions
diff --git a/server/controllers/api/users/registrations.ts b/server/controllers/api/users/registrations.ts
index 3d4e0aa18..5e213d6cc 100644
--- a/server/controllers/api/users/registrations.ts
+++ b/server/controllers/api/users/registrations.ts
@@ -3,7 +3,14 @@ import { Emailer } from '@server/lib/emailer'
3import { Hooks } from '@server/lib/plugins/hooks' 3import { Hooks } from '@server/lib/plugins/hooks'
4import { UserRegistrationModel } from '@server/models/user/user-registration' 4import { UserRegistrationModel } from '@server/models/user/user-registration'
5import { pick } from '@shared/core-utils' 5import { pick } from '@shared/core-utils'
6import { HttpStatusCode, UserRegister, UserRegistrationRequest, UserRegistrationState, UserRight } from '@shared/models' 6import {
7 HttpStatusCode,
8 UserRegister,
9 UserRegistrationRequest,
10 UserRegistrationState,
11 UserRegistrationUpdateState,
12 UserRight
13} from '@shared/models'
7import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger' 14import { auditLoggerFactory, UserAuditView } from '../../../helpers/audit-logger'
8import { logger } from '../../../helpers/logger' 15import { logger } from '../../../helpers/logger'
9import { CONFIG } from '../../../initializers/config' 16import { CONFIG } from '../../../initializers/config'
@@ -125,6 +132,7 @@ async function requestRegistration (req: express.Request, res: express.Response)
125 132
126async function acceptRegistration (req: express.Request, res: express.Response) { 133async function acceptRegistration (req: express.Request, res: express.Response) {
127 const registration = res.locals.userRegistration 134 const registration = res.locals.userRegistration
135 const body: UserRegistrationUpdateState = req.body
128 136
129 const userToCreate = buildUser({ 137 const userToCreate = buildUser({
130 username: registration.username, 138 username: registration.username,
@@ -150,26 +158,31 @@ async function acceptRegistration (req: express.Request, res: express.Response)
150 158
151 registration.userId = user.id 159 registration.userId = user.id
152 registration.state = UserRegistrationState.ACCEPTED 160 registration.state = UserRegistrationState.ACCEPTED
153 registration.moderationResponse = req.body.moderationResponse 161 registration.moderationResponse = body.moderationResponse
154 162
155 await registration.save() 163 await registration.save()
156 164
157 logger.info('Registration of %s accepted', registration.username) 165 logger.info('Registration of %s accepted', registration.username)
158 166
159 Emailer.Instance.addUserRegistrationRequestProcessedJob(registration) 167 if (body.preventEmailDelivery !== true) {
168 Emailer.Instance.addUserRegistrationRequestProcessedJob(registration)
169 }
160 170
161 return res.sendStatus(HttpStatusCode.NO_CONTENT_204) 171 return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
162} 172}
163 173
164async function rejectRegistration (req: express.Request, res: express.Response) { 174async function rejectRegistration (req: express.Request, res: express.Response) {
165 const registration = res.locals.userRegistration 175 const registration = res.locals.userRegistration
176 const body: UserRegistrationUpdateState = req.body
166 177
167 registration.state = UserRegistrationState.REJECTED 178 registration.state = UserRegistrationState.REJECTED
168 registration.moderationResponse = req.body.moderationResponse 179 registration.moderationResponse = body.moderationResponse
169 180
170 await registration.save() 181 await registration.save()
171 182
172 Emailer.Instance.addUserRegistrationRequestProcessedJob(registration) 183 if (body.preventEmailDelivery !== true) {
184 Emailer.Instance.addUserRegistrationRequestProcessedJob(registration)
185 }
173 186
174 logger.info('Registration of %s rejected', registration.username) 187 logger.info('Registration of %s rejected', registration.username)
175 188
diff --git a/server/middlewares/validators/user-registrations.ts b/server/middlewares/validators/user-registrations.ts
index e263c27c5..fcf655a2c 100644
--- a/server/middlewares/validators/user-registrations.ts
+++ b/server/middlewares/validators/user-registrations.ts
@@ -1,6 +1,6 @@
1import express from 'express' 1import express from 'express'
2import { body, param, query, ValidationChain } from 'express-validator' 2import { body, param, query, ValidationChain } from 'express-validator'
3import { exists, isIdValid } from '@server/helpers/custom-validators/misc' 3import { exists, isBooleanValid, isIdValid, toBooleanOrNull } from '@server/helpers/custom-validators/misc'
4import { isRegistrationModerationResponseValid, isRegistrationReasonValid } from '@server/helpers/custom-validators/user-registration' 4import { isRegistrationModerationResponseValid, isRegistrationReasonValid } from '@server/helpers/custom-validators/user-registration'
5import { CONFIG } from '@server/initializers/config' 5import { CONFIG } from '@server/initializers/config'
6import { Hooks } from '@server/lib/plugins/hooks' 6import { Hooks } from '@server/lib/plugins/hooks'
@@ -91,6 +91,11 @@ const acceptOrRejectRegistrationValidator = [
91 body('moderationResponse') 91 body('moderationResponse')
92 .custom(isRegistrationModerationResponseValid), 92 .custom(isRegistrationModerationResponseValid),
93 93
94 body('preventEmailDelivery')
95 .optional()
96 .customSanitizer(toBooleanOrNull)
97 .custom(isBooleanValid).withMessage('Should have preventEmailDelivery boolean'),
98
94 async (req: express.Request, res: express.Response, next: express.NextFunction) => { 99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
95 if (areValidationErrors(req, res)) return 100 if (areValidationErrors(req, res)) return
96 if (!await checkRegistrationIdExist(req.params.registrationId, res)) return 101 if (!await checkRegistrationIdExist(req.params.registrationId, res)) return
diff --git a/server/tests/api/users/registrations.ts b/server/tests/api/users/registrations.ts
index a9e1114e8..e6524f07d 100644
--- a/server/tests/api/users/registrations.ts
+++ b/server/tests/api/users/registrations.ts
@@ -329,6 +329,42 @@ describe('Test registrations', function () {
329 } 329 }
330 }) 330 })
331 331
332 it('Should be able to prevent email delivery on accept/reject', async function () {
333 this.timeout(50000)
334
335 let id1: number
336 let id2: number
337
338 {
339 const { id } = await server.registrations.requestRegistration({
340 username: 'user7',
341 email: 'user7@example.com',
342 registrationReason: 'tt'
343 })
344 id1 = id
345 }
346 {
347 const { id } = await server.registrations.requestRegistration({
348 username: 'user8',
349 email: 'user8@example.com',
350 registrationReason: 'tt'
351 })
352 id2 = id
353 }
354
355 await server.registrations.accept({ id: id1, moderationResponse: 'tt', preventEmailDelivery: true })
356 await server.registrations.reject({ id: id2, moderationResponse: 'tt', preventEmailDelivery: true })
357
358 await waitJobs([ server ])
359
360 const filtered = emails.filter(e => {
361 const address = e['to'][0]['address']
362 return address === 'user7@example.com' || address === 'user8@example.com'
363 })
364
365 expect(filtered).to.have.lengthOf(0)
366 })
367
332 it('Should request a registration without a channel, that will conflict with an already existing channel', async function () { 368 it('Should request a registration without a channel, that will conflict with an already existing channel', async function () {
333 let id1: number 369 let id1: number
334 let id2: number 370 let id2: number