03.12.2015 Views

laravel4cookbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Access Control List 68<br />

All the “insecure” routes are rendered in the first block — the block in which routes are subject to the<br />

guest filter. All the “secure” routes are rendered in the secure; where they are subject to the auth<br />

filter.<br />

1 Route::filter("auth", function()<br />

2 {<br />

3 if (Auth::guest())<br />

4 {<br />

5 return Redirect::route("user/login");<br />

6 }<br />

7 else<br />

8 {<br />

9 foreach (Auth::user()->groups as $group)<br />

10 {<br />

11 foreach ($group->resources as $resource)<br />

12 {<br />

13 $path = Route::getCurrentRoute()->getPath();<br />

14<br />

15 if ($resource->pattern == $path)<br />

16 {<br />

17 return;<br />

18 }<br />

19 }<br />

20 }<br />

21<br />

22 return Redirect::route("user/login");<br />

23 }<br />

24 });<br />

.<br />

This was extracted from app/filters.php.<br />

The new auth filter needs not only to make sure the user is authenticated, but also that one of<br />

the group to which they are assigned has the current route assigned to it also. Users can belong to<br />

multiple groups and so can resources; so this is the only (albeit inefficient way) to filter allowed<br />

resources from those which the user is not allowed access to.<br />

To test this out; alter the group to which your user account belongs to disallow access to the<br />

group/add route. When you try to visit it you will be redirected first to the user/login route and<br />

the not the user/profile route.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!