Skip to content

Commit

Permalink
Merge pull request #332 from shenie/master
Browse files Browse the repository at this point in the history
Add delete user-blocks api
  • Loading branch information
luisrudge authored May 22, 2019
2 parents 699f285 + ce7476c commit 77717b2
Show file tree
Hide file tree
Showing 4 changed files with 639 additions and 1 deletion.
176 changes: 176 additions & 0 deletions src/management/UserBlocksManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
var ArgumentError = require('rest-facade').ArgumentError;
var Auth0RestClient = require('../Auth0RestClient');
var RetryRestClient = require('../RetryRestClient');

/**
* Simple facade for consuming a REST API endpoint.
* @external RestClient
* @see https://github.com/ngonzalvez/rest-facade
*/

/**
* @class
* Abstracts interaction with the user-blocks endpoint.
* @constructor
* @memberOf module:management
*
* @param {Object} options The client options.
* @param {String} options.baseUrl The URL of the API.
* @param {Object} [options.headers] Headers to be included in all requests.
* @param {Object} [options.retry] Retry Policy Config
*/
var UserBlocksManager = function(options) {
if (options === null || typeof options !== 'object') {
throw new ArgumentError('Must provide manager options');
}

if (options.baseUrl === null || options.baseUrl === undefined) {
throw new ArgumentError('Must provide a base URL for the API');
}

if ('string' !== typeof options.baseUrl || options.baseUrl.length === 0) {
throw new ArgumentError('The provided base URL is invalid');
}

var clientOptions = {
errorFormatter: { message: 'message', name: 'error' },
headers: options.headers,
query: { repeatParams: false }
};

var userBlocksByIdResource = new Auth0RestClient(
options.baseUrl + '/user-blocks/:id',
clientOptions,
options.tokenProvider
);
this.userBlocksById = new RetryRestClient(userBlocksByIdResource, options.retry);

var userBlocksByIdentifierResource = new Auth0RestClient(
options.baseUrl + '/user-blocks',
clientOptions,
options.tokenProvider
);
this.userBlocksByIdentifier = new RetryRestClient(userBlocksByIdentifierResource, options.retry);
};

/**
* Get user blocks by id.
*
* @method get
* @memberOf module:management.UserBlocksManager.prototype
*
* @example
* management.userBlocks.get({ id: USER_ID }, function (err, blocks) {
* if (err) {
* // Handle error.
* }
*
* console.log(blocks);
* });
*
*
* @param {Object} params The user data object..
* @param {String} params.id The user id.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
UserBlocksManager.prototype.get = function(params) {
if (typeof params !== 'object' || typeof params.id !== 'string') {
throw new ArgumentError('You must provide an user id for the get method');
}

return this.userBlocksById.get.apply(this.userBlocksById, arguments);
};

/**
* Unblock an user by its id.
*
* @method delete
* @memberOf module:management.UserBlocksManager.prototype
*
* @example
* management.userBlocks.delete({ id: USER_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // User unblocked.
* });
*
*
* @param {Object} params The user data object..
* @param {String} params.id The user id.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
UserBlocksManager.prototype.delete = function(params) {
if (typeof params !== 'object' || typeof params.id !== 'string') {
throw new ArgumentError('You must provide an user id for the delete method');
}

return this.userBlocksById.delete.apply(this.userBlocksById, arguments);
};

/**
* Get user blocks by identifier.
*
* @method getByIdentifier
* @memberOf module:management.UserBlocksManager.prototype
*
* @example
* management.userBlocks.getByIdentifier({ identifier: USER_ID }, function (err, blocks) {
* if (err) {
* // Handle error.
* }
*
* console.log(blocks);
* });
*
*
* @param {Object} params The user data object..
* @param {String} params.identifier The user identifier, any of: username, phone_number, email.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
UserBlocksManager.prototype.getByIdentifier = function(params) {
if (typeof params !== 'object' || typeof params.identifier !== 'string') {
throw new ArgumentError('You must provide an user identifier for the getByIdentifier method');
}

return this.userBlocksByIdentifier.get.apply(this.userBlocksByIdentifier, arguments);
};

/**
* Unblock an user by identifier.
*
* @method deleteByIdentifier
* @memberOf module:management.UserBlocksManager.prototype
*
* @example
* management.userBlocks.deleteByIdentifier({ identifier: USER_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // User unblocked.
* });
*
*
* @param {Object} params The user data object..
* @param {String} params.identifier The user identifier, any of: username, phone_number, email.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
UserBlocksManager.prototype.deleteByIdentifier = function(params) {
if (typeof params !== 'object' || typeof params.identifier !== 'string') {
throw new ArgumentError('You must provide an user identifier for the deleteByIdentifier method');
}

return this.userBlocksByIdentifier.delete.apply(this.userBlocksByIdentifier, arguments);
};

module.exports = UserBlocksManager;
101 changes: 101 additions & 0 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var ClientsManager = require('./ClientsManager');
var ClientGrantsManager = require('./ClientGrantsManager');
var GrantsManager = require('./GrantsManager');
var UsersManager = require('./UsersManager');
var UserBlocksManager = require('./UserBlocksManager');
var ConnectionsManager = require('./ConnectionsManager');
var BlacklistedTokensManager = require('./BlacklistedTokensManager');
var RulesManager = require('./RulesManager');
Expand Down Expand Up @@ -169,6 +170,14 @@ var ManagementClient = function(options) {
*/
this.users = new UsersManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on the
* user-blocks endpoint.
*
* @type {UserBlocksManager}
*/
this.userBlocks = new UserBlocksManager(managerOptions);

