ParseRole.js 4.6 KB

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