Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support User: Add Support User libraries #2368

Merged
merged 2 commits into from
Jan 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -121,6 +121,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 )
} );
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using qs.parse instead of a hacked-in version is smart; if the params change, we won't have to rewrite parts of addSupportData.


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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future you may want to consider refactoring this so it doesn't need to depend on the closure, for easier testing, and for persistence through page reloads. I think this is fine for a first pass though.

}

fn( error, response );
}
);
},
restoreUser: function() {
supportUser = '';
supportToken = '';
},
request: ( params, callback ) => {
if ( supportUser && supportToken ) {
return request( addSupportData( params ), callback );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Great improvement here. This is much more readable.

}

return request( params, callback );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call on moving one level up to wrap request instead of each individual method.

} );
};