Jump to content

[SOLVED] OOP and Views


Highlander

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/
Share on other sites

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>";
    }
  }
}

?>

Link to comment
https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433488
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433758
Share on other sites

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>";
    }
  }
}

Link to comment
https://forums.phpfreaks.com/topic/84976-solved-oop-and-views/#findComment-433763
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.