dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

wiki_acl.ts (1556B)


      1 import { modelBehaviour } from '@/lib/model_helpers'
      2 import { ACL } from '@/lib/security/acl'
      3 import { RowDataPacket } from 'mysql2'
      4 
      5 const SQL_GET_WIKI_AND_PAGE_ACL = `
      6     select w.id       as wiki_id,
      7            w.acl_data as wiki_acl,
      8            p.id       as page_id,
      9            p.acl_data as page_acl
     10     from wikis w
     11          left join wiki_pages p
     12                    on p.wiki_id = w.id
     13                        and p.path = ?
     14     where w.slug = ?
     15 `
     16 
     17 export const getWikiAndPageACLViaPath = modelBehaviour<
     18   [slug: string, path: string],
     19   { wikiId: number, wiki?: ACL, pageId?: number, page?: ACL } | null
     20 >(async (conn, args) => {
     21   const [rows] = await conn.query<RowDataPacket[]>({
     22     sql: SQL_GET_WIKI_AND_PAGE_ACL,
     23   }, [args[1], args[0]])
     24 
     25   if (rows.length === 0) {
     26     return null
     27   }
     28 
     29   const row = rows[0]
     30   return {
     31     wikiId: row[0],
     32     wiki: row[1],
     33     pageId: row[2],
     34     page: row[3],
     35   }
     36 })
     37 
     38 const SQL_GET_WIKI_AND_PAGE_ACL_VIA_PAGE_ID = `
     39     select w.id, w.acl_data, p.id, p.acl_data
     40     from wiki_pages p
     41          inner join wikis w on p.wiki_id = w.id
     42     where p.id = ?
     43 `
     44 
     45 export const getWikiAndPageACLViaPageId = modelBehaviour<
     46   [pageId: number],
     47   { wikiId: number, wiki?: ACL, pageId?: number, page?: ACL } | null
     48 >(async (conn, args) => {
     49   const [rows] = await conn.query<RowDataPacket[]>({
     50     sql: SQL_GET_WIKI_AND_PAGE_ACL_VIA_PAGE_ID,
     51   }, args)
     52 
     53   if (rows.length === 0) {
     54     return null
     55   }
     56 
     57   const row = rows[0]
     58   return {
     59     wikiId: row[0],
     60     wiki: row[1],
     61     pageId: row[2],
     62     page: row[3],
     63   }
     64 })