mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 09:07:09 +00:00
76 lines
1.4 KiB
JavaScript
76 lines
1.4 KiB
JavaScript
|
|
|
||
|
|
class Organisation {
|
||
|
|
|
||
|
|
constructor (data) {
|
||
|
|
|
||
|
|
this.read = !!data?.read;
|
||
|
|
this.write = !!data?.write;
|
||
|
|
this.edit = !!data?.edit;
|
||
|
|
|
||
|
|
this.other = {};
|
||
|
|
|
||
|
|
return new Proxy(this, {
|
||
|
|
get (target, prop) {
|
||
|
|
if (prop in target) {
|
||
|
|
return target[prop]
|
||
|
|
} else {
|
||
|
|
return target.other[prop];
|
||
|
|
}
|
||
|
|
},
|
||
|
|
|
||
|
|
set (target, prop, value) {
|
||
|
|
const oldValue = target[prop] !== undefined ? target[prop] : target.other[prop];
|
||
|
|
const newValue = Boolean(value);
|
||
|
|
|
||
|
|
if (["read", "write", "edit"].includes(prop)) {
|
||
|
|
target[prop] = newValue;
|
||
|
|
} else {
|
||
|
|
target.other[prop] = newValue;
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
toJSON () {
|
||
|
|
return {
|
||
|
|
read: this.read,
|
||
|
|
write: this.write,
|
||
|
|
edit: this.edit,
|
||
|
|
...this.other
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
toString (replacer, space) {
|
||
|
|
return JSON.stringify(this.toJSON(), replacer, space);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Limit the operations to only those allowed by `other`
|
||
|
|
*/
|
||
|
|
filter (other) {
|
||
|
|
const filteredOrganisation = new Organisation();
|
||
|
|
|
||
|
|
filteredOrganisation.read = this.read && other.read;
|
||
|
|
filteredOrganisation.write = this.write && other.write;
|
||
|
|
filteredOrganisation.edit = this.edit && other.edit;
|
||
|
|
|
||
|
|
return filteredOrganisation;
|
||
|
|
}
|
||
|
|
|
||
|
|
intersect (other) {
|
||
|
|
return this.filter(other);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
if (typeof module !== 'undefined' && module.exports) {
|
||
|
|
module.exports = Organisation; // CJS export
|
||
|
|
}
|
||
|
|
|
||
|
|
// ESM export
|
||
|
|
if (typeof exports !== 'undefined' && !exports.default) {
|
||
|
|
exports.default = Organisation; // ESM export
|
||
|
|
}
|