From: rich Date: Wed, 8 Sep 2004 12:45:37 +0000 (+0000) Subject: Scripts for editing name, permissions. X-Git-Url: http://git.annexia.org/?a=commitdiff_plain;h=b90e3f4f9f352bd21d797d08729540e77b9ce72f;p=cocanwiki.git Scripts for editing name, permissions. --- diff --git a/conf/cocanwiki.conf b/conf/cocanwiki.conf index f17bfc3..2400d27 100644 --- a/conf/cocanwiki.conf +++ b/conf/cocanwiki.conf @@ -1,5 +1,5 @@ # Apache configuration for COCANWIKI. -# $Id: cocanwiki.conf,v 1.2 2004/09/07 16:19:43 rich Exp $ +# $Id: cocanwiki.conf,v 1.3 2004/09/08 12:45:37 rich Exp $ # Uncomment the following lines if necessary. You will probably need # to adjust the paths to reflect where cocanwiki is really installed. @@ -55,6 +55,7 @@ RewriteRule ^/_login$ /_bin/login_form.cmo [PT,L] RewriteRule ^/_logout$ /_bin/logout.cmo [PT,L,QSA] RewriteRule ^/_recent$ /_bin/recent.cmo [PT,L,QSA] RewriteRule ^/_sitemap$ /_bin/sitemap.cmo [PT,L,QSA] +RewriteRule ^/_users$ /_bin/users.cmo [PT,L,QSA] # Image and file downloads. RewriteRule ^/_file/(.*)$ /_bin/file.cmo?name=$1 [PT,L,QSA] diff --git a/html/_css/users.css b/html/_css/users.css index 87e7f64..28e1a3d 100644 --- a/html/_css/users.css +++ b/html/_css/users.css @@ -1,5 +1,5 @@ /* Stylesheet for COCANWIKI, derived from EWM. - * $Id: users.css,v 1.2 2004/09/08 12:02:29 rich Exp $ + * $Id: users.css,v 1.3 2004/09/08 12:45:37 rich Exp $ */ table#users { @@ -16,4 +16,9 @@ table#users th { table#users td { border: 1px solid #eee; padding: 6px; -} \ No newline at end of file +} + +table#edit_user th { + vertical-align: top; + text-align: right; +} diff --git a/scripts/Makefile b/scripts/Makefile index 4fa484b..987806b 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -1,5 +1,5 @@ # Makefile for COCANWIKI. -# $Id: Makefile,v 1.7 2004/09/08 10:42:20 rich Exp $ +# $Id: Makefile,v 1.8 2004/09/08 12:45:38 rich Exp $ include ../Makefile.config @@ -31,6 +31,8 @@ OBJS := create.cmo \ edit.cmo \ edit_page_css.cmo \ edit_page_css_form.cmo \ + edit_user.cmo \ + edit_user_form.cmo \ file.cmo \ files.cmo \ forgot_password.cmo \ diff --git a/scripts/edit_user.ml b/scripts/edit_user.ml new file mode 100644 index 0000000..f549c0d --- /dev/null +++ b/scripts/edit_user.ml @@ -0,0 +1,85 @@ +(* COCANWIKI scripts. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_user.ml,v 1.1 2004/09/08 12:45:38 rich Exp $ + *) + +open Apache +open Registry +open Cgi +open Printf + +open Cocanwiki +open Cocanwiki_strings +open Cocanwiki_ok + +let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) self = + let userid = int_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 sth = dbh#prepare_cached "select name from users + where hostid = ? and id = ?" in + sth#execute [`Int hostid; `Int userid]; + let original_name = sth#fetch1string () in + + let name = trim (q#param "name") in + + if original_name <> name then ( + if name = "" then ( + error ~back_button:true ~title:"Bad username" + q "The username you gave is empty."; + raise CgiExit + ); + + (* Check it's not a duplicate, then change it. *) + let sth = dbh#prepare_cached "select id from users + where hostid = ? and name = ?" in + sth#execute [`Int hostid; `String name]; + + (try + sth#fetch1 (); + error ~back_button:true ~title:"Username already taken" + q ("That username has already been taken by another user."); + raise CgiExit + with + Not_found -> ()); + + let sth = dbh#prepare_cached "update users set name = ? + where hostid = ? and id = ?" in + sth#execute [`String name; `Int hostid; `Int userid] + ); + + (* Change email address and permissions. *) + let email = trim (q#param "email") in + let email = if email = "" then `Null else `String email in + + let can_edit = q#param_true "can_edit" in + let can_manage_users = q#param_true "can_manage_users" 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" + 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."); + raise CgiExit + | _ -> ()); + + let sth = dbh#prepare_cached "update users set email = ?, + can_edit = ?, can_manage_users = ? + where hostid = ? and id = ?" in + sth#execute [email; `Bool can_edit; `Bool can_manage_users; + `Int hostid; `Int userid]; + + (* Finish up. *) + dbh#commit (); + + let buttons = [ ok_button "/_users" ] in + ok ~buttons ~title:"Saved" + q "Changes were saved." + +let () = + register_script ~restrict:[CanManageUsers] run diff --git a/scripts/edit_user_form.ml b/scripts/edit_user_form.ml new file mode 100644 index 0000000..7a2e63f --- /dev/null +++ b/scripts/edit_user_form.ml @@ -0,0 +1,54 @@ +(* COCANWIKI scripts. + * Written by Richard W.M. Jones . + * Copyright (C) 2004 Merjis Ltd. + * $Id: edit_user_form.ml,v 1.1 2004/09/08 12:45:38 rich Exp $ + *) + +open Apache +open Registry +open Cgi +open Printf + +open Cocanwiki +open Cocanwiki_template +open Cocanwiki_date + +let template = get_template "edit_user_form.html" + +let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ = + let userid = int_of_string (q#param "userid") in + + let sth = + dbh#prepare_cached + "select u.name, u.email, u.registration_date, + u.can_edit, u.can_manage_users, + (select count(*) from pages where logged_user = u.id), + (select count(*) from pages + where logged_user = u.id and url_deleted is null) + from users u where u.hostid = ? and u.id = ?" in + sth#execute [`Int hostid; `Int userid]; + + let name, email, registration_date, can_edit, can_manage_users, nr_edits, + nr_edits_live = + match sth#fetch1 () with + [`String name; (`Null | `String _) as email; + `Date registration_date; + `Bool can_edit; `Bool can_manage_users; + `Int nr_edits; `Int nr_edits_live] -> + name, email, registration_date, can_edit, can_manage_users, + nr_edits, nr_edits_live + | _ -> assert false in + + template#set "userid" (string_of_int userid); + template#set "name" name; + template#set "email" (match email with `Null -> "" | `String s -> s); + template#set "registration_date" (printable_date' registration_date); + template#conditional "can_edit" can_edit; + template#conditional "can_manage_users" can_manage_users; + template#set "nr_edits" (string_of_int nr_edits); + template#set "nr_edits_live" (string_of_int nr_edits_live); + + q#template template + +let () = + register_script ~restrict:[CanManageUsers] run diff --git a/scripts/signup.ml b/scripts/signup.ml index 84c560e..d16acb1 100644 --- a/scripts/signup.ml +++ b/scripts/signup.ml @@ -1,7 +1,7 @@ (* COCANWIKI scripts. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. - * $Id: signup.ml,v 1.2 2004/09/07 17:16:46 rich Exp $ + * $Id: signup.ml,v 1.3 2004/09/08 12:45:38 rich Exp $ *) open Apache @@ -53,8 +53,9 @@ let run r (q : cgi) (dbh : Dbi.connection) (hostid, _, _) _ = let email = if string_is_whitespace email then `Null else `String email in (* Not a duplicate? *) - let sth = dbh#prepare_cached "select id from users where name = ?" in - sth#execute [`String username]; + let sth = dbh#prepare_cached "select id from users + where hostid = ? and name = ?" in + sth#execute [`Int hostid; `String username]; (try sth#fetch1 (); diff --git a/templates/edit_user_form.html b/templates/edit_user_form.html new file mode 100644 index 0000000..43dbe87 --- /dev/null +++ b/templates/edit_user_form.html @@ -0,0 +1,67 @@ + + + +User: ::name_html:: + + + + + +

User: ::name_html::

+ + + +
+ + + + + + + + + + + + + + + + + + + + + +
Username:
Email:
Permissions: + +
+ +
+ +
+ + + + + +
+ + + + + \ No newline at end of file diff --git a/templates/page.html b/templates/page.html index 6b64934..51876ac 100644 --- a/templates/page.html +++ b/templates/page.html @@ -63,7 +63,7 @@ ::end::
  • ::if(user_logged_in):: ::username_html:: (logout) ::else:: Create account or log in ::end:: |
  • ::if(can_manage_users):: -
  • Manage users
  • +
  • Manage users
  • ::end:: diff --git a/templates/users.html b/templates/users.html index 3798c53..f4cd067 100644 --- a/templates/users.html +++ b/templates/users.html @@ -15,7 +15,7 @@ - +
    Name Username Email address Registration Permissions