]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitaAuthentication.ml
Multi-user matita: changed the status object to include a ``user'' method
[helm.git] / matitaB / matita / matitaAuthentication.ml
1 (* Copyright (C) 2004-2011, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 type session_id = Uuidm.t
27
28 (* user table: user id, (password, optional session id) *)
29 type user = string * (string * session_id option)
30
31 let user_tbl = (ref [] : user list ref)
32
33 (* session table: (user id, session id), matita status *)
34 type session = (string * session_id) * MatitaEngine.status 
35
36 let session_tbl = (ref [] : session list ref)
37
38 exception UsernameCollision of string
39
40 let lookup_user uid = List.assoc uid !user_tbl
41
42 let create_session uid status =
43   let pw,sid = List.assoc uid !user_tbl in
44   let clean_utbl = List.remove_assoc uid !user_tbl in
45   let new_session = Uuidm.create `V4 in
46   user_tbl := (uid,(pw,Some new_session))::clean_utbl;
47   let clean_stbl = match sid with
48     | Some sid' -> 
49        List.remove_assoc (uid,sid') !session_tbl
50     | _ -> !session_tbl
51   in session_tbl := ((uid,new_session),status)::clean_stbl
52 ;;
53
54 let logout_user uid =
55   match List.assoc uid !user_tbl with
56   | _,None -> ()
57   | pw, Some sid ->
58      user_tbl := (uid,(pw,None))::List.remove_assoc uid !user_tbl;
59      session_tbl := List.remove_assoc (uid,sid) !session_tbl
60 ;;
61
62 let remove_user uid =
63   user_tbl := List.remove_assoc uid !user_tbl
64 ;;
65
66 (* serialization and deserialization of the user table *)
67 let config_path () =
68   let path = Helm_registry.get "matita.basedir" in
69   let dirname = Filename.dirname path in
70   HExtlib.mkdir dirname;
71   path
72 ;;
73
74 let serialize () =
75   let clean_utbl = List.map (fun (uid,(pw,_)) -> uid,(pw,None)) !user_tbl in
76   let utbl_ch = open_out (config_path () ^ "/usertable.dump") in
77   Marshal.to_channel utbl_ch clean_utbl [];
78   close_out utbl_ch;
79 ;;
80
81 let deserialize () =
82   let utbl_ch = open_in (config_path () ^ "/usertable.dump") in
83   user_tbl := Marshal.from_channel utbl_ch;
84   close_in utbl_ch;
85   (* old_sessions are now invalid *)
86   session_tbl := [];
87 ;;
88
89 let add_user uid pw =
90   try
91     let _ = lookup_user uid in
92     raise (UsernameCollision uid)
93   with Not_found -> 
94     user_tbl := (uid,(pw,None))::!user_tbl;
95     serialize ()
96 ;;