ParseRole.js 4.8 KB

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