+ (* Allow the user to deliberately specify an extra "cookie"
+ * parameter, which we will send back as a cookie. This is
+ * useful for "mail my password"-type scripts.
+ *)
+ if q#param_exists "cookie" then (
+ let value = q#param "cookie" in
+ let cookie = Cookie.cookie "auth" value ~path:"/" in
+ Table.set (Request.headers_out r)
+ "Set-Cookie" cookie#to_string;
+ value
+ ) else (
+ (* Normal cookie, from the headers. *)
+ let header = Table.get (Request.headers_in r) "Cookie" in
+ let cookies = Cookie.parse header in
+ let cookie =
+ List.find (fun cookie -> cookie#name = "auth") cookies in
+ cookie#value
+ ) in
+
+ let rows =
+ PGSQL(dbh)
+ "select u.id, u.name, u.can_edit, u.can_manage_users,
+ u.can_manage_contacts, u.can_manage_site,
+ u.can_edit_global_css, u.can_import_mail,
+ u.email, u.email_notify
+ from usercookies uc, users u
+ where uc.cookie = $cookie
+ and uc.userid = u.id
+ and u.hostid = $hostid" in
+ match rows with
+ | [userid, name, can_edit, can_manage_users,
+ can_manage_contacts, can_manage_site,
+ can_edit_global_css, can_import_mail,
+ email, email_notify] ->
+ (* Every logged in user can view. *)
+ let perms = [CanView] in
+ let perms =
+ if can_edit then CanEdit :: perms
+ else perms in
+ let perms =
+ if can_manage_users then CanManageUsers :: perms
+ else perms in
+ let perms =
+ if can_manage_contacts then CanManageContacts :: perms
+ else perms in
+ let perms =
+ if can_manage_site then CanManageSite :: perms
+ else perms in
+ let perms =
+ if can_edit_global_css then CanEditGlobalCSS :: perms
+ else perms in
+ let perms =
+ if can_import_mail then CanImportMail :: perms
+ else perms in
+ (* Preferences. *)
+ let prefs = { email = email;
+ email_notify = email_notify; } in
+ User (userid, name, perms, prefs)
+ | [] -> raise Not_found
+ | _ -> assert false
+ with
+ Not_found -> Anonymous in
+
+ (* If the ~restrict parameter is given, then we want to check that
+ * the user has sufficient permission to run this script.
+ *)
+ let permitted =
+ if not anonymous && user = Anonymous then false
+ else
+ match restrict with
+ | [] -> true (* empty list = no restrictions *)
+ | rs ->
+ List.fold_left (||) false
+ (List.map (fun r -> test_permission host r user) rs) in
+
+ if permitted then (
+ (* Call the actual CGI script. *)
+ run r q dbh hostid host user
+ ) else (
+ if user = Anonymous then (
+ (* Not logged in and no permission to do the requested action,
+ * so redirect to the login script. If possible set the
+ * redirect parameter so that we return to the right URL.
+ *)
+ let redirect =
+ try
+ (* If we passed through mod_rewrite, then it saved the
+ * unmodified original URL in a subprocess environment
+ * variable called SCRIPT_URL:
+ *)
+ let tbl = Request.subprocess_env r in
+ Some (Table.get tbl "SCRIPT_URL")
+ with
+ Not_found ->
+ try
+ (* Otherwise try the ordinary uri field
+ * in request_rec.
+ *)
+ Some (Request.uri r)
+ with Not_found ->
+ None in
+
+ let url =
+ "http://" ^ hostname ^ "/_login" ^
+ match redirect with
+ | None -> ""
+ | Some url -> "?redirect=" ^ Cgi_escape.escape_url url in
+ q#redirect url
+ ) else
+ error ~back_button:true
+ ~title:"Access denied"
+ dbh hostid q
+ "You do not have permission to access this part of the site."
+ );