All files / src/security Security.js

97.78% Statements 353/361
92.22% Branches 308/334
100% Functions 75/75
97.77% Lines 351/359

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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041  1x 1x 1x                     696x       696x   79x             696x 368x     13248x   13248x         328x                     1x     4x   4x 1x     3x 2x 2x     3x   3x   2x 2x                               1x   3x   3x 1x 1x     3x   2x     2x 1x     1x 1x     1x                                 1x   6x 6x 6x   6x 1x     5x 2x 2x     5x 5x   5x 2x     5x 2x                           1x   4x 4x 4x   4x 1x     3x 2x 2x     3x 2x     3x                               1x 3x   3x 2x 2x     3x 2x     3x                       1x 2x                       1x     10x   10x 3x 3x     10x 1x       9x   9x   8x 8x                               1x   3x   3x 1x 1x     3x   2x         2x 1x     1x 1x     1x       1x                                 1x   6x 6x 6x   6x 1x     5x 2x 2x     5x   5x 5x     5x 2x     5x 2x                           1x   4x 4x 4x   4x 1x     3x 2x 2x     3x   3x 3x     3x 2x   2x 1x     1x 1x     1x     3x                               1x 3x   3x 2x 2x     3x 2x     3x               1x   3x 3x   3x 1x     2x         2x   1x   1x 1x     1x 1x   1x       1x 1x   1x   1x     1x                                 1x 2x                   1x   4x 4x   4x 1x     3x 2x 2x     3x   2x 2x                             1x   3x   3x 1x 1x     3x   2x     2x   2x 1x     1x 1x     1x       1x                       1x   3x 3x   3x 2x 2x     3x 2x                       1x   3x 3x   3x 1x     2x 1x 1x     2x 1x                             1x   4x 4x   4x 1x     3x 2x 2x     3x 2x                           1x   4x 4x 4x   4x 1x     3x 2x 2x     3x 3x   3x 2x     3x                               1x 3x   3x 2x 2x     3x 2x     3x               1x   3x 3x   3x 1x     2x         2x   1x   1x 1x     1x 1x   1x       1x 1x   1x   1x     1x             1x                       1x 2x                                     1x     13x 1x   12x 1x   11x 1x       10x   120x     47x     20x     12x       10x 5x     5x 3x     2x                     1x   5x 5x   5x 1x     4x 3x 3x     4x   3x 3x                           1x 2x 2x 2x     2x 2x 1x   1x       2x                       1x 2x 2x 2x     2x 2x 1x   1x       2x                 1x 2x 2x 2x     2x 2x 1x   1x                       1x 2x 2x 2x     2x 2x 1x   1x                         1x 2x 2x 2x     2x 2x 1x   1x                         1x 2x 2x 2x     2x 2x 1x   1x                             1x 2x 2x 2x     2x 2x 1x   1x       2x                       1x 2x 2x 2x     2x 2x 1x   1x         1x  
var
  Role = require('./Role'),
  Profile = require('./Profile'),
  User = require('./User');
 
/**
 * Kuzzle security constructor
 *
 * @param kuzzle
 * @returns {Security}
 * @constructor
 */
function Security(kuzzle) {
 
  Object.defineProperty(this, 'kuzzle', {
    value: kuzzle
  });
 
  Object.defineProperty(this, 'buildQueryArgs', {
    value: function (action) {
      return {
        controller: 'security',
        action: action
      };
    }
  });
 
  if (this.kuzzle.bluebird) {
    return this.kuzzle.bluebird.promisifyAll(this, {
      suffix: 'Promise',
      filter: function (name, func, target, passes) {
        var blacklist = ['role', 'profile', 'user', 'isActionAllowed'];
 
        return passes && blacklist.indexOf(name) === -1;
      }
    });
  }
 
  return this;
}
 
 
/**
 * Retrieve a single Role using its unique role ID.
 *
 * @param {string} id
 * @param {object|responseCallback} [options] - Optional parameters
 * @param {responseCallback} [cb] - returns Kuzzle's response
 */
