Fixed some problems found in testing. Now appears to be working fully.
[cocanwiki.git] / scripts / edit_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: edit_user.ml,v 1.12 2006/03/27 19:10:29 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_strings
29 open Cocanwiki_ok
30
31 let run r (q : cgi) dbh hostid _ self =
32   let userid = Int32.of_string (q#param "userid") in
33
34   (* Get the user's original name.  If we're going to change the
35    * name, we need to do additional checks.
36    *)
37   let original_name =
38     List.hd (
39       PGSQL(dbh)
40         "select name from users where hostid = $hostid and id = $userid"
41     ) in
42
43   let name = trim (q#param "name") in
44
45   if original_name <> name then (
46     if name = "" then (
47       error ~back_button:true ~title:"Bad username"
48         dbh hostid q "The username you gave is empty.";
49       return ()
50     );
51
52     (* Check it's not a duplicate, then change it. *)
53     let rows = PGSQL(dbh)
54       "select 1 from users where hostid = $hostid and name = $name" in
55
56     (match rows with
57      | [Some 1l] ->
58          error ~back_button:true ~title:"Username already taken"
59            dbh hostid q
60            ("That username has already been taken by another user.");
61          return ()
62      | _ -> ()
63     );
64
65     PGSQL(dbh) "update users set name = $name
66                  where hostid = $hostid and id = $userid"
67   );
68
69   (* Change permissions. *)
70   let can_edit = q#param_true "can_edit" in
71   let can_manage_users = q#param_true "can_manage_users" in
72   let can_manage_contacts = q#param_true "can_manage_contacts" in
73   let can_manage_site = q#param_true "can_manage_site" in
74   let can_edit_global_css = q#param_true "can_edit_global_css" in
75   let can_import_mail = q#param_true "can_import_mail" in
76
77   (* Trying to remove manage users permission from self? *)
78   (match can_manage_users, self with
79      | false, User (id, _, _, _) when id = userid ->
80          error ~back_button:true ~title:"Remove manage users from self"
81            dbh hostid q
82            ("You tried to remove 'Manage users' permission from yourself. " ^
83             "You can't do this.  You'll have to do it from another " ^
84             "user account.");
85          return ()
86      | _ -> ());
87
88   PGSQL(dbh)
89     "update users set
90        can_edit = $can_edit, can_manage_users = $can_manage_users,
91        can_manage_contacts = $can_manage_contacts,
92        can_manage_site = $can_manage_site,
93        can_edit_global_css = $can_edit_global_css,
94        can_import_mail = $can_import_mail
95      where hostid = $hostid and id = $userid";
96
97   (* Finish up. *)
98   PGOCaml.commit dbh;
99
100   let buttons = [ ok_button "/_users" ] in
101   ok ~buttons ~title:"Saved"
102     dbh hostid q "Changes were saved."
103
104 let () =
105   register_script ~restrict:[CanManageUsers] run