Added the per-user 'manage contacts' permission.
[cocanwiki.git] / scripts / cocanwiki.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: cocanwiki.ml,v 1.7 2004/09/17 15:24:54 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_ok
28
29 module Pool = DbiPool (Dbi_postgres)
30
31 (* This function is used to grab a database handle.  It's used in a couple
32  * of very special places, and is not for general consumption.
33  *)
34 let _get_dbh r = Pool.get r "cocanwiki"
35
36 (* The [CgiExit] exception should be folded back into the base
37  * mod_caml code at some point.  It just causes the 'run' function to
38  * return at that point safely.  (XXX)
39  *)
40 exception CgiExit
41
42 (* Permissions and restrictions.
43  *
44  * Use the optional ~restrict parameter to register_script to restrict
45  * who can use the script.  For example:
46  *   register_script ~restrict:[CanEdit ; CanManageUsers] run
47  *)
48 type permissions_t = CanEdit | CanManageUsers | CanManageContacts
49
50 (* The "user object". *)
51 type user_t = Anonymous                 (* Not logged in. *)
52             | User of int * string * permissions_t list
53                                         (* Userid, name, permissions. *)
54
55 let test_permission edit_anon perm user =
56   if perm = CanEdit && edit_anon then true
57   else match user with
58       Anonymous -> false
59     | User (_, _, perms) -> List.mem perm perms
60
61 let can_edit edit_anon = test_permission edit_anon CanEdit
62 let can_manage_users = test_permission false CanManageUsers
63 let can_manage_contacts = test_permission false CanManageContacts
64
65 (* The "host object". *)
66 type host_t = { hostname : string;
67                 edit_anon : bool; }
68
69 (* Our wrapper around the standard [register_script] function.
70  *
71  * The optional ~restrict and ~anonymous parameters work as follows:
72  *
73  * By default (neither parameter given), anonymous or logged-in users
74  * at any level are permitted to run the script.
75  *
76  * If ~anonymous:false then a user must be logged in to use the script.
77  *
78  * If ~restrict contains a list of permissions (eg. CanEdit, etc.) then
79  * the user must have the ability to do AT LEAST ONE of those actions.
80  * (Note that this does not necessarily imply that the user must be
81  * logged in, because in some circumstances even anonymous users have
82  * the CanEdit permission - very typical for a wiki).
83  *
84  * If ~anonymous:false and ~restrict is given then the user must be
85  * logged in AND have the ability to do AT LEAST ONE of those actions.
86  *)
87 let register_script ?(restrict = []) ?(anonymous = true) run =
88   (* Actually register the script with the real [Registry] module. *)
89   register_script
90     (fun r ->
91        let q = new cgi r in
92        let dbh = _get_dbh r in
93
94        (* Get the host ID, by comparing the Host: header with the hostnames
95         * table in the database.
96         *)
97        let hostid, hostname, edit_anon =
98          let hostname = try Request.hostname r
99          with Not_found -> failwith "No ``Host:'' header in request" in
100          let hostname = String.lowercase hostname in
101
102          let sth =
103            dbh#prepare_cached
104              "select h.id, h.canonical_hostname, h.edit_anon
105                 from hostnames hn, hosts h
106                where hn.name = ? and hn.hostid = h.id" in
107          sth#execute [`String hostname];
108
109          try
110            (match sth#fetch1 () with
111                 [ `Int id; `String hostname; `Bool edit_anon ] ->
112                   id, hostname, edit_anon
113               | _ -> assert false)
114          with
115              Not_found ->
116                failwith ("Hostname ``" ^ hostname ^ "'' not found in " ^
117                          "the hosts/hostnames tables in the database.") in
118
119        (* Create the host object. *)
120        let host = { hostname = hostname; edit_anon = edit_anon; } in
121
122        (* Look for the user's cookie, and determine from this the user
123         * object.
124         *)
125        let user =
126          try
127            let cookie =
128              (* Allow the user to deliberately specify an extra "cookie"
129               * parameter, which we will send back as a cookie.  This is
130               * useful for "mail my password"-type scripts.
131               *)
132              if q#param_exists "cookie" then (
133                let value = q#param "cookie" in
134                let cookie = Cookie.cookie ~name:"auth" ~value ~path:"/" () in
135                Table.set (Request.headers_out r) "Set-Cookie" cookie#as_string;
136                value
137              ) else (
138                (* Normal cookie, from the headers. *)
139                let header = Table.get (Request.headers_in r) "Cookie" in
140                let cookies = Cookie.parse header in
141                let cookie =
142                  List.find (fun cookie -> cookie#name = "auth") cookies in
143                cookie#value
144              ) in
145
146            let sth =
147              dbh#prepare_cached
148                "select u.id, u.name, u.can_edit, u.can_manage_users,
149                        u.can_manage_contacts
150                   from usercookies uc, users u
151                  where uc.cookie = ? and uc.userid = u.id and u.hostid = ?" in
152            sth#execute [`String cookie; `Int hostid];
153            (match sth#fetch1 () with
154                 [ `Int userid; `String name;
155                   `Bool can_edit; `Bool can_manage_users;
156                   `Bool can_manage_contacts ] ->
157                   let perms =
158                     (if can_edit then [ CanEdit ] else []) @
159                     (if can_manage_users then [ CanManageUsers ] else []) @
160                     (if can_manage_contacts then [ CanManageContacts ] else [])
161                   in
162                   User (userid, name, perms)
163               | _ -> assert false)
164          with
165              Not_found -> Anonymous
166        in
167
168        (* If the ~restrict parameter is given, then we want to check that
169         * the user has sufficient permission to run this script.
170         *)
171        let permitted =
172          if not anonymous && user = Anonymous then false
173          else
174            match restrict with
175                [] -> true               (* empty list = no restrictions *)
176              | rs ->
177                  List.fold_left ((||)) false
178                    (List.map (fun r -> test_permission edit_anon r user) rs) in
179
180        if permitted then (
181          (* Call the actual CGI script. *)
182          try
183            run r q dbh hostid host user
184          with
185              CgiExit -> ()
186        ) else
187          error ~back_button:true
188            ~title:"Access denied"
189            q "You do not have permission to access this part of the site."
190     )