Security.prototype.fetchRole = function (id, options, cb) {
  var
    data,
    self = this;
 
  if (!id) {
    throw new Error('Id parameter is mandatory for fetchRole function');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  data = {_id: id};
 
  self.kuzzle.callbackRequired('Security.fetchRole', cb);
 
  self.kuzzle.query(this.buildQueryArgs('getRole'), data, options, function (err, response) {
    cb(err, err ? undefined : new Role(self, response.result._id, response.result._source, response.result._meta));
  });
};
 
/**
 * Executes a search on roles according to a filter
 *
 * /!\ There is a small delay between role creation and their existence in our persistent search layer,
 * usually a couple of seconds.
 * That means that a role that was just been created won’t be returned by this function.
 *
 * @param {Object} filters - this object can contains an array `indexes` with a list of index id, a integer `from` and a integer `size`
 * @param {object|responseCallback} [options] - Optional parameters
 * @param {responseCallback} [cb] - returns Kuzzle's response
 *
 */
Security.prototype.searchRoles = function (filters, options, cb) {
  var
    self = this;
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.callbackRequired('Security.searchRoles', cb);
 
  self.kuzzle.query(this.buildQueryArgs('searchRoles'), {body: filters}, options, function (error, result) {
    var documents;
 
    if (error) {
      return cb(error);
    }
 
    documents = result.result.hits.map(function (doc) {
      return new Role(self, doc._id, doc._source, doc._meta);
    });
 
    cb(null, { total: result.result.total, roles: documents });
  });
};
 
/**
 * Create a new role in Kuzzle.
 *
 * Takes an optional argument object with the following property:
 *    - replaceIfExist (boolean, default: false):
 *        If the same role already exists: throw an error if sets to false.
 *        Replace the existing role otherwise
 *
 * @param {string} id - role identifier
 * @param {object} content - a plain javascript object representing the role
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 */
Security.prototype.createRole = function (id, content, options, cb) {
  var
    self = this,
    data = {},
    action = 'createRole';
 
  if (!id || typeof id !== 'string') {
    throw new Error('Security.createRole: cannot create a role without a role ID');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  data._id = id;
  data.body = content;
 
  if (options) {
    action = options.replaceIfExist ? 'createOrReplaceRole' : 'createRole';
  }
 
  self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
    cb(err, err ? undefined : new Role(self, res.result._id, res.result._source, res.result._meta));
  });
};
 
 
/**
 * Update a role in Kuzzle.
 *
 * @param {string} id - role identifier
 * @param {object} content - a plain javascript object representing the role's modification
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 * @returns {Security} this object
 */
Security.prototype.updateRole = function (id, content, options, cb) {
  var
    self = this,
    data = {_id: id, body: content},
    action = 'updateRole';
 
  if (!id || typeof id !== 'string') {
    throw new Error('Security.updateRole: cannot update a role without a role ID');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
    cb(err, err ? undefined : new Role(self, id, content, res.result._meta));
  });
 
  return this;
};
 
/**
 * Delete role.
 *
 * There is a small delay between role deletion and their deletion in our advanced search layer,
 * usually a couple of seconds.
 * That means that a role that was just been delete will be returned by this function
 *
 *
 * @param {string} id - Role id to delete
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {Security} this object
 */
Security.prototype.deleteRole = function (id, options, cb) {
  var data = {_id: id};
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query(this.buildQueryArgs('deleteRole'), data, options, cb && function (err, res) {
    cb(err, err ? undefined : res.result._id);
  });
 
  return this;
};
 
/**
 * Instantiate a new Role object. Workaround to the module.exports limitation, preventing multiple
 * constructors to be exposed without having to use a factory or a composed object.
 *
 * @param {string} id - role id
 * @param {object} content - role content
 * @param {object} meta - role metadata
 * @constructor
 */
Security.prototype.role = function(id, content, meta) {
  return new Role(this, id, content, meta);
};
 
 
/**
 * Get a specific profile from kuzzle
 *
 *
 * @param {string} id
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} cb - returns Kuzzle's response
 */
Security.prototype.fetchProfile = function (id, options, cb) {
  var
    data,
    self = this;
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  if (!id || typeof id !== 'string') {
    throw new Error('Id parameter is mandatory for fetchProfile function');
  }
 
 
  data = {_id: id};
 
  self.kuzzle.callbackRequired('Security.fetchProfile', cb);
 
  self.kuzzle.query(this.buildQueryArgs('getProfile'), data, options, function (error, response) {
    cb(error, error ? undefined : new Profile(self, response.result._id, response.result._source, response.result._meta));
  });
};
 
