123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579 |
- "use strict";
- Object.defineProperty(exports, "__esModule", {
- value: true
- });
- exports.default = void 0;
- var _ParseRole = _interopRequireDefault(require("./ParseRole"));
- var _ParseUser = _interopRequireDefault(require("./ParseUser"));
- function _interopRequireDefault(obj) {
- return obj && obj.__esModule ? obj : {
- default: obj
- };
- }
- const PUBLIC_KEY = '*';
- const VALID_PERMISSIONS = new Map(
-
- );
- VALID_PERMISSIONS.set('get', {});
- VALID_PERMISSIONS.set('find', {});
- VALID_PERMISSIONS.set('count', {});
- VALID_PERMISSIONS.set('create', {});
- VALID_PERMISSIONS.set('update', {});
- VALID_PERMISSIONS.set('delete', {});
- VALID_PERMISSIONS.set('addField', {});
- const VALID_PERMISSIONS_EXTENDED = new Map(
-
- );
- VALID_PERMISSIONS_EXTENDED.set('protectedFields', {});
- class ParseCLP {
-
-
- constructor(userId /*: ParseUser | ParseRole | PermissionsMap*/) {
- this.permissionsMap = {};
-
- for (const [operation, group] of VALID_PERMISSIONS.entries()) {
- this.permissionsMap[operation] = Object.assign({}, group);
- const action = operation.charAt(0).toUpperCase() + operation.slice(1);
- this[`get${action}RequiresAuthentication`] = function () {
- return this._getAccess(operation, 'requiresAuthentication');
- };
- this[`set${action}RequiresAuthentication`] = function (allowed) {
- this._setAccess(operation, 'requiresAuthentication', allowed);
- };
- this[`get${action}PointerFields`] = function () {
- return this._getAccess(operation, 'pointerFields', false);
- };
- this[`set${action}PointerFields`] = function (pointerFields) {
- this._setArrayAccess(operation, 'pointerFields', pointerFields);
- };
- this[`get${action}Access`] = function (entity) {
- return this._getAccess(operation, entity);
- };
- this[`set${action}Access`] = function (entity, allowed) {
- this._setAccess(operation, entity, allowed);
- };
- this[`getPublic${action}Access`] = function () {
- return this[`get${action}Access`](PUBLIC_KEY);
- };
- this[`setPublic${action}Access`] = function (allowed) {
- this[`set${action}Access`](PUBLIC_KEY, allowed);
- };
- this[`getRole${action}Access`] = function (role) {
- return this[`get${action}Access`](this._getRoleName(role));
- };
- this[`setRole${action}Access`] = function (role, allowed) {
- this[`set${action}Access`](this._getRoleName(role), allowed);
- };
- }
-
- for (const [operation, group] of VALID_PERMISSIONS_EXTENDED.entries()) {
- this.permissionsMap[operation] = Object.assign({}, group);
- }
- if (userId && typeof userId === 'object') {
- if (userId instanceof _ParseUser.default) {
- this.setReadAccess(userId, true);
- this.setWriteAccess(userId, true);
- } else if (userId instanceof _ParseRole.default) {
- this.setRoleReadAccess(userId, true);
- this.setRoleWriteAccess(userId, true);
- } else {
- for (const permission in userId) {
- const users = userId[permission];
- const isValidPermission = !!VALID_PERMISSIONS.get(permission);
- const isValidPermissionExtended = !!VALID_PERMISSIONS_EXTENDED.get(permission);
- const isValidGroupPermission = ['readUserFields', 'writeUserFields'].includes(permission);
- if (typeof permission !== 'string' || !(isValidPermission || isValidPermissionExtended || isValidGroupPermission)) {
- throw new TypeError('Tried to create an CLP with an invalid permission type.');
- }
- if (isValidGroupPermission) {
- if (users.every(pointer => typeof pointer === 'string')) {
- this.permissionsMap[permission] = users;
- continue;
- } else {
- throw new TypeError('Tried to create an CLP with an invalid permission value.');
- }
- }
- for (const user in users) {
- const allowed = users[user];
- if (typeof allowed !== 'boolean' && !isValidPermissionExtended && user !== 'pointerFields') {
- throw new TypeError('Tried to create an CLP with an invalid permission value.');
- }
- this.permissionsMap[permission][user] = allowed;
- }
- }
- }
- } else if (typeof userId === 'function') {
- throw new TypeError('ParseCLP constructed with a function. Did you forget ()?');
- }
- }
-
- toJSON() {
- return {
- ...this.permissionsMap
- };
- }
-
- equals(other ) {
- if (!(other instanceof ParseCLP)) {
- return false;
- }
- const permissions = Object.keys(this.permissionsMap);
- const otherPermissions = Object.keys(other.permissionsMap);
- if (permissions.length !== otherPermissions.length) {
- return false;
- }
- for (const permission in this.permissionsMap) {
- if (!other.permissionsMap[permission]) {
- return false;
- }
- const users = Object.keys(this.permissionsMap[permission]);
- const otherUsers = Object.keys(other.permissionsMap[permission]);
- if (users.length !== otherUsers.length) {
- return false;
- }
- for (const user in this.permissionsMap[permission]) {
- if (!other.permissionsMap[permission][user]) {
- return false;
- }
- if (this.permissionsMap[permission][user] !== other.permissionsMap[permission][user]) {
- return false;
- }
- }
- }
- return true;
- }
- _getRoleName(role ) {
- let name = role;
- if (role instanceof _ParseRole.default) {
-
- name = role.getName();
- }
- if (typeof name !== 'string') {
- throw new TypeError('role must be a Parse.Role or a String');
- }
- return `role:${name}`;
- }
- _parseEntity(entity ) {
- let userId = entity;
- if (userId instanceof _ParseUser.default) {
- userId = userId.id;
- if (!userId) {
- throw new Error('Cannot get access for a Parse.User without an id.');
- }
- } else if (userId instanceof _ParseRole.default) {
- userId = this._getRoleName(userId);
- }
- if (typeof userId !== 'string') {
- throw new TypeError('userId must be a string.');
- }
- return userId;
- }
- _setAccess(permission , userId , allowed ) {
- userId = this._parseEntity(userId);
- if (typeof allowed !== 'boolean') {
- throw new TypeError('allowed must be either true or false.');
- }
- const permissions = this.permissionsMap[permission][userId];
- if (!permissions) {
- if (!allowed) {
-
- return;
- } else {
- this.permissionsMap[permission][userId] = {};
- }
- }
- if (allowed) {
- this.permissionsMap[permission][userId] = true;
- } else {
- delete this.permissionsMap[permission][userId];
- }
- }
- _getAccess(permission , userId , returnBoolean = true) {
- userId = this._parseEntity(userId);
- const permissions = this.permissionsMap[permission][userId];
- if (returnBoolean) {
- if (!permissions) {
- return false;
- }
- return !!this.permissionsMap[permission][userId];
- }
- return permissions;
- }
- _setArrayAccess(permission , userId , fields ) {
- userId = this._parseEntity(userId);
- const permissions = this.permissionsMap[permission][userId];
- if (!permissions) {
- this.permissionsMap[permission][userId] = [];
- }
- if (!fields || Array.isArray(fields) && fields.length === 0) {
- delete this.permissionsMap[permission][userId];
- } else if (Array.isArray(fields) && fields.every(field => typeof field === 'string')) {
- this.permissionsMap[permission][userId] = fields;
- } else {
- throw new TypeError('fields must be an array of strings or undefined.');
- }
- }
- _setGroupPointerPermission(operation , pointerFields ) {
- const fields = this.permissionsMap[operation];
- if (!fields) {
- this.permissionsMap[operation] = [];
- }
- if (!pointerFields || Array.isArray(pointerFields) && pointerFields.length === 0) {
- delete this.permissionsMap[operation];
- } else if (Array.isArray(pointerFields) && pointerFields.every(field => typeof field === 'string')) {
- this.permissionsMap[operation] = pointerFields;
- } else {
- throw new TypeError(`${operation}.pointerFields must be an array of strings or undefined.`);
- }
- }
- _getGroupPointerPermissions(operation ) {
- return this.permissionsMap[operation];
- }
-
- setReadUserFields(pointerFields ) {
- this._setGroupPointerPermission('readUserFields', pointerFields);
- }
-
- getReadUserFields() {
- return this._getGroupPointerPermissions('readUserFields');
- }
-
- setWriteUserFields(pointerFields ) {
- this._setGroupPointerPermission('writeUserFields', pointerFields);
- }
-
- getWriteUserFields() {
- return this._getGroupPointerPermissions('writeUserFields');
- }
-
- setProtectedFields(userId , fields ) {
- this._setArrayAccess('protectedFields', userId, fields);
- }
-
- getProtectedFields(userId ) {
- return this._getAccess('protectedFields', userId, false);
- }
-
- setReadAccess(userId , allowed ) {
- this._setAccess('find', userId, allowed);
- this._setAccess('get', userId, allowed);
- this._setAccess('count', userId, allowed);
- }
-
- getReadAccess(userId ) {
- return this._getAccess('find', userId) && this._getAccess('get', userId) && this._getAccess('count', userId);
- }
-
- setWriteAccess(userId , allowed ) {
- this._setAccess('create', userId, allowed);
- this._setAccess('update', userId, allowed);
- this._setAccess('delete', userId, allowed);
- this._setAccess('addField', userId, allowed);
- }
-
- getWriteAccess(userId ) {
- return this._getAccess('create', userId) && this._getAccess('update', userId) && this._getAccess('delete', userId) && this._getAccess('addField', userId);
- }
-
- setPublicReadAccess(allowed ) {
- this.setReadAccess(PUBLIC_KEY, allowed);
- }
-
- getPublicReadAccess() {
- return this.getReadAccess(PUBLIC_KEY);
- }
-
- setPublicWriteAccess(allowed ) {
- this.setWriteAccess(PUBLIC_KEY, allowed);
- }
-
- getPublicWriteAccess() {
- return this.getWriteAccess(PUBLIC_KEY);
- }
-
- setPublicProtectedFields(fields ) {
- this.setProtectedFields(PUBLIC_KEY, fields);
- }
-
- getPublicProtectedFields() {
- return this.getProtectedFields(PUBLIC_KEY);
- }
-
- getRoleReadAccess(role ) {
- return this.getReadAccess(this._getRoleName(role));
- }
-
- getRoleWriteAccess(role ) {
- return this.getWriteAccess(this._getRoleName(role));
- }
-
- setRoleReadAccess(role , allowed ) {
- this.setReadAccess(this._getRoleName(role), allowed);
- }
-
- setRoleWriteAccess(role , allowed ) {
- this.setWriteAccess(this._getRoleName(role), allowed);
- }
-
- getRoleProtectedFields(role ) {
- return this.getProtectedFields(this._getRoleName(role));
- }
-
- setRoleProtectedFields(role , fields ) {
- this.setProtectedFields(this._getRoleName(role), fields);
- }
- }
- var _default = ParseCLP;
- exports.default = _default;
|