]> matita.cs.unibo.it Git - helm.git/blobdiff - matitaB/matita/matitaAuthentication.ml
Matitaweb: Some bugfixes concerning file flags.
[helm.git] / matitaB / matita / matitaAuthentication.ml
index 1b9a91b7160024d8debd958a1b70ee6dcd094cac..86189fddf6ec54c8a20995c2b952b7271470e73a 100644 (file)
 
 type session_id = Uuidm.t
 
+type matita_file = MatitaFilesystem.matita_flag * string
+
 (* user table: user id, (password, optional session id) *)
 type user = string * (string * session_id option)
 
 let user_tbl = (ref [] : user list ref)
 
-(* session table: (user id, session id), matita status *)
-type session = session_id * (MatitaEngine.status * MatitaEngine.status list)
+(* session table: session id, (user id, matita status, matita history *)
+type session = session_id * (string * MatitaEngine.status * MatitaEngine.status list)
 
 let session_tbl = (ref [] : session list ref)
 
@@ -39,6 +41,8 @@ exception UsernameCollision of string
 
 let lookup_user uid = List.assoc uid !user_tbl
 
+let user_of_session sid = let res,_,_ = List.assoc sid !session_tbl in res
+
 let create_session uid =
   let status = new MatitaEngine.status (Some uid) "cic:/matita" in
   let history = [status] in
@@ -51,28 +55,35 @@ let create_session uid =
        List.remove_assoc sid' !session_tbl
     | _ -> !session_tbl
   in 
-  session_tbl := (new_session,(status,history))::clean_stbl;
+  session_tbl := (new_session,(uid,status,history))::clean_stbl;
   new_session
 ;;
 
-let get_status sid = fst (List.assoc sid !session_tbl)
+let get_session_owner sid =
+  let uid,_,_ = List.assoc sid !session_tbl
+  in uid
 
-let get_history sid = snd (List.assoc sid !session_tbl)
+let get_status sid =
+  let _,st,_ = List.assoc sid !session_tbl
+  in st
+
+let get_history sid = 
+  let _,_,hist = List.assoc sid !session_tbl
+  in hist
 
 let set_status sid st =
-  let oldst, hist = List.assoc sid !session_tbl in
-  session_tbl := (sid,(st,hist))::(List.remove_assoc sid !session_tbl)
+  let uid, oldst, hist = List.assoc sid !session_tbl in
+  session_tbl := (sid,(uid,st,hist))::(List.remove_assoc sid !session_tbl)
    
 let set_history sid hist =
-  let st, oldhist = List.assoc sid !session_tbl in
-  session_tbl := (sid,(st,hist))::(List.remove_assoc sid !session_tbl)
-
-let logout_user uid =
-  match List.assoc uid !user_tbl with
-  | _,None -> ()
-  | pw, Some sid ->
-     user_tbl := (uid,(pw,None))::List.remove_assoc uid !user_tbl;
-     session_tbl := List.remove_assoc sid !session_tbl
+  let uid, st, oldhist = List.assoc sid !session_tbl in
+  session_tbl := (sid,(uid,st,hist))::(List.remove_assoc sid !session_tbl)
+
+let logout_user sid =
+  let uid,st,hist = List.assoc sid !session_tbl in
+  let pw,_ = List.assoc uid !user_tbl in
+  user_tbl := (uid,(pw,None))::List.remove_assoc uid !user_tbl;
+  session_tbl := List.remove_assoc sid !session_tbl
 ;;
 
 let remove_user uid =
@@ -95,18 +106,65 @@ let serialize () =
 ;;
 
 let deserialize () =
-  let utbl_ch = open_in (config_path () ^ "/usertable.dump") in
-  user_tbl := Marshal.from_channel utbl_ch;
-  close_in utbl_ch;
+  (try
+    let utbl_ch = open_in (config_path () ^ "/usertable.dump") in
+    user_tbl := Marshal.from_channel utbl_ch;
+    close_in utbl_ch;
+  with
+    | Sys_error _ -> 
+       user_tbl := []; serialize());
   (* old_sessions are now invalid *)
   session_tbl := [];
 ;;
 
+let write_ft uid ft =
+  let ft_ch = open_out (config_path () ^ "/ft_" ^ uid ^ ".dump") in
+  Marshal.to_channel ft_ch ft [];
+  close_out ft_ch;
+;;
+
+let read_ft uid =
+  try
+    let ft_ch = open_in (config_path () ^ "/ft_" ^ uid ^ ".dump") in
+    let ft = Marshal.from_channel ft_ch in
+    close_in ft_ch;
+    ft
+  with
+    | Sys_error _ -> 
+      (* this is an error, we should rebuild the table by a diff of
+         the directory listing and svn stat *) 
+      [] 
+;;
+
+let set_file_flag uid filename flag =
+  let filename = MatitaFilesystem.normalize_qfn filename in 
+  let ft = read_ft uid in
+  let oldflag = 
+    try List.assoc filename ft
+    with Not_found -> MatitaFilesystem.MSynchronized
+  in
+  if oldflag <> MatitaFilesystem.MConflict 
+    then
+      let ft = (filename,flag)::
+        List.filter (fun (fn,_) -> fn <> filename) ft in
+      write_ft uid ft
+    else ()
+;;
+
 let add_user uid pw =
   try
     let _ = lookup_user uid in
     raise (UsernameCollision uid)
   with Not_found -> 
+    let ft = MatitaFilesystem.checkout uid in
     user_tbl := (uid,(pw,None))::!user_tbl;
+    write_ft uid ft;
     serialize ()
 ;;
+
+let reset () =
+  user_tbl := [];
+  session_tbl := [];
+  MatitaFilesystem.reset_lib ();
+  serialize ();
+;;