/_sitemap.rss for COCANWIKI.
[cocanwiki.git] / scripts / change_password.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: change_password.ml,v 1.5 2006/03/27 18:09:46 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_ok
29
30 let run r (q : cgi) dbh hostid _ user =
31   let old_password = q#param "old_password" in
32
33   (* Check the old password was supplied correctly. *)
34   let userid =
35     match user with
36         Anonymous -> assert false (* cannot happen *)
37       | User (userid, _, _, _) -> userid in
38
39   let rows = PGSQL(dbh)
40     "select 1 from users
41       where hostid = $hostid and id = $userid and password = $old_password" in
42
43   let old_password_ok =
44     match rows with
45     | [Some 1l] -> true
46     | [] -> false
47     | _ -> assert false in
48
49   if not old_password_ok then (
50     error ~title:"Bad password"
51       ~back_button:true
52       dbh hostid q "The password you gave is wrong.";
53     return ()
54   );
55
56   let password1 = q#param "password1" in
57   let password2 = q#param "password2" in
58
59   if password1 = "" || password2 = "" then (
60     error ~back_button:true ~title:"Bad password"
61       dbh hostid q "The password you gave is empty.";
62     return ()
63   );
64
65   if password1 <> password2 then (
66     error ~back_button:true ~title:"Passwords don't match"
67       dbh hostid q "The two passwords you gave aren't identical.";
68     return ()
69   );
70
71   let password = password1 in
72
73   (* Change the password. *)
74   PGSQL(dbh)
75     "update users set password = $password, force_password_change = false
76       where hostid = $hostid and id = $userid";
77
78   PGOCaml.commit dbh;
79
80   let buttons = [ ok_button "/" ] in
81   ok ~buttons ~title:"Password changed"
82     dbh hostid q "The password was changed."
83
84 let () =
85   register_script ~anonymous:false run