ParseACL.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. }
  95. permissions = {};
  96. this.permissionsById[userId] = permissions;
  97. }
  98. if (allowed) {
  99. this.permissionsById[userId][accessType] = true;
  100. } else {
  101. delete permissions[accessType];
  102. if (Object.keys(permissions).length === 0) {
  103. delete this.permissionsById[userId];
  104. }
  105. }
  106. }
  107. }, {
  108. key: "_getAccess",
  109. value: function (accessType, userId) {
  110. if (userId instanceof _ParseUser.default) {
  111. userId = userId.id;
  112. if (!userId) {
  113. throw new Error('Cannot get access for a ParseUser without an ID');
  114. }
  115. } else if (userId instanceof _ParseRole.default) {
  116. var name = userId.getName();
  117. if (!name) {
  118. throw new TypeError('Role must have a name');
  119. }
  120. userId = "role:" + name;
  121. }
  122. var permissions = this.permissionsById[userId];
  123. if (!permissions) {
  124. return false;
  125. }
  126. return !!permissions[accessType];
  127. }
  128. }, {
  129. key: "setReadAccess",
  130. value: function (userId, allowed) {
  131. this._setAccess('read', userId, allowed);
  132. }
  133. }, {
  134. key: "getReadAccess",
  135. value: function (userId) {
  136. return this._getAccess('read', userId);
  137. }
  138. }, {
  139. key: "setWriteAccess",
  140. value: function (userId, allowed) {
  141. this._setAccess('write', userId, allowed);
  142. }
  143. }, {
  144. key: "getWriteAccess",
  145. value: function (userId) {
  146. return this._getAccess('write', userId);
  147. }
  148. }, {
  149. key: "setPublicReadAccess",
  150. value: function (allowed) {
  151. this.setReadAccess(PUBLIC_KEY, allowed);
  152. }
  153. }, {
  154. key: "getPublicReadAccess",
  155. value: function () {
  156. return this.getReadAccess(PUBLIC_KEY);
  157. }
  158. }, {
  159. key: "setPublicWriteAccess",
  160. value: function (allowed) {
  161. this.setWriteAccess(PUBLIC_KEY, allowed);
  162. }
  163. }, {
  164. key: "getPublicWriteAccess",
  165. value: function () {
  166. return this.getWriteAccess(PUBLIC_KEY);
  167. }
  168. }, {
  169. key: "getRoleReadAccess",
  170. value: function (role) {
  171. if (role instanceof _ParseRole.default) {
  172. role = role.getName();
  173. }
  174. if (typeof role !== 'string') {
  175. throw new TypeError('role must be a ParseRole or a String');
  176. }
  177. return this.getReadAccess("role:" + role);
  178. }
  179. }, {
  180. key: "getRoleWriteAccess",
  181. value: function (role) {
  182. if (role instanceof _ParseRole.default) {
  183. role = role.getName();
  184. }
  185. if (typeof role !== 'string') {
  186. throw new TypeError('role must be a ParseRole or a String');
  187. }
  188. return this.getWriteAccess("role:" + role);
  189. }
  190. }, {
  191. key: "setRoleReadAccess",
  192. value: function (role, allowed) {
  193. if (role instanceof _ParseRole.default) {
  194. role = role.getName();
  195. }
  196. if (typeof role !== 'string') {
  197. throw new TypeError('role must be a ParseRole or a String');
  198. }
  199. this.setReadAccess("role:" + role, allowed);
  200. }
  201. }, {
  202. key: "setRoleWriteAccess",
  203. value: function (role, allowed) {
  204. if (role instanceof _ParseRole.default) {
  205. role = role.getName();
  206. }
  207. if (typeof role !== 'string') {
  208. throw new TypeError('role must be a ParseRole or a String');
  209. }
  210. this.setWriteAccess("role:" + role, allowed);
  211. }
  212. }]);
  213. return ParseACL;
  214. }();
  215. var _default = ParseACL;
  216. exports.default = _default;