Added the per-user 'manage contacts' permission.
[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.4 2004/09/17 15:24:54 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 : Dbi.connection) hostid _ self =
32   let userid = int_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 sth = dbh#prepare_cached "select name from users
38                                  where hostid = ? and id = ?" in
39   sth#execute [`Int hostid; `Int userid];
40   let original_name = sth#fetch1string () in
41
42   let name = trim (q#param "name") in
43
44   if original_name <> name then (
45     if name = "" then (
46       error ~back_button:true ~title:"Bad username"
47         q "The username you gave is empty.";
48       raise CgiExit
49     );
50
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 [`Int hostid; `String name];
55
56     (try
57        sth#fetch1 ();
58        error ~back_button:true ~title:"Username already taken"
59          q ("That username has already been taken by another user.");
60        raise CgiExit
61      with
62          Not_found -> ());
63
64     let sth = dbh#prepare_cached "update users set name = ?
65                                    where hostid = ? and id = ?" in
66     sth#execute [`String name; `Int hostid; `Int userid]
67   );
68
69   (* Change email address and permissions. *)
70   let email = trim (q#param "email") in
71   let email = if email = "" then `Null else `String email in
72
73   let can_edit = q#param_true "can_edit" in
74   let can_manage_users = q#param_true "can_manage_users" in
75   let can_manage_contacts = q#param_true "can_manage_contacts" 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            q ("You tried to remove 'Manage users' permission from yourself. "^
82               "You can't do this.  You'll have to do it from another "^
83               "user account.");
84          raise CgiExit
85      | _ -> ());
86
87   let sth = dbh#prepare_cached "update users set email = ?,
88                                        can_edit = ?, can_manage_users = ?,
89                                        can_manage_contacts = ?
90                                  where hostid = ? and id = ?" in
91   sth#execute [email; `Bool can_edit; `Bool can_manage_users;
92                `Bool can_manage_contacts;
93                `Int hostid; `Int userid];
94
95   (* Finish up. *)
96   dbh#commit ();
97
98   let buttons = [ ok_button "/_users" ] in
99   ok ~buttons ~title:"Saved"
100     q "Changes were saved."
101
102 let () =
103   register_script ~restrict:[CanManageUsers] run