/**
 * Executes a search on profiles according to a filter
 *
 *
 * /!\ There is a small delay between profile creation and their existence in our persistent search layer,
 * usually a couple of seconds.
 * That means that a profile that was just been created won’t be returned by this function.
 *
 * @param {Object} filters - this object can contains an array `roles` with a list of roles id, a integer `from` and a integer `size`
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - returns Kuzzle's response
 */
Security.prototype.searchProfiles = function (filters, options, cb) {
  var
    self = this;
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.callbackRequired('Security.searchProfiles', cb);
 
  self.kuzzle.query(this.buildQueryArgs('searchProfiles'), {body: filters}, options, function (error, response) {
    var
      documents,
      scrollId;
 
    if (error) {
      return cb(error);
    }
 
    documents = response.result.hits.map(function (doc) {
      return new Profile(self, doc._id, doc._source, doc._meta);
    });
 
    Iif (response.result.scrollId) {
      scrollId = response.result.scrollId;
    }
 
    cb(null, { total: response.result.total, profiles: documents, scrollId: scrollId });
  });
};
 
/**
 * Create a new profile in Kuzzle.
 *
 * Takes an optional argument object with the following property:
 *    - replaceIfExist (boolean, default: false):
 *        If the same profile already exists: throw an error if sets to false.
 *        Replace the existing profile otherwise
 *
 * @param {string} id - profile identifier
 * @param {array} policies - list of policies to attach to the new profile
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 */
Security.prototype.createProfile = function (id, policies, options, cb) {
  var
    self = this,
    data = {},
    action = 'createProfile';
 
  if (!id || typeof id !== 'string') {
    throw new Error('Security.createProfile: cannot create a profile without a profile ID');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  data._id = id;
 
  Eif (policies) {
    data.body = { policies: policies };
  }
 
  if (options) {
    action = options.replaceIfExist ? 'createOrReplaceProfile' : 'createProfile';
  }
 
  self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
    cb(err, err ? undefined : new Profile(self, res.result._id, res.result._source, res.result._meta));
  });
};
 
 
/**
 * Update a profile in Kuzzle.
 *
 * @param {string} id - profile identifier
 * @param {array} policies - the list of policies to apply to this profile
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 * @returns {Security} this object
 */
Security.prototype.updateProfile = function (id, policies, options, cb) {
  var
    self = this,
    data = {},
    action = 'updateProfile';
 
  if (!id || typeof id !== 'string') {
    throw new Error('Security.updateProfile: cannot update a profile without a profile ID');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  data._id = id;
 
  Eif (policies) {
    data.body = {policies: policies};
  }
 
  self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
    var updatedContent = {};
 
    if (err) {
      return cb(err);
    }
 
    Object.keys(res.result._source).forEach(function (property) {
      updatedContent[property] = res.result._source[property];
    });
 
    cb(null, new Profile(self, res.result._id, updatedContent, res.result._meta));
  });
 
  return this;
};
 
/**
 * Delete profile.
 *
 * There is a small delay between profile deletion and their deletion in our advanced search layer,
 * usually a couple of seconds.
 * That means that a profile that was just been delete will be returned by this function
 *
 *
 * @param {string} id - Profile id to delete
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {Security} this object
 */
Security.prototype.deleteProfile = function (id, options, cb) {
  var data = {_id: id};
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query(this.buildQueryArgs('deleteProfile'), data, options, cb && function (err, res) {
    cb(err, err ? undefined : res.result._id);
  });
 
  return this;
};
 
/**
 * @param {string} scrollId
 * @param {object} [options]
 * @param {responseCallback} cb
 */
Security.prototype.scrollProfiles = function (scrollId, options, cb) {
  var
    request = {},
    self = this;
 
  if (!scrollId) {
    throw new Error('Security.scrollProfiles: scrollId is required');
  }
 
  Iif (!cb && typeof options === 'function') {
    cb = options;
    options = {};
  }
 
  this.kuzzle.callbackRequired('Security.scrollProfiles', cb);
 
  request.scrollId = scrollId;
 
  Eif (options && options.scroll) {
    request.scroll = options.scroll;
  }
 
  this.kuzzle.query({controller: 'security', action: 'scrollProfiles'}, request, options, function (error, result) {
    var profiles = [];
 
    Iif (error) {
      return cb(error);
    }
 
    result.result.hits.forEach(function (profile) {
      var newProfile = new Profile(self, profile._id, profile._source, profile._meta);
 
      newProfile.version = profile._version;
 
      profiles.push(newProfile);
    });
 
    cb(null, {
      total: result.result.total,
      profiles: profiles,
      scrollId: scrollId
    });
  });
};
 
