1 (* HCoop
Domtool (http
://hcoop
.sourceforge
.net
/)
2 * Copyright (c
) 2006, Adam Chlipala
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
.
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
.
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
.
19 (* Per
-user access control lists for resources various
*)
21 structure Acl
:> ACL
= struct
23 type acl
= {user
: string,
27 structure SM
= DataStructures
.StringMap
28 structure SS
= DataStructures
.StringSet
30 val acl
: SS
.set SM
.map SM
.map ref
= ref SM
.empty
32 fun query
{user
, class
, value
} =
33 case SM
.find (!acl
, user
) of
36 case SM
.find (classes
, class
) of
38 | SOME values
=> SS
.member (values
, value
)
41 case SM
.find (!acl
, user
) of
43 | SOME classes
=> SM
.foldri (fn (class
, values
, out
) =>
44 (class
, SS
.foldr (op::) [] values
) :: out
)
47 fun class
{user
, class
} =
48 case SM
.find (!acl
, user
) of
51 case SM
.find (classes
, class
) of
53 | SOME values
=> values
55 fun grant
{user
, class
, value
} =
57 val classes
= Option
.getOpt (SM
.find (!acl
, user
), SM
.empty
)
58 val values
= Option
.getOpt (SM
.find (classes
, class
), SS
.empty
)
60 acl
:= SM
.insert (!acl
, user
,
61 SM
.insert (classes
, class
,
62 SS
.add (values
, value
)))
65 fun revoke
{user
, class
, value
} =
67 val classes
= Option
.getOpt (SM
.find (!acl
, user
), SM
.empty
)
68 val values
= Option
.getOpt (SM
.find (classes
, class
), SS
.empty
)
70 val values
= if SS
.member (values
, value
) then
71 SS
.delete (values
, value
)
75 acl
:= SM
.insert (!acl
, user
,
76 SM
.insert (classes
, class
,
82 val inf
= TextIO.openIn fname
85 case TextIO.inputLine inf
of
88 case String.tokens
Char.isSpace line
of
92 case TextIO.inputLine inf
of
95 case String.tokens
Char.isSpace line
of
98 classes (SM
.insert (clss
, class
,
99 foldl SS
.add
' SS
.empty values
))
101 users (SM
.insert (usrs
, user
, classes SM
.empty
))
103 | _
=> raise Fail
"Unexpected ACL file format"
105 acl
:= users SM
.empty
106 before TextIO.closeIn inf
111 val outf
= TextIO.openOut fname
113 val writeValues
= SS
.app (fn value
=>
114 (TextIO.output (outf
, " ");
115 TextIO.output (outf
, value
)))
117 val writeClasses
= SM
.appi (fn (class
, values
) =>
118 (TextIO.output (outf
, class
);
120 TextIO.output (outf
, "\n")))
122 val writeUsers
= SM
.appi (fn (user
, classes
) =>
123 (TextIO.output (outf
, user
);
124 TextIO.output (outf
, "\n");
125 writeClasses classes
;
126 TextIO.output (outf
, "\n")))