Skip to content

Commit

Permalink
Login stuff from, uh, before
Browse files Browse the repository at this point in the history
git-svn-id: http://dev.brendonh.org/svn/evo/trunk@71 b2fba486-9eff-4e6f-b9e8-fc05d33d65a9
  • Loading branch information
brendonh committed Jul 23, 2009
1 parent f96b3b9 commit 6959bd9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
3 changes: 2 additions & 1 deletion evo/src/evo.erl
Expand Up @@ -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));
Expand Down
59 changes: 34 additions & 25 deletions evo/src/evosession.erl
Expand Up @@ -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, '"'])).


%%%-------------------------------------------------------------------
Expand All @@ -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
%%%-------------------------------------------------------------------
Expand All @@ -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"}.
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions evo/src/evotest.erl
Expand Up @@ -29,7 +29,7 @@ test() ->

%% Basics

T("EmptyTop", '<div />', [], '<div />'),
T("EmptyTop", '<div />', [], '<div>\n</div>'),
T("TextTop", '<div>Hi</div>', [], '<div>Hi</div>'),
T("PrefixSpace", '<div> Hi</div>', [], '<div> Hi</div>'),
T("SuffixSpace", '<div>Hi </div>', [], '<div>Hi </div>'),
Expand Down Expand Up @@ -91,7 +91,7 @@ test() ->
'<div e:render="foreach"><div e:render="foreach"><e:slot /></div></div>',
[[1,2,3], [4,5,6]],
'<div><div>123</div><div>456</div></div>'),
T("ForeachRowIndex", '<div e:render="foreach"><e:slot e:dataExp="{R,D}" /></div>', [a,b,c,d], '<div>{0,a}{1,b}{2,c}{3,d}</div>'),
T("ForeachRowIndex", '<div e:render="foreach"><e:slot e:dataExp="{Row,D}" /></div>', [a,b,c,d], '<div>{0,a}{1,b}{2,c}{3,d}</div>'),
T("ForeachOddEven", '<div e:render="foreach"><e:slot e:dataExp="OddEven" />,</div>', [a,b,c,d], '<div>odd,even,odd,even,</div>'),
F("ForeachClass", '<ul e:render="foreach"><li><e:attr name="class" e:render="data" e:dataExp="OddEven" /><e:slot /></li></ul>',
[a,b,c,d], '<ul><li class="odd">a</li><li class="even">b</li><li class="odd">c</li><li class="even">d</li></ul>'),
Expand Down Expand Up @@ -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} ->
Expand Down

0 comments on commit 6959bd9

Please sign in to comment.