/**
 * Instantiate a new Profile object. Workaround to the module.exports limitation, preventing multiple
 * constructors to be exposed without having to use a factory or a composed object.
 *
 * @param {string} id - profile id
 * @param {object} content - profile content
 * @param {object} meta - profile metadata
 * @constructor
 */
Security.prototype.profile = function(id, content, meta) {
  return new Profile(this, id, content, meta);
};
 
/**
 * Get a specific user from kuzzle using its unique ID
 *
 * @param {string} id
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} cb - returns Kuzzle's response
 */
Security.prototype.fetchUser = function (id, options, cb) {
  var
    data = {_id: id},
    self = this;
 
  if (!id || typeof id !== 'string') {
    throw new Error('Id parameter is mandatory for fetchUser function');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.callbackRequired('Security.fetchUser', cb);
 
  self.kuzzle.query(this.buildQueryArgs('getUser'), data, options, function (err, response) {
    cb(err, err ? undefined : new User(self, response.result._id, response.result._source, response.result._meta));
  });
};
 
/**
 * Executes a search on user according to a filter
 *
 * /!\ There is a small delay between user creation and their existence in our persistent search layer,
 * usually a couple of seconds.
 * That means that a user that was just been created won’t be returned by this function.
 *
 * @param {Object} filters - same filters as documents filters
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - returns Kuzzle's response
 */
Security.prototype.searchUsers = function (filters, options, cb) {
  var
    self = this;
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.callbackRequired('Security.searchUsers', cb);
 
  self.kuzzle.query(this.buildQueryArgs('searchUsers'), {body: filters}, options, function (error, response) {
    var
      documents,
      scrollId = null;
 
    if (error) {
      return cb(error);
    }
 
    documents = response.result.hits.map(function (doc) {
      return new User(self, doc._id, doc._source, doc._meta);
    });
 
    Iif (response.result.scrollId) {
      scrollId = response.result.scrollId;
    }
 
    cb(null, { total: response.result.total, users: documents, scrollId: scrollId });
  });
};
 
/**
 * Create a new user in Kuzzle.
 *
 * @param {string} id - user identifier
 * @param {object} content - attribute `profileIds` in `content` must only contain an array of profile ids
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 */
Security.prototype.createUser = function (id, content, options, cb) {
  var
    self = this,
    data = {_id: id, body: content};
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.query(self.buildQueryArgs('createUser'), data, null, cb && function (err, res) {
    cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
  });
};
 
/**
 * Replace an user in Kuzzle.
 *
 * @param {string} id - user identifier
 * @param {object} content - attribute `profileIds` in `content` must only contain an array of profile ids
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 */
Security.prototype.replaceUser = function (id, content, options, cb) {
  var
    self = this,
    data = {_id: id, body: content};
 
  if (!id || typeof id !== 'string') {
    throw new Error('Security.replaceUser: cannot replace a user without a user ID');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.query(this.buildQueryArgs('replaceUser'), data, options, cb && function (err, res) {
    cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
  });
};
 
/**
 * Create a new restricted user in Kuzzle.
 *
 * This function will create a new user. It is not usable to update an existing user.
 * This function allows anonymous users to create a "restricted" user with predefined rights.
 *
 * @param {string} id - user identifier
 * @param {object} content - attribute `profile` in `content` must only contains the profile id
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 */
Security.prototype.createRestrictedUser = function (id, content, options, cb) {
  var
    self = this,
    data = {_id: id, body: content};
 
  if (content.profileIds) {
    throw new Error('Security.createRestrictedUser: cannot provide profileIds');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.query(this.buildQueryArgs('createRestrictedUser'), data, null, cb && function (err, res) {
    cb(err, err ? undefined : new User(self, res.result._id, res.result._source));
  });
};
 
 
/**
 * Update an user in Kuzzle.
 *
 * @param {string} id - user identifier
 * @param {object} content - a plain javascript object representing the user's modification
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - (optional) Handles the query response
 * @returns {Security} this object
 */
Security.prototype.updateUser = function (id, content, options, cb) {
  var
    self = this,
    data = {},
    action = 'updateUser';
 
  if (!id || typeof id !== 'string') {
    throw new Error('Security.updateUser: cannot update an user without an user ID');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  data._id = id;
  data.body = content;
 
  self.kuzzle.query(this.buildQueryArgs(action), data, options, cb && function (err, res) {
    cb(err, err ? undefined : new User(self, res.result._id, res.result._source, res.result._meta));
  });
 
  return this;
};
 
/**
 * Delete user.
 *
 * There is a small delay between user deletion and their deletion in our advanced search layer,
 * usually a couple of seconds.
 * That means that a user that was just been delete will be returned by this function
 *
 *
 * @param {string} id - Profile id to delete
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {responseCallback} [cb] - Handles the query response
 * @returns {Security} this object
 */
Security.prototype.deleteUser = function (id, options, cb) {
  var data = {_id: id};
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query(this.buildQueryArgs('deleteUser'), data, options, cb && function (err, res) {
    cb(err, err ? undefined : res.result._id);
  });
 
  return this;
};
 
/**
 * @param {string} scrollId
 * @param {object} [options]
 * @param {responseCallback} cb
 */
Security.prototype.scrollUsers = function (scrollId, options, cb) {
  var
    request = {},
    self = this;
 
  if (!scrollId) {
    throw new Error('Security.scrollUsers: scrollId is required');
  }
 
  Iif (!cb && typeof options === 'function') {
    cb = options;
    options = {};
  }
 
  this.kuzzle.callbackRequired('Security.scrollUsers', cb);
 
  request.scrollId = scrollId;
 
  Eif (options && options.scroll) {
    request.scroll = options.scroll;
  }
 
  this.kuzzle.query({controller: 'security', action: 'scrollUsers'}, request, options, function (error, result) {
    var users = [];
 
    Iif (error) {
      return cb(error);
    }
 
    result.result.hits.forEach(function (user) {
      var newUser = new User(self, user._id, user._source, user._meta);
 
      newUser.version = user._version;
 
      users.push(newUser);
    });
 
    cb(null, {
      total: result.result.total,
      users: users,
      scrollId: scrollId
    });
  });
 
  return this;
};
 
/**
 * Instantiate a new User object. Workaround to the module.exports limitation, preventing multiple
 * constructors to be exposed without having to use a factory or a composed object.
 *
 * @param {string} id - user id
 * @param {object} content - user content
 * @param {object} meta - user metadata
 * @constructor
 */
Security.prototype.user = function(id, content, meta) {
  return new User(this, id, content, meta);
};
 
/**
 * Tells whether an action is allowed, denied or conditional based on the rights
 * rights provided as the first argument. An action is defined as a couple of
 * action and controller (mandatory), plus an index and a collection(optional).
 *
 * @param {object} rights - The rights rights associated to a user
 *                            (see getMyrights and getUserrights).
 * @param {string} controller - The controller to check the action onto.
 * @param {string} action - The action to perform.
 * @param {string} index - (optional) The name of index to perform the action onto.
 * @param {string} collection - (optional) The name of the collection to perform the action onto.
 *
 * @returns {string} ['allowed', 'denied', 'conditional'] where conditional cases
 *                   correspond to rights containing closures.
 *                   See also http://kuzzle.io/guide/#roles-definition
 */
Security.prototype.isActionAllowed = function(rights, controller, action, index, collection) {
  var filteredRights;
 
  if (!rights || typeof rights !== 'object') {
    throw new Error('rights parameter is mandatory for isActionAllowed function');
  }
  if (!controller || typeof controller !== 'string') {
    throw new Error('controller parameter is mandatory for isActionAllowed function');
  }
  if (!action || typeof action !== 'string') {
    throw new Error('action parameter is mandatory for isActionAllowed function');
  }
 
  // We filter in all the rights that match the request (including wildcards).
  filteredRights = rights
    .filter(function (right) {
      return right.controller === controller || right.controller === '*';
    })
    .filter(function (right) {
      return right.action === action || right.action === '*';
    })
    .filter(function (right) {
      return right.index === index || right.index === '*';
    })
    .filter(function (right) {
      return right.collection === collection || right.collection === '*';
    });
 
  // Then, if at least one right allows the action, we return 'allowed'
  if (filteredRights.some(function (item) { return item.value === 'allowed'; })) {
    return 'allowed';
  }
  // If no right allows the action, we check for conditionals.
  if (filteredRights.some(function (item) { return item.value === 'conditional'; })) {
    return 'conditional';
  }
  // Otherwise we return 'denied'.
  return 'denied';
};
 
 
/**
 * Gets the rights array of a given user.
 *
 * @param {string} userId The id of the user.
 * @param {object|responseCallback} [options] - (optional) arguments
 * @param {function} cb The callback containing the normalized array of rights.
 */
Security.prototype.getUserRights = function (userId, options, cb) {
  var
    data = {_id: userId},
    self = this;
 
  if (!userId || typeof userId !== 'string') {
    throw new Error('userId parameter is mandatory for getUserRights function');
  }
 
  if (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  self.kuzzle.callbackRequired('Kuzzle.getUserRights', cb);
 
  this.kuzzle.query(this.buildQueryArgs('getUserRights'), data, options, cb && function (err, res) {
    cb(err, err ? undefined : res.result.hits);
  });
};
 
/**
 * Create credentials of the specified <strategy> for the user <kuid>.
 *
 * @param strategy
 * @param kuid
 * @param credentials
 * @param options
 * @param cb
 * @returns {Security}
 */
Security.prototype.createCredentials = function (strategy, kuid, credentials, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'createCredentials'}, {_id: kuid, strategy: strategy, body: credentials}, options, function(err, res) {
    if (!err) {
      cb && cb(null, res.result._source);
    } else {
      cb && cb(err);
    }
  });
 
  return this;
};
 
/**
 * Delete credentials of the specified <strategy> for the user <kuid> .
 *
 * @param strategy
 * @param kuid
 * @param options
 * @param cb
 * @returns {Security}
 */
Security.prototype.deleteCredentials = function (strategy, kuid, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'deleteCredentials'}, {strategy: strategy, _id: kuid}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
 
  return this;
};
 
/**
 * Retrieve a list of accepted fields per authentication strategy.
 *
 * @param options
 * @param cb
 */
Security.prototype.getAllCredentialFields = function (options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'getAllCredentialFields'}, {}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
};
 
/**
 * Retrieve the list of accepted field names by the specified <strategy>.
 *
 * @param strategy
 * @param options
 * @param cb
 */
Security.prototype.getCredentialFields = function (strategy, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'getCredentialFields'}, {strategy: strategy}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
};
 
