Source: group/groups.js

  1. const Base = require('../base');
  2. const join = require('../deps/utils/join');
  3. const GROUP_PATH = 'group';
  4. /**
  5. * The `Groups` class allows to work with groups on a Nuxeo Platform instance.
  6. *
  7. * **Cannot directly be instantiated**
  8. *
  9. * @example
  10. * var Nuxeo = require('nuxeo')
  11. * var nuxeo = new Nuxeo({
  12. * baseURL: 'http://localhost:8080/nuxeo',
  13. * auth: {
  14. * method: 'basic',
  15. * username: 'Administrator',
  16. * password: 'Administrator'
  17. * }
  18. * });
  19. * nuxeo.groups()
  20. * .fetch('administrators')
  21. * .then(function(res) {
  22. * // res.groupname === 'administrators'
  23. * // res.grouplabel === 'Administrators group'
  24. * })
  25. * .catch(function(error) {
  26. * throw new Error(error));
  27. * });
  28. */
  29. class Groups extends Base {
  30. /**
  31. * Creates a Groups object.
  32. * @param {object} opts - The configuration options.
  33. * @param {string} opts.nuxeo - The {@link Nuxeo} object linked to this Groups object.
  34. */
  35. constructor(opts) {
  36. super(opts);
  37. this._nuxeo = opts.nuxeo;
  38. }
  39. /**
  40. * Fetches a group given a groupname.
  41. * @param {string} groupname - The groupname.
  42. * @param {object} [opts] - Options overriding the ones from this object.
  43. * @returns {Promise} A Promise object resolved with the {@link Group}.
  44. */
  45. fetch(groupname, opts = {}) {
  46. const options = this._computeOptions(opts);
  47. const path = join(GROUP_PATH, groupname);
  48. options.groups = this;
  49. return this._nuxeo.request(path)
  50. .get(options);
  51. }
  52. /**
  53. * Creates a group.
  54. * @param {object} user - The group to be created.
  55. * @param {object} [opts] - Options overriding the ones from this object.
  56. * @returns {Promise} A Promise object resolved with the created {@link Group}.
  57. */
  58. create(group, opts = {}) {
  59. opts.body = {
  60. 'entity-type': 'group',
  61. groupname: group.groupname,
  62. grouplabel: group.grouplabel,
  63. memberUsers: group.memberUsers,
  64. memberGroups: group.memberGroups,
  65. };
  66. const options = this._computeOptions(opts);
  67. options.groups = this;
  68. return this._nuxeo.request(GROUP_PATH)
  69. .post(options);
  70. }
  71. /**
  72. * Updates a group. Assumes that the group object has an groupname field.
  73. * @param {object} group - The group to be updated.
  74. * @param {object} [opts] - Options overriding the ones from this object.
  75. * @returns {Promise} A Promise object resolved with the updated {@link Group}.
  76. */
  77. update(group, opts = {}) {
  78. const id = group.id || group.groupname;
  79. opts.body = {
  80. id,
  81. 'entity-type': 'group',
  82. groupname: group.groupname,
  83. grouplabel: group.grouplabel,
  84. memberUsers: group.memberUsers,
  85. memberGroups: group.memberGroups,
  86. };
  87. const options = this._computeOptions(opts);
  88. const path = join(GROUP_PATH, group.groupname);
  89. options.groups = this;
  90. return this._nuxeo.request(path)
  91. .put(options);
  92. }
  93. /**
  94. * Deletes a group given a groupname.
  95. * @param {string} groupname - The groupname.
  96. * @param {object} [opts] - Options overriding the ones from this object.
  97. * @returns {Promise} A Promise object resolved with the result of the DELETE request.
  98. */
  99. delete(groupname, opts = {}) {
  100. const options = this._computeOptions(opts);
  101. const path = join(GROUP_PATH, groupname);
  102. return this._nuxeo.request(path)
  103. .delete(options);
  104. }
  105. }
  106. module.exports = Groups;