Serial columns now 64 bits.
[cocanwiki.git] / scripts / delete_user.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: delete_user.ml,v 1.4 2004/10/30 10:16:10 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 : Dbi.connection) hostid {hostname = hostname} self =
31   if q#param_true "cancel" then (
32     (* Request cancelled. *)
33     q#redirect ("http://" ^ hostname ^ "/_users");
34     return ()
35   );
36
37   let userid = int_of_string (q#param "userid") in
38
39   (* Check userid belongs to host.  The statements below wouldn't
40    * enforce this correctly ...
41    *)
42   let sth =
43     dbh#prepare_cached "select 1 from users where id = ? and hostid = ?" in
44   sth#execute [`Int userid; `Int hostid];
45
46   assert (sth#fetch1int () = 1);
47
48   (* Can't delete self! *)
49   let () =
50     match self with
51       | User (id, _, _, _) when id = userid ->
52           error ~back_button:true ~title:"Delete own account"
53             q "You cannot delete your own user account.";
54           return ()
55       | _ -> () in
56
57   (* Delete the user. *)
58   let sth =
59     dbh#prepare_cached "delete from recently_visited
60                          where userid = ? and hostid = ?" in
61   sth#execute [`Int userid; `Int hostid];
62
63   let sth =
64     dbh#prepare_cached "delete from pending_email_changes
65                          where userid = ?" in
66   sth#execute [`Int userid];
67
68
69   let sth = dbh#prepare_cached "delete from usercookies where userid = ?" in
70   sth#execute [`Int userid];
71
72   let sth = dbh#prepare_cached "update pages set logged_user = null
73                                  where logged_user = ? and hostid = ?" in
74   sth#execute [`Int userid; `Int hostid];
75
76   let sth =
77     dbh#prepare_cached "delete from users where id = ? and hostid = ?" in
78   sth#execute [`Int userid; `Int hostid];
79
80   dbh#commit ();
81
82   ok ~title:"Account deleted" ~buttons:[ok_button "/_users"]
83     q "That user account was deleted."
84
85 let () =
86   register_script ~restrict:[CanManageUsers] run