ParseACL.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
  7. var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
  8. var _ParseRole = _interopRequireDefault(require("./ParseRole"));
  9. var _ParseUser = _interopRequireDefault(require("./ParseUser"));
  10. var PUBLIC_KEY = '*';
  11. var ParseACL = function () {
  12. function ParseACL(arg1) {
  13. (0, _classCallCheck2.default)(this, ParseACL);
  14. this.permissionsById = {};
  15. if (arg1 && typeof arg1 === 'object') {
  16. if (arg1 instanceof _ParseUser.default) {
  17. this.setReadAccess(arg1, true);
  18. this.setWriteAccess(arg1, true);
  19. } else {
  20. for (var _userId in arg1) {
  21. var accessList = arg1[_userId];
  22. this.permissionsById[_userId] = {};
  23. for (var _permission in accessList) {
  24. var allowed = accessList[_permission];
  25. if (_permission !== 'read' && _permission !== 'write') {
  26. throw new TypeError('Tried to create an ACL with an invalid permission type.');
  27. }
  28. if (typeof allowed !== 'boolean') {
  29. throw new TypeError('Tried to create an ACL with an invalid permission value.');
  30. }
  31. this.permissionsById[_userId][_permission] = allowed;
  32. }
  33. }
  34. }
  35. } else if (typeof arg1 === 'function') {
  36. throw new TypeError('ParseACL constructed with a function. Did you forget ()?');
  37. }
  38. }
  39. (0, _createClass2.default)(ParseACL, [{
  40. key: "toJSON",
  41. value: function () {
  42. var permissions = {};
  43. for (var p in this.permissionsById) {
  44. permissions[p] = this.permissionsById[p];
  45. }
  46. return permissions;
  47. }
  48. }, {
  49. key: "equals",
  50. value: function (other) {
  51. if (!(other instanceof ParseACL)) {
  52. return false;
  53. }
  54. var users = Object.keys(this.permissionsById);
  55. var otherUsers = Object.keys(other.permissionsById);
  56. if (users.length !== otherUsers.length) {
  57. return false;
  58. }
  59. for (var u in this.permissionsById) {
  60. if (!other.permissionsById[u]) {
  61. return false;
  62. }
  63. if (this.permissionsById[u].read !== other.permissionsById[u].read) {
  64. return false;
  65. }
  66. if (this.permissionsById[u].write !== other.permissionsById[u].write) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. }, {
  73. key: "_setAccess",
  74. value: function (accessType, userId, allowed) {
  75. if (userId instanceof _ParseUser.default) {
  76. userId = userId.id;
  77. } else if (userId instanceof _ParseRole.default) {
  78. var name = userId.getName();
  79. if (!name) {
  80. throw new TypeError('Role must have a name');
  81. }
  82. userId = 'role:' + name;
  83. }
  84. if (typeof userId !== 'string') {
  85. throw new TypeError('userId must be a string.');
  86. }
  87. if (typeof allowed !== 'boolean') {
  88. throw new TypeError('allowed must be either true or false.');
  89. }
  90. var permissions = this.permissionsById[userId];
  91. if (!permissions) {
  92. if (!allowed) {
  93. return;
  94. } else {
  95. permissions = {};
  96. this.permissionsById[userId] = permissions;
  97. }
  98. }
  99. if (allowed) {
  100. this.permissionsById[userId][accessType] = true;
  101. } else {
  102. delete permissions[accessType];
  103. if (Object.keys(permissions).length === 0) {
  104. delete this.permissionsById[userId];
  105. }
  106. }
  107. }
  108. }, {
  109. key: "_getAccess",
  110. value: function (accessType, userId) {
  111. if (userId instanceof _ParseUser.default) {
  112. userId = userId.id;
  113. if (!userId) {
  114. throw new Error('Cannot get access for a ParseUser without an ID');
  115. }
  116. } else if (userId instanceof _ParseRole.default) {
  117. var name = userId.getName();
  118. if (!name) {
  119. throw new TypeError('Role must have a name');
  120. }
  121. userId = 'role:' + name;
  122. }
  123. var permissions = this.permissionsById[userId];
  124. if (!permissions) {
  125. return false;
  126. }
  127. return !!permissions[accessType];
  128. }
  129. }, {
  130. key: "setReadAccess",
  131. value: function (userId, allowed) {
  132. this._setAccess('read', userId, allowed);
  133. }
  134. }, {
  135. key: "getReadAccess",
  136. value: function (userId) {
  137. return this._getAccess('read', userId);
  138. }
  139. }, {
  140. key: "setWriteAccess",
  141. value: function (userId, allowed) {
  142. this._setAccess('write', userId, allowed);
  143. }
  144. }, {
  145. key: "getWriteAccess",
  146. value: function (userId) {
  147. return this._getAccess('write', userId);
  148. }
  149. }, {
  150. key: "setPublicReadAccess",
  151. value: function (allowed) {
  152. this.setReadAccess(PUBLIC_KEY, allowed);
  153. }
  154. }, {
  155. key: "getPublicReadAccess",
  156. value: function () {
  157. return this.getReadAccess(PUBLIC_KEY);
  158. }
  159. }, {
  160. key: "setPublicWriteAccess",
  161. value: function (allowed) {
  162. this.setWriteAccess(PUBLIC_KEY, allowed);
  163. }
  164. }, {
  165. key: "getPublicWriteAccess",
  166. value: function () {
  167. return this.getWriteAccess(PUBLIC_KEY);
  168. }
  169. }, {
  170. key: "getRoleReadAccess",
  171. value: function (role) {
  172. if (role instanceof _ParseRole.default) {
  173. role = role.getName();
  174. }
  175. if (typeof role !== 'string') {
  176. throw new TypeError('role must be a ParseRole or a String');
  177. }
  178. return this.getReadAccess('role:' + role);
  179. }
  180. }, {
  181. key: "getRoleWriteAccess",
  182. value: function (role) {
  183. if (role instanceof _ParseRole.default) {
  184. role = role.getName();
  185. }
  186. if (typeof role !== 'string') {
  187. throw new TypeError('role must be a ParseRole or a String');
  188. }
  189. return this.getWriteAccess('role:' + role);
  190. }
  191. }, {
  192. key: "setRoleReadAccess",
  193. value: function (role, allowed) {
  194. if (role instanceof _ParseRole.default) {
  195. role = role.getName();
  196. }
  197. if (typeof role !== 'string') {
  198. throw new TypeError('role must be a ParseRole or a String');
  199. }
  200. this.setReadAccess('role:' + role, allowed);
  201. }
  202. }, {
  203. key: "setRoleWriteAccess",
  204. value: function (role, allowed) {
  205. if (role instanceof _ParseRole.default) {
  206. role = role.getName();
  207. }
  208. if (typeof role !== 'string') {
  209. throw new TypeError('role must be a ParseRole or a String');
  210. }
  211. this.setWriteAccess('role:' + role, allowed);
  212. }
  213. }]);
  214. return ParseACL;
  215. }();
  216. var _default = ParseACL;
  217. exports.default = _default;