ParseRole.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. /**
  44. * Represents a Role on the Parse server. Roles represent groupings of
  45. * Users for the purposes of granting permissions (e.g. specifying an ACL
  46. * for an Object). Roles are specified by their sets of child users and
  47. * child roles, all of which are granted any permissions that the parent
  48. * role has.
  49. *
  50. * <p>Roles must have a name (which cannot be changed after creation of the
  51. * role), and must specify an ACL.</p>
  52. *
  53. * @alias Parse.Role
  54. * @augments Parse.Object
  55. */
  56. var ParseRole = /*#__PURE__*/function (_ParseObject) {
  57. (0, _inherits2.default)(ParseRole, _ParseObject);
  58. var _super = _createSuper(ParseRole);
  59. /**
  60. * @param {string} name The name of the Role to create.
  61. * @param {Parse.ACL} acl The ACL for this role. Roles must have an ACL.
  62. * A Parse.Role is a local representation of a role persisted to the Parse
  63. * cloud.
  64. */
  65. function ParseRole(name
  66. /*: string*/
  67. , acl
  68. /*: ParseACL*/
  69. ) {
  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 ()
  87. /*: ?string*/
  88. {
  89. var name = this.get('name');
  90. if (name == null || typeof name === 'string') {
  91. return name;
  92. }
  93. return '';
  94. }
  95. /**
  96. * Sets the name for a role. This value must be set before the role has
  97. * been saved to the server, and cannot be set once the role has been
  98. * saved.
  99. *
  100. * <p>
  101. * A role's name can only contain alphanumeric characters, _, -, and
  102. * spaces.
  103. * </p>
  104. *
  105. * <p>This is equivalent to calling role.set("name", name)</p>
  106. *
  107. * @param {string} name The name of the role.
  108. * @param {object} options Standard options object with success and error
  109. * callbacks.
  110. * @returns {(ParseObject|boolean)} true if the set succeeded.
  111. */
  112. }, {
  113. key: "setName",
  114. value: function (name
  115. /*: string*/
  116. , options
  117. /*:: ?: mixed*/
  118. )
  119. /*: ParseObject | boolean*/
  120. {
  121. return this.set('name', name, options);
  122. }
  123. /**
  124. * Gets the Parse.Relation for the Parse.Users that are direct
  125. * children of this role. These users are granted any privileges that this
  126. * role has been granted (e.g. read or write access through ACLs). You can
  127. * add or remove users from the role through this relation.
  128. *
  129. * <p>This is equivalent to calling role.relation("users")</p>
  130. *
  131. * @returns {Parse.Relation} the relation for the users belonging to this
  132. * role.
  133. */
  134. }, {
  135. key: "getUsers",
  136. value: function ()
  137. /*: ParseRelation*/
  138. {
  139. return this.relation('users');
  140. }
  141. /**
  142. * Gets the Parse.Relation for the Parse.Roles that are direct
  143. * children of this role. These roles' users are granted any privileges that
  144. * this role has been granted (e.g. read or write access through ACLs). You
  145. * can add or remove child roles from this role through this relation.
  146. *
  147. * <p>This is equivalent to calling role.relation("roles")</p>
  148. *
  149. * @returns {Parse.Relation} the relation for the roles belonging to this
  150. * role.
  151. */
  152. }, {
  153. key: "getRoles",
  154. value: function ()
  155. /*: ParseRelation*/
  156. {
  157. return this.relation('roles');
  158. }
  159. }, {
  160. key: "validate",
  161. value: function (attrs
  162. /*: AttributeMap*/
  163. , options
  164. /*:: ?: mixed*/
  165. )
  166. /*: ParseError | boolean*/
  167. {
  168. var isInvalid = (0, _get2.default)((0, _getPrototypeOf2.default)(ParseRole.prototype), "validate", this).call(this, attrs, options);
  169. if (isInvalid) {
  170. return isInvalid;
  171. }
  172. if ('name' in attrs && attrs.name !== this.getName()) {
  173. var newName = attrs.name;
  174. if (this.id && this.id !== attrs.objectId) {
  175. // Check to see if the objectId being set matches this.id
  176. // This happens during a fetch -- the id is set before calling fetch
  177. // Let the name be set in this case
  178. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can only be set before it has been saved.");
  179. }
  180. if (typeof newName !== 'string') {
  181. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name must be a String.");
  182. }
  183. if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
  184. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can be only contain alphanumeric characters, _, -, and spaces.");
  185. }
  186. }
  187. return false;
  188. }
  189. }]);
  190. return ParseRole;
  191. }(_ParseObject2.default);
  192. _ParseObject2.default.registerSubclass('_Role', ParseRole);
  193. var _default = ParseRole;
  194. exports.default = _default;