ParseRole.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  7. var _ParseACL = _interopRequireDefault(require("./ParseACL"));
  8. var _ParseError = _interopRequireDefault(require("./ParseError"));
  9. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  10. function _interopRequireDefault(e) {
  11. return e && e.__esModule ? e : {
  12. default: e
  13. };
  14. }
  15. /**
  16. * Represents a Role on the Parse server. Roles represent groupings of
  17. * Users for the purposes of granting permissions (e.g. specifying an ACL
  18. * for an Object). Roles are specified by their sets of child users and
  19. * child roles, all of which are granted any permissions that the parent
  20. * role has.
  21. *
  22. * <p>Roles must have a name (which cannot be changed after creation of the
  23. * role), and must specify an ACL.</p>
  24. *
  25. * @alias Parse.Role
  26. * @augments Parse.Object
  27. */
  28. class ParseRole extends _ParseObject.default {
  29. /**
  30. * @param {string} name The name of the Role to create.
  31. * @param {Parse.ACL} acl The ACL for this role. Roles must have an ACL.
  32. * A Parse.Role is a local representation of a role persisted to the Parse
  33. * cloud.
  34. */
  35. constructor(name, acl) {
  36. super('_Role');
  37. if (typeof name === 'string' && acl instanceof _ParseACL.default) {
  38. this.setName(name);
  39. this.setACL(acl);
  40. }
  41. }
  42. /**
  43. * Gets the name of the role. You can alternatively call role.get("name")
  44. *
  45. * @returns {string} the name of the role.
  46. */
  47. getName() {
  48. const name = this.get('name');
  49. if (name == null || typeof name === 'string') {
  50. return name;
  51. }
  52. return '';
  53. }
  54. /**
  55. * Sets the name for a role. This value must be set before the role has
  56. * been saved to the server, and cannot be set once the role has been
  57. * saved.
  58. *
  59. * <p>
  60. * A role's name can only contain alphanumeric characters, _, -, and
  61. * spaces.
  62. * </p>
  63. *
  64. * <p>This is equivalent to calling role.set("name", name)</p>
  65. *
  66. * @param {string} name The name of the role.
  67. * @param {object} options Standard options object with success and error
  68. * callbacks.
  69. * @returns {(ParseObject|boolean)} true if the set succeeded.
  70. */
  71. setName(name, options) {
  72. this._validateName(name);
  73. return this.set('name', name, options);
  74. }
  75. /**
  76. * Gets the Parse.Relation for the Parse.Users that are direct
  77. * children of this role. These users are granted any privileges that this
  78. * role has been granted (e.g. read or write access through ACLs). You can
  79. * add or remove users from the role through this relation.
  80. *
  81. * <p>This is equivalent to calling role.relation("users")</p>
  82. *
  83. * @returns {Parse.Relation} the relation for the users belonging to this
  84. * role.
  85. */
  86. getUsers() {
  87. return this.relation('users');
  88. }
  89. /**
  90. * Gets the Parse.Relation for the Parse.Roles that are direct
  91. * children of this role. These roles' users are granted any privileges that
  92. * this role has been granted (e.g. read or write access through ACLs). You
  93. * can add or remove child roles from this role through this relation.
  94. *
  95. * <p>This is equivalent to calling role.relation("roles")</p>
  96. *
  97. * @returns {Parse.Relation} the relation for the roles belonging to this
  98. * role.
  99. */
  100. getRoles() {
  101. return this.relation('roles');
  102. }
  103. _validateName(newName) {
  104. if (typeof newName !== 'string') {
  105. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name must be a String.");
  106. }
  107. if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
  108. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.');
  109. }
  110. }
  111. validate(attrs, options) {
  112. const isInvalid = super.validate(attrs, options);
  113. if (isInvalid) {
  114. return isInvalid;
  115. }
  116. if ('name' in attrs && attrs.name !== this.getName()) {
  117. const newName = attrs.name;
  118. if (this.id && this.id !== attrs.objectId) {
  119. // Check to see if the objectId being set matches this.id
  120. // This happens during a fetch -- the id is set before calling fetch
  121. // Let the name be set in this case
  122. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can only be set before it has been saved.");
  123. }
  124. try {
  125. this._validateName(newName);
  126. } catch (e) {
  127. return e;
  128. }
  129. }
  130. return false;
  131. }
  132. }
  133. _CoreManager.default.setParseRole(ParseRole);
  134. _ParseObject.default.registerSubclass('_Role', ParseRole);
  135. var _default = exports.default = ParseRole;