(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: edit_user.ml,v 1.12 2006/03/27 19:10:29 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf open Cocanwiki open Cocanwiki_strings open Cocanwiki_ok let run r (q : cgi) dbh hostid _ self = let userid = Int32.of_string (q#param "userid") in (* Get the user's original name. If we're going to change the * name, we need to do additional checks. *) let original_name = List.hd ( PGSQL(dbh) "select name from users where hostid = $hostid and id = $userid" ) in let name = trim (q#param "name") in if original_name <> name then ( if name = "" then ( error ~back_button:true ~title:"Bad username" dbh hostid q "The username you gave is empty."; return () ); (* Check it's not a duplicate, then change it. *) let rows = PGSQL(dbh) "select 1 from users where hostid = $hostid and name = $name" in (match rows with | [Some 1l] -> error ~back_button:true ~title:"Username already taken" dbh hostid q ("That username has already been taken by another user."); return () | _ -> () ); PGSQL(dbh) "update users set name = $name where hostid = $hostid and id = $userid" ); (* Change permissions. *) let can_edit = q#param_true "can_edit" in let can_manage_users = q#param_true "can_manage_users" in let can_manage_contacts = q#param_true "can_manage_contacts" in let can_manage_site = q#param_true "can_manage_site" in let can_edit_global_css = q#param_true "can_edit_global_css" in let can_import_mail = q#param_true "can_import_mail" in (* Trying to remove manage users permission from self? *) (match can_manage_users, self with | false, User (id, _, _, _) when id = userid -> error ~back_button:true ~title:"Remove manage users from self" dbh hostid q ("You tried to remove 'Manage users' permission from yourself. " ^ "You can't do this. You'll have to do it from another " ^ "user account."); return () | _ -> ()); PGSQL(dbh) "update users set can_edit = $can_edit, can_manage_users = $can_manage_users, can_manage_contacts = $can_manage_contacts, can_manage_site = $can_manage_site, can_edit_global_css = $can_edit_global_css, can_import_mail = $can_import_mail where hostid = $hostid and id = $userid"; (* Finish up. *) PGOCaml.commit dbh; let buttons = [ ok_button "/_users" ] in ok ~buttons ~title:"Saved" dbh hostid q "Changes were saved." let () = register_script ~restrict:[CanManageUsers] run