Preliminary regeneration support
[hcoop/domtool2.git] / src / acl.sml
1 (* HCoop Domtool (http://hcoop.sourceforge.net/)
2 * Copyright (c) 2006, Adam Chlipala
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 *)
18
19 (* Per-user access control lists for resources various *)
20
21 structure Acl :> ACL = struct
22
23 type acl = {user : string,
24 class : string,
25 value : string}
26
27 structure SM = DataStructures.StringMap
28 structure SS = DataStructures.StringSet
29
30 val acl : SS.set SM.map SM.map ref = ref SM.empty
31
32 fun query {user, class, value} =
33 case SM.find (!acl, user) of
34 NONE => false
35 | SOME classes =>
36 case SM.find (classes, class) of
37 NONE => false
38 | SOME values => SS.member (values, value)
39
40 fun queryAll user =
41 case SM.find (!acl, user) of
42 NONE => []
43 | SOME classes => SM.foldri (fn (class, values, out) =>
44 (class, SS.foldr (op::) [] values) :: out)
45 [] classes
46
47 fun users () = SM.foldri (fn (user, _, ls) => user :: ls) [] (!acl)
48
49 fun whoHas {class, value} =
50 SM.foldri (fn (user, classes, users) =>
51 case SM.find (classes, class) of
52 NONE => users
53 | SOME values =>
54 if SS.member (values, value) then
55 user :: users
56 else
57 users) [] (!acl)
58
59 fun class {user, class} =
60 case SM.find (!acl, user) of
61 NONE => SS.empty
62 | SOME classes =>
63 case SM.find (classes, class) of
64 NONE => SS.empty
65 | SOME values => values
66
67 fun grant {user, class, value} =
68 let
69 val classes = Option.getOpt (SM.find (!acl, user), SM.empty)
70 val values = Option.getOpt (SM.find (classes, class), SS.empty)
71 in
72 acl := SM.insert (!acl, user,
73 SM.insert (classes, class,
74 SS.add (values, value)))
75 end
76
77 fun revoke {user, class, value} =
78 let
79 val classes = Option.getOpt (SM.find (!acl, user), SM.empty)
80 val values = Option.getOpt (SM.find (classes, class), SS.empty)
81
82 val values = if SS.member (values, value) then
83 SS.delete (values, value)
84 else
85 values
86 in
87 acl := SM.insert (!acl, user,
88 SM.insert (classes, class,
89 values))
90 end
91
92 fun read fname =
93 let
94 val inf = TextIO.openIn fname
95
96 fun users usrs =
97 case TextIO.inputLine inf of
98 NONE => usrs
99 | SOME line =>
100 case String.tokens Char.isSpace line of
101 [user] =>
102 let
103 fun classes clss =
104 case TextIO.inputLine inf of
105 NONE => clss
106 | SOME line =>
107 case String.tokens Char.isSpace line of
108 [] => clss
109 | class :: values =>
110 classes (SM.insert (clss, class,
111 foldl SS.add' SS.empty values))
112 in
113 users (SM.insert (usrs, user, classes SM.empty))
114 end
115 | _ => raise Fail "Unexpected ACL file format"
116 in
117 acl := users SM.empty
118 before TextIO.closeIn inf
119 end
120
121 fun write fname =
122 let
123 val outf = TextIO.openOut fname
124
125 val writeValues = SS.app (fn value =>
126 (TextIO.output (outf, " ");
127 TextIO.output (outf, value)))
128
129 val writeClasses = SM.appi (fn (class, values) =>
130 if SS.isEmpty values then
131 ()
132 else
133 (TextIO.output (outf, class);
134 writeValues values;
135 TextIO.output (outf, "\n")))
136
137 val writeUsers = SM.appi (fn (user, classes) =>
138 if SM.numItems classes = 0 then
139 ()
140 else
141 (TextIO.output (outf, user);
142 TextIO.output (outf, "\n");
143 writeClasses classes;
144 TextIO.output (outf, "\n")))
145 in
146 writeUsers (!acl);
147 TextIO.closeOut outf
148 end
149
150 end