开发者

using Catalyst::Model::MenuGrinder

I am trying to add a dynamic menu to my WebApp using Catalyst::Model::MenuGrinder the documentation isn't very clear on how to configure access to via roles using this model. has anyone implemented dynamic menu's using this Model? I found this article in the Catalyst Advent calendar and have set 开发者_开发百科it up correctly I just don't know how to configure it to restrict menu options based on roles.

Any help would be appreciated thanks


As documented in the RequirePrivilege module, you restrict a menu entry based on privileges by adding keys like need_priv and need_user_in_realm. MenuGrinder "privileges" correspond to Catalyst authentication "roles", so if you want to require that a user has the role "admin" for a section, you just do

<item>
    <label>Admin Section</label>
    <need_priv>admin</need_priv>
    <item>...</item>
    <item>...</item>
</item>


Just take a look at your template file which generates the menu actually. Let's say for example that the "Clothes" menu should be display for admins only. You can edit you xml file, like:

<item role="adminonly"><label>Clothes</label>...

and after that you should only check in the menuitem BLOCK if the user has the desired role:

[% BLOCK menuitem %]
<ul [%- IF menu.class %]class="[% menu.class %]"[% END %]>
[% FOREACH item = menu.item %]
[% IF item.role %]
    [% IF c.user_exists && c.check_user_roles( item.role ) %]
    <li [% IF item.active %]class="active"[% END %]>
      <a href="[% item.target %]">[% item.label %]</a>
      [% IF item.item %]
      [% PROCESS menuitem
         menu = item
      %]
    [% END %]
    </li>
    [% END %]
[% END %]
[% END %]
</ul> 
[% END %]

[% PROCESS menuitem %]

PS: Never tested the above code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