+csv dep for PG'OCaml.
[cocanwiki.git] / scripts / create_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: create_user.ml,v 1.9 2006/12/06 09:46:57 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 open Cocanwiki_strings
30
31 let run r (q : cgi) dbh 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
35
36   if username = "" || password1 = "" || password2 = "" then (
37     error ~back_button:true ~title:"Bad username or password"
38       r dbh hostid q "The username or password you gave is empty.";
39     return ()
40   );
41
42   if password1 <> password2 then (
43     error ~back_button:true ~title:"Passwords don't match"
44       r dbh hostid q "The two passwords you gave aren't identical.";
45     return ()
46   );
47
48   let password = password1 in
49
50   if UTF8.length username > 32 || UTF8.length password > 128 then (
51     error ~back_button:true ~title:"Username or password too long"
52       r dbh hostid q
53       "Usernames should be less than 32 characters long.  For passwords we let you have a generous 128 characters.";
54     return ()
55   );
56
57   (* Not a duplicate? *)
58   let rows = PGSQL(dbh)
59     "select id from users where hostid = $hostid and name = $username" in
60
61   (match rows with
62    | [_] ->
63        error ~back_button:true ~title:"Username already taken"
64          r dbh hostid q "Someone has already taken that username.";
65        return ()
66    | [] -> ()
67    | _ -> assert false
68   );
69
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   let can_edit_macros = q#param_true "can_edit_macros" in
77   let force_password_change = q#param_true "force_password_change" in
78
79   (* Create the user account. *)
80   PGSQL(dbh)
81     "insert into users (name, password,
82                         hostid, can_edit, can_manage_users,
83                         can_manage_contacts, can_manage_site,
84                         can_edit_global_css, can_import_mail,
85                         can_edit_macros,
86                         force_password_change)
87      values ($username, $password, $hostid, $can_edit, $can_manage_users,
88              $can_manage_contacts, $can_manage_site,
89              $can_edit_global_css, $can_import_mail,
90              $can_edit_macros,
91              $force_password_change)";
92
93   PGOCaml.commit dbh;
94
95   let buttons = [ ok_button "/_users" ] in
96
97   ok ~title:"Account created" ~buttons
98     r dbh hostid q ("An account was created for " ^ username ^ ".")
99
100 let () =
101   register_script ~restrict:[CanManageUsers] run