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
98 when Some pwd = Helm_registry.get_opt Helm_registry.string (password_key pid) -> ()
99 | _ -> raise (Access_denied (string_of_permission for_what, pid))
101 let create ?id ?clone ?clone_password ?(read_perm=true) ?(write_perm=true) ?(admin_perm=true) ?password () =
104 None -> create_fresh_id ()
105 | Some pid when Helm_registry.has (read_permission_key pid) -> raise (Profile_exists pid)
112 check_permission pid clone_password `Read ;
113 to_list_rel ~prefix:(params_key pid) ()
115 List.iter (fun (key, value) -> Helm_registry.set_string (params_key pid ^ "." ^ key) value) params ;
116 Helm_registry.set_bool (read_permission_key pid) read_perm ;
117 Helm_registry.set_bool (write_permission_key pid) write_perm ;
118 Helm_registry.set_bool (admin_permission_key pid) admin_perm ;
119 Helm_registry.set_opt Helm_registry.of_string (password_key pid) password ;
122 let remove pid ?password () =
123 check_permission pid password `Admin ;
124 List.iter (fun (key, _) -> Helm_registry.unset key) (Helm_registry.to_list ~prefix:(profile_key pid) ())
126 let get_params pid ?password () =
127 check_permission pid password `Read ;
128 to_list_rel ~prefix:(params_key pid) ()
130 let set_param pid ?password ~key ~value () =
131 check_permission pid password `Write ;
133 Some value' -> Helm_registry.set (param_key pid key) value'
134 | None -> Helm_registry.unset (param_key pid key)
136 let get_param pid ?password ~key () =
137 check_permission pid password `Read ;
138 Helm_registry.get (param_key pid key)
140 let set_password pid ?old_password new_password =
141 check_permission pid old_password `Admin ;
142 Helm_registry.set_opt Helm_registry.of_string (password_key pid) new_password
144 let set_permission pid ?password for_what value =
145 check_permission pid password `Admin ;
146 Helm_registry.set_bool (permission_key for_what pid) value
148 let get_permission pid ?password for_what =
149 check_permission pid password `Admin ;
150 Helm_registry.get_bool (permission_key for_what pid)