Skip to content

Commit a5c3647

Browse files
committed
completed test object (simple mutable permissions) for reversibility
support
1 parent c9cb3de commit a5c3647

File tree

1 file changed

+85
-18
lines changed

1 file changed

+85
-18
lines changed

test/data/types/PermissionTest.ts

Lines changed: 85 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import { Identity } from 'data/identity';
12
import { Hash, HashedObject, HashReference, InvalidateAfterOp, MutableObject, MutationOp } from 'data/model';
23
import { MultiMap } from 'util/multimap';
34

45
type PermissionTestOp = GrantOp|RevokeAfterOp|UseOp;
56

67
type PermissionUse = UseOp;
78

8-
type Grantee = string;
99
type Capability = string;
1010
type Key = string;
1111

@@ -14,10 +14,10 @@ class GrantOp extends MutationOp {
1414

1515
static className = 'hhs-test/GrantOp';
1616

17-
grantee? : Grantee;
17+
grantee? : Identity;
1818
capability? : Capability;
1919

20-
constructor(targetObject?: PermissionTest, grantee?: Grantee, capability?: Capability, causalOps?: IterableIterator<MutationOp>) {
20+
constructor(targetObject?: PermissionTest, grantee?: Identity, capability?: Capability, causalOps?: IterableIterator<MutationOp>) {
2121
super(targetObject, causalOps);
2222

2323
this.grantee = grantee;
@@ -100,6 +100,10 @@ class UseOp extends MutationOp {
100100
return false;
101101
}
102102

103+
if (!(causalOp.grantee !== undefined && causalOp.grantee.equals(this.getAuthor()))) {
104+
return false;
105+
}
106+
103107
return true;
104108
}
105109

@@ -133,6 +137,77 @@ class PermissionTest extends MutableObject {
133137
this._grantOps = new Map();
134138
}
135139

140+
getClassName(): string {
141+
return PermissionTest.className;
142+
}
143+
144+
init(): void {
145+
146+
}
147+
148+
private isRootOp(op: MutationOp): boolean {
149+
const root = this.getAuthor();
150+
151+
return root !== undefined && root.equals(op.getAuthor()) && op.getCausalOps().size() === 0;
152+
}
153+
154+
private isAdminOp(op: MutationOp): boolean {
155+
156+
const causalOps = op.getCausalOps();
157+
if (causalOps.size() !== 1) {
158+
return false;
159+
}
160+
161+
const causalOp = causalOps.values().next().value as MutationOp;
162+
163+
if (!(causalOp instanceof GrantOp) || !this.isRootOp(causalOp)) {
164+
return false;
165+
}
166+
167+
if (!(causalOp.grantee !== undefined && causalOp.grantee.equals(op.getAuthor()))) {
168+
return false;
169+
}
170+
171+
if (causalOp.capability !== 'admin') {
172+
return false;
173+
}
174+
175+
return true;
176+
177+
}
178+
179+
shouldAcceptMutationOp(op: MutationOp): boolean {
180+
181+
if (super.shouldAcceptMutationOp(op)) {
182+
183+
if (op instanceof GrantOp) {
184+
if (op.capability === 'admin' && this.isRootOp(op)) {
185+
return true;
186+
} else if (op.capability === 'user' && (this.isRootOp(op) || this.isAdminOp(op))) {
187+
return true;
188+
} else {
189+
return false;
190+
}
191+
} else if (op instanceof RevokeAfterOp) {
192+
if (op.getTargetOp().capability === 'admin' && this.isRootOp(op)) {
193+
return true;
194+
} else if (op.getTargetOp().capability === 'user' && (this.isRootOp(op) || this.isAdminOp(op))) {
195+
return true;
196+
} else {
197+
return false;
198+
}
199+
} else {
200+
return true
201+
}
202+
203+
204+
} else {
205+
return false;
206+
}
207+
208+
209+
}
210+
136211
async mutate(op: MutationOp): Promise<boolean> {
137212

138213
let mutated = false;
@@ -150,14 +225,6 @@ class PermissionTest extends MutableObject {
150225
return mutated;
151226
}
152227

153-
getClassName(): string {
154-
return PermissionTest.className;
155-
}
156-
157-
init(): void {
158-
159-
}
160-
161228
async validate(references: Map<string, HashedObject>): Promise<boolean> {
162229
references;
163230

@@ -174,7 +241,7 @@ class PermissionTest extends MutableObject {
174241
175242
}*/
176243

177-
hasCapability(grantee: Grantee, capability: Capability): boolean {
244+
hasCapability(grantee: Identity, capability: Capability): boolean {
178245

179246
let result = false;
180247

@@ -197,7 +264,7 @@ class PermissionTest extends MutableObject {
197264
return result;
198265
}
199266

200-
useCapability(grantee: Grantee, capability: Capability): UseOp {
267+
useCapability(grantee: Identity, capability: Capability): UseOp {
201268

202269
let useOp = this.useCapabilityIfAvailable(grantee, capability);
203270

@@ -209,7 +276,7 @@ class PermissionTest extends MutableObject {
209276

210277
}
211278

212-
useCapabilityIfAvailable(grantee: Grantee, capability: Capability): UseOp|undefined {
279+
useCapabilityIfAvailable(grantee: Identity, capability: Capability): UseOp|undefined {
213280
let useOp: UseOp|undefined = undefined;
214281

215282
const grantOp = this.findValidGrant(grantee, capability);
@@ -222,7 +289,7 @@ class PermissionTest extends MutableObject {
222289
return useOp;
223290
}
224291

225-
private findValidGrant(grantee: Grantee, capability: Capability): GrantOp|undefined {
292+
private findValidGrant(grantee: Identity, capability: Capability): GrantOp|undefined {
226293

227294
let chosenGrantOp: GrantOp|undefined = undefined;
228295
let chosenGrantOpHash: Hash|undefined = undefined;
@@ -249,11 +316,11 @@ class PermissionTest extends MutableObject {
249316
}
250317

251318
static getGranteeCapabilityKeyForGrantOp(op: GrantOp): Key {
252-
return PermissionTest.getGranteeCapabilityKey(op.grantee as Grantee, op.capability as Capability);
319+
return PermissionTest.getGranteeCapabilityKey(op.grantee as Identity, op.capability as Capability);
253320
}
254321

255-
static getGranteeCapabilityKey(grantee: Grantee, capability: Capability): Key {
256-
return grantee.replace(/-/g, '--') + '-' + capability.replace(/-/g, '--');
322+
static getGranteeCapabilityKey(grantee: Identity, capability: Capability): Key {
323+
return grantee.hash().replace(/-/g, '--') + '-' + capability.replace(/-/g, '--');
257324
}
258325

259326
}

0 commit comments

Comments
 (0)