-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support User: Add Support User libraries
- Loading branch information
Showing
7 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} ); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,5 +25,6 @@ | |
"logout_url", | ||
"siftscience_key", | ||
"facebook_api_key", | ||
"discover_blog_id" | ||
"discover_blog_id", | ||
"support-user" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters