From 5651e0feb87fe2d1a2cf069ab5cba1d3ae08e874 Mon Sep 17 00:00:00 2001 From: Jordan West Date: Mon, 18 Jan 2016 10:54:41 +1000 Subject: [PATCH] Support User: Add Support User libraries --- client/boot/index.js | 5 +++ client/lib/user/dev-support-user.js | 22 ++++++++++ client/lib/user/user.js | 19 +++++++++ client/lib/wp/browser.js | 7 +++- client/lib/wp/support.js | 62 +++++++++++++++++++++++++++++ config/client.json | 3 +- config/development.json | 1 + 7 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 client/lib/user/dev-support-user.js create mode 100644 client/lib/wp/support.js diff --git a/client/boot/index.js b/client/boot/index.js index 47135d65d9deb..3e05dcc483efd 100644 --- a/client/boot/index.js +++ b/client/boot/index.js @@ -154,6 +154,11 @@ function boot() { i18n.setLocaleSlug( user.get().localeSlug ); } ); + // Temporary support for development of the Support User feature + if ( config.isEnabled( 'support-user' ) ) { + require( 'lib/user/dev-support-user' )( user ); + } + translatorJumpstart.init(); reduxStore = createReduxStore(); diff --git a/client/lib/user/dev-support-user.js b/client/lib/user/dev-support-user.js new file mode 100644 index 0000000000000..3d306b0b13c5e --- /dev/null +++ b/client/lib/user/dev-support-user.js @@ -0,0 +1,22 @@ +/** + * This is a temporary file to assist development of the support user feature. + */ + +import config from 'config'; + +export default function( user ) { + if ( config.isEnabled( 'support-user' ) ) { + const callback = ( error ) => { + if ( error ) { + console.error( error ); + } else { + console.log( 'success' ); + } + }; + + window.supportUser = { + login: ( username, password ) => user.changeUser( username, password, callback ), + logout: () => user.restoreUser() + }; + } +} diff --git a/client/lib/user/user.js b/client/lib/user/user.js index adde48fa7bf67..a865818687baa 100644 --- a/client/lib/user/user.js +++ b/client/lib/user/user.js @@ -238,6 +238,25 @@ User.prototype.set = function( attributes ) { return changed; }; +User.prototype.changeUser = function( username, password, callback ) { + if ( config.isEnabled( 'support-user' ) ) { + wpcom.changeUser( username, password, function( error ) { + if ( ! error ) { + this.fetch(); + } + callback( error ); + }.bind( this ) ); + } +}; + +User.prototype.restoreUser = function() { + if ( config.isEnabled( 'support-user' ) ) { + wpcom.restoreUser(); + + this.fetch(); + } +}; + /** * Expose `User` */ diff --git a/client/lib/wp/browser.js b/client/lib/wp/browser.js index b91ac9b0bf618..64b3f941bfde8 100644 --- a/client/lib/wp/browser.js +++ b/client/lib/wp/browser.js @@ -9,6 +9,7 @@ const debug = debugFactory( 'calypso:wp' ); */ import wpcomUndocumented from 'lib/wpcom-undocumented'; import config from 'config'; +import wpcomSupport from 'lib/wp/support'; let wpcom; @@ -36,4 +37,8 @@ if ( config.isEnabled( 'oauth' ) ) { /** * Expose `wpcom` */ -module.exports = wpcom; +if ( config.isEnabled( 'support-user' ) ) { + module.exports = wpcomSupport( wpcom ); +} else { + module.exports = wpcom; +} diff --git a/client/lib/wp/support.js b/client/lib/wp/support.js new file mode 100644 index 0000000000000..d371767fff8dd --- /dev/null +++ b/client/lib/wp/support.js @@ -0,0 +1,62 @@ +/** + * External dependencies + */ +import qs from 'qs'; + +export default function wpcomSupport( wpcom ) { + let supportUser = ''; + let supportToken = ''; + + /** + * Add the supportUser and supportToken to the query. + * @param {Object} params The original request params object + * @return {Object} The new query object with support data injected + */ + const addSupportData = function( params ) { + // Unwind the query string + let query = qs.parse( params.query ); + + // Inject the credentials + query.support_user = supportUser; + query._support_token = supportToken + + return Object.assign( {}, params, { + query: qs.stringify( query ) + } ); + }; + + const request = wpcom.request.bind( wpcom ); + + return Object.assign( wpcom, { + changeUser: function( username, password, fn ) { + return wpcom.req.post( + { + apiVersion: '1.1', + path: `/internal/support/${ username }/grant` + }, + { + password: password + }, + ( error, response ) => { + if ( ! error ) { + supportUser = response.username; + supportToken = response.token; + } + + fn( error, response ); + } + ); + }, + restoreUser: function() { + supportUser = ''; + supportToken = ''; + }, + request: ( params, callback ) => { + if ( supportUser && supportToken ) { + return request( addSupportData( params ), callback ); + } + + return request( params, callback ); + } + } ); +}; diff --git a/config/client.json b/config/client.json index d94e73920b8e6..45002221563ca 100644 --- a/config/client.json +++ b/config/client.json @@ -25,5 +25,6 @@ "logout_url", "siftscience_key", "facebook_api_key", - "discover_blog_id" + "discover_blog_id", + "support-user" ] diff --git a/config/development.json b/config/development.json index 09d6497cd310d..3d16334aa7653 100644 --- a/config/development.json +++ b/config/development.json @@ -122,6 +122,7 @@ "me/trophies": false, "help": true, + "support-user": true, "notifications2beta": true, "muse": true,