Skip to content

Commit

Permalink
Support before_/3
Browse files Browse the repository at this point in the history
  • Loading branch information
evanmiller committed Sep 1, 2012
1 parent a697e77 commit a5324dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
10 changes: 8 additions & 2 deletions doc-src/api-controller.html
Expand Up @@ -113,7 +113,13 @@ <h3>Routes</h3>
<a name="auth"></a>
<h3>Authorization</h3>

<p>If an action takes three arguments, then the function <code>before_/1</code> in your controller will be passed the action name as a string and should return one of:</P>
<p>If an action takes three arguments, then the function <code>before_/3</code> in your controller will be passed:</p>
<ol>
<li>the action name as a string</li>
<li>the request method as an atom</li>
<li>the list of URL tokens</li>
</ol>
<p><code>before_/3 should return one of:</P>

<div class="code spec">
{ok, ExtraInfo}
Expand All @@ -131,7 +137,7 @@ <h3>Authorization</h3>
<p>Probably most common before_ looks like:</p>

<div class="code spec">
before_(_) -&gt;<br />
before_(_, _, _) -&gt;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<span class="function">my_user_lib</span>:<span class="function">require_login</span>(Req).
</div>

Expand Down
27 changes: 13 additions & 14 deletions src/boss/boss_web_controller.erl
Expand Up @@ -739,22 +739,21 @@ execute_action({Controller, Action, Tokens} = Location, AppInfo, Req, SessionID,
ExportStrings = lists:map(
fun({Function, Arity}) -> {atom_to_list(Function), Arity} end,
Module:module_info(exports)),
RequestMethod = Req:request_method(),
ControllerInstance = case proplists:get_value("new", ExportStrings) of
1 ->
Module:new(Req);
2 ->
Module:new(Req, SessionID)
1 -> Module:new(Req);
2 -> Module:new(Req, SessionID)
end,
AuthInfo = case lists:member({"before_", 2}, ExportStrings) of
true ->
case ControllerInstance:before_(Action) of
ok ->
{ok, undefined};
OtherInfo ->
OtherInfo
end;
false ->
{ok, undefined}
AuthResult = case proplists:get_value("before_", ExportStrings) of
2 -> ControllerInstance:before_(Action);
4 -> ControllerInstance:before_(Action, RequestMethod, Tokens);
_ -> ok
end,
AuthInfo = case AuthResult of
ok ->
{ok, undefined};
OtherInfo ->
OtherInfo
end,
case AuthInfo of
{ok, Info} ->
Expand Down

0 comments on commit a5324dc

Please sign in to comment.