From ddabd5e808f631ff570e0aba5c0d13d59ead7e90 Mon Sep 17 00:00:00 2001 From: Nazar Gargol Date: Wed, 24 Jul 2019 20:21:42 +0200 Subject: [PATCH] Migrated setup method --- .../server/api/shared/validators/input/all.js | 5 +++ core/server/api/v2/authentication.js | 36 ++++++++++++++++--- .../serializers/output/authentication.js | 9 +++++ core/server/web/api/v2/admin/routes.js | 2 +- 4 files changed, 47 insertions(+), 5 deletions(-) diff --git a/core/server/api/shared/validators/input/all.js b/core/server/api/shared/validators/input/all.js index c2096a26a6..f46e43992c 100644 --- a/core/server/api/shared/validators/input/all.js +++ b/core/server/api/shared/validators/input/all.js @@ -192,5 +192,10 @@ module.exports = { resetPassword() { debug('validate resetPassword'); return this.add(...arguments); + }, + + setup() { + debug('validate setup'); + return this.add(...arguments); } }; diff --git a/core/server/api/v2/authentication.js b/core/server/api/v2/authentication.js index e44610ebc3..acfd947f24 100644 --- a/core/server/api/v2/authentication.js +++ b/core/server/api/v2/authentication.js @@ -7,6 +7,33 @@ const invitations = require('../../services/invitations'); module.exports = { docName: 'authentication', + setup: { + permissions: false, + validation: { + docName: 'setup' + }, + query(frame) { + return Promise.resolve() + .then(() => { + return auth.setup.assertSetupCompleted(false)(); + }) + .then(() => { + const setupDetails = { + name: frame.data.setup[0].name, + email: frame.data.setup[0].email, + password: frame.data.setup[0].password, + blogTitle: frame.data.setup[0].blogTitle, + status: 'active' + }; + + return auth.setup.setupUser(setupDetails); + }) + .then((data) => { + return auth.setup.doSettings(data, api.settings); + }); + } + }, + generateResetToken: { permissions: true, options: [ @@ -15,7 +42,7 @@ module.exports = { query(frame) { return Promise.resolve() .then(() => { - return auth.setup.assertSetupCompleted(true); + return auth.setup.assertSetupCompleted(true)(); }) .then(() => { return auth.passwordreset.generateToken(frame.data.email, api.settings); @@ -25,6 +52,7 @@ module.exports = { }); } }, + resetPassword: { validation: { docName: 'passwordreset', @@ -41,7 +69,7 @@ module.exports = { query(frame) { return Promise.resolve() .then(() => { - return auth.setup.assertSetupCompleted(true); + return auth.setup.assertSetupCompleted(true)(); }) .then(() => { return auth.passwordreset.extractTokenParts(frame); @@ -68,7 +96,7 @@ module.exports = { query(frame) { return Promise.resolve() .then(() => { - return auth.setup.assertSetupCompleted(true); + return auth.setup.assertSetupCompleted(true)(); }) .then(() => { return invitations.accept(frame.data); @@ -84,7 +112,7 @@ module.exports = { query(frame) { return Promise.resolve() .then(() => { - return auth.setup.assertSetupCompleted(true); + return auth.setup.assertSetupCompleted(true)(); }) .then(() => { const email = frame.data.email; diff --git a/core/server/api/v2/utils/serializers/output/authentication.js b/core/server/api/v2/utils/serializers/output/authentication.js index c59269959d..a300a62ad0 100644 --- a/core/server/api/v2/utils/serializers/output/authentication.js +++ b/core/server/api/v2/utils/serializers/output/authentication.js @@ -1,7 +1,16 @@ const common = require('../../../../../lib/common'); +const mapper = require('./utils/mapper'); const debug = require('ghost-ignition').debug('api:v2:utils:serializers:output:authentication'); module.exports = { + setup(user, apiConfig, frame) { + frame.response = { + users: [ + mapper.mapUser(user, {options: {context: {internal: true}}}) + ] + }; + }, + acceptInvitation(data, apiConfig, frame) { debug('acceptInvitation'); diff --git a/core/server/web/api/v2/admin/routes.js b/core/server/web/api/v2/admin/routes.js index f21222fb7f..b7296f5089 100644 --- a/core/server/web/api/v2/admin/routes.js +++ b/core/server/web/api/v2/admin/routes.js @@ -189,7 +189,7 @@ module.exports = function apiRoutes() { router.put('/authentication/passwordreset', shared.middlewares.brute.globalBlock, api.http(apiv2.authentication.resetPassword)); router.post('/authentication/invitation', api.http(apiv2.authentication.acceptInvitation)); router.get('/authentication/invitation', api.http(apiv2.authentication.isInvitation)); - router.post('/authentication/setup', api.http(api.authentication.setup)); + router.post('/authentication/setup', api.http(apiv2.authentication.setup)); router.put('/authentication/setup', mw.authAdminApi, api.http(api.authentication.updateSetup)); router.get('/authentication/setup', api.http(api.authentication.isSetup));