ParseRole.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  9. var _ParseACL = _interopRequireDefault(require("./ParseACL"));
  10. var _ParseError = _interopRequireDefault(require("./ParseError"));
  11. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  12. /**
  13. * Represents a Role on the Parse server. Roles represent groupings of
  14. * Users for the purposes of granting permissions (e.g. specifying an ACL
  15. * for an Object). Roles are specified by their sets of child users and
  16. * child roles, all of which are granted any permissions that the parent
  17. * role has.
  18. *
  19. * <p>Roles must have a name (which cannot be changed after creation of the
  20. * role), and must specify an ACL.</p>
  21. *
  22. * @alias Parse.Role
  23. * @augments Parse.Object
  24. */
  25. class ParseRole extends _ParseObject.default {
  26. /**
  27. * @param {string} name The name of the Role to create.
  28. * @param {Parse.ACL} acl The ACL for this role. Roles must have an ACL.
  29. * A Parse.Role is a local representation of a role persisted to the Parse
  30. * cloud.
  31. */
  32. constructor(name, acl) {
  33. super('_Role');
  34. if (typeof name === 'string' && acl instanceof _ParseACL.default) {
  35. this.setName(name);
  36. this.setACL(acl);
  37. }
  38. }
  39. /**
  40. * Gets the name of the role. You can alternatively call role.get("name")
  41. *
  42. * @returns {string} the name of the role.
  43. */
  44. getName() {
  45. const name = this.get('name');
  46. if (name == null || typeof name === 'string') {
  47. return name;
  48. }
  49. return '';
  50. }
  51. /**
  52. * Sets the name for a role. This value must be set before the role has
  53. * been saved to the server, and cannot be set once the role has been
  54. * saved.
  55. *
  56. * <p>
  57. * A role's name can only contain alphanumeric characters, _, -, and
  58. * spaces.
  59. * </p>
  60. *
  61. * <p>This is equivalent to calling role.set("name", name)</p>
  62. *
  63. * @param {string} name The name of the role.
  64. * @param {object} options Standard options object with success and error
  65. * callbacks.
  66. * @returns {(ParseObject|boolean)} true if the set succeeded.
  67. */
  68. setName(name, options) {
  69. this._validateName(name);
  70. return this.set('name', name, options);
  71. }
  72. /**
  73. * Gets the Parse.Relation for the Parse.Users that are direct
  74. * children of this role. These users are granted any privileges that this
  75. * role has been granted (e.g. read or write access through ACLs). You can
  76. * add or remove users from the role through this relation.
  77. *
  78. * <p>This is equivalent to calling role.relation("users")</p>
  79. *
  80. * @returns {Parse.Relation} the relation for the users belonging to this
  81. * role.
  82. */
  83. getUsers() {
  84. return this.relation('users');
  85. }
  86. /**
  87. * Gets the Parse.Relation for the Parse.Roles that are direct
  88. * children of this role. These roles' users are granted any privileges that
  89. * this role has been granted (e.g. read or write access through ACLs). You
  90. * can add or remove child roles from this role through this relation.
  91. *
  92. * <p>This is equivalent to calling role.relation("roles")</p>
  93. *
  94. * @returns {Parse.Relation} the relation for the roles belonging to this
  95. * role.
  96. */
  97. getRoles() {
  98. return this.relation('roles');
  99. }
  100. _validateName(newName) {
  101. if (typeof newName !== 'string') {
  102. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name must be a String.");
  103. }
  104. if (!/^[0-9a-zA-Z\-_ ]+$/.test(newName)) {
  105. throw new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can be only contain alphanumeric characters, _, " + '-, and spaces.');
  106. }
  107. }
  108. validate(attrs, options) {
  109. const isInvalid = super.validate(attrs, options);
  110. if (isInvalid) {
  111. return isInvalid;
  112. }
  113. if ('name' in attrs && attrs.name !== this.getName()) {
  114. const newName = attrs.name;
  115. if (this.id && this.id !== attrs.objectId) {
  116. // Check to see if the objectId being set matches this.id
  117. // This happens during a fetch -- the id is set before calling fetch
  118. // Let the name be set in this case
  119. return new _ParseError.default(_ParseError.default.OTHER_CAUSE, "A role's name can only be set before it has been saved.");
  120. }
  121. try {
  122. this._validateName(newName);
  123. } catch (e) {
  124. return e;
  125. }
  126. }
  127. return false;
  128. }
  129. }
  130. _CoreManager.default.setParseRole(ParseRole);
  131. _ParseObject.default.registerSubclass('_Role', ParseRole);
  132. var _default = exports.default = ParseRole;