Skip to content

Commit

Permalink
Merge pull request #2368 from Automattic/add/support-user-libs
Browse files Browse the repository at this point in the history
Support User: Add Support User libraries
  • Loading branch information
jordwest committed Jan 17, 2016
2 parents b9a104e + da75b57 commit 0698c5d
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 2 deletions.
5 changes: 5 additions & 0 deletions client/boot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
22 changes: 22 additions & 0 deletions client/lib/user/dev-support-user.js
Original file line number Diff line number Diff line change
@@ -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()
};
}
}
19 changes: 19 additions & 0 deletions client/lib/user/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
*/
Expand Down
7 changes: 6 additions & 1 deletion client/lib/wp/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion config/client.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"logout_url",
"siftscience_key",
"facebook_api_key",
"discover_blog_id"
"discover_blog_id",
"support-user"
]
1 change: 1 addition & 0 deletions config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
"me/trophies": false,

"help": true,
"support-user": true,

"notifications2beta": true,
"muse": true,
Expand Down
62 changes: 62 additions & 0 deletions shared/lib/wp/support.js
Original file line number Diff line number Diff line change
@@ -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 );
}
} );
};

0 comments on commit 0698c5d

Please sign in to comment.