From 6959bd9cd0701286a42f53c4b4f734df92f1c8a8 Mon Sep 17 00:00:00 2001 From: Brendon Hogger Date: Thu, 23 Jul 2009 04:02:04 +0000 Subject: [PATCH] Login stuff from, uh, before git-svn-id: http://dev.brendonh.org/svn/evo/trunk@71 b2fba486-9eff-4e6f-b9e8-fc05d33d65a9 --- evo/src/evo.erl | 3 ++- evo/src/evosession.erl | 59 ++++++++++++++++++++++++------------------ evo/src/evotest.erl | 6 ++--- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/evo/src/evo.erl b/evo/src/evo.erl index ff9832b..e8e9411 100644 --- a/evo/src/evo.erl +++ b/evo/src/evo.erl @@ -248,7 +248,8 @@ emitTag(#templateState{tag={e,slot}}=State) -> Data; Keys -> lists:foldl( - fun(E, A) -> proplists:get_value(list_to_atom(E), A) end, + fun ("_top", _) -> get_cache(0); + (E, A) -> proplists:get_value(list_to_atom(E), A) end, Data, string:tokens(Keys, ".")) end, emitTag(evorender:format(State, Final)); diff --git a/evo/src/evosession.erl b/evo/src/evosession.erl index a42836c..823bfc9 100644 --- a/evo/src/evosession.erl +++ b/evo/src/evosession.erl @@ -13,8 +13,9 @@ -export([respond/5, nav/2]). %% API --export([save_session/1, user_info/1, get_user/2]). +-export([save_session/1, user_info/1, get_user/2, login/3]). +-define(Q(S), lists:concat(['"', S, '"'])). %%%------------------------------------------------------------------- @@ -36,6 +37,32 @@ save_session(Conf) -> nav(_Conf, _Args) -> []. + +login(Username, GivenPass, Conf) -> + {CouchDB, DB} = ?GV(couchdb, Conf), + {json, Users} = erlang_couchdb:invoke_view(CouchDB, DB, "users", "byUsername", + [{include_docs, true}, {key, ?Q(Username)}]), + {Valid, User} = case erlang_couchdb:get_value([<<"rows">>, <<"doc">>], Users) of + [] -> {false, none}; + [{struct, U}] -> check_creds(U, GivenPass) + end, + + case Valid of + true -> + {Key, OldSession} = ?GV(session, Conf), + UserID = ?GV(<<"_id">>, User), + NewSession = [{<<"userID">>, UserID}, + {userInfo, get_user(UserID, Conf)} + |OldSession], + NewConf = [{session, {Key, NewSession}}|Conf], + save_session(NewConf), + {ok, NewConf}; + false -> + {error, invalid} + end. + + + %%%------------------------------------------------------------------- %%% Login / logout Evo pages %%%------------------------------------------------------------------- @@ -44,8 +71,6 @@ nav(_Conf, _Args) -> []. gen_server:call(?CONFNAME(Conf, "evotemplate"), {C,T,D,Cf,State#state.templateCallback})). --define(Q(S), lists:concat(['"', S, '"'])). - template(login) -> {file, "templates/auto/login.html"}; template(loggedin) -> {file, "templates/auto/loggedin.html"}. @@ -70,29 +95,13 @@ respond(Req, 'GET', [], Conf, _Args) -> respond(Req, 'POST', [], Conf, Args) -> case user_info(Conf) of [] -> - {CouchDB, DB} = ?GV(couchdb, Conf), Creds = mochiweb_multipart:parse_form(Req), Username = ?GV("username", Creds), + GivenPass = ?GV("password", Creds), - {json, Users} = erlang_couchdb:invoke_view(CouchDB, DB, "users", "byUsername", - [{include_docs, true}, {key, ?Q(Username)}]), - {Valid, User} = case erlang_couchdb:get_value([<<"rows">>, <<"doc">>], Users) of - [] -> {false, none}; - [{struct, U}] -> check_creds(U, Creds) - end, - - case Valid of - true -> - {Key, OldSession} = ?GV(session, Conf), - UserID = ?GV(<<"_id">>, User), - NewSession = [{<<"userID">>, UserID}, - {userInfo, get_user(UserID, Conf)} - |OldSession], - NewConf = [{session, {Key, NewSession}}|Conf], - save_session(NewConf), - redirect(Req, NewConf); - false -> - respond(Req, 'GET', [], [{error, "Invalid credentials"}|Conf], Args) + case login(Username, GivenPass, Conf) of + {ok, NewConf} -> redirect(Req, NewConf); + {error, _} -> respond(Req, 'GET', [], [{error, "Invalid credentials"}|Conf], Args) end; _ -> redirect(Req, Conf) end; @@ -130,9 +139,8 @@ respond(Req, _, _, _Conf, _Args) -> {response, Req:not_found()}. -check_creds(User, Creds) -> +check_creds(User, GivenPass) -> {struct, UNPW} = ?GV(<<"unpw">>, User), - GivenPass = ?GV("password", Creds), RealPass = ?GV(<<"password">>, UNPW), HashFunc = hashfunc(?GVD(<<"hash">>, UNPW, <<"plaintext">>)), case list_to_binary(HashFunc(GivenPass)) == RealPass of @@ -166,6 +174,7 @@ session_from_cookie(_Req, Conf, "/static/" ++ _) -> {update, Conf}; session_from_cookie(Req, Conf, _) -> CookieName = lists:concat(["evo_session_", ?SITENAME(Conf)]), + {Key, DBSession} = case Req:get_cookie_value(CookieName) of undefined -> create_session(Conf); Existing -> retrieve_session(Existing, Conf) diff --git a/evo/src/evotest.erl b/evo/src/evotest.erl index 0ee1c6f..92eb6d4 100644 --- a/evo/src/evotest.erl +++ b/evo/src/evotest.erl @@ -29,7 +29,7 @@ test() -> %% Basics - T("EmptyTop", '
', [], '
'), + T("EmptyTop", '
', [], '
\n
'), T("TextTop", '
Hi
', [], '
Hi
'), T("PrefixSpace", '
Hi
', [], '
Hi
'), T("SuffixSpace", '
Hi
', [], '
Hi
'), @@ -91,7 +91,7 @@ test() -> '
', [[1,2,3], [4,5,6]], '
123
456
'), - T("ForeachRowIndex", '
', [a,b,c,d], '
{0,a}{1,b}{2,c}{3,d}
'), + T("ForeachRowIndex", '
', [a,b,c,d], '
{0,a}{1,b}{2,c}{3,d}
'), T("ForeachOddEven", '
,
', [a,b,c,d], '
odd,even,odd,even,
'), F("ForeachClass", '
', [a,b,c,d], '
  • a
  • b
  • c
  • d
'), @@ -139,7 +139,7 @@ test() -> run_template(Template, Data, Name, Output) -> Self = self(), - Template ! {run, Data, [{pretty, false}], Self}, + Template ! {run, Data, [{pretty, raw}], Self}, receive {Template, result, R} ->