ParseRole.js 6.2 KB

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