[Catalyst] Numerous Authorization queries

Brian Cassidy brian.cassidy at gmail.com
Thu Nov 13 12:25:02 GMT 2008


On Thu, Nov 13, 2008 at 7:09 AM, Stuart Dodds <stuart at doddsweb.co.uk> wrote:
> That means every iteration the check_user_roles() sub is being called to
> display the link and hence the db is being hit every time.
>
> First of all is this behavior supposed to happen?

Yes, this is the "intended" behaviour. The data isn't cached in any
way. What I've done to circumvent that is to load all of the roles as
a hashref in $c->stash->{ user_roles } in my auto sub, and just
consult that stash data whenever i need to check roles.

if ( $c->user_exists ) {
    $c->stash->{ user_roles }
       = { map { $_ => 1 } $c->user->roles };
}

So i can do:

[% IF user_roles.editor %]
...
[% END %]

-Brian



More information about the Catalyst mailing list