ParseRole.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  4. var _Reflect$construct = require("@babel/runtime-corejs3/core-js-stable/reflect/construct");
  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. this._validateName(name);
  122. return this.set('name', name, options);
  123. }
  124. /**
  125. * Gets the Parse.Relation for the Parse.Users that are direct
  126. * children of this role. These users are granted any privileges that this
  127. * role has been granted (e.g. read or write access through ACLs). You can
  128. * add or remove users from the role through this relation.
  129. *
  130. * <p>This is equivalent to calling role.relation("users")</p>
  131. *
  132. * @returns {Parse.Relation} the relation for the users belonging to this
  133. * role.
  134. */
  135. }, {
  136. key: "getUsers",
  137. value: function ()
  138. /*: ParseRelation*/
  139. {
  140. return this.relation('users');
  141. }
  142. /**
  143. * Gets the Parse.Relation for the Parse.Roles that are direct
  144. * children of this role. These roles' users are granted any privileges that
  145. * this role has been granted (e.g. read or write access through ACLs). You
  146. * can add or remove child roles from this role through this relation.
  147. *
  148. * <p>This is equivalent to calling role.relation("roles")</p>
  149. *
  150. * @returns {Parse.Relation} the relation for the roles belonging to this
  151. * role.
  152. */
  153. }, {
  154. key: "getRoles",
  155. value: function ()
  156. /*: ParseRelation*/
  157. {
  158. return this.relation('roles');
  159. }
  160. }, {
  161. key: "_validateName",
  162. value: function (newName) {
  163. if (typeof newName !== 'string') {
  164. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name must be a String.");
  165. }
  166. if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
  167. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.');
  168. }
  169. }
  170. }, {
  171. key: "validate",
  172. value: function (attrs
  173. /*: AttributeMap*/
  174. , options
  175. /*:: ?: mixed*/
  176. )
  177. /*: ParseError | boolean*/
  178. {
  179. var isInvalid = (0, _get2.default)((0, _getPrototypeOf2.default)(ParseRole.prototype), "validate", this).call(this, attrs, options);
  180. if (isInvalid) {
  181. return isInvalid;
  182. }
  183. if ('name' in attrs && attrs.name !== this.getName()) {
  184. var newName = attrs.name;
  185. if (this.id && this.id !== attrs.objectId) {
  186. // Check to see if the objectId being set matches this.id
  187. // This happens during a fetch -- the id is set before calling fetch
  188. // Let the name be set in this case
  189. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can only be set before it has been saved.");
  190. }
  191. try {
  192. this._validateName(newName);
  193. } catch (e) {
  194. return e;
  195. }
  196. }
  197. return false;
  198. }
  199. }]);
  200. return ParseRole;
  201. }(_ParseObject2.default);
  202. _ParseObject2.default.registerSubclass('_Role', ParseRole);
  203. var _default = ParseRole;
  204. exports.default = _default;