/**
 * Get credential information of the specified <strategy> for the user <kuid>.
 *
 * @param strategy
 * @param kuid
 * @param options
 * @param cb
 */
Security.prototype.getCredentials = function (strategy, kuid, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'getCredentials'}, {strategy: strategy, _id: kuid}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
};
 
/**
 * Check the existence of the specified <strategy>’s credentials for the user <kuid>.
 *
 * @param strategy
 * @param kuid
 * @param options
 * @param cb
 */
Security.prototype.hasCredentials = function (strategy, kuid, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'hasCredentials'}, {strategy: strategy, _id: kuid}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
};
 
/**
 * Updates credentials of the specified <strategy> for the user <kuid>.
 *
 * @param strategy
 * @param kuid
 * @param credentials
 * @param options
 * @param cb
 * @returns {Security}
 */
Security.prototype.updateCredentials = function (strategy, kuid, credentials, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'updateCredentials'}, {strategy: strategy, _id: kuid, body: credentials}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
 
  return this;
};
 
/**
 * Validate credentials of the specified <strategy> for the user <kuid>.
 *
 * @param strategy
 * @param kuid
 * @param credentials
 * @param options
 * @param cb
 */
Security.prototype.validateCredentials = function (strategy, kuid, credentials, options, cb) {
  Eif (!cb && typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  this.kuzzle.query({controller: 'security', action: 'validateCredentials'}, {strategy: strategy, _id: kuid, body: credentials}, options, typeof cb !== 'function' ? null : function(err, res) {
    if (!err) {
      cb && cb(null, res.result);
    } else {
      cb && cb(err);
    }
  });
};
 
module.exports = Security;