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.11 2006/03/27 18:09:46 rich Exp $
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.
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.
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.
28 open Cocanwiki_strings
31 let run r (q : cgi) dbh hostid _ self =
32 let userid = Int32.of_string (q#param "userid") in
34 (* Get the user's original name. If we're going to change the
35 * name, we need to do additional checks.
37 let sth = dbh#prepare_cached "select name from users
38 where hostid = ? and id = ?" in
39 sth#execute [Some hostid; Some userid];
40 let original_name = sth#fetch1string () in
42 let name = trim (q#param "name") in
44 if original_name <> name then (
46 error ~back_button:true ~title:"Bad username"
47 dbh hostid q "The username you gave is empty.";
51 (* Check it's not a duplicate, then change it. *)
52 let sth = dbh#prepare_cached "select id from users
53 where hostid = ? and name = ?" in
54 sth#execute [Some hostid; Some name];
58 error ~back_button:true ~title:"Username already taken"
60 ("That username has already been taken by another user.");
65 let sth = dbh#prepare_cached "update users set name = ?
66 where hostid = ? and id = ?" in
67 sth#execute [Some name; Some hostid; Some userid]
70 (* Change permissions. *)
71 let can_edit = q#param_true "can_edit" in
72 let can_manage_users = q#param_true "can_manage_users" in
73 let can_manage_contacts = q#param_true "can_manage_contacts" in
74 let can_manage_site = q#param_true "can_manage_site" in
75 let can_edit_global_css = q#param_true "can_edit_global_css" in
76 let can_import_mail = q#param_true "can_import_mail" in
78 (* Trying to remove manage users permission from self? *)
79 (match can_manage_users, self with
80 | false, User (id, _, _, _) when id = userid ->
81 error ~back_button:true ~title:"Remove manage users from self"
83 ("You tried to remove 'Manage users' permission from yourself. " ^
84 "You can't do this. You'll have to do it from another " ^
89 let sth = dbh#prepare_cached "update users set
90 can_edit = ?, can_manage_users = ?,
91 can_manage_contacts = ?,
93 can_edit_global_css = ?,
95 where hostid = ? and id = ?" in
96 sth#execute [`Bool can_edit; `Bool can_manage_users;
97 `Bool can_manage_contacts; `Bool can_manage_site;
98 `Bool can_edit_global_css; `Bool can_import_mail;
99 Some hostid; Some userid];
104 let buttons = [ ok_button "/_users" ] in
105 ok ~buttons ~title:"Saved"
106 dbh hostid q "Changes were saved."
109 register_script ~restrict:[CanManageUsers] run