-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Document TypeScript API Co-authored-by: David Hatch <david@osohq.com> Co-authored-by: Leina McDermott <leina05@gmail.com> Co-authored-by: Sam Scott <sam@osohq.com>
- Loading branch information
1 parent
e1b5e7d
commit 01c6e9c
Showing
64 changed files
with
13,041 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const { Oso } = require('oso'); | ||
|
||
const oso = new Oso(); | ||
|
||
const EXPENSES = [ | ||
{ amount: 500, submitted_by: 'alice', location: 'NYC', project_id: 2 }, | ||
]; | ||
|
||
// expense-class-start | ||
class Expense { | ||
constructor({ amount, submitted_by, location, project_id }) { | ||
// ... | ||
this.amount = amount; | ||
this.submitted_by = submitted_by; | ||
this.location = location; | ||
this.project_id = project_id; | ||
} | ||
|
||
static id(id) { | ||
if (id < EXPENSES.length) return new Expense({ ...EXPENSES[id] }); | ||
return new Expense(); | ||
} | ||
} | ||
|
||
oso.registerClass(Expense); | ||
|
||
const MANAGERS = { | ||
cora: ['bhavik'], | ||
bhavik: ['alice'], | ||
}; | ||
|
||
// user-class-start | ||
class User { | ||
constructor(name, location) { | ||
// ... | ||
this.name = name; | ||
this.location = location || 'NYC'; | ||
} | ||
|
||
*employees() { | ||
if (MANAGERS[this.name]) { | ||
for (const name in MANAGERS[this.name]) { | ||
yield new User(name); | ||
} | ||
} | ||
} | ||
} | ||
|
||
oso.registerClass(User); | ||
|
||
class Project { | ||
constructor(id, teamId) { | ||
this.id = id; | ||
this.teamId = teamId; | ||
} | ||
|
||
static id(id) { | ||
return new Project(id, 0); | ||
} | ||
} | ||
|
||
oso.registerClass(Project); | ||
|
||
class Team { | ||
constructor(organizationId) { | ||
this.organizationId = organizationId; | ||
} | ||
|
||
static id() { | ||
return new Team(0); | ||
} | ||
} | ||
|
||
oso.registerClass(Team); | ||
|
||
class Organization { | ||
constructor(name) { | ||
this.name = name; | ||
} | ||
|
||
static id() { | ||
return new Organization('ACME'); | ||
} | ||
} | ||
|
||
oso.registerClass(Organization); | ||
|
||
module.exports = { | ||
Expense, | ||
oso, | ||
User, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const { oso, Expense, User } = require('./01-simple'); | ||
|
||
const EXPENSES_DEFAULT = { | ||
submitted_by: 'steve', | ||
location: 'NYC', | ||
amount: 50, | ||
project_id: 2, | ||
}; | ||
const sam = new User('sam'); | ||
|
||
test('01-simple', async () => { | ||
await oso.loadFile('../01-simple.polar'); | ||
const samEx = new Expense({ ...EXPENSES_DEFAULT, submitted_by: sam.name }); | ||
expect(await oso.isAllowed(sam, 'view', samEx)).toBe(true); | ||
const steveEx = new Expense({ ...EXPENSES_DEFAULT }); | ||
expect(await oso.isAllowed(sam, 'view', steveEx)).toBe(false); | ||
}); | ||
|
||
test('02-rbac', async () => { | ||
await oso.loadFile('../02-rbac.polar'); | ||
await oso.loadStr( | ||
'role(_: User { name: "sam" }, "admin", _: Project { id: 2 });' | ||
); | ||
const proj0Ex = new Expense({ ...EXPENSES_DEFAULT, project_id: 0 }); | ||
expect(await oso.isAllowed(sam, 'view', proj0Ex)).toBe(false); | ||
const proj2Ex = new Expense({ ...EXPENSES_DEFAULT }); | ||
expect(await oso.isAllowed(sam, 'view', proj2Ex)).toBe(true); | ||
}); | ||
|
||
test('03-hierarchy', async () => { | ||
await oso.loadFile('../03-hierarchy.polar'); | ||
const bhavik = new User('bhavik'); | ||
const aliceEx = new Expense({ ...EXPENSES_DEFAULT, submitted_by: 'alice' }); | ||
expect(await oso.isAllowed(bhavik, 'view', aliceEx)).toBe(true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "oso-docs-abac", | ||
"version": "0", | ||
"main": "n/a", | ||
"license": "Apache-2.0", | ||
"private": true, | ||
"dependencies": { | ||
"jest": "^26.4.2", | ||
"oso": "../../../../languages/js" | ||
} | ||
} |
Oops, something went wrong.