3 * Stefano Zacchiroli <zack@cs.unibo.it>
4 * for the HELM Team http://helm.cs.unibo.it/
6 * This file is part of HELM, an Hypertextual, Electronic
7 * Library of Mathematics, developed at the Computer Science
8 * Department, University of Bologna, Italy.
10 * HELM is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * HELM is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with HELM; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * For details, see the HELM World-Wide-Web page,
26 * http://helm.cs.unibo.it/
29 type profile_id = string
31 let profiles = "profiles"
36 let string_of_permission =
42 let permission_key for_what pid =
43 profile_key pid ^ ".permissions." ^ (string_of_permission for_what)
45 let read_permission_key = permission_key `Read
46 let write_permission_key = permission_key `Write
47 let admin_permission_key = permission_key `Admin
49 let password_key pid =
50 profile_key pid ^ ".password"
53 profile_key pid ^ ".params"
55 let param_key pid param =
56 params_key pid ^ "." ^ param
58 exception Access_denied of string * profile_id
59 exception Profile_exists of profile_id
64 if n >= 0 && n < 10 then
66 else if n >= 10 && n < 16 then
67 String.make 1 (Char.chr (n - 10 + (Char.code 'A')))
74 | n -> aux ((hex_of_nibble (Random.int 16)) ^ s) (n - 1)
80 let key = new_key () in
81 if Helm_registry.has (read_permission_key key) then
88 let list () = fst (Helm_registry.ls profiles)
90 let to_list_rel ~prefix () =
91 List.map (fun (key,value) -> Pcre.replace ~pat:("^" ^ Pcre.quote (prefix ^ ".")) key, value)
92 (Helm_registry.to_list ~prefix ())
94 let check_permission pid password for_what =
95 match password, Helm_registry.get_bool (permission_key for_what pid) with
97 | Some pwd, false when Some pwd = Helm_registry.get_opt Helm_registry.get (password_key pid) -> ()
98 | _ -> raise (Access_denied (string_of_permission for_what, pid))
100 let create ?id ?clone ?clone_password ?(read_perm=true) ?(write_perm=true) ?(admin_perm=true) ?password () =
103 None -> create_fresh_id ()
104 | Some pid when Helm_registry.has (read_permission_key pid) -> raise (Profile_exists pid)
111 check_permission pid clone_password `Read ;
112 to_list_rel ~prefix:(params_key pid) ()
114 List.iter (fun (key, value) -> Helm_registry.set_string (params_key pid ^ "." ^ key) value) params ;
115 Helm_registry.set_bool (read_permission_key pid) read_perm ;
116 Helm_registry.set_bool (write_permission_key pid) write_perm ;
117 Helm_registry.set_bool (admin_permission_key pid) admin_perm ;
118 Helm_registry.set_opt Helm_registry.set_string (password_key pid) password ;
121 let remove pid ?password () =
122 check_permission pid password `Admin ;
123 List.iter (fun (key, _) -> Helm_registry.unset key) (Helm_registry.to_list ~prefix:(profile_key pid) ())
125 let get_params pid ?password () =
126 check_permission pid password `Read ;
127 to_list_rel ~prefix:(params_key pid) ()
129 let set_param pid ?password ~key ~value () =
130 check_permission pid password `Write ;
132 Some value' -> Helm_registry.set (param_key pid key) value'
133 | None -> Helm_registry.unset (param_key pid key)
135 let get_param pid ?password ~key () =
136 check_permission pid password `Read ;
137 Helm_registry.get (param_key pid key)
139 let set_password pid ?old_password new_password =
140 check_permission pid old_password `Admin ;
141 Helm_registry.set_opt Helm_registry.set (password_key pid) new_password
143 let set_permission pid ?password for_what value =
144 check_permission pid password `Admin ;
145 Helm_registry.set_bool (permission_key for_what pid) value
147 let get_permission pid ?password for_what =
148 check_permission pid password `Admin ;
149 Helm_registry.get_bool (permission_key for_what pid)