@@ -153,7 +153,7 @@ public class UsersController: Controller {
153
153
guard ApiCoreBase . configuration. auth. allowRegistrations == true else {
154
154
throw Error . registrationsNotPermitted
155
155
}
156
- return try register ( req)
156
+ return try UsersManager . register ( req)
157
157
}
158
158
159
159
// Me
@@ -193,7 +193,7 @@ public class UsersController: Controller {
193
193
guard ApiCoreBase . configuration. auth. allowInvitations == true else {
194
194
throw Error . invitationsNotPermitted
195
195
}
196
- return try invite ( req)
196
+ return try UsersManager . invite ( req)
197
197
}
198
198
199
199
// Input invitation data
@@ -299,144 +299,3 @@ public class UsersController: Controller {
299
299
}
300
300
301
301
}
302
-
303
- // MARK: Common registration helpers
304
-
305
- extension UsersController {
306
-
307
- private static func checkDomain( email: String , for allowedDomains: [ String ] ) throws {
308
- if !allowedDomains. isEmpty {
309
- guard let domain = email. domainFromEmail ( ) , !domain. isEmpty else {
310
- throw Error . domainNotAllowedForRegistration
311
- }
312
- guard allowedDomains. contains ( domain) else {
313
- throw Error . domainNotAllowedForRegistration
314
- }
315
- }
316
- }
317
-
318
- private static func checkExistingUser( email: String , on req: Request ) -> Future < User ? > {
319
- return User . query ( on: req) . filter ( \User . email == email) . first ( ) . map ( to: User ? . self) { existingUser in
320
- guard let existingUser = existingUser else {
321
- return nil
322
- }
323
- return existingUser
324
- }
325
- }
326
-
327
- private static func save( _ user: User , targetUri: String ? , isInvite: Bool , on req: Request ) throws -> Future < User > {
328
- return user. save ( on: req) . flatMap ( to: User . self) { user in
329
- let jwtService = try req. make ( JWTService . self)
330
- let jwtToken = try jwtService. signEmailConfirmation (
331
- user: user,
332
- type: ( isInvite ? . invitation : . registration) ,
333
- redirectUri: targetUri,
334
- on: req
335
- )
336
-
337
- // TODO: Add base64 encoded server image to the template!!!
338
- let templateModel = try User . EmailTemplate (
339
- verification: jwtToken,
340
- link: req. serverURL ( ) . absoluteString. finished ( with: " / " ) + " users/ \( isInvite ? " input-invite " : " verify " ) ?token= " + jwtToken,
341
- user: user,
342
- sender: isInvite ? req. me. user ( ) : nil
343
- )
344
-
345
- let templateType : EmailTemplate . Type = isInvite ? InvitationEmailTemplate . self : RegistrationEmailTemplate . self
346
-
347
- return try templateType. parsed (
348
- model: templateModel,
349
- on: req
350
- ) . flatMap ( to: User . self) { double in
351
- let from = ApiCoreBase . configuration. mail. email
352
- let subject = isInvite ? " Invitation " : " Registration " // TODO: Localize!!!!!!
353
- let mail = Mailer . Message ( from: from, to: user. email, subject: subject, text: double. string, html: double. html)
354
- return try req. mail. send ( mail) . map ( to: User . self) { mailResult in
355
- switch mailResult {
356
- case . success:
357
- return user
358
- default :
359
- throw AuthError . emailFailedToSend
360
- }
361
- }
362
- }
363
- }
364
- }
365
-
366
- private static func invite( _ req: Request ) throws -> Future < Response > {
367
- return try User . Auth. EmailConfirmation. fill ( post: req) . flatMap ( to: Response . self) { emailConfirmation in
368
- if !ApiCoreBase. configuration. auth. allowedDomainsForInvitations. isEmpty {
369
- guard ApiCoreBase . configuration. auth. allowInvitations == true else {
370
- throw Error . invitationsNotPermitted
371
- }
372
- }
373
- try checkDomain (
374
- email: emailConfirmation. email,
375
- for: ApiCoreBase . configuration. auth. allowedDomainsForInvitations
376
- ) // Check if domain is allowed in the system
377
-
378
- return try User . Invitation. fill ( post: req) . flatMap ( to: Response . self) { data in
379
- return checkExistingUser ( email: data. email, on: req) . flatMap ( to: Response . self) { existingUser in
380
- let user : User
381
- if let existingUser = existingUser {
382
- if existingUser. verified == true {
383
- // QUESTION: Do we want a more specific error? In this case no need to re-send invite as user is already registered
384
- throw AuthError . emailExists
385
- } else {
386
- user = existingUser
387
- }
388
- } else {
389
- user = try data. newUser ( on: req)
390
- }
391
-
392
- if ApiCoreBase . configuration. general. singleTeam == true { // Single team scenario
393
- return Team . adminTeam ( on: req) . flatMap ( to: Response . self) { singleTeam in
394
- return try save ( user, targetUri: emailConfirmation. targetUri, isInvite: true , on: req) . flatMap ( to: Response . self) { newUser in
395
- return singleTeam. users. attach ( newUser, on: req) . flatMap ( to: Response . self) { _ in
396
- return try newUser. asDisplay ( ) . asResponse ( . created, to: req)
397
- }
398
- }
399
- }
400
- } else {
401
- return try save ( user, targetUri: emailConfirmation. targetUri, isInvite: true , on: req) . flatMap ( to: Response . self) { user in
402
- return try user. asDisplay ( ) . asResponse ( . created, to: req)
403
- }
404
- }
405
- }
406
- }
407
- }
408
- }
409
-
410
- private static func register( _ req: Request ) throws -> Future < Response > {
411
- return try User . Auth. EmailConfirmation. fill ( post: req) . flatMap ( to: Response . self) { emailConfirmation in
412
- try checkDomain (
413
- email: emailConfirmation. email,
414
- for: ApiCoreBase . configuration. auth. allowedDomainsForRegistration
415
- ) // Check if domain is allowed in the system
416
-
417
- return try User . Registration. fill ( post: req) . flatMap ( to: Response . self) { data in
418
- return checkExistingUser ( email: data. email, on: req) . flatMap ( to: Response . self) { user in
419
- guard user == nil else {
420
- throw AuthError . emailExists
421
- }
422
- let user = try data. newUser ( on: req)
423
-
424
- if ApiCoreBase . configuration. general. singleTeam == true { // Single team scenario
425
- return Team . adminTeam ( on: req) . flatMap ( to: Response . self) { singleTeam in
426
- return try save ( user, targetUri: emailConfirmation. targetUri, isInvite: false , on: req) . flatMap ( to: Response . self) { newUser in
427
- return singleTeam. users. attach ( newUser, on: req) . flatMap ( to: Response . self) { _ in
428
- return try newUser. asDisplay ( ) . asResponse ( . created, to: req)
429
- }
430
- }
431
- }
432
- } else {
433
- return try save ( user, targetUri: emailConfirmation. targetUri, isInvite: false , on: req) . flatMap ( to: Response . self) { user in
434
- return try user. asDisplay ( ) . asResponse ( . created, to: req)
435
- }
436
- }
437
- }
438
- }
439
- }
440
- }
441
-
442
- }
0 commit comments