This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_access.ts
44 lines (37 loc) · 1.57 KB
/
_access.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import * as CNK from "@effect-ts/core/Collections/Immutable/Chunk"
import { Chunk } from "@effect-ts/core/Collections/Immutable/Chunk"
import * as O from "@effect-ts-app/core/Option"
import * as MO from "@effect-ts-app/core/Schema"
import { makeAuthorize } from "@effect-ts-app/infra/app"
import { Task, TaskList, UserId } from "@effect-ts-demo/todo-types"
import * as TaskListAccess from "@/TaskLists/_access"
export function canAccess_(lists: Chunk<TaskList>) {
const can = canAccessInternal_(lists)
return (t: Task, userId: UserId) => can(t, userId)
}
export function canAccessE_(lists: Chunk<TaskList>) {
const can = canAccessInternal_(lists)
return (t: MO.EncodedOf<typeof Task.Model>, userId: UserId) => can(t, userId)
}
function canAccessInternal_(lists: Chunk<TaskList>) {
return (
t: Pick<MO.EncodedOf<typeof Task.Model>, "createdBy" | "listId">,
userId: UserId
) =>
// TODO: probably should loose access regardless if the user created it?
t.createdBy === userId ||
(t.listId !== "inbox" &&
CNK.find_(lists, (l) => l.id === t.listId)
["|>"](O.map(TaskListAccess.canAccess(userId)))
["|>"](O.getOrElse(() => false)))
}
export function canAccess(lists: Chunk<TaskList>) {
const xs = canAccess_(lists)
return (userId: UserId) => (t: Task) => xs(t, userId)
}
export function canAccessE(lists: Chunk<TaskList>) {
const xs = canAccessE_(lists)
return (userId: UserId) => (t: MO.EncodedOf<typeof Task.Model>) => xs(t, userId)
}
export const TaskAuth = (lists: Chunk<TaskList>) =>
makeAuthorize(canAccess_(lists), "Task", (t) => t.id)