Skip to content
This repository has been archived by the owner on Jul 3, 2020. It is now read-only.

Commit

Permalink
cookbook : view helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jmleroux committed Jun 22, 2014
1 parent 724f75a commit c7ff2c0
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/07. Cookbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ any other recipe you'd like to add, please open an issue!
- [When to use Guards](/docs/07.%20Cookbook.md#when-using-guards-then)
- [A Real World Application Part 2 - Only delete your own Posts](/docs/07.%20Cookbook.md#a-real-world-application-part-2---only-delete-your-own-posts)
- [A Real World Application Part 3 - But Admins can delete everything](/docs/07.%20Cookbook.md#a-real-world-application-part-3---admins-can-delete-everything)
- [A Real World Application Part 4 - Only admins have the menu item for managing the posts]
(/docs/07.%20Cookbook.md#a-real-world-application-part-4---checking-permissions-in-the-view)
- [Using ZfcRbac with Doctrine ORM](/docs/07.%20Cookbook.md#using-zfcrbac-with-doctrine-orm)
- [How to deal with roles with lot of permissions?](/docs/07.%20Cookbook.md#how-to-deal-with-roles-with-lot-of-permissions)
- [Using ZfcRbac and ZF2 Assetic](/docs/07.%20Cookbook.md#using-zfcrbac-and-zf2-assetic)
Expand Down Expand Up @@ -478,6 +480,39 @@ class MustBeAuthorAssertion implements AssertionInterface
}
```

## A Real World Application Part 4 - Checking permissions in the view

If some part of the view needs to be protected, you can use the shipped ```isGranted``` view helper.

For example, lets's say that only users with the permissions ```post.manage``` will have a menu item to acces
the adminsitration panel :

In your template post-index.phtml

```php
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/posts/list">View posts</a></li>
<?php if ($this->isGranted('post.manage'): ?>
<li><a href="/posts/admin">Manage posts</a></li>
<?php endif ?>
</ul>
```

You can even protect your menu item regarding a role, by using the ```hasRole``` view helper :

```php
<ul class="nav">
<li><a href="/">Home</a></li>
<li><a href="/posts/list">View posts</a></li>
<?php if ($this->hasRole('admin'): ?>
<li><a href="/posts/admin">Manage posts</a></li>
<?php endif ?>
</ul>
```

In this last example, the menu item will be hidden for users who don't have the ```admin``` role.

## Using ZfcRbac with Doctrine ORM

First your User entity class must implement `ZfcRbac\Identity\IdentityInterface` :
Expand Down

0 comments on commit c7ff2c0

Please sign in to comment.