2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: cocanwiki.ml,v 1.5 2004/09/09 09:35:33 rich Exp $
14 module Pool = DbiPool (Dbi_postgres)
16 (* This function is used to grab a database handle. It's used in a couple
17 * of very special places, and is not for general consumption.
19 let _get_dbh r = Pool.get r "cocanwiki"
21 (* The [CgiExit] exception should be folded back into the base
22 * mod_caml code at some point. It just causes the 'run' function to
23 * return at that point safely. (XXX)
27 (* Permissions and restrictions.
29 * Use the optional ~restrict parameter to register_script to restrict
30 * who can use the script. For example:
31 * register_script ~restrict:[CanEdit ; CanManageUsers] run
33 type permissions_t = CanEdit | CanManageUsers
35 (* The "user object". *)
36 type user_t = Anonymous (* Not logged in. *)
37 | User of int * string * permissions_t list
38 (* Userid, name, permissions. *)
40 let test_permission edit_anon perm user =
41 if perm = CanEdit && edit_anon then true
44 | User (_, _, perms) -> List.mem perm perms
46 let can_edit edit_anon = test_permission edit_anon CanEdit
47 let can_manage_users = test_permission false CanManageUsers
49 (* The "host object". *)
50 type host_t = { hostname : string;
53 (* Our wrapper around the standard [register_script] function.
55 * The optional ~restrict and ~anonymous parameters work as follows:
57 * By default (neither parameter given), anonymous or logged-in users
58 * at any level are permitted to run the script.
60 * If ~anonymous:false then a user must be logged in to use the script.
62 * If ~restrict contains a list of permissions (eg. CanEdit, etc.) then
63 * the user must have the ability to do AT LEAST ONE of those actions.
64 * (Note that this does not necessarily imply that the user must be
65 * logged in, because in some circumstances even anonymous users have
66 * the CanEdit permission - very typical for a wiki).
68 * If ~anonymous:false and ~restrict is given then the user must be
69 * logged in AND have the ability to do AT LEAST ONE of those actions.
71 let register_script ?(restrict = []) ?(anonymous = true) run =
72 (* Actually register the script with the real [Registry] module. *)
76 let dbh = _get_dbh r in
78 (* Get the host ID, by comparing the Host: header with the hostnames
79 * table in the database.
81 let hostid, hostname, edit_anon =
82 let hostname = try Request.hostname r
83 with Not_found -> failwith "No ``Host:'' header in request" in
84 let hostname = String.lowercase hostname in
88 "select h.id, h.canonical_hostname, h.edit_anon
89 from hostnames hn, hosts h
90 where hn.name = ? and hn.hostid = h.id" in
91 sth#execute [`String hostname];
94 (match sth#fetch1 () with
95 [ `Int id; `String hostname; `Bool edit_anon ] ->
96 id, hostname, edit_anon
100 failwith ("Hostname ``" ^ hostname ^ "'' not found in " ^
101 "the hosts/hostnames tables in the database.") in
103 (* Create the host object. *)
104 let host = { hostname = hostname; edit_anon = edit_anon; } in
106 (* Look for the user's cookie, and determine from this the user
112 (* Allow the user to deliberately specify an extra "cookie"
113 * parameter, which we will send back as a cookie. This is
114 * useful for "mail my password"-type scripts.
116 if q#param_exists "cookie" then (
117 let value = q#param "cookie" in
118 let cookie = Cookie.cookie ~name:"auth" ~value ~path:"/" () in
119 Table.set (Request.headers_out r) "Set-Cookie" cookie#as_string;
122 (* Normal cookie, from the headers. *)
123 let header = Table.get (Request.headers_in r) "Cookie" in
124 let cookies = Cookie.parse header in
126 List.find (fun cookie -> cookie#name = "auth") cookies in
132 "select u.id, u.name, u.can_edit, u.can_manage_users
133 from usercookies uc, users u
134 where uc.cookie = ? and uc.userid = u.id and u.hostid = ?" in
135 sth#execute [`String cookie; `Int hostid];
136 (match sth#fetch1 () with
137 [ `Int userid; `String name;
138 `Bool can_edit; `Bool can_manage_users ] ->
140 (if can_edit then [ CanEdit ] else []) @
141 (if can_manage_users then [ CanManageUsers ] else []) in
142 User (userid, name, perms)
145 Not_found -> Anonymous
148 (* If the ~restrict parameter is given, then we want to check that
149 * the user has sufficient permission to run this script.
152 if not anonymous && user = Anonymous then false
155 [] -> true (* empty list = no restrictions *)
157 List.fold_left ((||)) false
158 (List.map (fun r -> test_permission edit_anon r user) rs) in
161 (* Call the actual CGI script. *)
163 run r q dbh hostid host user
167 error ~back_button:true
168 ~title:"Access denied"
169 q "You do not have permission to access this part of the site."