+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / change_password.ml
index 37d92aa..fe2b8ba 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: change_password.ml,v 1.4 2005/11/24 14:54:11 rich Exp $
+ * $Id: change_password.ml,v 1.6 2006/12/06 09:46:56 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,7 +27,7 @@ open Printf
 open Cocanwiki
 open Cocanwiki_ok
 
-let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
+let run r (q : cgi) dbh hostid _ user =
   let old_password = q#param "old_password" in
 
   (* Check the old password was supplied correctly. *)
@@ -36,20 +36,20 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
        Anonymous -> assert false (* cannot happen *)
       | User (userid, _, _, _) -> userid in
 
-  let sth = dbh#prepare_cached "select 1 from users
-                                 where hostid = ? and id = ?
-                                   and password = ?" in
-  sth#execute [`Int hostid; `Int userid; `String old_password];
+  let rows = PGSQL(dbh)
+    "select 1 from users
+      where hostid = $hostid and id = $userid and password = $old_password" in
 
   let old_password_ok =
-    try 1 = sth#fetch1int ()
-    with
-       Not_found -> false in
+    match rows with
+    | [Some 1l] -> true
+    | [] -> false
+    | _ -> assert false in
 
   if not old_password_ok then (
     error ~title:"Bad password"
       ~back_button:true
-      dbh hostid q "The password you gave is wrong.";
+      dbh hostid q "The password you gave is wrong.";
     return ()
   );
 
@@ -58,30 +58,28 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ user =
 
   if password1 = "" || password2 = "" then (
     error ~back_button:true ~title:"Bad password"
-      dbh hostid q "The password you gave is empty.";
+      dbh hostid q "The password you gave is empty.";
     return ()
   );
 
   if password1 <> password2 then (
     error ~back_button:true ~title:"Passwords don't match"
-      dbh hostid q "The two passwords you gave aren't identical.";
+      dbh hostid q "The two passwords you gave aren't identical.";
     return ()
   );
 
   let password = password1 in
 
   (* Change the password. *)
-  let sth =
-    dbh#prepare_cached
-      "update users set password = ?, force_password_change = false
-        where hostid = ? and id = ?" in
-  sth#execute [`String password; `Int hostid; `Int userid];
+  PGSQL(dbh)
+    "update users set password = $password, force_password_change = false
+      where hostid = $hostid and id = $userid";
 
-  dbh#commit ();
+  PGOCaml.commit dbh;
 
   let buttons = [ ok_button "/" ] in
   ok ~buttons ~title:"Password changed"
-    dbh hostid q "The password was changed."
+    dbh hostid q "The password was changed."
 
 let () =
   register_script ~anonymous:false run