all files / express-stormpath/lib/helpers/ expand-account.js

3.17% Statements 2/63
0% Branches 0/32
0% Functions 0/16
3.17% Lines 2/63
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148                                                                                                                                                                                                                                                                                                   
'use strict';
 
var async = require('async');
 
/**
 * This callback, when called, will pass along a fully expanded Stormpath
 * Account object for future processing.
 *
 * @callback accountCallback
 * @private
 *
 * @param {Object} err - An error (if no account could be retrieved, or one of
 *   the requests failed).
 * @param {Object} account - The fully expanded Stormpath Account object.
 */
 
/**
 * Given an Account, we'll automatically expand all of the useful linked fields
 * to make working with the Account object easier.
 *
 * @method
 * @private
 *
 * @param {Object} app - The Express application object.
 * @param {Object} account - The Stormpath Account object to expand.
 * @param {accountCallback} - The callback which is called to continue
 *   processing the request.
 */
module.exports = function (account, expand, logger, accountCallback) {
  expand = expand || {};
 
  // First, we need to expand our user attributes, this ensures the user is
  // fully expanded for easy developer usage.
  async.parallel([
    function (cb) {
      if (!expand.apiKeys) {
        return cb();
      }
 
      account.getApiKeys(function (err, apiKeys) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s api keys.');
          return accountCallback(err);
        }
 
        account.apiKeys = apiKeys;
        cb();
      });
    },
    function (cb) {
      if (!expand.customData) {
        return cb();
      }
 
      account.getCustomData(function (err, customData) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s custom data.');
          return accountCallback(err);
        }
 
        account.customData = customData;
        cb();
      });
    },
    function (cb) {
      if (!expand.directory) {
        return cb();
      }
 
      account.getDirectory(function (err, directory) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s directory.');
          return accountCallback(err);
        }
 
        account.directory = directory;
        cb();
      });
    },
    function (cb) {
      if (!expand.groups) {
        return cb();
      }
 
      account.getGroups(function (err, groups) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s groups.');
          return accountCallback(err);
        }
 
        account.groups = groups;
        cb();
      });
    },
    function (cb) {
      if (!expand.groupMemberships) {
        return cb();
      }
 
      account.getGroupMemberships(function (err, groupMemberships) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s group memberships.');
          return accountCallback(err);
        }
 
        account.groupMemberships = groupMemberships;
        cb();
      });
    },
    function (cb) {
      if (!expand.providerData) {
        return cb();
      }
 
      account.getProviderData(function (err, providerData) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s provider data.');
          return accountCallback(err);
        }
 
        account.providerData = providerData;
        cb();
      });
    },
    function (cb) {
      if (!expand.tenant) {
        return cb();
      }
 
      account.getTenant(function (err, tenant) {
        if (err) {
          logger.info('Couldn\'t expand ' + account.email + '\'s tenant.');
          return accountCallback(err);
        }
 
        account.tenant = tenant;
        cb();
      });
    }
  ], function (err) {
    if (err) {
      logger.info(err);
    }
 
    accountCallback(null, account);
  });
};