+ (* 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