Permalink
Browse files

Support before_/3

  • Loading branch information...
1 parent a697e77 commit a5324dcaa7611803deea693eb0e169fe8407104e @evanmiller evanmiller committed Sep 1, 2012
Showing with 21 additions and 16 deletions.
  1. +8 −2 doc-src/api-controller.html
  2. +13 −14 src/boss/boss_web_controller.erl
@@ -113,7 +113,13 @@
<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}
@@ -131,7 +137,7 @@
<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>
@@ -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} ->

0 comments on commit a5324dc

Please sign in to comment.