From 1c78eb4a09edaa0439a487a5ba6981867ef5d01f Mon Sep 17 00:00:00 2001 From: 728 Date: Sat, 25 Nov 2023 10:59:20 +0800 Subject: [PATCH] =?UTF-8?q?pre=20=E5=88=9B=E5=BB=BA=E6=93=8D=E4=BD=9C=20#4?= =?UTF-8?q?5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 4 ++-- src/pra/can.ts | 42 +++++++++++++++++++++++++++++++++-- src/pra/pos.ts | 7 ++++-- src/pra/pre.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 src/pra/pre.ts diff --git a/readme.md b/readme.md index 3996e68..12c87d5 100644 --- a/readme.md +++ b/readme.md @@ -18,8 +18,8 @@ - `usr.ts` 用户数据操作 `user` - `soc.ts` 俱乐部数据操作 `sociation` `social` - `agd.ts` 活动数据操作 `agenda` - - `rec.ts` 记录数据操作 `record` - - `msg.ts` 文章数据操作 `message` + - `rec.ts` 记录数据操作 `record` - `credit` `debit` `earn` + - `msg.ts` 文章数据操作 `message` - `wsl` `lit` - `aut.ts` 权限数据操作 `author` `authority` * `pra` 业务操作 `praxic` - `can.ts` 操作权限 diff --git a/src/pra/can.ts b/src/pra/can.ts index 410c2d1..3d094c8 100644 --- a/src/pra/can.ts +++ b/src/pra/can.ts @@ -1,9 +1,31 @@ -import { is_id, is_lim, is_nbr, lim_code } from "../eid/is.ts"; -import type { Psg } from "./pas.ts" +import type { Pas, Psg } from "./pas.ts" +import type { Soc } from "../eid/typ.ts" +import type { Pre } from "./pre.ts" +import { is_cdt, is_id, is_in, is_lim, is_nam, is_nbr, is_rec, lim_code } from "../eid/is.ts" +import { is_adm } from "../ont/adm.ts" // deno-lint-ignore no-explicit-any export type Ret any> = Awaited> +export function is_aut( + pas: Pas, + aut?: Pas["aut"][0], +): boolean { + return aut ? pas.aut.includes(aut) : pas.aut.length > 0 +} +export function is_sec( + pas: Pas, + soc?: Soc["_id"], +): boolean { + return soc ? is_in(pas.sec, soc) : pas.sec.length > 0 +} +export function is_usr( + pas: Pas, + soc?: Soc["_id"], +): boolean { + return soc ? pas.cdt.some(c => c._id.soc == soc) : pas.cdt.length > 0 +} + export function is_psg( p: Psg ): boolean { @@ -15,3 +37,19 @@ export function is_psg( } return false } + +export function is_pre( + pas: Pas, + p: Pre, +): boolean { + switch (p.pre) { + case "usr": return (is_aut(pas) || is_sec(pas)) && is_nbr(p.nbr) && is_adm(p.adm1, p.adm2) + case "soc": return is_aut(pas, "aut") && is_nam(p.nam) && is_adm(p.adm1, p.adm2) + case "agd": return is_sec(pas, p.soc) && is_nam(p.nam) + case "cdt": return is_sec(pas, p.cdt._id.soc) && is_cdt(p.cdt) && p.cdt.sec == pas.usr + case "dbt": return is_usr(pas, p.dbt._id.soc) && p.dbt._id.usr == pas.usr && is_rec(p.dbt) + case "ern": return is_sec(pas, p.ern._id.soc) && is_rec(p.ern) && p.ern.sec == pas.usr + case "wsl": case "lit": return is_aut(pas, p.pre) && is_nam(p.nam) + } + return false +} diff --git a/src/pra/pos.ts b/src/pra/pos.ts index a419081..993e318 100644 --- a/src/pra/pos.ts +++ b/src/pra/pos.ts @@ -1,7 +1,8 @@ -import { Ret } from "./can.ts" +import type { Ret } from "./can.ts" import { Psg, pas, psg } from "./pas.ts" +import { Pre, pre } from "./pre.ts" -export type Pos = Psg +export type Pos = Psg | Pre export type PosRet = Ret export async function pos( @@ -15,6 +16,8 @@ export async function pos( const p = jwt ? await pas(jwt) : null if ("psg" in json) return psg(p, json) + else if (!p) return { ret: null } + else if ("pre" in json) return pre(p, json) return { ret: null } } diff --git a/src/pra/pre.ts b/src/pra/pre.ts new file mode 100644 index 0000000..d964473 --- /dev/null +++ b/src/pra/pre.ts @@ -0,0 +1,59 @@ +import type { Agd, Cdt, Dbt, Ern, Lit, Soc, Usr, Wsl } from "../eid/typ.ts" +import type { Pas } from "./pas.ts" +import { agd_c } from "../eid/agd.ts" +import { soc_c, soc_r } from "../eid/soc.ts" +import { usr_c } from "../eid/usr.ts" +import { Ret, is_pre } from "./can.ts" + +export type Pre = { + pre: "usr", + nbr: NonNullable, + adm1: Usr["adm1"], + adm2: Usr["adm2"], +} | { + pre: "soc", + nam: Soc["nam"], + adm1: Soc["adm1"], + adm2: Soc["adm2"], +} | { + pre: "agd", + nam: Agd["nam"], + soc: Agd["soc"], +} | { + pre: "cdt", + cdt: Cdt, +} | { + pre: "dbt", + dbt: Dbt, +} | { + pre: "ern", + ern: Ern, +} | { + pre: "wsl", + nam: Wsl["nam"], +} | { + pre: "lit", + nam: Lit["nam"], +} + +export type PreRet = { + usr: Ret, + soc: Ret, + agd: Ret, +} + +export async function pre( + pas: Pas, + p: Pre, +) { + if (!is_pre(pas, p)) return null + switch (p.pre) { + case "usr": return usr_c(p.nbr, p.adm1, p.adm2) + case "soc": return soc_c(p.nam, p.adm1, p.adm2) + case "agd": { + const s = await soc_r(p.soc, { adm1: 1, adm2: 1 }) + return s ? agd_c(p.nam, s.adm1, s.adm2, p.soc) : null + } + } + return null +}