Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,3 @@
|
||||
public long post login = app.login
|
||||
|
||||
authenticated get current = app.userId
|
||||
@@ -0,0 +1,6 @@
|
||||
global type Credential {
|
||||
username: Text(1, 80) lower unique
|
||||
user_id: data.user.id
|
||||
private password_hash: Text
|
||||
active: Bool default true
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
global type Membership {
|
||||
user_id: data.user.id
|
||||
tenant_id: data.tenant.id
|
||||
active: Bool default true
|
||||
}
|
||||
|
||||
unique(user_id, tenant_id)
|
||||
@@ -0,0 +1,10 @@
|
||||
global type Session {
|
||||
uuid: Uuid generated unique
|
||||
subject: Text
|
||||
user_id: data.user.id
|
||||
tenant_id: data.tenant.id
|
||||
tenant_key: Text
|
||||
site: Text
|
||||
expires_at: DateTime
|
||||
revoked: Bool default false
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
global type Tenant {
|
||||
key: Text unique
|
||||
active: Bool default true
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
global type User {
|
||||
uuid: Uuid generated unique
|
||||
active: Bool default true
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
type Identity {
|
||||
id: Text
|
||||
user_id: data.user.id?
|
||||
tenant_id: data.tenant.id?
|
||||
}
|
||||
|
||||
flow verify(claims: dever.auth.Claims) (identity: Identity) {
|
||||
session_id = uuid.parse(claims.session)
|
||||
check session_id != null else Unauthorized
|
||||
selected_key = claims.tenant
|
||||
check selected_key != null else Unauthorized
|
||||
now = dever.time.now()
|
||||
session = data.session.first(uuid == session_id and subject == claims.subject and site == claims.site and tenant_key == selected_key and revoked == false and expires_at > now)
|
||||
check session != null else Unauthorized
|
||||
user_active = data.user.exists(id == session.user_id and active == true)
|
||||
check user_active else Unauthorized
|
||||
tenant_active = data.tenant.exists(id == session.tenant_id and key == session.tenant_key and active == true)
|
||||
check tenant_active else Unauthorized
|
||||
member_active = data.membership.exists(user_id == session.user_id and tenant_id == session.tenant_id and active == true)
|
||||
check member_active else Unauthorized
|
||||
identity = Identity { id = session.subject, user_id = session.user_id, tenant_id = session.tenant_id }
|
||||
}
|
||||
|
||||
flow userId() (user_id: data.user.id) {
|
||||
current: data.user.id? = dever.auth.userId()
|
||||
check current != null else Unauthorized
|
||||
user_id = current
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
flow login(username: Text, password: Secret, tenant: Text) (user_id: data.user.id) {
|
||||
normalized = text.lower(text.trim(username))
|
||||
credential = data.credential.first(username == normalized and active == true)
|
||||
check credential != null else Unauthorized
|
||||
valid = blocking(dever.crypto.passwordVerify(password, credential.password_hash))
|
||||
check valid else Unauthorized
|
||||
site = dever.site.key()
|
||||
session = app.establishLogin(credential.id, credential.password_hash, tenant, site)
|
||||
dever.auth.issueCookie(session.subject, uuid.toText(session.uuid), session.tenant_key)
|
||||
user_id = session.user_id
|
||||
}
|
||||
|
||||
transaction flow establishLogin(credential_id: data.credential.id, verified_hash: Text, selected_key: Text, site_key: Text) (session: data.session.Session) {
|
||||
credential = data.credential.first(id == credential_id and password_hash == verified_hash and active == true)
|
||||
check credential != null else Unauthorized
|
||||
user = data.user.first(id == credential.user_id and active == true)
|
||||
check user != null else Unauthorized
|
||||
tenant = data.tenant.first(key == selected_key and active == true)
|
||||
check tenant != null else Unauthorized
|
||||
member_active = data.membership.exists(user_id == user.id and tenant_id == tenant.id and active == true)
|
||||
check member_active else Unauthorized
|
||||
now = dever.time.now()
|
||||
session = data.session.create({
|
||||
subject = uuid.toText(user.uuid)
|
||||
user_id = user.id
|
||||
tenant_id = tenant.id
|
||||
tenant_key = tenant.key
|
||||
site = site_key
|
||||
expires_at = dever.time.add(now, dever.time.duration(3600000))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
post create = app.create
|
||||
|
||||
post review = app.review
|
||||
@@ -0,0 +1,10 @@
|
||||
type Status {
|
||||
Pending
|
||||
Approved
|
||||
Rejected
|
||||
}
|
||||
|
||||
type Request {
|
||||
title: Text(1, 120)
|
||||
status: Status default Pending
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
flow create(title: Text) (request: data.request.Request) {
|
||||
request = data.request.create({ title = title })
|
||||
}
|
||||
|
||||
transaction flow review(request_id: data.request.id, approve: Bool) () {
|
||||
chosen = app.decision(approve)
|
||||
changed = data.request.update({ where = id == request_id and status == Pending }, { status = chosen })
|
||||
check changed == 1 else Conflict
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
rule decision(approve: Bool) (status: data.request.Status)
|
||||
approve {
|
||||
true => Approved
|
||||
false => Rejected
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
rest data.category
|
||||
@@ -0,0 +1,3 @@
|
||||
get list = app.list
|
||||
|
||||
get titles = app.titles
|
||||
@@ -0,0 +1,3 @@
|
||||
type Category {
|
||||
name: Text(1, 80) unique
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
type Item {
|
||||
title: Text(1, 120)
|
||||
category_id: data.category.id?
|
||||
private supplier_cost: Decimal(12, 2)
|
||||
}
|
||||
|
||||
sql titles(limit: Int >= 1 and <= 100) (rows: List<app.Title>) {
|
||||
sqlite = "SELECT title FROM demo_catalog_item ORDER BY id LIMIT ?1"
|
||||
postgres = "SELECT title FROM demo_catalog_item ORDER BY id LIMIT $1"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
type Title {
|
||||
title: Text
|
||||
}
|
||||
|
||||
type Item {
|
||||
id: data.item.id
|
||||
title: Text
|
||||
category: Text?
|
||||
}
|
||||
|
||||
type Page {
|
||||
items: List<Item>
|
||||
total: Int
|
||||
}
|
||||
|
||||
type Error {
|
||||
error CategoryNotLoaded
|
||||
}
|
||||
|
||||
flow list(page: Int >= 1) (result: Page) {
|
||||
found = data.item.page(order = id.asc, with = [category], page = page, size = 20)
|
||||
items = app.summaries(found.items)
|
||||
result = Page { items = items, total = found.total }
|
||||
}
|
||||
|
||||
flow titles(limit: Int >= 1 and <= 100) (rows: List<Title>) {
|
||||
rows = data.item.titles(limit)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
loop summaries(rows: List<data.item.Item>) (items: List<Item>)
|
||||
row in rows {
|
||||
list Item { id = row.id, title = row.title, category = app.loadedName(row.category) }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
rule loadedName(value: Related<data.category.Category?>) (name: Text?)
|
||||
value {
|
||||
Loaded(category) => app.categoryName(category)
|
||||
Unloaded => fail(Error.CategoryNotLoaded)
|
||||
}
|
||||
|
||||
rule categoryName(value: data.category.Category?) (name: Text?)
|
||||
value {
|
||||
null => null
|
||||
other => value.name
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
post submit = app.submit
|
||||
@@ -0,0 +1,3 @@
|
||||
database default
|
||||
|
||||
job importRows(input: app.Request) retry(1) timeout(30000) = app.importRows
|
||||
@@ -0,0 +1,4 @@
|
||||
type Item {
|
||||
code: Text(1, 40) unique
|
||||
title: Text(1, 120)
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
type Row {
|
||||
code: Text
|
||||
title: Text
|
||||
}
|
||||
|
||||
type Request {
|
||||
key: Text
|
||||
rows: List<Row>
|
||||
}
|
||||
|
||||
transaction flow submit(input: Request) (task_id: Id) {
|
||||
task_id = enqueue(job.importRows, input, input.key)
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
loop importRows(input: Request) ()
|
||||
row in input.rows {
|
||||
do data.item.create({ code = row.code, title = row.title })
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
long post pay = app.pay
|
||||
@@ -0,0 +1,3 @@
|
||||
database default
|
||||
|
||||
long job notifyPaid(input: app.Notice) retry(3) timeout(30000) = app.notifyPaid
|
||||
@@ -0,0 +1,12 @@
|
||||
type Status {
|
||||
Pending
|
||||
Paid
|
||||
}
|
||||
|
||||
type Order {
|
||||
uuid: Uuid generated unique
|
||||
private owner owner_id: demo.account.data.user.id = dever.auth.userId()
|
||||
amount: Decimal(12, 2)
|
||||
status: Status default Pending
|
||||
receipt: Text?
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
type Notice {
|
||||
key: Uuid
|
||||
receipt: Text
|
||||
}
|
||||
|
||||
flow pay(order_id: data.id) () {
|
||||
user_id = demo.account.userId()
|
||||
order = data.first(id == order_id and owner_id == user_id and status == Pending)
|
||||
check order != null else Conflict
|
||||
receipt = port.pay(order.uuid, order.amount)
|
||||
app.markPaid(order, receipt)
|
||||
}
|
||||
|
||||
transaction flow markPaid(order: data.Order, receipt: Text) () {
|
||||
user_id = demo.account.userId()
|
||||
changed = data.update({ where = id == order.id and owner_id == user_id and status == Pending and amount == order.amount }, { status = Paid, receipt = receipt })
|
||||
check changed == 1 else Conflict
|
||||
enqueue(job.notifyPaid, Notice { key = order.uuid, receipt = receipt }, uuid.toText(order.uuid))
|
||||
}
|
||||
|
||||
flow notifyPaid(input: Notice) () {
|
||||
port.notify(input.key, input.receipt)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pay(key: Uuid, amount: Decimal) (receipt: Text) fails dever.process.Error
|
||||
|
||||
notify(key: Uuid, receipt: Text) () fails dever.process.Error
|
||||
@@ -0,0 +1,9 @@
|
||||
flow checkOne(value: Text, kept: List<Text>) (selected: List<Text>) {
|
||||
accepted = port.accept(value)
|
||||
selected = app.keep(accepted, value, kept)
|
||||
}
|
||||
|
||||
flow findOne(value: Text) (acc: Text?, stop: Bool) {
|
||||
stop = port.accept(value)
|
||||
acc = app.foundValue(stop, value)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
loop select(values: List<Text>) (selected: List<Text>)
|
||||
value in values {
|
||||
fold(kept = [], app.checkOne(value, kept))
|
||||
}
|
||||
|
||||
loop find(values: List<Text>) (found: Text?)
|
||||
value in values {
|
||||
foldUntil(previous = null, app.findOne(value))
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
accept(value: Text) (accepted: Bool) fails dever.process.Error
|
||||
@@ -0,0 +1,11 @@
|
||||
rule keep(accepted: Bool, value: Text, kept: List<Text>) (selected: List<Text>)
|
||||
accepted {
|
||||
true => append(kept, value)
|
||||
false => kept
|
||||
}
|
||||
|
||||
rule foundValue(accepted: Bool, value: Text) (found: Text?)
|
||||
accepted {
|
||||
true => value
|
||||
false => null
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
public post names = app.names
|
||||
@@ -0,0 +1,33 @@
|
||||
type Node {
|
||||
id: Int
|
||||
name: Text
|
||||
children: List<Int>
|
||||
}
|
||||
|
||||
type Cursor {
|
||||
nodes: Map<Int, Node>
|
||||
pending: List<Int>
|
||||
position: Int
|
||||
seen: Map<Int, Bool>
|
||||
}
|
||||
|
||||
start(nodes: List<Node>, root: Int) (cursor: Cursor) {
|
||||
cursor = Cursor { nodes = app.indexNodes(nodes), pending = [root], position = 0, seen = put({}, root, true) }
|
||||
}
|
||||
|
||||
nextNode(cursor: Cursor) (step: Walk<Node, Cursor>) {
|
||||
step = app.visit(get(cursor.pending, cursor.position), cursor)
|
||||
}
|
||||
|
||||
expand(node_id: Int, cursor: Cursor) (step: Walk<Node, Cursor>) {
|
||||
node = get(cursor.nodes, node_id)
|
||||
check node != null else Invalid
|
||||
next = cursor
|
||||
next.position = next.position + 1
|
||||
next = app.enqueueChildren(node.children, next)
|
||||
step = Next(node, next)
|
||||
}
|
||||
|
||||
enqueueChild(child: Int, cursor: Cursor) (next: Cursor) {
|
||||
next = app.enqueue(get(cursor.seen, child) != null, child, cursor)
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
loop names(nodes: List<Node>, root: Int) (names: List<Text>)
|
||||
node in walk(app.start(nodes, root), app.nextNode) {
|
||||
list node.name
|
||||
}
|
||||
|
||||
loop indexNodes(nodes: List<Node>) (indexed: Map<Int, Node>)
|
||||
node in nodes {
|
||||
index node.id
|
||||
}
|
||||
|
||||
loop enqueueChildren(children: List<Int>, cursor: Cursor) (next: Cursor)
|
||||
child in children {
|
||||
fold(queue = cursor, app.enqueueChild(child, queue))
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
rule visit(node_id: Int?, cursor: Cursor) (step: Walk<Node, Cursor>)
|
||||
node_id {
|
||||
null => Done
|
||||
other => app.expand(node_id, cursor)
|
||||
}
|
||||
|
||||
rule enqueue(seen: Bool, child: Int, cursor: Cursor) (next: Cursor)
|
||||
seen {
|
||||
true => cursor
|
||||
false => Cursor {
|
||||
nodes = cursor.nodes
|
||||
pending = append(cursor.pending, child)
|
||||
position = cursor.position
|
||||
seen = put(cursor.seen, child, true)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user