If forced to log in to access the site, set redirect so we go back to the page
authorrich <rich>
Mon, 21 Nov 2005 15:28:35 +0000 (15:28 +0000)
committerrich <rich>
Mon, 21 Nov 2005 15:28:35 +0000 (15:28 +0000)
that the user was expecting.

scripts/lib/cocanwiki.ml
scripts/login.ml

index c2aca12..88a7e44 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: cocanwiki.ml,v 1.6 2005/11/16 10:45:41 rich Exp $
+ * $Id: cocanwiki.ml,v 1.7 2005/11/21 15:28:36 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -230,9 +230,34 @@ let register_script ?(restrict = []) ?(anonymous = true) run =
         (* Call the actual CGI script. *)
         run r q dbh hostid host user
        ) else (
-        if user = Anonymous then
-          q#redirect ("http://" ^ hostname ^ "/_login")
-        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"
             q "You do not have permission to access this part of the site."
index 4b55be7..4594e6b 100644 (file)
@@ -1,7 +1,7 @@
 (* COCANWIKI - a wiki written in Objective CAML.
  * Written by Richard W.M. Jones <rich@merjis.com>.
  * Copyright (C) 2004 Merjis Ltd.
- * $Id: login.ml,v 1.6 2005/03/31 14:24:04 rich Exp $
+ * $Id: login.ml,v 1.7 2005/11/21 15:28:35 rich Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -66,7 +66,17 @@ let run r (q : cgi) (dbh : Dbi.connection) hostid _ _ =
       else
        Cookie.cookie "auth" cookie ~path:"/" in
 
-    ok ~title:"Logged in" ~buttons:[ok_button redirect] ~cookie
+    let ok_button = ok_button redirect in
+    let buttons =
+      if redirect <> "/" && redirect <> "/index" then (
+       ok_button ::
+         [ { Template.StdPages.label = " Home Page ";
+             Template.StdPages.link = "/";
+             Template.StdPages.method_ = None;
+             Template.StdPages.params = [] } ]
+      ) else [ ok_button ] in
+
+    ok ~title:"Logged in" ~buttons ~cookie
       q ("Welcome " ^ username ^ "." ^
         if force_password_change then "  Please change your password now."
         else "")