All files / src/security User.js

98.81% Statements 83/84
93.55% Branches 58/62
100% Functions 17/17
98.81% Lines 83/84

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256  1x                 50x     48x                               48x 46x     874x   874x           1x                       1x 4x 3x     1x   1x           1x 1x       1x   1x                 1x 5x 2x     3x 1x     3x 2x     3x                   1x   3x 3x   3x 1x     2x 2x 2x     2x 2x     2x                     1x   3x 3x   3x 1x     2x 2x 2x       2x 2x     2x                           1x   2x 2x   2x 2x 2x     2x 2x     2x               1x 6x               1x 4x               1x 2x                 1x   5x 5x 5x   5x 3x 3x     5x   3x 1x     2x 6x 6x 3x 2x     1x 1x     3x   3x 1x           1x  
var
  KuzzleSecurityDocument = require('./SecurityDocument');
 
/**
 * @param {Security} Security
 * @param {string} id
 * @param {Object} content
 * @constructor
 */
function User(Security, id, content, meta) {
  KuzzleSecurityDocument.call(this, Security, id, content, meta);
 
  // Define properties
  Object.defineProperties(this, {
    // private properties
    deleteActionName: {
      value: 'deleteUser'
    },
    updateActionName: {
      value: 'updateUser'
    },
    credentials: {
      value: {},
      writable: true,
      enumerable: true
    }
  });
 
  // promisifying
  if (Security.kuzzle.bluebird) {
    return Security.kuzzle.bluebird.promisifyAll(this, {
      suffix: 'Promise',
      filter: function (name, func, target, passes) {
        var whitelist = ['create', 'replace', 'saveRestricted', 'update', 'getProfiles'];
 
        return passes && whitelist.indexOf(name) !== -1;
      }
    });
  }
}
 
User.prototype = Object.create(KuzzleSecurityDocument.prototype, {
  constructor: {
    value: User
  }
});
 
/**
 * Set profiles in content
 * @param {array} profileIds - an array of profiles ids string
 *
 * @returns {User} this
 */
User.prototype.setProfiles = function (profileIds) {
  if (!Array.isArray(profileIds) || typeof profileIds[0] !== 'string') {
    throw new Error('Parameter "profileIds" must be an array of strings');
  }
 
  this.content.profileIds = profileIds;
 
  return this;
};
 
/**
 * @param {object} credentials
 */
User.prototype.setCredentials = function (credentials) {
  Iif (typeof credentials !== 'object') {
    throw new Error('Parameter "credentials" must be a object');
  }
 
  this.credentials = credentials;
 
  return this;
};
 
/**
 * Add a profile
 * @param {string} profileId - a profile ids string
 *
 * @returns {User} this
 */
User.prototype.addProfile = function (profileId) {
  if (typeof profileId !== 'string') {
    throw new Error('Parameter "profileId" must be a string');
  }
 
  if (!this.content.profileIds) {
    this.content.profileIds = [];
  }
 
  if (this.content.profileIds.indexOf(profileId) === -1) {
    this.content.profileIds.push(profileId);
  }
 
  return this;
};
 
/**
 * Creates this user into Kuzzle
 *
 * @param {object|responseCallback} [options] - Optional parameters
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {User} this
 */
User.prototype.create = function (options, cb) {
  var
    data = this.creationSerialize(),
    self = this;
 
  if (!this.content.profileIds) {
    throw new Error('Argument "profileIds" is mandatory in a user. This argument contains an array of profile identifiers.');
  }
 
  Eif (options && cb === undefined && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query(this.Security.buildQueryArgs('createUser'), data, null, cb && function (err) {
    cb(err, err ? undefined : self);
  });
 
  return this;
};
 
 
/**
 * Replaces the latest version of this user in Kuzzle by the current content of this object.
 *
 * @param {object|responseCallback} [options] - Optional parameters
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {User} this
 */
User.prototype.replace = function (options, cb) {
  var
    data = this.serialize(),
    self = this;
 
  if (!this.content.profileIds) {
    throw new Error('Argument "profileIds" is mandatory in a user. This argument contains an array of profile identifiers.');
  }
 
  Eif (options && cb === undefined && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
 
  this.kuzzle.query(this.Security.buildQueryArgs('replaceUser'), data, null, cb && function (err) {
    cb(err, err ? undefined : self);
  });
 
  return this;
};
 
/**
 * Saves this user as restricted into Kuzzle.
 *
 * This function will create a new user. It is not usable to update an existing user.
 * The "profileIds" property must not be provided, or the request will be rejected by Kuzzle.
 * This function allows anonymous users to create a "restricted" user with predefined rights.
 *
 * @param {object|responseCallback} [options] - Optional parameters
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {User} this
 */
User.prototype.saveRestricted = function (options, cb) {
  var
    data = this.serialize(),
    self = this;
 
  Eif (options && cb === undefined && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.query(this.Security.buildQueryArgs('createRestrictedUser'), data, options, cb && function (error) {
    cb(error, error ? undefined : self);
  });
 
  return self;
};
 
/**
 * Serialize this object into a JSON object
 *
 * @return {object} JSON object representing this User
 */
User.prototype.serialize = function () {
  return {_id: this.id, body: this.content, meta: this.meta};
};
 
/**
 * Serialize this object into a JSON object
 *
 * @return {object} JSON object representing this User
 */
User.prototype.creationSerialize = function () {
  return {_id: this.id, body: {content: this.content, credentials: this.credentials, meta: this.meta}};
};
 
/**
 * Return the associated profiles IDs
 *
 * @return {array.<string>} the associated profiles IDs
 */
User.prototype.getProfileIds = function () {
  return this.content.profileIds || [];
};
 
/**
 * Return the associated Profile objects
 *
 * @param {object|responseCallback} [options] - Optional parameters
 * @param {responseCallback} cb - Handles the query response
 */
User.prototype.getProfiles = function (options, cb) {
  var 
    self = this,
    fetchedProfiles = [],
    errored = false;
 
  if (options && !cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.Security.kuzzle.callbackRequired('User.getProfiles', cb);
 
  if (!self.content.profileIds) {
    return cb(null, fetchedProfiles);
  }
 
  self.content.profileIds.forEach(function (profileId) {
    self.Security.fetchProfile(profileId, options, function (error, profile) {
      if (error) {
        if (errored) {
          return;
        }
 
        errored = true; // prevents multiple callback resolutions
        return cb(error);
      }
 
      fetchedProfiles.push(profile);
 
      if (fetchedProfiles.length === self.content.profileIds.length) {
        cb(null, fetchedProfiles);
      }
    });
  });
};
 
module.exports = User;