2 * Written by Richard W.M. Jones <rich@merjis.com>.
3 * Copyright (C) 2004 Merjis Ltd.
4 * $Id: cocanwiki.ml,v 1.3 2004/09/07 14:58:34 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 (* Our wrapper around the standard [register_script] function. *)
50 let register_script ?(restrict = []) run =
51 (* Actually register the script with the real [Registry] module. *)
55 let dbh = _get_dbh r in
57 (* Get the host ID, by comparing the Host: header with the hostnames
58 * table in the database.
60 let hostid, hostname, edit_anon =
61 let hostname = try Request.hostname r
62 with Not_found -> failwith "No ``Host:'' header in request" in
63 let hostname = String.lowercase hostname in
67 "select h.id, h.canonical_hostname, h.edit_anon
68 from hostnames hn, hosts h
69 where hn.name = ? and hn.hostid = h.id" in
70 sth#execute [`String hostname];
73 (match sth#fetch1 () with
74 [ `Int id; `String hostname; `Bool edit_anon ] ->
75 id, hostname, edit_anon
79 failwith ("Hostname ``" ^ hostname ^ "'' not found in " ^
80 "the hosts/hostnames tables in the database.") in
82 (* Look for the user's cookie, and determine from this the user
88 (* Allow the user to deliberately specify an extra "cookie"
89 * parameter, which we will send back as a cookie. This is
90 * useful for "mail my password"-type scripts.
92 if q#param_exists "cookie" then (
93 let value = q#param "cookie" in
94 let cookie = Cookie.cookie ~name:"auth" ~value ~path:"/" () in
95 Table.set (Request.headers_out r) "Set-Cookie" cookie#as_string;
98 (* Normal cookie, from the headers. *)
99 let header = Table.get (Request.headers_in r) "Cookie" in
100 let cookies = Cookie.parse header in
102 List.find (fun cookie -> cookie#name = "auth") cookies in
108 "select u.id, u.name, u.can_edit, u.can_manage_users
109 from usercookies uc, users u
110 where uc.cookie = ? and uc.userid = u.id and u.hostid = ?" in
111 sth#execute [`String cookie; `Int hostid];
112 (match sth#fetch1 () with
113 [ `Int userid; `String name;
114 `Bool can_edit; `Bool can_manage_users ] ->
116 (if can_edit then [ CanEdit ] else []) @
117 (if can_manage_users then [ CanManageUsers ] else []) in
118 User (userid, name, perms)
121 Not_found -> Anonymous
124 (* If the ~restrict parameter is given, then we want to check that
125 * the user has sufficient permission to run this script.
129 [] -> true (* empty list = no restrictions *)
131 List.fold_left ((||)) false
132 (List.map (fun r -> test_permission edit_anon r user) rs) in
135 (* Call the actual CGI script. *)
137 run r q dbh (hostid, hostname, edit_anon) user
141 error ~back_button:true
142 ~title:"Access denied"
143 q "You do not have permission to access this part of the site."