Skip to content

Instantly share code, notes, and snippets.

@jabez007
Created February 20, 2019 13:06
Show Gist options
  • Save jabez007/66d02d91ab37307fd41faee138977d07 to your computer and use it in GitHub Desktop.
Save jabez007/66d02d91ab37307fd41faee138977d07 to your computer and use it in GitHub Desktop.
Creates a JavaScript object with Firebase like '.read'/'.write' properties from a given object
function withACL(obj) {
const __properties__ = {};
if (typeof(obj) === 'object') {
Object.keys(obj)
.forEach((key) => {
Object.defineProperty(this, key, {
get() {
if (!!__properties__[key].value) {
return __properties__[key].value;
} else {
return __properties__[key];
}
},
set(newValue) {
__properties__[key] = new withACL(newValue);
},
});
this[key] = obj[key];
});
} else {
this.value = obj;
this['.read'] = {
users: {},
groups: {},
};
this.grantRead = function (entity) {
// check if array or string
// check each if user or group
this['.read'].users[entity] = true;
};
this.hasReadAccess = function () {
// return an array of all users (decompose groups into users) with 'true'
return this['.read'];
};
this['.write'] = {
users: {},
groups: {},
};
this.grantWrite = function (entity) {
// check if array or string
// check each if user or group
this['.write'].users[entity] = true;
}
this.hasWriteAccess = function () {
// return an array of all users (decompose groups into users) with 'true'
return this['.write'];
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment