aboutsummaryrefslogtreecommitdiff
path: root/src/fields/util.ts
diff options
context:
space:
mode:
authorusodhi <61431818+usodhi@users.noreply.github.com>2020-07-09 12:25:46 +0530
committerusodhi <61431818+usodhi@users.noreply.github.com>2020-07-09 12:25:46 +0530
commit12285b8c0aff514a2345874508c35b6de1026ef4 (patch)
treeaf42356d2984f5236b2e9f7480c656677d6402da /src/fields/util.ts
parent5e9a710d046150aaea6d8d50e1ecc271f44ca50b (diff)
acls now work I think + some cleanup
Diffstat (limited to 'src/fields/util.ts')
-rw-r--r--src/fields/util.ts84
1 files changed, 44 insertions, 40 deletions
diff --git a/src/fields/util.ts b/src/fields/util.ts
index a3e7a36f8..a1af1d3c5 100644
--- a/src/fields/util.ts
+++ b/src/fields/util.ts
@@ -1,5 +1,5 @@
import { UndoManager } from "../client/util/UndoManager";
-import { Doc, Field, FieldResult, UpdatingFromServer, LayoutSym, AclSym, AclPrivate, AclEdit, AclReadonly, AclAddonly } from "./Doc";
+import { Doc, FieldResult, UpdatingFromServer, LayoutSym, AclPrivate, AclEdit, AclReadonly, AclAddonly, AclSym, fetchProto } from "./Doc";
import { SerializationHelper } from "../client/util/SerializationHelper";
import { ProxyField, PrefetchProxy } from "./Proxy";
import { RefField } from "./RefField";
@@ -9,7 +9,6 @@ import { Parent, OnUpdate, Update, Id, SelfProxy, Self } from "./FieldSymbols";
import { DocServer } from "../client/DocServer";
import { ComputedField } from "./ScriptField";
import { ScriptCast } from "./Types";
-import GroupManager from "../client/util/GroupManager";
function _readOnlySetter(): never {
@@ -108,57 +107,65 @@ export function OVERRIDE_ACL(val: boolean) {
_overrideAcl = val;
}
-const HierarchyMapping = new Map<symbol, number>([
- [AclPrivate, 0],
- [AclReadonly, 1],
- [AclAddonly, 2],
- [AclEdit, 3]
-]);
+let currentUserGroups: string[] = [];
+let currentUserEmail: string;// = Doc.CurrentUserEmail;
-export function getEffectiveAcl(target: any): symbol {
+export function setGroups(groups: string[]) {
+ currentUserGroups = groups;
+ currentUserEmail = Doc.CurrentUserEmail;
+}
- console.log("in getEffectiveAcl");
- if (target[AclSym].ACL) return target[AclSym].ACL;
+export function getEffectiveAcl(target: any, in_prop?: string | symbol | number): symbol {
- let effectiveAcl = AclEdit;
+ const HierarchyMapping = new Map<symbol, number>([
+ [AclPrivate, 0],
+ [AclReadonly, 1],
+ [AclAddonly, 2],
+ [AclEdit, 3]
+ ]);
- for (const [key, value] of Object.entries(target[AclSym])) {
- if (key.startsWith("ACL-")) {
- if (GroupManager.Instance.currentUserGroups.includes(key.substring(4)) || Doc.CurrentUserEmail === key.substring(4).replace("_", ".")) {
- if (HierarchyMapping.get(value as symbol)! > HierarchyMapping.get(effectiveAcl)!) {
- effectiveAcl = value as symbol;
- if (effectiveAcl === AclEdit) break;
- }
- }
- }
+ if (!target[AclSym] && target instanceof Doc) {
+ fetchProto(target);
}
- return effectiveAcl;
-}
+ if (target[AclSym] && Object.keys(target[AclSym]).length) {
+
+ if (target.author === currentUserEmail) return AclEdit;
+
+ if (_overrideAcl || (in_prop && DocServer.PlaygroundFields?.includes(in_prop.toString()))) return AclEdit;
-function testPermission(target: any, in_prop: string | symbol | number): boolean {
+ if (target[AclSym].ACL) return target[AclSym].ACL;
- console.log("here");
- // if (target[AclSym].ACL !== AclEdit && !_overrideAcl && !DocServer.PlaygroundFields.includes(in_prop.toString())) return false;
- if (target[AclSym].ACL === AclEdit) return true;
- for (const [key, value] of Object.entries(target[AclSym])) {
- if (key.startsWith("ACL-")) {
- if (GroupManager.Instance.currentUserGroups.includes(key.substring(4))) {
- if (value === AclEdit) return true;
+ let effectiveAcl = AclPrivate;
+ let aclPresent = false;
+
+ for (const [key, value] of Object.entries(target[AclSym])) {
+ if (key.startsWith("ACL-")) {
+ if (currentUserGroups.includes(key.substring(4)) || currentUserEmail === key.substring(4).replace("_", ".")) {
+ if (HierarchyMapping.get(value as symbol)! >= HierarchyMapping.get(effectiveAcl)!) {
+ aclPresent = true;
+ effectiveAcl = value as symbol;
+ if (effectiveAcl === AclEdit) break;
+ }
+ }
}
}
+ return aclPresent ? effectiveAcl : AclEdit;
+ }
+ else {
+ return AclEdit;
}
- return _overrideAcl || DocServer.PlaygroundFields.includes(in_prop.toString());
}
+
const layoutProps = ["panX", "panY", "width", "height", "nativeWidth", "nativeHeight", "fitWidth", "fitToBox",
"chromeStatus", "viewType", "gridGap", "xMargin", "yMargin", "autoHeight"];
export function setter(target: any, in_prop: string | symbol | number, value: any, receiver: any): boolean {
- console.log("in setter")
let prop = in_prop;
- // if (target[AclSym] && !_overrideAcl && !DocServer.PlaygroundFields.includes(in_prop.toString())) return true; // generalise to a testpermission function
- if (!testPermission(target, in_prop)) return true;
+ if (getEffectiveAcl(target, in_prop) !== AclEdit) {
+ return true;
+ }
if (typeof prop === "string" && prop !== "__id" && prop !== "__fields" && (prop.startsWith("_") || layoutProps.includes(prop))) {
if (!prop.startsWith("_")) {
console.log(prop + " is deprecated - switch to _" + prop);
@@ -176,11 +183,9 @@ export function setter(target: any, in_prop: string | symbol | number, value: an
}
export function getter(target: any, in_prop: string | symbol | number, receiver: any): any {
- console.log("in getter")
let prop = in_prop;
- const effectiveAcl = getEffectiveAcl(target);
- if (in_prop === AclSym) return _overrideAcl ? undefined : effectiveAcl;
- if (effectiveAcl === AclPrivate && !_overrideAcl) return undefined;
+ if (in_prop === AclSym) return _overrideAcl ? undefined : target[AclSym];
+ if (getEffectiveAcl(target) === AclPrivate && !_overrideAcl) return undefined;
if (prop === LayoutSym) {
return target.__LAYOUT__;
}
@@ -217,7 +222,6 @@ function getFieldImpl(target: any, prop: string | number, receiver: any, ignoreP
}
if (field === undefined && !ignoreProto && prop !== "proto") {
const proto = getFieldImpl(target, "proto", receiver, true);//TODO tfs: instead of receiver we could use target[SelfProxy]... I don't which semantics we want or if it really matters
- console.log("here");
if (proto instanceof Doc && getEffectiveAcl(proto) !== AclPrivate) {
return getFieldImpl(proto[Self], prop, receiver, ignoreProto);
}