Skip to content

Commit

Permalink
inject acl into add-win set
Browse files Browse the repository at this point in the history
  • Loading branch information
trungnotchung committed Nov 15, 2024
1 parent 74e9ebc commit 618694f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/blueprints/src/AccessControlList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
type CRO,
type Operation,
type ResolveConflictsType,
Role,
SemanticsType,
type Vertex,
} from "@topology-foundation/object";
import { Role } from "@topology-foundation/object/src/constants.js";

export class ACL implements CRO {
operations: string[] = ["grant", "revoke"];
roles: Map<string, number>;
semanticsType = SemanticsType.pair;

constructor(nodeIds: string[] | undefined) {
constructor(nodeIds?: string[] | undefined) {
this.roles = new Map<string, Role>();
if (nodeIds) {
for (const nodeId of nodeIds) {
Expand Down
37 changes: 32 additions & 5 deletions packages/blueprints/src/AddWinsSet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import {
SemanticsType,
type Vertex,
} from "@topology-foundation/object";
import { ACL } from "../AccessControlList/index.js";

export class AddWinsSet<T> implements CRO {
operations: string[] = ["add", "remove"];
operations: string[] = ["add", "remove", "grant", "revoke"];
state: Map<T, boolean>;
semanticsType = SemanticsType.pair;
accessControlList: ACL;

constructor() {
constructor(nodeIds?: string[] | undefined) {
this.state = new Map<T, boolean>();
this.accessControlList = new ACL(nodeIds);
}

private _add(value: T): void {
Expand Down Expand Up @@ -51,16 +54,32 @@ export class AddWinsSet<T> implements CRO {
vertices[0].operation?.type !== vertices[1].operation?.type &&
vertices[0].operation?.value === vertices[1].operation?.value
) {
return vertices[0].operation.type === "add"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
const vertex0Type =
vertices[0].operation.type === "add" ||
vertices[0].operation.type === "remove";
const vertex1Type =
vertices[1].operation.type === "add" ||
vertices[1].operation.type === "remove";

if (vertex0Type !== vertex1Type) {
return { action: ActionType.Nop };
}

if (vertex0Type) {
return vertices[0].operation.type === "add"
? { action: ActionType.DropRight }
: { action: ActionType.DropLeft };
}

return this.accessControlList.resolveConflicts(vertices);
}
return { action: ActionType.Nop };
}

// merged at HG level and called as a callback
mergeCallback(operations: Operation[]): void {
this.state = new Map<T, boolean>();
const aclOperations: Operation[] = [];
for (const op of operations) {
switch (op.type) {
case "add":
Expand All @@ -69,9 +88,17 @@ export class AddWinsSet<T> implements CRO {
case "remove":
if (op.value !== null) this._remove(op.value);
break;
case "grant":
aclOperations.push(op);
break;
case "revoke":
aclOperations.push(op);
break;
default:
break;
}
}

this.accessControlList.mergeCallback(aclOperations);
}
}
5 changes: 0 additions & 5 deletions packages/object/src/constants.ts

This file was deleted.

6 changes: 6 additions & 0 deletions packages/object/src/hashgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export enum OperationType {
NOP = "-1",
}

export enum Role {
NONE = 0,
GUEST = 1,
ADMIN = 2,
}

export enum ActionType {
DropLeft = 0,
DropRight = 1,
Expand Down

0 comments on commit 618694f

Please sign in to comment.