From a96cc93949fd8cf4bd91ce2fa49bb32b93a7de32 Mon Sep 17 00:00:00 2001 From: John Christopher Anderson Date: Thu, 24 Jun 2010 04:04:31 +0000 Subject: send browsers to /_utils/session.html when they need to log in git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@957407 13f79535-47bb-0310-9956-ffa450edef68 --- share/Makefile.am | 1 + share/www/script/futon.js | 13 ++++++ share/www/session.html | 96 +++++++++++++++++++++++++++++++++++++++++++++ share/www/style/layout.css | 6 +++ src/couchdb/couch_httpd.erl | 42 ++++++++++++++------ 5 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 share/www/session.html diff --git a/share/Makefile.am b/share/Makefile.am index 3c4b5da5..9c68e159 100644 --- a/share/Makefile.am +++ b/share/Makefile.am @@ -42,6 +42,7 @@ nobase_dist_localdata_DATA = \ www/couch_tests.html \ www/custom_test.html \ www/database.html \ + www/session.html \ www/dialog/_admin_party.html \ www/dialog/_compact_cleanup.html \ www/dialog/_create_admin.html \ diff --git a/share/www/script/futon.js b/share/www/script/futon.js index 752717e9..2c29a5eb 100644 --- a/share/www/script/futon.js +++ b/share/www/script/futon.js @@ -10,6 +10,18 @@ // License for the specific language governing permissions and limitations under // the License. +// $$ inspired by @wycats: http://yehudakatz.com/2009/04/20/evented-programming-with-jquery/ +function $$(node) { + var data = $(node).data("$$"); + if (data) { + return data; + } else { + data = {}; + $(node).data("$$", data); + return data; + } +}; + (function($) { function Session() { @@ -126,6 +138,7 @@ $.couch.session({ success : function(r) { var userCtx = r.userCtx; + $$("#userCtx").userCtx = userCtx; if (userCtx.name) { $("#userCtx .name").text(userCtx.name).attr({href : "/_utils/document.html?"+encodeURIComponent(r.info.authentication_db)+"/org.couchdb.user%3A"+encodeURIComponent(userCtx.name)}); if (userCtx.roles.indexOf("_admin") != -1) { diff --git a/share/www/session.html b/share/www/session.html new file mode 100644 index 00000000..581640b0 --- /dev/null +++ b/share/www/session.html @@ -0,0 +1,96 @@ + + + + + Session + + + + + + + + + + + + + +
+

Overview + Session

+
+

Establish or Modify Your Session

+
+
+ + + or + + + Welcome ?! +
+ Logout +
+ + Welcome ?! +
+ Setup more admins or + Logout +
+ + Welcome to Admin Party! +
+ Everyone is admin. Fix this +
+
+
+
+ +
+ + diff --git a/share/www/style/layout.css b/share/www/style/layout.css index b8725f4a..191f3874 100644 --- a/share/www/style/layout.css +++ b/share/www/style/layout.css @@ -603,3 +603,9 @@ form#replicator p.actions { padding: 1px; clear: left; margin: 0; font-family: "DejaVu Sans Mono",Menlo,Courier,monospace; font-size: 11px; } + + +/* Session */ +#loginSignup { + font-size:200%; +} \ No newline at end of file diff --git a/src/couchdb/couch_httpd.erl b/src/couchdb/couch_httpd.erl index 96be539e..c05fca45 100644 --- a/src/couchdb/couch_httpd.erl +++ b/src/couchdb/couch_httpd.erl @@ -686,28 +686,46 @@ error_info({Error, Reason}) -> error_info(Error) -> {500, <<"unknown_error">>, couch_util:to_binary(Error)}. -send_error(_Req, {already_sent, Resp, _Error}) -> - {ok, Resp}; - -send_error(#httpd{mochi_req=MochiReq}=Req, Error) -> - {Code, ErrorStr, ReasonStr} = error_info(Error), - Headers = if Code == 401 -> +error_headers(#httpd{mochi_req=MochiReq}=Req, Code, ErrorStr, ReasonStr) -> + if Code == 401 -> % this is where the basic auth popup is triggered case MochiReq:get_header_value("X-CouchDB-WWW-Authenticate") of undefined -> case couch_config:get("httpd", "WWW-Authenticate", nil) of nil -> - []; + % If the client is a browser and the basic auth popup isn't turned on + % redirect to the session page. + case ErrorStr of + <<"unauthorized">> -> + % if the accept header matches html, then do the redirect. else proceed as usual. + case re:run(MochiReq:get_header_value("Accept"), "html", [{capture, none}]) of + nomatch -> + {Code, []}; + match -> + UrlReturn = ?l2b(couch_util:url_encode(MochiReq:get(path))), + UrlReason = ?l2b(couch_util:url_encode(ReasonStr)), + {302, [{"Location", couch_httpd:absolute_uri(Req, <<"/_utils/session.html?return=",UrlReturn/binary,"&reason=",UrlReason/binary>>)}]} + end; + _Else -> + {Code, []} + end; Type -> - [{"WWW-Authenticate", Type}] + {Code, [{"WWW-Authenticate", Type}]} end; Type -> - [{"WWW-Authenticate", Type}] + {Code, [{"WWW-Authenticate", Type}]} end; true -> - [] - end, - send_error(Req, Code, Headers, ErrorStr, ReasonStr). + {Code, []} + end. + +send_error(_Req, {already_sent, Resp, _Error}) -> + {ok, Resp}; + +send_error(Req, Error) -> + {Code, ErrorStr, ReasonStr} = error_info(Error), + {Code1, Headers} = error_headers(Req, Code, ErrorStr, ReasonStr), + send_error(Req, Code1, Headers, ErrorStr, ReasonStr). send_error(Req, Code, ErrorStr, ReasonStr) -> send_error(Req, Code, [], ErrorStr, ReasonStr). -- cgit v1.2.3