/**
* Simple abstraction for performing CRUD operations on the
* guardian endpoint.
Expand Down Expand Up @@ -1502,6 +1511,98 @@ utils.wrapPropertyMethod(
'users.regenerateRecoveryCode'
);

/**
* Get user blocks by its id.
*
* @method getUserBlocks
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.getUserBlocks({ id: USER_ID }, function (err, blocks) {
* if (err) {
* // Handle error.
* }
*
* console.log(blocks);
* });
*
* @param {Object} params The user data object..
* @param {String} params.id The user id.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'getUserBlocks', 'userBlocks.get');

/**
* Unblock an user by its id.
*
* @method unblockUser
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.unblockUser({ id: USER_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // User unblocked.
* });
*
* @param {Object} params The user data object..
* @param {String} params.id The user id.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'unblockUser', 'userBlocks.delete');

/**
* Get user blocks by its identifier.
*
* @method getUserBlocksByIdentifier
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.getUserBlocksByIdentifier({ identifier: USER_ID }, function (err, blocks) {
* if (err) {
* // Handle error.
* }
*
* console.log(blocks);
* });
*
* @param {Object} params The user data object..
* @param {String} params.identifier The user identifier, any of: username, phone_number, email.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'getUserBlocksByIdentifier', 'userBlocks.getByIdentifier');

/**
* Unblock an user by its id.
*
* @method unblockUser
* @memberOf module:management.ManagementClient.prototype
*
* @example
* management.unblockUserByIdentifier({ identifier: USER_ID }, function (err) {
* if (err) {
* // Handle error.
* }
*
* // User unblocked.
* });
*
* @param {Object} params The user data object..
* @param {String} params.identifier The user identifier, any of: username, phone_number, email.
* @param {Function} [cb] Callback function
*
* @return {Promise|undefined}
*/
utils.wrapPropertyMethod(ManagementClient, 'unblockUserByIdentifier', 'userBlocks.deleteByIdentifier');

/**
* Get a single Guardian enrollment.
*
Expand Down
11 changes: 10 additions & 1 deletion test/management/management-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var ManagementClient = require('../../src/management');

var ArgumentError = require('rest-facade').ArgumentError;
var UsersManager = require('../../src/management/UsersManager');
var UserBlocksManager = require('../../src/management/UserBlocksManager');
var BlacklistedTokensManager = require('../../src/management/BlacklistedTokensManager');
var ClientsManager = require('../../src/management/ClientsManager');
var ClientGrantsManager = require('../../src/management/ClientGrantsManager');
Expand Down Expand Up @@ -101,6 +102,10 @@ describe('ManagementClient', function() {
property: 'users',
cls: UsersManager
},
UserBlocksManager: {
property: 'userBlocks',
cls: UserBlocksManager
},
BlacklistedTokensManager: {
property: 'blacklistedTokens',
cls: BlacklistedTokensManager
Expand Down Expand Up @@ -760,7 +765,11 @@ describe('ManagementClient', function() {
'deleteUserMultifcator',
'setRulesConfig',
'getRulesConfigs',
'deleteRulesConfig'
'deleteRulesConfig',
'getUserBlocks',
'unblockUser',
'getUserBlocksByIdentifier',
'unblockUserByIdentifier'
];

before(function() {
Expand Down
Loading

0 comments on commit 77717b2

Please sign in to comment.