ParseRole.js 7.2 KB

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