Highlander Posted January 8, 2008 Share Posted January 8, 2008 Hello guys! This is my first post so bare with me here I'm reading on OOP and MVC pattern and I need some opinions about this code: <?php class MemberView { # Reference to a model private $model; # Creates a new view from a model public function __construct( $memberModel ) { $this->model = $memberModel; } /* displays the list of members */ public function display( ) { $members = $this->model->getMembers( ); foreach ($members as $member) { $name = $member->getName( ); echo " <p>Name: $name</p> "; } } } ?> This code just prints out all members. But now to the question: How do I add access control to this view? I mean if a member that is logged in and views this given page has access to delete a member, then MemberView needs to add a link to a delete action. But how is the best way to do this? Quote Link to comment https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/ Share on other sites More sharing options...
trq Posted January 8, 2008 Share Posted January 8, 2008 Many different ways of doing this, but assuming your using standard sessions to log users in, you might use something as simple as.... <?php public function display( ) { $members = $this->model->getMembers( ); foreach ($members as $member) { $name = $member->getName( ); if ($_SESSION['level'] > 1) { echo "<p>Name: $name <a href='/delete/$name'>Delete</a></p>"; } else { echo "<p>Name: $name</p>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433488 Share on other sites More sharing options...
Highlander Posted January 8, 2008 Author Share Posted January 8, 2008 That will work for simple auth, but the problem is that I must check if a member is member of a given group. One solution I have thought of was: <?php if( $logged_in_member->checkIfMemberOfGroup ("Admin") ) { echo "<a href='/delete/id'>delete</a>"; } ?> The problem with this solution is that I need to store a member object on each page. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433758 Share on other sites More sharing options...
rhodesa Posted January 8, 2008 Share Posted January 8, 2008 Merge the two and like magic.... public function display( ) { global $logged_in_member; $members = $this->model->getMembers( ); foreach ($members as $member) { $name = $member->getName( ); if ($logged_in_member->checkIfMemberOfGroup("Admin")) { echo "<p>Name: $name <a href='/delete/$name'>Delete</a></p>"; } else { echo "<p>Name: $name</p>"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433763 Share on other sites More sharing options...
Highlander Posted January 8, 2008 Author Share Posted January 8, 2008 Sounds like a plan. Don't like the global variable but works for now. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.