]> git.plutz.net Git - shellwiki/blob - acl.sh
event macro stubs
[shellwiki] / acl.sh
1 #!/bin/sh
2
3 [ "$include_acl" ] && return 0
4 include_acl="$0"
5
6 # Copyright 2022 - 2023 Paul Hänsch
7
8 # Permission to use, copy, modify, and/or distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
11
12 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
15 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
18 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 # ACL_OVERRIDE="${ACL_OVERRIDE:-Admin:read,write}"
21 ACL_DEFAULT="${ACL_DEFAULT:-Known:read,write${BR}All:read}"
22
23 acl_cachepath=''
24 acl_collection=''
25
26 acl_collect(){
27   local path="$1"
28   # Get directory part of PATH_INFO
29   local path="${path%/*}/./"
30   local pagefile head acl
31
32   printf '%s\n' "$ACL_OVERRIDE"
33
34   while :; do
35     [ "$path" = / ] && break
36     path="${path%/*/}/"
37
38     # Do not use `mdfile` function here because of specialties
39     # in translation handler (`handlers/10_translations.sh`)
40     if   [ -f "$_DATA/pages/$path/#page.md" ]; then
41       pagefile="$_DATA/pages/$path/#page.md"
42     elif [ -f "$_EXEC/pages/$path/#page.md" ]; then
43       pagefile="$_EXEC/pages/$path/#page.md"
44     else
45       continue
46     fi
47
48     acl="$(sed -En '
49       s;\r$;;;
50       /^%acl([\t ]+.*)?$/bACL;
51       20q;
52       b;
53
54       :ACL
55       s;(%(acl)?)?[\t ]*;;
56       p; n; s;\r$;;;
57       /^(%[ \t]+|%acl[ \t]+|[ \t]+)[^ \t\r]+$/bACL;
58       /^(%[ \t]*|%acl[ \t]*)$/bACL;
59     ' <"$pagefile")"
60
61     printf %s\\n "${acl}"
62   done
63
64   printf '%s\n' "$ACL_DEFAULT"
65 }
66
67 acl_read(){
68   local page="${1:-${PATH_INFO}}"
69   local acl
70
71   if [ "$acl_cachepath" != "$page" ]; then
72     acl_cachepath="$page"
73     acl_collection="$(acl_collect "$page")"
74   fi
75
76   while read -r acl; do
77     case ${acl##*:} in
78       read|*,read,*|read,*|*,read)
79          acl="${acl%%:*}:read";;
80       *) acl="${acl%%:*}:";;
81     esac
82     [ "$USER_NAME" ] && case $acl in
83        "Known:read") return 0;;
84        "Known:")     return 1;;
85       "+Known:read") return 0;;
86       "-Known:read") return 1;;
87        "@${USER_NAME}:read") return 0;;
88        "@${USER_NAME}:")      return 1;;
89       "+@{$USER_NAME}:read") return 0;;
90       "-@{$USER_NAME}:read") return 1;;
91     esac
92     case $acl in
93        "All:read") return 0;;
94        "All:")     return 1;;
95       "+All:read") return 0;;
96       "-All:read") return 1;;
97     esac
98    done <<-EOF
99         ${acl_collection}
100         EOF
101   return 1
102 }
103
104 acl_write(){
105   local page="${1:-${PATH_INFO}}"
106   local acl
107
108   if [ "$acl_cachepath" != "$page" ]; then
109     acl_cachepath="$page"
110     acl_collection="$(acl_collect "$page")"
111   fi
112
113   while read -r acl; do
114     case ${acl##*:} in
115       write|*,write,*|write,*|*,write)
116          acl="${acl%%:*}:write";;
117       *) acl="${acl%%:*}:";;
118     esac
119     [ "$USER_NAME" ] && case ${acl} in
120        "Known:write") return 0;;
121        "Known:")      return 1;;
122       "+Known:write") return 0;;
123       "-Known:write") return 1;;
124        "@${USER_NAME}:write") return 0;;
125        "@${USER_NAME}:")      return 1;;
126       "+@{$USER_NAME}:write") return 0;;
127       "-@{$USER_NAME}:write") return 1;;
128     esac
129     case $acl in
130        "All:write") return 0;;
131        "All:")      return 1;;
132       "+All:write") return 0;;
133       "-All:write") return 1;;
134     esac
135   done <<-EOF
136         ${acl_collection}
137         EOF
138   return 1
139 }