ParseACL.js 6.9 KB

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