ParseRole.js 4.9 KB

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