before_ getting called for actions with only two arguments?

54 views
Skip to first unread message

Jitin Luthra

unread,
Oct 22, 2014, 6:36:56 AM10/22/14
to chica...@googlegroups.com
Hi,

I am using a before_ function in my controller and have some actions in my controller with only two arguments. 
However, the before_ function gets called even for those actions with two arguments. 
Did it not used to be that the before_ code would only get called for actions having 3 arguments?

Sample code:

-module(test_hello_controller,[Req]).
-compile(export_all).

before_(_,_,_)->
{redirect,"http://yahoo.com"}.

hi('GET',[])->
{output,"ok"}.

When trying to load the 'hi' action, I get redirected to yahoo.com .

Thanks,
Jitin

can2nac

unread,
Oct 22, 2014, 11:14:11 AM10/22/14
to chica...@googlegroups.com
http://www.chicagoboss.org/api-controller.html , see Authorization section

before_(_,_,_)->
 case some_func_result(Req) of 
   false -> {redirect,"http://yahoo.com"};
   {true,Data} -> {ok, Data}
 end.

hi('GET',[], Data)->
{output,"ok"}.

hello('GET',[], _)->
{output,"ok"}.

the code is from the top of my head, so please, doublecheck.

Jitin Luthra

unread,
Oct 22, 2014, 5:20:16 PM10/22/14
to chica...@googlegroups.com
Hi !
Thanks a lot for your reply.

However, I think I was asking something a little different. I can understand how you describe the use of before_ function for authorization. My situation is that I have a controller where some actions require authorization while others do not. 
I think that the docs and the tutorial suggest that the way this gets handled is that only those actions that have three arguments - such as hello('GET',[],_) invoke the before_ function, while the actions with two arguments such as hello('GET',[]) do not call the before_ function. Does it not work like that?  

Look at this code:

before_(_,_,_)->
       {redirect,"http://yahoo.com"}.

hello('GET',[])->
 {output,"ok"}.


I have a hello('GET',[]) with two arguments. Why should what I write in before_ even matter at all (since hello has only two arguments and not three)? 

And yet when I open the hello action it redirects me to yahoo.com. This is what confuses me. Is this supposed to happen like this? 

Thanks,
Jitin

Graeme Defty

unread,
Oct 22, 2014, 10:01:07 PM10/22/14
to chica...@googlegroups.com
Yes, That's how I recall it too, though my before_ works in either case, so I can't be sure.

Certainly the documentation (http://www.chicagoboss.org/api-controller.html) seems to support your recollection:

If an action takes three arguments, then the function before_/3 in your controller will be passed:
  • the action name as a string
  • the request method as an atom
  • the list of URL tokens
before_/3 should return one of:

etc



Cheers,

g




--
You received this message because you are subscribed to the Google Groups "ChicagoBoss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chicagoboss...@googlegroups.com.
Visit this group at http://groups.google.com/group/chicagoboss.
To view this discussion on the web visit https://groups.google.com/d/msgid/chicagoboss/3c4672ce-a610-4222-b43c-3b1f8d112dac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

chan sisowath

unread,
Oct 22, 2014, 11:49:59 PM10/22/14
to chica...@googlegroups.com
hi,

from the code source, it is always executed
if you export before_/1 or before_/3

https://github.com/ChicagoBoss/ChicagoBoss/blob/master/src/boss/controller_adapters/boss_controller_adapter_pmod.erl#L57

maybe you can pattern match on the action name ?

before_("hi", _Method, _Tokens) -> do_check();
before_(_Action, _Method, _Tokens) -> dont_check().

chan.



can2nac

unread,
Oct 23, 2014, 2:30:46 PM10/23/14
to chica...@googlegroups.com
i have the same situation in my one of my controls: some of functions should work for both auth and non auth people. For this control i have misc:is_auth(soft,SessionID). which returns {ok,{not_found}}; in case user was not found and then  i ignore response with help of pattern matching. For controls which are for auth users only i use misc:is_auth(hard,SessionID). which makes redirect in case of non auth user.

Nick Garanko

unread,
Oct 23, 2014, 4:44:14 PM10/23/14
to chica...@googlegroups.com
Hey Guys,

Not sure if my message is related to topic :)

If you need to apply some code before controller is executed - make use of filters 

Here's example 'login_required_filter' and it's configuration I wrote for one of my toy projects.

Kind regards,
-Nick

Jitin Luthra

unread,
Oct 25, 2014, 9:42:45 PM10/25/14
to chica...@googlegroups.com
Thanks for the replies.
So I guess the before_ function does apply to all actions in a given controller - regardless of if they have two or three arguments.
The solutions suggested work well but right now I am just going to create a separate controller for public actions without the before_ function.

Also the method Nick suggested is great - it separates the 'policies' from the controllers - so you don't have to define authorization in the controller itself but can control which controllers / actions will need authorization from the config file.

Thanks,
Jitin
Reply all
Reply to author
Forward
0 new messages