Created
June 29, 2023 13:25
-
-
Save ChristianSch/b396ca06078fde7a9f66b7e5eeb4bd10 to your computer and use it in GitHub Desktop.
workspaces.ts
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
import { | |
Namespace, | |
Context, | |
} from "@ory/permission-namespace-types"; | |
class User implements Namespace {} | |
class Workspace implements Namespace { | |
related: { | |
owners: User[]; | |
users: User[]; | |
} | |
permits = { | |
// all users that are owners or users of this workspace can access it | |
view: (ctx: Context): boolean => | |
this.related.users.includes(ctx.subject) || | |
this.related.owners.includes(ctx.subject), | |
} | |
} | |
class Unit implements Namespace { | |
related: { | |
// the unit is only owned by one workspace, but all relations are many-to-many in keto | |
workspaces: Workspace[]; | |
users: User[]; | |
} | |
permits = { | |
// all workspace owners and users can view this unit | |
view: (ctx: Context): boolean => | |
this.related.workspaces.traverse((w) => | |
w.related.owners.includes(ctx.subject) | |
) || this.related.users.includes(ctx.subject), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment