From f826db1edfaefcad4a310db161500f6d5f2eed04 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Mar 2018 16:39:38 -0600 Subject: [PATCH 1/3] Restore the userCreated event --- docs/source/reference/server.md | 8 ++- graph/subscriptions/setupFunctions.js | 47 +++++++------- graph/typeDefs.graphql | 4 ++ perms/constants/subscription.js | 1 + perms/reducers/subscription.js | 1 + .../server/passport.js | 9 ++- .../server/passport.js | 11 +++- services/users.js | 37 ++++------- test/server/graph/mutations/changeUsername.js | 2 + test/server/graph/mutations/editComment.js | 4 ++ test/server/graph/mutations/ignoreUser.js | 9 +++ .../graph/mutations/setUserBanStatus.js | 2 + .../mutations/setUserSuspensionStatus.js | 2 + .../graph/mutations/setUserUsernameStatus.js | 2 + test/server/graph/queries/asset.js | 39 +++++++----- test/server/graph/queries/user.js | 3 +- test/server/routes/api/auth/index.js | 6 +- test/server/routes/api/user/index.js | 24 ++++--- test/server/services/comments.js | 15 +++-- test/server/services/tags.js | 4 +- test/server/services/tokens.js | 3 + test/server/services/users.js | 63 ++++++++++--------- 22 files changed, 176 insertions(+), 120 deletions(-) diff --git a/docs/source/reference/server.md b/docs/source/reference/server.md index ce8cebae1..93b9ee69f 100644 --- a/docs/source/reference/server.md +++ b/docs/source/reference/server.md @@ -318,7 +318,13 @@ module.exports = { let user; try { - user = await UsersService.findOrCreateExternalUser(profile); + const { id, provider, displayName } = profile; + user = await UsersService.findOrCreateExternalUser( + req.context, + id, + provider, + displayName + ); } catch (err) { return done(err); } diff --git a/graph/subscriptions/setupFunctions.js b/graph/subscriptions/setupFunctions.js index 467c4e98f..ee29d6971 100644 --- a/graph/subscriptions/setupFunctions.js +++ b/graph/subscriptions/setupFunctions.js @@ -1,16 +1,17 @@ const { - SUBSCRIBE_COMMENT_ACCEPTED, - SUBSCRIBE_COMMENT_REJECTED, - SUBSCRIBE_COMMENT_FLAGGED, - SUBSCRIBE_COMMENT_RESET, - SUBSCRIBE_ALL_COMMENT_EDITED, SUBSCRIBE_ALL_COMMENT_ADDED, - SUBSCRIBE_ALL_USER_SUSPENDED, + SUBSCRIBE_ALL_COMMENT_EDITED, SUBSCRIBE_ALL_USER_BANNED, - SUBSCRIBE_ALL_USERNAME_REJECTED, + SUBSCRIBE_ALL_USER_CREATED, + SUBSCRIBE_ALL_USER_SUSPENDED, SUBSCRIBE_ALL_USERNAME_APPROVED, - SUBSCRIBE_ALL_USERNAME_FLAGGED, SUBSCRIBE_ALL_USERNAME_CHANGED, + SUBSCRIBE_ALL_USERNAME_FLAGGED, + SUBSCRIBE_ALL_USERNAME_REJECTED, + SUBSCRIBE_COMMENT_ACCEPTED, + SUBSCRIBE_COMMENT_FLAGGED, + SUBSCRIBE_COMMENT_REJECTED, + SUBSCRIBE_COMMENT_RESET, } = require('../../perms/constants'); const merge = require('lodash/merge'); @@ -139,6 +140,8 @@ const setupFunctions = { } return !args.user_id || user.id === args.user_id; }, + userCreated: (options, args, user, ctx) => + ctx.user && ctx.user.can(SUBSCRIBE_ALL_USER_CREATED), }; /** @@ -153,19 +156,17 @@ module.exports = plugins.get('server', 'setupFunctions').reduce( return merge(acc, setupFunctions); }, - Object.keys(setupFunctions) - .map(key => { - const filter = setupFunctions[key]; - - return { - [key]: (options, args) => ({ - [key]: { - filter: (user, ctx) => filter(options, args, user, ctx), - }, - }), - }; - }) - .reduce((setupFunction, setupFunctions) => { - return merge(setupFunctions, setupFunction); - }, {}) + // Process the default setupFunctions. + Object.entries(setupFunctions) + .map(([key, filter]) => ({ + [key]: (options, args) => ({ + [key]: { + filter: (user, ctx) => filter(options, args, user, ctx), + }, + }), + })) + .reduce( + (setupFunction, setupFunctions) => merge(setupFunctions, setupFunction), + {} + ) ); diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index ea574ee6b..1ffa3b29f 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -1588,6 +1588,10 @@ type Subscription { # Get an update whenever a username has been changed. `user_id` must match id # of current user except for users with the `ADMIN` or `MODERATOR` role. usernameChanged(user_id: ID): UsernameChangedPayload + + # Get an update whenever a user is created. Only accessible to users with the + # `ADMIN` or `MODERATOR` roles. + userCreated: User } ################################################################################ diff --git a/perms/constants/subscription.js b/perms/constants/subscription.js index e27730396..30ef4656d 100644 --- a/perms/constants/subscription.js +++ b/perms/constants/subscription.js @@ -11,4 +11,5 @@ module.exports = { SUBSCRIBE_ALL_USERNAME_APPROVED: 'SUBSCRIBE_ALL_USERNAME_APPROVED', SUBSCRIBE_ALL_USERNAME_FLAGGED: 'SUBSCRIBE_ALL_USERNAME_FLAGGED', SUBSCRIBE_ALL_USERNAME_CHANGED: 'SUBSCRIBE_ALL_USERNAME_CHANGED', + SUBSCRIBE_ALL_USER_CREATED: 'SUBSCRIBE_ALL_USER_CREATED', }; diff --git a/perms/reducers/subscription.js b/perms/reducers/subscription.js index 44fb43af6..345a79b73 100644 --- a/perms/reducers/subscription.js +++ b/perms/reducers/subscription.js @@ -15,6 +15,7 @@ module.exports = (user, perm) => { case types.SUBSCRIBE_ALL_USERNAME_APPROVED: case types.SUBSCRIBE_ALL_USERNAME_FLAGGED: case types.SUBSCRIBE_ALL_USERNAME_CHANGED: + case types.SUBSCRIBE_ALL_USER_CREATED: return check(user, ['ADMIN', 'MODERATOR']); default: break; diff --git a/plugins/talk-plugin-facebook-auth/server/passport.js b/plugins/talk-plugin-facebook-auth/server/passport.js index 477ed48a0..7bec1799a 100644 --- a/plugins/talk-plugin-facebook-auth/server/passport.js +++ b/plugins/talk-plugin-facebook-auth/server/passport.js @@ -25,7 +25,14 @@ module.exports = passport => { async (req, accessToken, refreshToken, profile, done) => { let user; try { - user = await UsersService.findOrCreateExternalUser(profile); + const { id, provider, displayName } = profile; + + user = await UsersService.findOrCreateExternalUser( + req.context, + id, + provider, + displayName + ); } catch (err) { return done(err); } diff --git a/plugins/talk-plugin-google-auth/server/passport.js b/plugins/talk-plugin-google-auth/server/passport.js index c3463557b..8386f319c 100644 --- a/plugins/talk-plugin-google-auth/server/passport.js +++ b/plugins/talk-plugin-google-auth/server/passport.js @@ -24,9 +24,16 @@ module.exports = passport => { async (req, accessToken, refreshToken, profile, done) => { let user; try { - user = await UsersService.findOrCreateExternalUser(profile); + const { id, provider, displayName } = profile; + + user = await UsersService.findOrCreateExternalUser( + req.context, + id, + provider, + displayName + ); } catch (err) { - return done(err.toString()); + return done(err); } return ValidateUserLogin(profile, user, done); diff --git a/services/users.js b/services/users.js index f5319122c..47969004a 100644 --- a/services/users.js +++ b/services/users.js @@ -352,7 +352,7 @@ class UsersService { * @param {Object} profile - User social/external profile * @param {Function} done [description] */ - static async findOrCreateExternalUser({ id, provider, displayName }) { + static async findOrCreateExternalUser(ctx, id, provider, displayName) { let user = await UserModel.findOne({ profiles: { $elemMatch: { @@ -388,6 +388,9 @@ class UsersService { // Save the user in the database. await user.save(); + // Emit that the user was created. + ctx.pubsub.publish('userCreated', user); + return user; } @@ -438,23 +441,6 @@ class UsersService { ); } - /** - * Creates local users. - * @param {Array} users Users to create - * @return {Promise} Resolves with the users that were created - */ - static createLocalUsers(users) { - return Promise.all( - users.map(user => { - return UsersService.createLocalUser( - user.email, - user.password, - user.username - ); - }) - ); - } - /** * Check the requested username for blocked words and special chars * @param {String} username word to be checked for profanity @@ -489,24 +475,24 @@ class UsersService { */ static isValidPassword(password) { if (!password) { - return Promise.reject(errors.ErrMissingPassword); + throw errors.ErrMissingPassword; } if (password.length < 8) { - return Promise.reject(errors.ErrPasswordTooShort); + throw errors.ErrPasswordTooShort; } - return Promise.resolve(password); + return password; } /** * Creates the local user with a given email, password, and name. + * @param {Object} ctx application context for the request * @param {String} email email of the new user * @param {String} password plaintext password of the new user - * @param {String} username name of the display user - * @param {Function} done callback + * @param {String} username name of the display user */ - static async createLocalUser(email, password, username) { + static async createLocalUser(ctx, email, password, username) { if (!email) { throw errors.ErrMissingEmail; } @@ -553,6 +539,9 @@ class UsersService { throw err; } + // Emit that the user was created. + ctx.pubsub.publish('userCreated', user); + return user; } diff --git a/test/server/graph/mutations/changeUsername.js b/test/server/graph/mutations/changeUsername.js index f54ea9ac1..eaff85434 100644 --- a/test/server/graph/mutations/changeUsername.js +++ b/test/server/graph/mutations/changeUsername.js @@ -11,7 +11,9 @@ describe('graph.mutations.changeUsername', () => { let user; beforeEach(async () => { await SettingsService.init(); + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'test@test.com', 'testpassword1!', 'kirk' diff --git a/test/server/graph/mutations/editComment.js b/test/server/graph/mutations/editComment.js index 476279ae8..7ce8d865a 100644 --- a/test/server/graph/mutations/editComment.js +++ b/test/server/graph/mutations/editComment.js @@ -16,8 +16,10 @@ describe('graph.mutations.editComment', () => { beforeEach(async () => { timekeeper.reset(); await SettingsService.init(); + const ctx = Context.forSystem(); asset = await AssetModel.create({}); user = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' @@ -124,7 +126,9 @@ describe('graph.mutations.editComment', () => { body: `hello there! ${String(Math.random()).slice(2)}`, }); + const ctx = Context.forSystem(); const userB = await UsersService.createLocalUser( + ctx, 'usernameB@example.com', 'password', 'usernameB' diff --git a/test/server/graph/mutations/ignoreUser.js b/test/server/graph/mutations/ignoreUser.js index e1cb620e6..72b59a474 100644 --- a/test/server/graph/mutations/ignoreUser.js +++ b/test/server/graph/mutations/ignoreUser.js @@ -34,12 +34,15 @@ describe('graph.mutations.ignoreUser', () => { }); it('users can ignoreUser', async () => { + const ctx = Context.forSystem(); let currentUser = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' ); const userToIgnore = await UsersService.createLocalUser( + ctx, 'usernameB@example.com', 'password', 'usernameB' @@ -83,7 +86,9 @@ describe('graph.mutations.ignoreUser', () => { }); it('users cannot ignore themselves', async () => { + const ctx = Context.forSystem(); const user = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' @@ -124,18 +129,22 @@ describe('graph.mutations.stopIgnoringUser', () => { // We're going to ignore 2 users, // then stopIgnoring 1 of them // then assert myIgnoredUsers only lists the one remaining + const ctx = Context.forSystem(); let currentUser = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' ); const usersToIgnore = await Promise.all([ UsersService.createLocalUser( + ctx, 'usernameB@example.com', 'password', 'usernameB' ), UsersService.createLocalUser( + ctx, 'usernameC@example.com', 'password', 'usernameC' diff --git a/test/server/graph/mutations/setUserBanStatus.js b/test/server/graph/mutations/setUserBanStatus.js index 4d2973b5a..497ec6ea2 100644 --- a/test/server/graph/mutations/setUserBanStatus.js +++ b/test/server/graph/mutations/setUserBanStatus.js @@ -17,7 +17,9 @@ describe('graph.mutations.banUser', () => { beforeEach(async () => { await SettingsService.init(); + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' diff --git a/test/server/graph/mutations/setUserSuspensionStatus.js b/test/server/graph/mutations/setUserSuspensionStatus.js index f0b81cf49..fb0f4b96c 100644 --- a/test/server/graph/mutations/setUserSuspensionStatus.js +++ b/test/server/graph/mutations/setUserSuspensionStatus.js @@ -19,7 +19,9 @@ describe('graph.mutations.suspendUser', () => { beforeEach(async () => { await SettingsService.init(); + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' diff --git a/test/server/graph/mutations/setUserUsernameStatus.js b/test/server/graph/mutations/setUserUsernameStatus.js index d37bf4bf3..cf3f8b6ad 100644 --- a/test/server/graph/mutations/setUserUsernameStatus.js +++ b/test/server/graph/mutations/setUserUsernameStatus.js @@ -19,7 +19,9 @@ const { expect } = chai; beforeEach(async () => { await SettingsService.init(); + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' diff --git a/test/server/graph/queries/asset.js b/test/server/graph/queries/asset.js index f976bd37d..1bd5a0ee3 100644 --- a/test/server/graph/queries/asset.js +++ b/test/server/graph/queries/asset.js @@ -17,23 +17,28 @@ describe('graph.queries.asset', () => { { id: '1', url: 'https://example.com/?id=1' }, { id: '2', url: 'https://example.com/?id=2' }, ]); - users = await UsersService.createLocalUsers([ - { - email: 'usernameA@example.com', - password: 'password', - username: 'usernameA', - }, - { - email: 'usernameB@example.com', - password: 'password', - username: 'usernameB', - }, - { - email: 'usernameC@example.com', - password: 'password', - username: 'usernameC', - }, - ]); + const ctx = Context.forSystem(); + users = await Promise.all( + [ + { + email: 'usernameA@example.com', + password: 'password', + username: 'usernameA', + }, + { + email: 'usernameB@example.com', + password: 'password', + username: 'usernameB', + }, + { + email: 'usernameC@example.com', + password: 'password', + username: 'usernameC', + }, + ].map(({ email, username, password }) => + UsersService.createLocalUser(ctx, email, password, username) + ) + ); comments = await Promise.all( [0, 0, 1, 1].map(idx => CommentsService.publicCreate({ diff --git a/test/server/graph/queries/user.js b/test/server/graph/queries/user.js index 3ec8abaf9..16bb116d6 100644 --- a/test/server/graph/queries/user.js +++ b/test/server/graph/queries/user.js @@ -14,8 +14,9 @@ describe('graph.queries.user', () => { let user; beforeEach(async () => { await SettingsService.init(); - + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'usernameA@example.com', 'password', 'usernameA' diff --git a/test/server/routes/api/auth/index.js b/test/server/routes/api/auth/index.js index 7080cf157..f80fbbc37 100644 --- a/test/server/routes/api/auth/index.js +++ b/test/server/routes/api/auth/index.js @@ -1,12 +1,12 @@ const app = require('../../../../../app'); +const Context = require('../../../../../graph/context'); +const UsersService = require('../../../../../services/users'); const chai = require('chai'); chai.should(); chai.use(require('chai-http')); const expect = chai.expect; -const UsersService = require('../../../../../services/users'); - describe('/api/v1/auth', () => { describe('#get', () => { it('should return nothing when no user is logged in', () => { @@ -32,7 +32,9 @@ describe('/api/v1/auth/local', () => { }; await SettingsService.init(settings); + const ctx = Context.forSystem(); mockUser = await UsersService.createLocalUser( + ctx, 'maria@gmail.com', 'password!', 'Maria' diff --git a/test/server/routes/api/user/index.js b/test/server/routes/api/user/index.js index e7e3f6c72..3faadba6e 100644 --- a/test/server/routes/api/user/index.js +++ b/test/server/routes/api/user/index.js @@ -3,6 +3,7 @@ const passport = require('../../../passport'); const app = require('../../../../../app'); const mailer = require('../../../../../services/mailer'); +const Context = require('../../../../../graph/context'); const SettingsService = require('../../../../../services/settings'); const settings = { id: '1', @@ -20,19 +21,16 @@ const UsersService = require('../../../../../services/users'); describe('/api/v1/users/:user_id/email/confirm', () => { let mockUser; - beforeEach(() => - SettingsService.init(settings) - .then(() => { - return UsersService.createLocalUser( - 'ana@gmail.com', - '123321123', - 'Ana' - ); - }) - .then(user => { - mockUser = user; - }) - ); + beforeEach(async () => { + await SettingsService.init(settings); + const ctx = Context.forSystem(); + mockUser = await UsersService.createLocalUser( + ctx, + 'ana@gmail.com', + '123321123', + 'Ana' + ); + }); describe('#post', () => { it('should send an email when we hit the endpoint', () => { diff --git a/test/server/services/comments.js b/test/server/services/comments.js index 0c1a72ab7..f5b974e7b 100644 --- a/test/server/services/comments.js +++ b/test/server/services/comments.js @@ -1,9 +1,9 @@ -const CommentModel = require('../../../models/comment'); const ActionModel = require('../../../models/action'); - -const UsersService = require('../../../services/users'); -const SettingsService = require('../../../services/settings'); +const CommentModel = require('../../../models/comment'); const CommentsService = require('../../../services/comments'); +const Context = require('../../../graph/context'); +const SettingsService = require('../../../services/settings'); +const UsersService = require('../../../services/users'); const settings = { id: '1', @@ -119,9 +119,14 @@ describe('services.CommentsService', () => { beforeEach(async () => { await SettingsService.init(settings); + const ctx = Context.forSystem(); await Promise.all([ CommentModel.create(comments), - UsersService.createLocalUsers(users), + Promise.all( + users.map(({ email, password, username }) => + UsersService.createLocalUser(ctx, email, password, username) + ) + ), ActionModel.create(actions), ]); }); diff --git a/test/server/services/tags.js b/test/server/services/tags.js index 92e566f7c..c13cf69c9 100644 --- a/test/server/services/tags.js +++ b/test/server/services/tags.js @@ -1,8 +1,8 @@ const TagsService = require('../../../services/tags'); const UsersService = require('../../../services/users'); const SettingsService = require('../../../services/settings'); - const CommentModel = require('../../../models/comment'); +const Context = require('../../../graph/context'); const chai = require('chai'); const expect = chai.expect; @@ -11,7 +11,9 @@ describe('services.TagsService', () => { let comment, user; beforeEach(async () => { await SettingsService.init(); + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'stampi@gmail.com', '1Coral!!', 'Stampi' diff --git a/test/server/services/tokens.js b/test/server/services/tokens.js index 7ff598936..4551b7e4e 100644 --- a/test/server/services/tokens.js +++ b/test/server/services/tokens.js @@ -1,6 +1,7 @@ const TokensService = require('../../../services/tokens'); const UsersService = require('../../../services/users'); const SettingsService = require('../../../services/settings'); +const Context = require('../../../graph/context'); const chai = require('chai'); chai.use(require('chai-as-promised')); @@ -10,7 +11,9 @@ describe('services.TokensService', () => { let user; beforeEach(async () => { await SettingsService.init(); + const ctx = Context.forSystem(); user = await UsersService.createLocalUser( + ctx, 'sockmonster@gmail.com', '2Coral!!', 'Sockmonster' diff --git a/test/server/services/users.js b/test/server/services/users.js index 1676f517b..4f2bc7c65 100644 --- a/test/server/services/users.js +++ b/test/server/services/users.js @@ -1,6 +1,7 @@ const UsersService = require('../../../services/users'); const SettingsService = require('../../../services/settings'); const mailer = require('../../../services/mailer'); +const Context = require('../../../graph/context'); const chai = require('chai'); chai.use(require('chai-as-promised')); @@ -18,23 +19,28 @@ describe('services.UsersService', () => { }; await SettingsService.init(settings); - mockUsers = await UsersService.createLocalUsers([ - { - email: 'stampi@gmail.com', - username: 'Stampi', - password: '1Coral!-', - }, - { - email: 'sockmonster@gmail.com', - username: 'Sockmonster', - password: '2Coral!2', - }, - { - email: 'marvel@gmail.com', - username: 'Marvel', - password: '3Coral!3', - }, - ]); + const ctx = Context.forSystem(); + mockUsers = await Promise.all( + [ + { + email: 'stampi@gmail.com', + username: 'Stampi', + password: '1Coral!-', + }, + { + email: 'sockmonster@gmail.com', + username: 'Sockmonster', + password: '2Coral!2', + }, + { + email: 'marvel@gmail.com', + username: 'Marvel', + password: '3Coral!3', + }, + ].map(({ email, username, password }) => + UsersService.createLocalUser(ctx, email, password, username) + ) + ); sinon.spy(mailer, 'send'); }); @@ -89,19 +95,16 @@ describe('services.UsersService', () => { describe('#createLocalUser', () => { it('should not create a user with duplicate username', () => { - return UsersService.createLocalUsers([ - { - email: 'otrostampi@gmail.com', - username: 'StampiTheSecond', - password: '1Coralito!', - }, - ]) - .then(user => { - expect(user).to.be.null; - }) - .catch(error => { - expect(error).to.not.be.null; - }); + const ctx = Context.forSystem(); + + return expect( + UsersService.createLocalUser( + ctx, + 'otrostampi@gmail.com', + '1Coralito!', + 'Stampi' + ) + ).be.rejected; }); }); From ccda23ec6058164e50ba7bb5ca10d4ae9e7d05f3 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Mar 2018 16:49:44 -0600 Subject: [PATCH 2/3] fixed bug with endpoint --- routes/api/v1/users.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/routes/api/v1/users.js b/routes/api/v1/users.js index 8c1a7b7f3..e0de3b5db 100644 --- a/routes/api/v1/users.js +++ b/routes/api/v1/users.js @@ -11,7 +11,13 @@ router.post('/', async (req, res, next) => { const redirectUri = req.header('X-Pym-Url') || req.header('Referer'); try { - let user = await UsersService.createLocalUser(email, password, username); + // Adjusted the user creation endpoint. + let user = await UsersService.createLocalUser( + req.context, + email, + password, + username + ); // Send an email confirmation. The Front end will know about the // requireEmailConfirmation as it's included in the settings get endpoint. From da6b5c7f360c0bb01f5d106faaa24e7eb27c75a5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Mar 2018 16:54:18 -0600 Subject: [PATCH 3/3] fixed some missing calls to service --- bin/cli-setup | 4 +++- routes/api/v1/setup.js | 5 ++++- services/setup.js | 9 +++++++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/bin/cli-setup b/bin/cli-setup index 282a8a2d7..572e8c7fc 100755 --- a/bin/cli-setup +++ b/bin/cli-setup @@ -15,6 +15,7 @@ const SetupService = require('../services/setup'); const UsersService = require('../services/users'); const MigrationService = require('../services/migration'); const errors = require('../errors'); +const Context = require('../graph/context'); // Register the shutdown criteria. util.onshutdown([() => mongoose.disconnect()]); @@ -184,7 +185,8 @@ const performSetup = async () => { }, ]); - let { user: newUser } = await SetupService.setup({ + const ctx = Context.forSystem(); + let { user: newUser } = await SetupService.setup(ctx, { settings: settings.toObject(), user: { email: user.email, diff --git a/routes/api/v1/setup.js b/routes/api/v1/setup.js index c3cf9518e..553e42f44 100644 --- a/routes/api/v1/setup.js +++ b/routes/api/v1/setup.js @@ -21,7 +21,10 @@ router.post('/', async (req, res, next) => { const { settings, user: { email, password, username } } = req.body; try { - await SetupService.setup({ settings, user: { email, password, username } }); + await SetupService.setup(req.context, { + settings, + user: { email, password, username }, + }); res.status(204).end(); } catch (err) { return next(err); diff --git a/services/setup.js b/services/setup.js index 29f84ff40..84f915ff2 100644 --- a/services/setup.js +++ b/services/setup.js @@ -61,7 +61,7 @@ module.exports = class SetupService { /** * This will perform the setup. */ - static async setup({ settings, user: { email, password, username } }) { + static async setup(ctx, { settings, user: { email, password, username } }) { // Validate the settings first. await SetupService.validate({ settings, @@ -79,7 +79,12 @@ module.exports = class SetupService { // Settings are created! Create the user. // Create the user. - let user = await UsersService.createLocalUser(email, password, username); + let user = await UsersService.createLocalUser( + ctx, + email, + password, + username + ); // Grant them administrative privileges and confirm the email account. await Promise.all([