I am currently going through the revamping of our home-brew template system (basically native PHP templates, and little more, very lightweight), and have suddenly run into quite a dilemma regarding the proper place for certain authorization checks. Our home made framework is MVC based (or at least I would like to think that it is, since I developed it), and the way things currently work, when a user is not logged in, he simply gets redirected to the login page. Simple enough, and it can all be handled in the controller page, so this doesn't really pose much of a problem. The issue is that certain templates that appear in almost every page (like the menu you see once you are logged in), have sections that are dependant on the authorization level of the user (more specifically, what 'privileges' they have within the system), and I am at a loss as to how to handle the situation. I suppose one could say there are two general approaches:
1. The MVC / OOP purist would likely propose to run the auth checks in the controller page, which would then be in charge for setting or unsetting certain portions of the template either through variables or other more complex mechanisms (XML transformations?...). The main problem that I have with this approach is that it feels backwards to the issue at hand (it is the template that needs to know what parts to show or not based on user authorization, and setting this outside the view seems counter-intuitive), and perhaps more importantly, that it would require a lot of repeated code at the top of every page just to set all of these variables in the template that indicate what menu items are visible.
2. The second approach, more straightforward, but probably breaking some of the nice layering of MVC, would be to pass the auth object along to the template and allow the template to query it for existing privileges whenever it is needed. For simplicity's sake, I feel like this is what I am more inclined to do.PHP Code:// index.php (controller)
$tpl = new Template();
$oAuth = new UserAuth();
if($oAuth->checkPrivilege('view_users')){
$tpl->setVar('can_view_users', true); // kinda silly, or ....
$tpl->setVar('users_nav', '<td>Users</td>'); // much worse!
}
I can't help but to see some potentially serious drawbacks with both approaches, and it almost suggests to me that there is some inherent coupling between fine-grained authorization systems and views, as everything that you see is potentially something that you are "allowed to see" - if that makes any sense.PHP Code:// template.php
<?
$oAuth = $this->_data['oAuth'];
?>
<table width="100%" id="nav_auth">
<tr>
<td>Store</td>
<? if($oAuth->checkPrivilege('view_users')){ ?>
<td>Users</td>
<? }
if($oAuth->checkPrivilege('view_pricing')){ ?>
<td>Pricing</td>
<? } ?>
<td>Sales</td>
</tr>
</table>
So what I would like to know is, which one of the two approaches would you go with, and what problems do you foresee with each of them? For the grand prize: is there a third, more natural approach, suggesting some kind of auth merchanism built into the view, or some other way to represent this coupling more naturally / directly?



), and the way things currently work, when a user is not logged in, he simply gets redirected to the login page. Simple enough, and it can all be handled in the controller page, so this doesn't really pose much of a problem. The issue is that certain templates that appear in almost every page (like the menu you see once you are logged in), have sections that are dependant on the authorization level of the user (more specifically, what 'privileges' they have within the system), and I am at a loss as to how to handle the situation. I suppose one could say there are two general approaches:

. Your checkPrivilege() method gets it's information from some kind of (possibly compiled) config file, yes? By always passing the authorisation object into the template you have in effect distributed that config file (permission declarations) throughout the whole system.

Bookmarks