[Dancer-users] What's my route handler?

David Cantrell david at cantrell.org.uk
Wed Feb 15 16:58:15 CET 2012

I have some fairly complex user authentication to do - users can be in
multiple groups, users in certain groups can legitimately access certain
URLs but not others.  I've got login working nicely, mostly cribbed from
the cookbook, but am now trying to figure out a nice way of handling the
different authentication requirements for my different routes.

For example, if I am a mafioso I might have these routes ...

get '/billing' => sub { ... };
get '/killing' => sub { ... };

Users in the 'Accountants' group should be able to access /billing, but
only users in the 'Hitmen' group should be able to access /killing.  Of
course, the big bad boss should be able to access both.

Putting users into groups is trivial, and I already have a 'before' hook
that checks that a user is logged in.  But what I'd really like to do,
in the interests of making it dead simple to add routes later and
minimise the places where I have to write authentication code, is to
extend that hook so that it can look at the subroutine attributes on the
route handlers and compare those to the user's groups:

get '/billing' => sub :CapoFamiglia :Accountants { ... };
get '/killing' => sub :CapoFamiglia :Hitmen      { ... };

So, my question is ... how can I get at those subroutine references from
inside my 'before' hook so that I can attributes::get($route_handler) ?

David Cantrell | Reality Engineer, Ministry of Information

More information about the Dancer-users mailing list