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: create_user.ml,v 1.2 2004/09/22 10:19:26 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.
29 open Cocanwiki_strings
31 let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
32 let username = trim (q#param "username") in
33 let password1 = trim (q#param "password1") in
34 let password2 = trim (q#param "password2") in
36 if username = "" || password1 = "" || password2 = "" then (
37 error ~back_button:true ~title:"Bad username or password"
38 q "The username or password you gave is empty.";
42 if password1 <> password2 then (
43 error ~back_button:true ~title:"Passwords don't match"
44 q "The two passwords you gave aren't identical.";
48 let password = password1 in
51 Uh oh ... Not making UNICODE assumptions ... XXX
52 if String.length username > 32 || String.length password > 32 then
55 let email = trim (q#param "email") in
56 let email = if string_is_whitespace email then `Null else `String email in
58 (* Not a duplicate? *)
59 let sth = dbh#prepare_cached "select id from users
60 where hostid = ? and name = ?" in
61 sth#execute [`Int hostid; `String username];
65 error ~back_button:true ~title:"Username already taken"
66 q "Someone has already taken that username.";
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
77 (* Create the user account. *)
78 let sth = dbh#prepare_cached "insert into users (name, password, email,
79 hostid, can_edit, can_manage_users,
80 can_manage_contacts, can_manage_site,
82 values (?, ?, ?, ?, ?, ?, ?, ?, ?)" in
83 sth#execute [`String username; `String password; email; `Int hostid;
84 `Bool can_edit; `Bool can_manage_users;
85 `Bool can_manage_contacts; `Bool can_manage_site;
86 `Bool can_edit_global_css];
90 let buttons = [ ok_button "/_users" ] in
92 ok ~title:"Account created" ~buttons
93 q ("An account was created for " ^ username ^ ".")
96 register_script ~restrict:[CanManageUsers] run