ParseRole.js 4.8 KB

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