ParseRole.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. this._validateName(name);
  94. return this.set('name', name, options);
  95. }
  96. /**
  97. * Gets the Parse.Relation for the Parse.Users that are direct
  98. * children of this role. These users are granted any privileges that this
  99. * role has been granted (e.g. read or write access through ACLs). You can
  100. * add or remove users from the role through this relation.
  101. *
  102. * <p>This is equivalent to calling role.relation("users")</p>
  103. *
  104. * @returns {Parse.Relation} the relation for the users belonging to this
  105. * role.
  106. */
  107. getUsers()
  108. /*: ParseRelation*/
  109. {
  110. return this.relation('users');
  111. }
  112. /**
  113. * Gets the Parse.Relation for the Parse.Roles that are direct
  114. * children of this role. These roles' users are granted any privileges that
  115. * this role has been granted (e.g. read or write access through ACLs). You
  116. * can add or remove child roles from this role through this relation.
  117. *
  118. * <p>This is equivalent to calling role.relation("roles")</p>
  119. *
  120. * @returns {Parse.Relation} the relation for the roles belonging to this
  121. * role.
  122. */
  123. getRoles()
  124. /*: ParseRelation*/
  125. {
  126. return this.relation('roles');
  127. }
  128. _validateName(newName) {
  129. if (typeof newName !== 'string') {
  130. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name must be a String.");
  131. }
  132. if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
  133. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.');
  134. }
  135. }
  136. validate(attrs
  137. /*: AttributeMap*/
  138. , options
  139. /*:: ?: mixed*/
  140. )
  141. /*: ParseError | boolean*/
  142. {
  143. const isInvalid = super.validate(attrs, options);
  144. if (isInvalid) {
  145. return isInvalid;
  146. }
  147. if ('name' in attrs && attrs.name !== this.getName()) {
  148. const newName = attrs.name;
  149. if (this.id && this.id !== attrs.objectId) {
  150. // Check to see if the objectId being set matches this.id
  151. // This happens during a fetch -- the id is set before calling fetch
  152. // Let the name be set in this case
  153. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can only be set before it has been saved.");
  154. }
  155. try {
  156. this._validateName(newName);
  157. } catch (e) {
  158. return e;
  159. }
  160. }
  161. return false;
  162. }
  163. }
  164. _ParseObject.default.registerSubclass('_Role', ParseRole);
  165. var _default = ParseRole;
  166. exports.default = _default;